Skip to content
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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ext/ChainRulesCoreExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

module ChainRulesCoreExt

using CUDA: CuArray
using CUDA: CuArray, CUDA
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
using CUDA: CuArray, CUDA
using 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...)
Copy link
Contributor

Choose a reason for hiding this comment

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

rand too?

Suggested change
ChainRulesCore.@non_differentiable CUDA.randn(::Any...)
ChainRulesCore.@non_differentiable CUDA.rand(::Any...)
ChainRulesCore.@non_differentiable CUDA.randn(::Any...)

Tab completion says there are a few more, but not marked public, so IDK:

julia> CUDA.rand
rand              rand_logn
rand_logn!        rand_poisson
rand_poisson!     randexp_unlikely
randn             randn_unlikely


end
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
GPUArrays = "0c68f7d7-f131-5f86-a1c3-88cf8149b2d7"
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
Expand All @@ -25,3 +26,4 @@ SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
19 changes: 19 additions & 0 deletions test/extensions/zygote.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using GPUArraysCore: GPUArraysCore
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
using GPUArraysCore: GPUArraysCore
using GPUArrays

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}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
function call_rand(v::GPUArraysCore.AbstractGPUVector{T}) where {T}
function call_rand(v::AbstractGPUVector{T}) where {T}

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