-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add MPSMatrixRandom
#321
Merged
Merged
Add MPSMatrixRandom
#321
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ac8a03e
Add MPSMatrixRandom
christiangnrd f7bac7b
Support rand! and rand using MPS where appropriate
christiangnrd e129b39
Comply with Metal documentation by preventing copies between buffer s…
christiangnrd 29b56a4
Address review comments
christiangnrd 4235697
Update compat note
christiangnrd 2595914
Remove reliance of gpu random functionality in copy tests.
christiangnrd c477138
Fix segmentation fault
christiangnrd 65067a0
Fix doctests
christiangnrd c952493
Docstring
christiangnrd 47cbf7e
`current_device()` -> `device()`
christiangnrd 0220ee5
Adapt to storage changes
christiangnrd 05f4752
Update docs and clean up tests
christiangnrd 69f73ce
NFC.
maleadt 994cb08
Address review comments
christiangnrd b729034
Correct version in docstring (and trigger CI)
christiangnrd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
@cenum MPSMatrixRandomDistribution::UInt begin | ||
MPSMatrixRandomDistributionDefault = 1 | ||
MPSMatrixRandomDistributionUniform = 2 | ||
MPSMatrixRandomDistributionNormal = 3 | ||
end | ||
|
||
# | ||
# matrix random descriptor | ||
# | ||
|
||
export MPSMatrixRandomDistributionDescriptor | ||
|
||
@objcwrapper immutable=false MPSMatrixRandomDistributionDescriptor <: NSObject | ||
|
||
@objcproperties MPSMatrixRandomDistributionDescriptor begin | ||
@autoproperty distributionType::MPSMatrixRandomDistribution | ||
@autoproperty maximum::Float32 setter=setMaximum | ||
@autoproperty mean::Float32 setter=setMean | ||
@autoproperty minimum::Float32 setter=setMimimum | ||
@autoproperty standardDeviation::Float32 setter=setStandardDeviation | ||
end | ||
|
||
|
||
function MPSMatrixRandomDefaultDistributionDescriptor() | ||
desc = @objc [MPSMatrixRandomDistributionDescriptor defaultDistributionDescriptor]::id{MPSMatrixRandomDistributionDescriptor} | ||
obj = MPSMatrixRandomDistributionDescriptor(desc) | ||
return obj | ||
end | ||
|
||
# Default constructor | ||
MPSMatrixRandomDistributionDescriptor() = MPSMatrixRandomDefaultDistributionDescriptor() | ||
|
||
function MPSMatrixRandomNormalDistributionDescriptor(mean, standardDeviation) | ||
desc = @objc [MPSMatrixRandomDistributionDescriptor normalDistributionDescriptorWithMean:mean::Float32 | ||
standardDeviation:standardDeviation::Float32]::id{MPSMatrixRandomDistributionDescriptor} | ||
obj = MPSMatrixRandomDistributionDescriptor(desc) | ||
return obj | ||
end | ||
|
||
function MPSMatrixRandomNormalDistributionDescriptor(mean, standardDeviation, minimum, maximum) | ||
desc = @objc [MPSMatrixRandomDistributionDescriptor normalDistributionDescriptorWithMean:mean::Float32 | ||
standardDeviation:standardDeviation::Float32 | ||
minimum:minimum::Float32 | ||
maximum:maximum::Float32]::id{MPSMatrixRandomDistributionDescriptor} | ||
obj = MPSMatrixRandomDistributionDescriptor(desc) | ||
return obj | ||
end | ||
|
||
function MPSMatrixRandomUniformDistributionDescriptor(minimum, maximum) | ||
desc = @objc [MPSMatrixRandomDistributionDescriptor uniformDistributionDescriptorWithMinimum:minimum::Float32 | ||
maximum:maximum::Float32]::id{MPSMatrixRandomDistributionDescriptor} | ||
obj = MPSMatrixRandomDistributionDescriptor(desc) | ||
return obj | ||
end | ||
|
||
|
||
@objcwrapper immutable=false MPSMatrixRandom <: MPSKernel | ||
|
||
@objcproperties MPSMatrixRandom begin | ||
@autoproperty batchSize::NSUInteger | ||
@autoproperty batchStart::NSUInteger | ||
@autoproperty destinationDataType::id{MPSDataType} | ||
@autoproperty distributionType::id{MPSMatrixRandomDistributionDescriptor} | ||
end | ||
|
||
function encode!(cmdbuf::MTLCommandBuffer, kernel::K, destinationMatrix::MPSMatrix) where {K<:MPSMatrixRandom} | ||
@objc [kernel::id{K} encodeToCommandBuffer:cmdbuf::id{MTLCommandBuffer} | ||
destinationMatrix:destinationMatrix::id{MPSMatrix}]::Nothing | ||
end | ||
function encode!(cmdbuf::MTLCommandBuffer, kernel::K, destinationVector::MPSVector) where {K<:MPSMatrixRandom} | ||
@objc [kernel::id{K} encodeToCommandBuffer:cmdbuf::id{MTLCommandBuffer} | ||
destinationVector:destinationVector::id{MPSVector}]::Nothing | ||
end | ||
|
||
@objcwrapper immutable=false MPSMatrixRandomMTGP32 <: MPSMatrixRandom | ||
@objcwrapper immutable=false MPSMatrixRandomPhilox <: MPSMatrixRandom | ||
|
||
for R in [:MPSMatrixRandomMTGP32, :MPSMatrixRandomPhilox] | ||
@eval begin | ||
function $R(device) | ||
kernel = @objc [$R alloc]::id{$R} | ||
obj = $R(kernel) | ||
finalizer(release, obj) | ||
@objc [obj::id{$R} initWithDevice:device::id{MTLDevice}]::id{$R} | ||
return obj | ||
end | ||
function $R(device, destinationDataType, seed) | ||
kernel = @objc [$R alloc]::id{$R} | ||
obj = $R(kernel) | ||
finalizer(release, obj) | ||
@objc [obj::id{$R} initWithDevice:device::id{MTLDevice} | ||
destinationDataType:destinationDataType::MPSDataType | ||
seed:seed::NSUInteger]::id{$R} | ||
return obj | ||
end | ||
function $R(device, destinationDataType, seed, distributionDescriptor) | ||
kernel = @objc [$R alloc]::id{$R} | ||
obj = $R(kernel) | ||
finalizer(release, obj) | ||
@objc [obj::id{$R} initWithDevice:device::id{MTLDevice} | ||
destinationDataType:destinationDataType::MPSDataType | ||
seed:seed::NSUInteger | ||
distributionDescriptor:distributionDescriptor::id{MPSMatrixRandomDistributionDescriptor}]::id{$R} | ||
return obj | ||
end | ||
end | ||
end | ||
|
||
synchronize_state(kern::MPSMatrixRandomMTGP32, cmdbuf::MTLCommandBuffer) = | ||
@objc [obj::id{MPSMatrixRandomMTGP32} synchronizeStateOnCommandBuffer:cmdbuf::id{MTLCommandBuffer}]::Nothing | ||
|
||
|
||
@inline function _mpsmat_rand!(randkern::MPSMatrixRandom, dest::MtlArray{T}, ::Type{T2}; | ||
queue::MTLCommandQueue = global_queue(device()), | ||
async::Bool=false) where {T,T2} | ||
byteoffset = dest.offset * sizeof(T) | ||
bytesize = sizeof(dest) | ||
|
||
# Even though `append_copy`` seems to work with any size or offset values, the documentation at | ||
# https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400767-copyfrombuffer?language=objc | ||
# mentions that both must be multiples of 4 bytes in MacOS so error when they are not | ||
(bytesize % 4 == 0) || error(lazy"Destination buffer bytesize ($(bytesize)) must be a multiple of 4.") | ||
(byteoffset % 4 == 0) || error(lazy"Destination buffer offset ($(byteoffset)) must be a multiple of 4.") | ||
|
||
cmdbuf = if bytesize % 16 == 0 && dest.offset == 0 | ||
MTLCommandBuffer(queue) do cmdbuf | ||
vecDesc = MPSVectorDescriptor(bytesize ÷ sizeof(T2), T2) | ||
mpsdest = MPSVector(dest, vecDesc) | ||
encode!(cmdbuf, randkern, mpsdest) | ||
end | ||
else | ||
MTLCommandBuffer(queue) do cmdbuf | ||
len = UInt(ceil(bytesize / sizeof(T2)) * 4) | ||
vecDesc = MPSVectorDescriptor(len, T2) | ||
tempVec = MPSTemporaryVector(cmdbuf, vecDesc) | ||
encode!(cmdbuf, randkern, tempVec) | ||
MTLBlitCommandEncoder(cmdbuf) do enc | ||
MTL.append_copy!(enc, dest.data[], byteoffset, tempVec.data, tempVec.offset, bytesize) | ||
end | ||
end | ||
end | ||
|
||
async || wait_completed(cmdbuf) | ||
return | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using Random | ||
using Metal: DefaultStorageMode | ||
|
||
""" | ||
MPS.RNG() | ||
|
||
A random number generator using `rand()` in a device kernel. | ||
""" | ||
mutable struct RNG <: AbstractRNG | ||
device::MTLDevice | ||
uniformInteger::MPSMatrixRandomPhilox | ||
uniformFloat32::MPSMatrixRandomPhilox | ||
normalFloat32::MPSMatrixRandomPhilox | ||
end | ||
|
||
|
||
make_seed() = Base.rand(RandomDevice(), UInt) | ||
|
||
function RNG(device::MTLDevice, seed::Integer) | ||
seed = seed%UInt | ||
RNG(device, | ||
MPSMatrixRandomPhilox(device, UInt32, seed, MPSMatrixRandomDefaultDistributionDescriptor()), | ||
MPSMatrixRandomPhilox(device, Float32, seed, MPSMatrixRandomUniformDistributionDescriptor(0, 1)), | ||
MPSMatrixRandomPhilox(device, Float32, seed, MPSMatrixRandomNormalDistributionDescriptor(0, 1)),) | ||
end | ||
@autoreleasepool RNG(seed::Integer) = RNG(device(), seed) | ||
RNG(device::MTLDevice) = RNG(device, make_seed()) | ||
|
||
@autoreleasepool RNG() = RNG(device(), make_seed()) | ||
|
||
Base.copy(rng::RNG) = RNG(copy(rng.device), copy(rng.uniformInteger), copy(rng.uniformFloat32), copy(rng.normalFloat32)) | ||
|
||
@autoreleasepool function Random.seed!(rng::RNG, seed::Integer) | ||
rng.uniformInteger = MPSMatrixRandomPhilox(rng.device, UInt32, seed, MPSMatrixRandomDefaultDistributionDescriptor()) | ||
rng.uniformFloat32 = MPSMatrixRandomPhilox(rng.device, Float32, seed, MPSMatrixRandomUniformDistributionDescriptor(0, 1)) | ||
rng.normalFloat32 = MPSMatrixRandomPhilox(rng.device, Float32, seed, MPSMatrixRandomNormalDistributionDescriptor(0, 1)) | ||
return rng | ||
end | ||
|
||
Random.seed!(rng::RNG) = Random.seed!(rng, make_seed()) | ||
|
||
const GLOBAL_RNGs = Dict{MTLDevice,MPS.RNG}() | ||
@autoreleasepool function default_rng() | ||
dev = device() | ||
get!(GLOBAL_RNGs, dev) do | ||
RNG(dev) | ||
end | ||
end | ||
|
||
const UniformTypes = [Float32,UInt8,Int8,UInt16,Int16,UInt32,Int32,UInt64,Int64] | ||
const UniformType = Union{[Type{T} for T in UniformTypes]...} | ||
const UniformArray = MtlArray{<:Union{Float32,UInt8,Int8,UInt16,Int16,UInt32,Int32,UInt64,Int64}} | ||
@autoreleasepool function Random.rand!(rng::RNG, A::MtlArray{T}) where {T<:Union{UInt8,Int8,UInt16,Int16,UInt32,Int32,UInt64,Int64}} | ||
isempty(A) && return A | ||
_mpsmat_rand!(rng.uniformInteger, A, UInt32) | ||
return A | ||
end | ||
|
||
@autoreleasepool function Random.rand!(rng::RNG, A::MtlArray{Float32}) | ||
isempty(A) && return A | ||
_mpsmat_rand!(rng.uniformFloat32, A, Float32) | ||
return A | ||
end | ||
|
||
const NormalType = Type{Float32} | ||
const NormalArray = MtlArray{<:Float32} | ||
@autoreleasepool function Random.randn!(rng::RNG, A::MtlArray{Float32}) | ||
isempty(A) && return A | ||
_mpsmat_rand!(rng.normalFloat32, A, Float32) | ||
return A | ||
end | ||
|
||
# CPU arrays | ||
function Random.rand!(rng::RNG, A::AbstractArray{T,N}) where {T <: Union{UniformTypes...}, N} | ||
isempty(A) && return A | ||
B = MtlArray{T,N,SharedStorage}(undef, size(A)) | ||
rand!(rng, B) | ||
copyto!(A, unsafe_wrap(Array{T},B)) | ||
return A | ||
end | ||
function Random.randn!(rng::RNG, A::AbstractArray{T,N}) where {T <: Float32, N} | ||
isempty(A) && return A | ||
B = MtlArray{T,N,SharedStorage}(undef, size(A)) | ||
randn!(rng, B) | ||
copyto!(A, unsafe_wrap(Array{T},B)) | ||
return A | ||
end | ||
|
||
# Out of place | ||
Random.rand(rng::RNG, T::UniformType, dims::Dims; storage=DefaultStorageMode) = | ||
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...)) | ||
|
||
# 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...)) | ||
Random.randn(rng::RNG, T::NormalType, dim1::Integer, dims::Integer...; storage=DefaultStorageMode) = | ||
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...)) | ||
Random.randn(rng::RNG, dim1::Integer, dims::Integer...; storage=DefaultStorageMode) = | ||
Random.randn!(rng, MtlArray{Float32,length(dims) + 1,storage}(undef, dim1, dims...)) | ||
|
||
# scalars | ||
Random.rand(rng::RNG, T::UniformType=Float32; storage=SharedStorage) = rand(rng, T, 4; storage)[1] | ||
Random.randn(rng::RNG, T::NormalType=Float32; storage=SharedStorage) = randn(rng, T, 4; storage)[1] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should rather add a warning or assertion to
append_copy
directly.