-
Notifications
You must be signed in to change notification settings - Fork 5
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
Initial work on CUDA-compat #25
base: main
Are you sure you want to change the base?
Changes from 9 commits
4d54b64
6bbe297
d9f3f1a
a3619ca
0dde183
1f795a1
847a520
51dc577
7a56c05
5ec4317
e1fa4b9
fb07372
84fce4c
b573508
e8457d4
c21400a
0ced915
45c8e0a
4be9588
1e72b9d
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module NormalizingFlowsCUDAExt | ||
|
||
using CUDA | ||
using NormalizingFlows: Random, Distributions | ||
|
||
# Make allocation of output array live on GPU. | ||
function Distributions.rand( | ||
rng::CUDA.RNG, | ||
s::Distributions.Sampleable{<:Distributions.ArrayLikeVariate,Distributions.Continuous}, | ||
) | ||
return @inbounds Distributions.rand!( | ||
rng, Distributions.sampler(s), CuArray{float(eltype(s))}(undef, size(s)) | ||
) | ||
end | ||
|
||
function Distributions.rand( | ||
rng::CUDA.RNG, | ||
s::Distributions.Sampleable{<:Distributions.ArrayLikeVariate,Distributions.Continuous}, | ||
n::Int, | ||
) | ||
return @inbounds Distributions.rand!( | ||
rng, Distributions.sampler(s), CuArray{float(eltype(s))}(undef, length(s), n) | ||
) | ||
end | ||
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. Usage of Maybe |
||
|
||
function Distributions._rand!(rng::CUDA.RNG, d::Distributions.MvNormal, x::CuVecOrMat) | ||
# Replaced usage of scalar indexing. | ||
CUDA.randn!(rng, x) | ||
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. @zuhengxu do you know why this change of yours was necessary? I thought 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. ahh, you are right---this is not necessary. I think I just made the change to ensure it's actually calling the cuda sampling. I can change it back. |
||
Distributions.unwhiten!(d.Σ, x) | ||
x .+= d.μ | ||
return x | ||
end | ||
|
||
function Distributions.insupport( | ||
::Type{D}, x::CuVector{T} | ||
) where {T<:Real,D<:Distributions.AbstractMvLogNormal} | ||
return all(0 .< x .< Inf) | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using CUDA, Test, LinearAlgebra, Distributions | ||
|
||
if CUDA.functional() | ||
@testset "rand with CUDA" begin | ||
dists = [ | ||
MvNormal(CUDA.zeros(2), I), | ||
MvNormal(CUDA.zeros(2), cu([1.0 0.5; 0.5 1.0])), | ||
MvLogNormal(CUDA.zeros(2), I), | ||
MvLogNormal(CUDA.zeros(2), cu([1.0 0.5; 0.5 1.0])), | ||
] | ||
|
||
@testset "$dist" for dist in dists | ||
x = rand(CUDA.default_rng(), dist) | ||
@info logpdf(dist, x) | ||
@test x isa CuArray | ||
end | ||
end | ||
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.
Is this needed now (after you removed the test-file you were using)?
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.
This file is needed if we want some of the Flux.jl chain to run properly on GPU. But you are right, it's not used for the current examples---they are all runing on CPUs. I'll remove it later