diff --git a/lib/mps/random.jl b/lib/mps/random.jl index 62ec4344c..88d42881e 100644 --- a/lib/mps/random.jl +++ b/lib/mps/random.jl @@ -93,21 +93,21 @@ end # Out of place Random.rand(rng::RNG, T::UniformType, dims::Dims; storage=DefaultStorageMode) = - Random.rand!(rng, MtlArray{T,length(dims),storage}(undef, dims...; rand_padding=true)) + Random.rand!(rng, MtlArray{T,length(dims),storage}(undef, dims...)) Random.randn(rng::RNG, T::NormalType, dims::Dims; storage=DefaultStorageMode) = - Random.randn!(rng, MtlArray{T,length(dims),storage}(undef, dims...; rand_padding=true)) + Random.randn!(rng, MtlArray{T,length(dims),storage}(undef, dims...)) # support all dimension specifications Random.rand(rng::RNG, T::UniformType, dim1::Integer, dims::Integer...; storage=DefaultStorageMode) = - Random.rand!(rng, MtlArray{T,length(dims) + 1,storage}(undef, dim1, dims...; rand_padding=true)) + Random.rand!(rng, MtlArray{T,length(dims) + 1,storage}(undef, dim1, dims...)) Random.randn(rng::RNG, T::NormalType, dim1::Integer, dims::Integer...; storage=DefaultStorageMode) = - Random.randn!(rng, MtlArray{T,length(dims) + 1,storage}(undef, dim1, dims...; rand_padding=true)) + Random.randn!(rng, MtlArray{T,length(dims) + 1,storage}(undef, dim1, dims...)) # untyped out-of-place Random.rand(rng::RNG, dim1::Integer, dims::Integer...; storage=DefaultStorageMode) = - Random.rand!(rng, MtlArray{Float32,length(dims) + 1,storage}(undef, dim1, dims...; rand_padding=true)) + Random.rand!(rng, MtlArray{Float32,length(dims) + 1,storage}(undef, dim1, dims...)) Random.randn(rng::RNG, dim1::Integer, dims::Integer...; storage=DefaultStorageMode) = - Random.randn!(rng, MtlArray{Float32,length(dims) + 1,storage}(undef, dim1, dims...; rand_padding=true)) + Random.randn!(rng, MtlArray{Float32,length(dims) + 1,storage}(undef, dim1, dims...)) # scalars Random.rand(rng::RNG, T::UniformType=Float32; storage=Shared) = rand(rng, T, 1; storage)[] diff --git a/lib/mtl/buffer.jl b/lib/mtl/buffer.jl index 30bb8b817..cb3435b33 100644 --- a/lib/mtl/buffer.jl +++ b/lib/mtl/buffer.jl @@ -21,8 +21,6 @@ end ## allocation -const BUFFER_PADDING_FOR_RAND::Int = 16 -@inline bufferbytesize(bytesize::T) where {T <: Integer} = ceil(T, bytesize / BUFFER_PADDING_FOR_RAND) * T(BUFFER_PADDING_FOR_RAND) function MTLBuffer(dev::Union{MTLDevice,MTLHeap}, bytesize::Integer; storage=Private, hazard_tracking=DefaultTracking, diff --git a/src/array.jl b/src/array.jl index b3c5c397c..7d85ca51a 100644 --- a/src/array.jl +++ b/src/array.jl @@ -48,10 +48,10 @@ mutable struct MtlArray{T,N,S} <: AbstractGPUArray{T,N} offset::Int # offset of the data in the buffer, in number of elements dims::Dims{N} - function MtlArray{T,N,S}(::UndefInitializer, dims::Dims{N}; rand_padding::Bool=false) where {T,N,S} + function MtlArray{T,N,S}(::UndefInitializer, dims::Dims{N}) where {T,N,S} check_eltype(T) maxsize = prod(dims) * sizeof(T) - rand_padding && (maxsize = MTL.bufferbytesize(maxsize)) + bufsize = if Base.isbitsunion(T) # type tag array past the data maxsize + prod(dims) @@ -123,40 +123,40 @@ const DefaultStorageMode = let str = @load_preference("default_storage", "Privat end end -MtlArray{T,N}(::UndefInitializer, dims::Dims{N}; kwargs...) where {T,N} = - MtlArray{T,N,DefaultStorageMode}(undef, dims; kwargs...) +MtlArray{T,N}(::UndefInitializer, dims::Dims{N}) where {T,N} = + MtlArray{T,N,DefaultStorageMode}(undef, dims) # storage, type and dimensionality specified -MtlArray{T,N,S}(::UndefInitializer, dims::NTuple{N,Integer}; kwargs...) where {T,N,S} = - MtlArray{T,N,S}(undef, convert(Tuple{Vararg{Int}}, dims); kwargs...) -MtlArray{T,N,S}(::UndefInitializer, dims::Vararg{Integer,N}; kwargs...) where {T,N,S} = - MtlArray{T,N,S}(undef, convert(Tuple{Vararg{Int}}, dims); kwargs...) +MtlArray{T,N,S}(::UndefInitializer, dims::NTuple{N,Integer}) where {T,N,S} = + MtlArray{T,N,S}(undef, convert(Tuple{Vararg{Int}}, dims)) +MtlArray{T,N,S}(::UndefInitializer, dims::Vararg{Integer,N}) where {T,N,S} = + MtlArray{T,N,S}(undef, convert(Tuple{Vararg{Int}}, dims)) # type and dimensionality specified -MtlArray{T,N}(::UndefInitializer, dims::NTuple{N,Integer}; kwargs...) where {T,N} = - MtlArray{T,N}(undef, convert(Tuple{Vararg{Int}}, dims); kwargs...) -MtlArray{T,N}(::UndefInitializer, dims::Vararg{Integer,N}; kwargs...) where {T,N} = - MtlArray{T,N}(undef, convert(Tuple{Vararg{Int}}, dims); kwargs...) +MtlArray{T,N}(::UndefInitializer, dims::NTuple{N,Integer}) where {T,N} = + MtlArray{T,N}(undef, convert(Tuple{Vararg{Int}}, dims)) +MtlArray{T,N}(::UndefInitializer, dims::Vararg{Integer,N}) where {T,N} = + MtlArray{T,N}(undef, convert(Tuple{Vararg{Int}}, dims)) # only type specified -MtlArray{T}(::UndefInitializer, dims::NTuple{N,Integer}; kwargs...) where {T,N} = - MtlArray{T,N}(undef, convert(Tuple{Vararg{Int}}, dims); kwargs...) -MtlArray{T}(::UndefInitializer, dims::Vararg{Integer,N}; kwargs...) where {T,N} = - MtlArray{T,N}(undef, convert(Tuple{Vararg{Int}}, dims); kwargs...) +MtlArray{T}(::UndefInitializer, dims::NTuple{N,Integer}) where {T,N} = + MtlArray{T,N}(undef, convert(Tuple{Vararg{Int}}, dims)) +MtlArray{T}(::UndefInitializer, dims::Vararg{Integer,N}) where {T,N} = + MtlArray{T,N}(undef, convert(Tuple{Vararg{Int}}, dims)) # empty vector constructor MtlArray{T,1,S}() where {T,S} = MtlArray{T,1,S}(undef, 0) MtlArray{T,1}() where {T} = MtlArray{T,1}(undef, 0) Base.similar(a::MtlArray{T,N,S}; storage=S, kwargs...) where {T,N,S} = - MtlArray{T,N,storage}(undef, size(a); kwargs...) + MtlArray{T,N,storage}(undef, size(a)) Base.similar(::MtlArray{T,<:Any,S}, dims::Base.Dims{N}; storage=S, kwargs...) where {T,N,S} = - MtlArray{T,N,storage}(undef, dims; kwargs...) + MtlArray{T,N,storage}(undef, dims) Base.similar(::MtlArray{<:Any,<:Any,S}, ::Type{T}, dims::Base.Dims{N}; storage=S, kwargs...) where {T,N,S} = - MtlArray{T,N,storage}(undef, dims; kwargs...) + MtlArray{T,N,storage}(undef, dims) -function Base.copy(a::MtlArray; kwargs...) - b = similar(a; kwargs...) +function Base.copy(a::MtlArray) + b = similar(a) @inbounds copyto!(b, a) end @@ -209,27 +209,27 @@ end ## interop with other arrays -@inline function MtlArray{T,N}(xs::AbstractArray{T,N}; kwargs...) where {T,N} - A = MtlArray{T,N}(undef, size(xs); kwargs...) +@inline function MtlArray{T,N}(xs::AbstractArray{T,N}) where {T,N} + A = MtlArray{T,N}(undef, size(xs)) @inline copyto!(A, convert(Array{T}, xs)) return A end -@inline function MtlArray{T,N,S}(xs::AbstractArray{T,N}; kwargs...) where {T,N,S} - A = MtlArray{T,N,S}(undef, size(xs); kwargs...) +@inline function MtlArray{T,N,S}(xs::AbstractArray{T,N}) where {T,N,S} + A = MtlArray{T,N,S}(undef, size(xs)) @inline copyto!(A, convert(Array{T}, xs)) return A end -MtlArray{T,N}(xs::AbstractArray{OT,N}; kwargs...) where {T,N,OT} = MtlArray{T,N}(map(T, xs); kwargs...) -MtlArray{T,N,S}(xs::AbstractArray{OT,N}; kwargs...) where {T,N,S,OT} = MtlArray{T,N,S}(map(T, xs); kwargs...) +MtlArray{T,N}(xs::AbstractArray{OT,N}) where {T,N,OT} = MtlArray{T,N}(map(T, xs)) +MtlArray{T,N,S}(xs::AbstractArray{OT,N}) where {T,N,S,OT} = MtlArray{T,N,S}(map(T, xs)) # underspecified constructors -MtlArray{T}(xs::AbstractArray{OT,N}; kwargs...) where {T,N,OT} = MtlArray{T,N}(xs; kwargs...) -(::Type{MtlArray{T,N} where T})(x::AbstractArray{OT,N}; kwargs...) where {OT,N} = MtlArray{OT,N}(x; kwargs...) -MtlArray(A::AbstractArray{T,N}; kwargs...) where {T,N} = MtlArray{T,N}(A; kwargs...) +MtlArray{T}(xs::AbstractArray{OT,N}) where {T,N,OT} = MtlArray{T,N}(xs) +(::Type{MtlArray{T,N} where T})(x::AbstractArray{OT,N}) where {OT,N} = MtlArray{OT,N}(x) +MtlArray(A::AbstractArray{T,N}) where {T,N} = MtlArray{T,N}(A) # copy xs to match Array behavior with same storage mode -MtlArray{T,N,S}(xs::MtlArray{T,N,S}; kwargs...) where {T,N,S} = copy(xs; kwargs...) +MtlArray{T,N,S}(xs::MtlArray{T,N,S}) where {T,N,S} = copy(xs) ## derived types diff --git a/src/random.jl b/src/random.jl index 6650bf125..bc6458253 100644 --- a/src/random.jl +++ b/src/random.jl @@ -33,9 +33,9 @@ end # GPUArrays out-of-place rand(T::MPS.UniformType, dims::Dims; storage=DefaultStorageMode) = - Random.rand!(mpsrand_rng(), MtlArray{T,length(dims),storage}(undef, dims...; rand_padding=true)) + Random.rand!(mpsrand_rng(), MtlArray{T,length(dims),storage}(undef, dims...)) randn(T::MPS.NormalType, dims::Dims; storage=DefaultStorageMode) = - Random.randn!(mpsrand_rng(), MtlArray{T,length(dims),storage}(undef, dims...; rand_padding=true)) + Random.randn!(mpsrand_rng(), MtlArray{T,length(dims),storage}(undef, dims...)) rand(T::Type, dims::Dims; storage=DefaultStorageMode) = Random.rand!(gpuarrays_rng(), MtlArray{T,length(dims),storage}(undef, dims...)) randn(T::Type, dims::Dims; storage=DefaultStorageMode) = @@ -43,9 +43,9 @@ randn(T::Type, dims::Dims; storage=DefaultStorageMode) = # support all dimension specifications rand(T::MPS.UniformType, dim1::Integer, dims::Integer...; storage=DefaultStorageMode) = - Random.rand!(mpsrand_rng(), MtlArray{T,length(dims) + 1,storage}(undef, dim1, dims...; rand_padding=true)) + Random.rand!(mpsrand_rng(), MtlArray{T,length(dims) + 1,storage}(undef, dim1, dims...)) randn(T::MPS.NormalType, dim1::Integer, dims::Integer...; storage=DefaultStorageMode) = - Random.randn!(mpsrand_rng(), MtlArray{T,length(dims) + 1,storage}(undef, dim1, dims...; rand_padding=true)) + Random.randn!(mpsrand_rng(), MtlArray{T,length(dims) + 1,storage}(undef, dim1, dims...)) rand(T::Type, dim1::Integer, dims::Integer...; storage=DefaultStorageMode) = Random.rand!(gpuarrays_rng(), MtlArray{T,length(dims) + 1,storage}(undef, dim1, dims...)) @@ -54,9 +54,9 @@ randn(T::Type, dim1::Integer, dims::Integer...; storage=DefaultStorageMode) = # untyped out-of-place rand(dim1::Integer, dims::Integer...; storage=DefaultStorageMode) = - Random.rand!(mpsrand_rng(), MtlArray{Float32,length(dims) + 1,storage}(undef, dim1, dims...; rand_padding=true)) + Random.rand!(mpsrand_rng(), MtlArray{Float32,length(dims) + 1,storage}(undef, dim1, dims...)) randn(dim1::Integer, dims::Integer...; storage=DefaultStorageMode) = - Random.randn!(mpsrand_rng(), MtlArray{Float32,length(dims) + 1,storage}(undef, dim1, dims...; rand_padding=true)) + Random.randn!(mpsrand_rng(), MtlArray{Float32,length(dims) + 1,storage}(undef, dim1, dims...)) # scalars rand(T::Type=Float32; storage=Shared) = rand(T, 1; storage)[] diff --git a/test/array.jl b/test/array.jl index 0e2301c86..bb3d59104 100644 --- a/test/array.jl +++ b/test/array.jl @@ -13,7 +13,7 @@ let arr = MtlVector{Int}(undef, 0) end @testset "constructors" begin - xs = MtlArray{Int8}(undef, 2, 3; rand_padding=false) + xs = MtlArray{Int8}(undef, 2, 3) @test device(xs) == current_device() @test Base.elsize(xs) == sizeof(Int8) @test xs.data[].length == 6 @@ -21,18 +21,6 @@ end @test xs2.data[].length == 6 @test pointer(xs2) != pointer(xs) - - xs = MtlArray{Int8}(undef, 2, 3; rand_padding=true) - @test device(xs) == current_device() - @test Base.elsize(xs) == sizeof(Int8) - @test xs.data[].length == 16 - xs2 = MtlArray{Int8, 2}(xs) - @test xs2.data[].length == 6 - @test pointer(xs2) != pointer(xs) - xs3 = MtlArray{Int8, 2}(xs; rand_padding=true) - @test xs3.data[].length == 16 - @test pointer(xs3) != pointer(xs) - @test collect(MtlArray([1 2; 3 4])) == [1 2; 3 4] @test collect(mtl([1, 2, 3])) == [1, 2, 3] @test testf(vec, rand(Float32, 5,3)) diff --git a/test/random.jl b/test/random.jl index 9753c6ea2..b1a8342be 100644 --- a/test/random.jl +++ b/test/random.jl @@ -14,7 +14,7 @@ const OOPLACE_TUPLES = [[(Metal.rand, rand, T) for T in RAND_TYPES]; rng = Metal.MPS.RNG() @testset "$f with $T" for (f, T) in INPLACE_TUPLES @testset "$d" for d in (1, 3, (3, 3), (3, 3, 3), 16, (16, 16), (16, 16, 16)) - A = MtlArray{T}(undef, d; rand_padding=true) + A = MtlArray{T}(undef, d) fill!(A, T(0)) f(A) @test !iszero(collect(A)) @@ -27,7 +27,7 @@ const OOPLACE_TUPLES = [[(Metal.rand, rand, T) for T in RAND_TYPES]; end @testset "0" begin - A = MtlArray(rand(T, 0); rand_padding=true) + A = MtlArray(rand(T, 0)) b = rand(T) fill!(A, b) @test A isa MtlArray{T,1} @@ -40,7 +40,7 @@ const OOPLACE_TUPLES = [[(Metal.rand, rand, T) for T in RAND_TYPES]; @testset "in-place for views" begin @testset "$f with $T" for (f, T) in INPLACE_TUPLES alen = 100 - A = MtlArray{T}(undef, alen; rand_padding=false) + A = MtlArray{T}(undef, alen) function test_view!(X::MtlArray{T}, idx) where {T} fill!(X, T(0)) view_X = @view X[idx] @@ -105,7 +105,7 @@ const OOPLACE_TUPLES = [[(Metal.rand, rand, T) for T in RAND_TYPES]; @testset "MPS.RNG with views" begin rng = Metal.MPS.RNG() @testset "$f with $T" for (f, T) in ((randn!, Float32),(rand!, Int64),(rand!, Float32), (rand!, UInt16), (rand!,Int8)) - A = MtlArray{T}(undef, 100; rand_padding=false) + A = MtlArray{T}(undef, 100) fill!(A, T(0)) idx = 4:51 view_A = @view A[idx]