|
| 1 | +module RandomExt |
| 2 | + |
| 3 | +@static if Sys.isapple() |
| 4 | + |
| 5 | +using BFloat16s |
| 6 | +using AppleAccelerate: BNNS |
| 7 | +using .BNNS: BNNSFilterParameters, |
| 8 | + BNNSRandomGeneratorMethodAES_CTR, |
| 9 | + BNNSCreateRandomGenerator, |
| 10 | + BNNSCreateRandomGeneratorWithSeed, |
| 11 | + BNNSRandomGeneratorStateSize, |
| 12 | + BNNSRandomGeneratorSetState, |
| 13 | + BNNSRandomGeneratorGetState, |
| 14 | + BNNSNDArrayDescriptor, |
| 15 | + BNNSRandomFillNormalFloat, |
| 16 | + BNNSRandomFillUniformFloat, |
| 17 | + BNNSRandomFillUniformInt |
| 18 | +using Random: Random, AbstractRNG |
| 19 | + |
| 20 | +""" |
| 21 | + RNG() |
| 22 | +
|
| 23 | +A random number generator using AppleAccelerate's BNNS functionality. |
| 24 | +""" |
| 25 | +mutable struct RNG <: AbstractRNG |
| 26 | + ptr::Ptr{Nothing} |
| 27 | + function RNG(filter_parameters::Union{Nothing, BNNSFilterParameters}=nothing) |
| 28 | + params = isnothing(filter_parameters) ? Ptr{BNNSFilterParameters}(0) : [filter_parameters] |
| 29 | + res = new(BNNSCreateRandomGenerator(BNNSRandomGeneratorMethodAES_CTR, params)) |
| 30 | + # finalizer(res) do |
| 31 | + # BNNSDestroyRandomGenerator(res.ptr) |
| 32 | + # end |
| 33 | + return res |
| 34 | + end |
| 35 | + function RNG(seed::Integer, filter_parameters::Union{Nothing, BNNSFilterParameters}=nothing) |
| 36 | + seed = seed%UInt64 |
| 37 | + params = isnothing(filter_parameters) ? Ptr{BNNSFilterParameters}(0) : [filter_parameters] |
| 38 | + res = new(BNNSCreateRandomGeneratorWithSeed(BNNSRandomGeneratorMethodAES_CTR, seed, params)) |
| 39 | + # finalizer(res) do |
| 40 | + # BNNSDestroyRandomGenerator(res.ptr) |
| 41 | + # end |
| 42 | + return res |
| 43 | + end |
| 44 | +end |
| 45 | + |
| 46 | +BNNS.bnns_rng() = RNG() |
| 47 | +BNNS.bnns_rng(seed::Integer) = RNG(seed) |
| 48 | + |
| 49 | +@static if isdefined(Base, :Memory) #VERSION >= v"1.11" |
| 50 | + function _get_rng_state(rng::RNG) |
| 51 | + stateSize = BNNSRandomGeneratorStateSize(rng.ptr) |
| 52 | + state = Memory{UInt8}(undef, Int64(stateSize)) |
| 53 | + BNNSRandomGeneratorGetState(rng.ptr, stateSize, state) |
| 54 | + return state |
| 55 | + end |
| 56 | +else |
| 57 | + function _get_rng_state(rng::RNG) |
| 58 | + stateSize = BNNSRandomGeneratorStateSize(rng.ptr) |
| 59 | + state = Vector{UInt8}(undef, Int64(stateSize)) |
| 60 | + BNNSRandomGeneratorGetState(rng.ptr, stateSize, state) |
| 61 | + return state |
| 62 | + end |
| 63 | +end |
| 64 | + |
| 65 | +function Base.copy!(dest::RNG, src::RNG) |
| 66 | + state = _get_rng_state(src) |
| 67 | + BNNSRandomGeneratorSetState(dest.ptr, length(state), state) |
| 68 | + return dest |
| 69 | +end |
| 70 | + |
| 71 | +function Base.copy(rng::RNG) |
| 72 | + newrng = RNG() |
| 73 | + return copy!(newrng, rng) |
| 74 | +end |
| 75 | + |
| 76 | +Base.:(==)(rng1::RNG, rng2::RNG) = _get_rng_state(rng1) == _get_rng_state(rng2) |
| 77 | + |
| 78 | +function Random.seed!(rng::RNG, seed::Integer) |
| 79 | + return copy!(rng, RNG(seed)) |
| 80 | +end |
| 81 | + |
| 82 | +function Random.seed!(rng::RNG) |
| 83 | + return copy!(rng, RNG()) |
| 84 | +end |
| 85 | + |
| 86 | +const GLOBAL_RNG = Ref{RNG}() |
| 87 | +function BNNS.default_rng() |
| 88 | + if !isassigned(GLOBAL_RNG) |
| 89 | + GLOBAL_RNG[] = BNNS.bnns_rng() |
| 90 | + end |
| 91 | + return GLOBAL_RNG[] |
| 92 | +end |
| 93 | + |
| 94 | +const BNNSInt = Union{UInt8,Int8,UInt16,Int16,UInt32,Int32,UInt64,Int64} |
| 95 | +const BNNSFloat = Union{Float16, Float32, BFloat16} |
| 96 | + |
| 97 | +const BNNSUniform = Union{<:BNNSInt,<:BNNSFloat} |
| 98 | +const BNNSNormal = BNNSFloat |
| 99 | + |
| 100 | +function Random.rand!(rng::RNG, A::DenseArray{T}) where {T<:BNNSInt} |
| 101 | + isempty(A) && return A |
| 102 | + desc = Ref(BNNSNDArrayDescriptor(A)) |
| 103 | + res = BNNSRandomFillUniformInt(rng.ptr, desc, typemin(signed(T)), typemax(signed(T))) |
| 104 | + @assert res == 0 |
| 105 | + return A |
| 106 | +end |
| 107 | +function Random.rand!(rng::RNG, A::DenseArray{T}) where {T<:BNNSFloat} |
| 108 | + isempty(A) && return A |
| 109 | + desc = Ref(BNNSNDArrayDescriptor(A)) |
| 110 | + res = BNNSRandomFillUniformFloat(rng.ptr, desc, T(0), T(1)) |
| 111 | + @assert res == 0 |
| 112 | + return A |
| 113 | +end |
| 114 | +function Random.randn!(rng::RNG, A::DenseArray{T}) where {T<:BNNSFloat} |
| 115 | + isempty(A) && return A |
| 116 | + desc = Ref(BNNSNDArrayDescriptor(A)) |
| 117 | + res = BNNSRandomFillNormalFloat(rng.ptr, desc, Float32(0), Float32(1)) |
| 118 | + @assert res == 0 |
| 119 | + return A |
| 120 | +end |
| 121 | + |
| 122 | +# Out of place |
| 123 | +Random.rand(rng::RNG, ::Type{T}, dims::Dims) where T <: BNNSUniform = |
| 124 | + Random.rand!(rng, Array{T,length(dims)}(undef, dims...)) |
| 125 | +Random.randn(rng::RNG, ::Type{T}, dims::Dims) where T <: BNNSNormal = |
| 126 | + Random.randn!(rng, Array{T,length(dims)}(undef, dims...)) |
| 127 | + |
| 128 | +# support all dimension specifications |
| 129 | +Random.rand(rng::RNG, ::Type{T}, dim1::Integer, dims::Integer...) where T <: BNNSUniform = |
| 130 | + Random.rand!(rng, Array{T,length(dims) + 1}(undef, dim1, dims...)) |
| 131 | +Random.randn(rng::RNG, ::Type{T}, dim1::Integer, dims::Integer...) where T <: BNNSNormal = |
| 132 | + Random.randn!(rng, Array{T,length(dims) + 1}(undef, dim1, dims...)) |
| 133 | + |
| 134 | +# untyped out-of-place |
| 135 | +Random.rand(rng::RNG, dim1::Integer, dims::Integer...) = |
| 136 | + Random.rand!(rng, Array{Float32,length(dims) + 1}(undef, dim1, dims...)) |
| 137 | +Random.randn(rng::RNG, dim1::Integer, dims::Integer...) = |
| 138 | + Random.randn!(rng, Array{Float32,length(dims) + 1}(undef, dim1, dims...)) |
| 139 | + |
| 140 | +# scalars |
| 141 | +Random.rand(rng::RNG, T::Union{Type{Float16}, Type{Float32}, Type{BFloat16}, |
| 142 | +Type{Int8}, Type{UInt8}, |
| 143 | +Type{Int16}, Type{UInt16}, |
| 144 | +Type{Int32}, Type{UInt32}, |
| 145 | +Type{Int64}, Type{UInt64}}=Float32) = Random.rand(rng, T, 1)[1] |
| 146 | + |
| 147 | +# This is the only way I could fix method ambiguity |
| 148 | +Random.randn(rng::RNG, T::Type{BFloat16}) = Random.randn(rng, T, 1)[1] |
| 149 | +Random.randn(rng::RNG, T::Type{Float16}) = Random.randn(rng, T, 1)[1] |
| 150 | +Random.randn(rng::RNG, T::Type{Float32}) = Random.randn(rng, T, 1)[1] |
| 151 | +Random.randn(rng::RNG) = Random.randn(rng, Float32) |
| 152 | + |
| 153 | + |
| 154 | +# GPUArrays out-of-place |
| 155 | +function BNNS.rand(::Type{T}, dims::Dims) where T <: BNNSUniform |
| 156 | + return Random.rand!(BNNS.default_rng(), Array{T,length(dims)}(undef, dims...)) |
| 157 | +end |
| 158 | +function BNNS.randn(::Type{T}, dims::Dims) where T <: BNNSNormal |
| 159 | + return Random.randn!(BNNS.default_rng(), Array{T,length(dims)}(undef, dims...)) |
| 160 | +end |
| 161 | + |
| 162 | +# support all dimension specifications |
| 163 | +function BNNS.rand(::Type{T}, dim1::Integer, dims::Integer...) where T <: BNNSUniform |
| 164 | + return Random.rand!(BNNS.default_rng(), Array{T,length(dims) + 1}(undef, dim1, dims...)) |
| 165 | +end |
| 166 | +function BNNS.randn(::Type{T}, dim1::Integer, dims::Integer...) where T <: BNNSNormal |
| 167 | + return Random.randn!(BNNS.default_rng(), Array{T,length(dims) + 1}(undef, dim1, dims...)) |
| 168 | +end |
| 169 | + |
| 170 | +# untyped out-of-place |
| 171 | +BNNS.rand(dim1::Integer, dims::Integer...) = |
| 172 | + Random.rand!(BNNS.default_rng(), Array{Float32,length(dims) + 1}(undef, dim1, dims...)) |
| 173 | +BNNS.randn(dim1::Integer, dims::Integer...) = |
| 174 | + Random.randn!(BNNS.default_rng(), Array{Float32,length(dims) + 1}(undef, dim1, dims...)) |
| 175 | + |
| 176 | +# scalars |
| 177 | +BNNS.rand(T::Type=Float32) = BNNS.rand(T, 1)[1] |
| 178 | +BNNS.randn(T::Type=Float32) = BNNS.randn(T, 1)[1] |
| 179 | + |
| 180 | +# seeding |
| 181 | +function BNNS.seed!(seed=Base.rand(UInt64)) |
| 182 | + Random.seed!(BNNS.default_rng(), seed) |
| 183 | +end |
| 184 | + |
| 185 | +end |
| 186 | +end # module |
0 commit comments