Skip to content

Commit

Permalink
Remove no longer used rand_padding feature
Browse files Browse the repository at this point in the history
  • Loading branch information
christiangnrd committed Apr 7, 2024
1 parent b8b8126 commit 5370c92
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 62 deletions.
12 changes: 6 additions & 6 deletions lib/mps/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)[]
Expand Down
2 changes: 0 additions & 2 deletions lib/mtl/buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
62 changes: 31 additions & 31 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions src/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ 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) =
Random.randn!(gpuarrays_rng(), MtlArray{T,length(dims),storage}(undef, dims...))

# 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...))
Expand All @@ -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)[]
Expand Down
14 changes: 1 addition & 13 deletions test/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,14 @@ 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
xs2 = MtlArray{Int8, 2}(xs)
@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))
Expand Down
8 changes: 4 additions & 4 deletions test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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}
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 5370c92

Please sign in to comment.