Skip to content

(feat): rand! / randn! — random-array pool constructors (CPU + Metal + CUDA) - #43

Merged
mgyoo86 merged 5 commits into
masterfrom
feature/rand-randn-pool-constructors
Jun 26, 2026
Merged

mgyoo86 merged 5 commits into
masterfrom
feature/rand-randn-pool-constructors

Conversation

@mgyoo86

@mgyoo86 mgyoo86 commented Jun 26, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds rand! / randn! as pooled peers of zeros! / ones!.
They extend Random.rand! / Random.randn! with methods on the pool type and are re-exported

API

rand!(pool, [T,] dims...)   # uniform — U[0,1) floats, full-range ints
rand!(pool, S, dims...)     # sample a range/collection, e.g. rand!(pool, 1:6, n)   (CPU only)
randn!(pool, [T,] dims...)  # standard normal N(0,1)

GPU (Metal + CUDA)

Live GPU pools inherit the typed/default forms for free — the core impl fills the acquired CuArray/MtlArray via Random.rand!/randn! on-device, same mechanism as zeros! (verified on Metal).

The collection/range form is CPU-only (no GPU sampler) and raises a
clear error; each extension adds only DisabledPool fallbacks plus that guard.

Out of scope

GPU collection sampling · randexp! · rng-argument forms · rand!(pool, Bit, …).

Note

Fixed a 'previously' broken tests in CUDA runtets.

mgyoo86 added 2 commits June 26, 2026 10:13
Add rand!/randn! convenience constructors as peers of zeros!/ones!,
extending Random.rand!/randn! with pool-backed methods. They are
re-exported (same binding), so `using AdaptiveArrayPools` exposes them
with no conflict alongside `using Random`.

- Random added as a hard dependency (stdlib, ~0 load cost)
- Forms: rand!(pool, [T,] dims...) and the collection/range sampling form
  rand!(pool, S, dims...) (e.g. rand!(pool, 1:6, n)); randn! for N(0,1)
- Wired into @with_pool at all three sites (type pre-registration,
  call->_impl! transform, escape detection). The collection form
  self-records its eltype touch since the macro cannot pre-register
  eltype(S)
- DisabledPool{:cpu} fallbacks; CPU-only (GPU parity deferred)
- Seed-equivalent with plain rand/randn: pool arrays are genuine Arrays,
  so RNG consumption matches exactly

Tests (test/test_random.jl): seed-equivalence incl. consecutive-draw and
@with_pool RNG-neutrality, zero-allocation, type-stability (@inferred),
@maybe_with_pool fallback, escape detection, and DisabledPool.
The live GPU pool inherits the core _rand_impl!/_randn_impl! (which fill the
acquired CuArray/MtlArray via Random.rand!/randn!, run on-device by CUDA.jl /
Metal.jl) — verified on Metal for Float32/Float16/Int32. Each extension adds:

- DisabledPool{:cuda}/{:metal} rand!/randn! fallbacks (CUDA.rand/randn,
  Metal.rand/randn)
- A clear ArgumentError for the collection/range form rand!(pool, S, dims):
  GPU RNGs have no arbitrary-collection sampler, so it is CPU-only. Overriding
  _rand_impl! covers both the @with_pool macro path and the direct path, for
  live and disabled pools.

Tests: test/metal/test_random.jl (34 pass locally) and test/cuda/test_random.jl
(mirror, to run on a CUDA machine).
@codecov

codecov Bot commented Jun 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.61%. Comparing base (3e356a9) to head (e13fcef).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #43      +/-   ##
==========================================
+ Coverage   95.48%   95.61%   +0.13%     
==========================================
  Files          14       14              
  Lines        3232     3309      +77     
==========================================
+ Hits         3086     3164      +78     
+ Misses        146      145       -1     
Files with missing lines Coverage Δ
src/AdaptiveArrayPools.jl 100.00% <ø> (ø)
src/convenience.jl 99.10% <100.00%> (+0.40%) ⬆️
src/macros.jl 96.79% <100.00%> (+0.09%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds pooled random-array constructors rand! / randn! as peers of zeros! / ones!, integrating them into @with_pool (type extraction, call transformation, escape detection) and providing CPU + Metal + CUDA behavior (including explicit GPU rejection of collection/range sampling).

Changes:

  • Extend and re-export Random.rand! / Random.randn! with AbstractArrayPool methods, plus _rand_impl! / _randn_impl! for macro-transformed calls.
  • Add CPU + Metal + CUDA tests covering RNG stream equivalence, @with_pool behavior, DisabledPool fallbacks, and GPU collection-form rejection.
  • Update package deps/docs/README to include Random and document the new convenience functions.

Reviewed changes

Copilot reviewed 14 out of 15 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
test/test_random.jl New comprehensive CPU test coverage for pooled rand!/randn! semantics and RNG equivalence.
test/runtests.jl Includes the new CPU random-constructor test file in the suite.
test/Project.toml Adds Random to test dependencies.
test/metal/test_random.jl New Metal backend tests for typed/default GPU random fills + rejection of collection sampling.
test/metal/runtests.jl Includes the new Metal random-constructor tests.
test/cuda/test_random.jl New CUDA backend tests mirroring Metal coverage (to run on CUDA-capable hosts).
test/cuda/runtests.jl Includes the new CUDA random-constructor tests.
src/macros.jl Registers rand!/randn! in type extraction, call→impl transforms, and escape-detection sets.
src/convenience.jl Implements pooled rand!/randn!, internal impls, and DisabledPool CPU fallbacks.
src/AdaptiveArrayPools.jl Imports/re-exports Random.rand!/randn! and exports them as part of the public API.
README.md Documents rand!/randn! in the convenience-function table and explains RNG behavior.
Project.toml Adds Random to [deps] and [compat].
ext/AdaptiveArrayPoolsMetalExt/convenience.jl Adds Metal DisabledPool fallbacks and GPU-side collection-form rejection via _rand_impl! override.
ext/AdaptiveArrayPoolsCUDAExt/convenience.jl Adds CUDA DisabledPool fallbacks and GPU-side collection-form rejection via _rand_impl! override.
docs/design/rand_randn_pool_constructors.md New design doc describing the feature and rationale (currently contains a few stale statements).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/convenience.jl
Comment thread docs/design/rand_randn_pool_constructors.md Outdated
Comment thread docs/design/rand_randn_pool_constructors.md Outdated
mgyoo86 added 3 commits June 26, 2026 14:26
test/cuda/test_extension.jl simulated a @with_pool scope by setting
pool._current_depth = 2 directly, but skipped the lockstep growth of the
per-depth _touched_has_others vector that a real checkpoint! performs.
get_typed_pool! then wrote _touched_has_others[2] into a 1-element vector —
masked by @inbounds normally, but a BoundsError under Pkg.test's
--check-bounds=yes (seen on NERSC/CI).

Use the real checkpoint!(pool), exactly as the Metal test already did, and
assert _touched_has_others == [false, true] in both the CUDA and Metal tests
so the depth flag is verified rather than just executed. Test-only change.
Codecov flagged 13 uncovered patch lines — all never-called method bodies:
- convenience.jl: randn! typed-NTuple public, the two _randn_impl! NTuple
  delegations (reached via the macro size(x) transform), and the rand!/randn!
  DisabledPool NTuple fallbacks
- macros.jl: the qualified-name AdaptiveArrayPools.rand!/randn! → _impl!
  transform branches

Add two testsets exercising every NTuple form (direct + size(x) macro path +
DisabledPool) and a qualified-name call inside @with_pool. All 13 lines now hit.

Also refresh the design doc: status -> Implemented, GPU-parity row -> done
(addresses the stale-doc points from the Copilot PR review).
docs/design/rand_randn_pool_constructors.md is an internal/local working
note, not part of the shipped package. Remove it from version control (kept
locally, untracked) so it no longer appears in the PR.
@mgyoo86
mgyoo86 merged commit 1263978 into master Jun 26, 2026
14 checks passed
@mgyoo86
mgyoo86 deleted the feature/rand-randn-pool-constructors branch June 26, 2026 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants