Repository navigation
Restore non-differentiable rand!(::AbstractRNG, ::AbstractArray) - #866
Merged
Merged
Conversation
GPUArrays and CUDA implement `rand!(rng, A)` directly, without a sampler, and forward their out-of-place `rand(rng, ...)` methods to it. Since #855 removed this rule, Zygote differentiates into their kernels and errors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #866 +/- ##
=======================================
Coverage 96.65% 96.65%
=======================================
Files 28 28
Lines 3614 3615 +1
=======================================
+ Hits 3493 3494 +1
Misses 121 121 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Fixes #865.
Problem
#855 removed
rand!(::AbstractRNG, ::AbstractArray)because for Random's RNGs it forwards to the sampler rules. GPUArrays and CUDA, however, implementrand!(rng, A)directly without a sampler, and forward all their out-of-placerand(rng, T, dims...),rand(rng, dims...)methods to it. Zygote therefore differentiates into their kernels.Change
Restore only
@non_differentiable rand!(::AbstractRNG, ::AbstractArray), the lowest-level function that all GPU paths reach. Its output depends only oneltype(A), and the previous values ofAare overwritten.Not added, compared with the proposal in #865:
rand(::AbstractRNG, ::Type, ...),rand(::AbstractRNG, ::Integer...): forwarders torand!in GPUArrays and CUDA.rand(::AbstractRNG, ::Dims): forwarder as well; moreover, in Baserand(rng, (5, 7))samples from the tuple.rand!(::AbstractArray): Base and CUDA forward it to the 2-argrand!.rand!(::AbstractRNG, ::AbstractArray, ::Type)would not help: GPU packages define the 2-arg method, so it is never reached.Zygote on JLArrays (GPUArrays reference backend)
rand!(GPUArrays.RNG{JLArray}(...), A),rand!(Xoshiro(1), A)rand(GPUArrays.RNG{JLArray}(...), Float32, 3), same with(3,),rand(rng, 3),rand(rng, (3,))rand(rng, [x]),x .* rand!(rng, zeros(3))Not verified
No CUDA available.
rand!(CUDA.default_rng(), A)hits the rule directly.rand!(A)andrand(CUDA.default_rng(), Float32, 3)reach it afterlibrary_rng()andCuArray{T}(undef, dims), which, judging from the errors in #865, Zygote already traces fine. @CarloLucibello, could you re-run your table on this branch?🤖 Generated with Claude Code