-
Notifications
You must be signed in to change notification settings - Fork 232
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
make CUDA randn work with Zygote #2581
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -2,12 +2,15 @@ | |||||||
|
||||||||
module ChainRulesCoreExt | ||||||||
|
||||||||
using CUDA: CuArray | ||||||||
using CUDA: CuArray, CUDA | ||||||||
|
||||||||
isdefined(Base, :get_extension) ? (import ChainRulesCore) : (import ..ChainRulesCore) | ||||||||
|
||||||||
## support ChainRulesCore inplaceability | ||||||||
|
||||||||
ChainRulesCore.is_inplaceable_destination(::CuArray) = true | ||||||||
|
||||||||
# allow usage of rand with Zygote | ||||||||
ChainRulesCore.@non_differentiable CUDA.randn(::Any...) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Tab completion says there are a few more, but not marked public, so IDK:
|
||||||||
|
||||||||
end |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,19 @@ | ||||||
using GPUArraysCore: GPUArraysCore | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
GPUArrays re-exports the Core functionality. |
||||||
using CUDA | ||||||
using Zygote | ||||||
|
||||||
function call_rand(v::AbstractVector{T}) where {T} | ||||||
randn(T, 4,4) * v[1:4] | ||||||
end | ||||||
function call_rand(v::GPUArraysCore.AbstractGPUVector{T}) where {T} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
CUDA.randn(T, 4,4) * v[1:4] | ||||||
end | ||||||
|
||||||
@testset "randn" begin | ||||||
v_orig = collect(1.0f0:10.0f0) | ||||||
mb = call_rand(v_orig) | ||||||
v = CuArray(v_orig) | ||||||
m = call_rand(v) | ||||||
gr = Zygote.gradient(v -> sum(call_rand(v)), v) | ||||||
@test gr[1:4] .!= 0 | ||||||
end |
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.