Repository navigation
Conversation
…for fresh fills with simple calls. determinism obviously remains if an rng is passed
…mittent test failure. fence Float64 tests to CPU-only to avoid breaking CI on Metal and oneAPI
…en AK.rand!(x) is called. This gives stream-like behaviour without any on-device state but still faster than CURAND. Add RNGTest in prototyping for BigCrush and SmallCrush (Philox, SM64, and Threefry all now confirmed to pass in the AK implementation)
…owing CounterRNG which now must have an offset. if needed, AK.reset!(::CounterRNG) can be called to reset a streaming CounterRNG every time AK.rand! is called, if needed
… and even offsets + array lengths. Added convenience rand() and randn() convenience constructors using KernelAbstractions.allocate based on the provided backend, Type, and dims
…sabling package images
…pen-interval helpers into utilities.jl and switch naming of internals. Simplify rand!/randn! wrappers and add convenience constructors for omitted rng, backend, or type, with backend-dependent defaults (Float64 on CPU, Float32 otherwise). Add explicit zero-arg guards so rand()/randn() require at least one dimension. Update rand.md with concise convenience semantics and examples, including type-only no-rng calls, plus doc entries for rand/randn. Expand tests to cover default-type dispatch, CPU fallback routes, typed no-rng overloads, and invalid-signature/kwarg throw behavior while preserving deterministic CounterRNG offset progression.
…avoid sync with odd offsets with randn!
…rng, but contribute threaded deterministic CPU generation that outperforms Base, and the option for completely determinstic generation regardless of backend.
Member
|
Thanks, but I'm not inclined to take a 3KLOC RNG that only serves the CPU back-end, which we're trying to deprecate in favor of a unified KA + PoCL approach... Wouldn't this be better suited as a Base/Random.jl contribution? |
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This is a reworked follow-up to #80. Thanks again @maleadt for the thorough review and benchmarks there, and for crediting the
[0, 1)fix in GPUArrays.jl#872. The issues you and @nhz2 found were all totally valid, and have either been sorted or deleted upon the new dependence on PhiloxRNG.jl.What I'm aiming for
rand!/randn!for CPU arrays that outperforms Base's single-threaded fill, with the same output for any number of threads.Changes since #80
AK.rand!/AK.randn!on GPU arrays simply forward toRandom.rand!/Random.randn!, so there is no second default GPU implementation.CounterRNGis built on PhiloxRNG.jl (philox4x32_10,randn_f32/randn_f64,boxmuller). AK only adds the mapping from elements to counters, offset-based streaming, and boundary handling.rand!/randn!calls of different types never reuses counters; a regression test covers both mixed-call cases.randn!no longer synchronises (odd offsets and lengths are handled inside a single launch), and nothing widens toFloat64on the GPU path. Uniform floats stay in[0, 1).CPU:
StreamRNG(default for CPU arrays)This is a Xoshiro256++ stream with bounded-time jump-ahead, where each task jumps directly to its first element with at most 64 power-of-two jumps. The output is identical for any thread count, and
rand!reproduces Base's scalarrand(::Xoshiro, T)stream from the same state.randn!uses a ziggurat with exactly one stream output per sample: rare rejections draw from a local SplitMix64 sequence instead of the stream. I validated it with χ² tests on 2³² samples (full distribution, tail and consecutive pairs), using Base'srandn!as a control10M
Float64, Apple M5 Pro:rand!randn!Backend-agnostic: opt-in
CounterRNGCounterRNGnow depends on PhiloxRNG.jl, calling itsphilox4x32_10for the random blocks and itsrandn_f32/randn_f64/boxmullerfor normals, rather than carrying any Philox or Box-Muller code of its own. Each element is a pure function of the seed and its position (offset + i), so values don't depend on backend, launch configuration or thread count, and two consecutive 5-element fills equal one 10-element fill. A seeded 10M fill is bit-identical between CPU (1 and 18 threads) and Metal. I haven't yet verified CUDA, AMD or oneAPI. The reason I want to keep this implementation is (as in the aforementioned goals) the usefulness of a 100% deterministic RNG regardless of backend. Nothing is duplicated, but AK handles the counter in such a way that successive calls produce a deterministic streamAs expected, it's memory-bound on the GPU, at the same throughput as the default path, i.e. Metal's
Random.rand!/randn!, which use GPUArrays' Philox4x32-10 RNG (100M elements, Apple M5 Pro, GB/s):(For reference, a write-only
fill!of aFloat32array runs at 239 GB/s on the same device, so 32-bit fills from either RNG are at the memory bandwidth limit.)CounterRNGMetal.rand!/randn!(GPUArrays)rand!Float32rand!Int32rand!UInt8randn!Float32randn!Float16New dependency:
PhiloxRNG. This also adds back theRandomstandard library, whichmainrecently dropped, for forwarding toRandom.rand!/Random.randn!.Please let me know what you think about the new structure. As mentioned, the deterministic behaviour is essential for some of our work and I think others will find it useful. If, however, you and the rest of the team think it doesn't belong here then we can find a home for it in another repo.