-
-
Notifications
You must be signed in to change notification settings - Fork 609
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add CUDACUDNN extension * why test is complaining about cuDNN not a dependency? * try require cuDNN in Flux * news and cleanup * require Main * relax test GroupedConvTranspose gpu test
- Loading branch information
1 parent
34a5f98
commit d3a083c
Showing
36 changed files
with
295 additions
and
197 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Flux, CUDA | ||
|
||
BN = BatchNorm(3) |> gpu; | ||
x = randn(2, 2, 3, 4) |> gpu; | ||
|
||
NNlib.batchnorm(BN.γ, BN.β, x, BN.μ, BN.σ², BN.momentum; | ||
alpha=1, beta=0, eps=BN.ϵ, | ||
training=Flux._isactive(BN, x)) | ||
|
This file contains 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
This file contains 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
File renamed without changes.
File renamed without changes.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module FluxCUDAExt | ||
|
||
using Flux | ||
import Flux: _cuda | ||
using Flux: FluxCPUAdaptor, FluxCUDAAdaptor, fmap | ||
using CUDA | ||
using NNlib | ||
using Zygote | ||
using ChainRulesCore | ||
using Random | ||
using Adapt | ||
import Adapt: adapt_storage | ||
|
||
|
||
const USE_CUDA = Ref{Union{Nothing, Bool}}(nothing) | ||
|
||
function check_use_cuda() | ||
if !isnothing(USE_CUDA[]) | ||
return | ||
end | ||
|
||
USE_CUDA[] = CUDA.functional() | ||
if !USE_CUDA[] | ||
@info """ | ||
The CUDA function is being called but CUDA.jl is not functional. | ||
Defaulting back to the CPU. (No action is required if you want to run on the CPU). | ||
""" maxlog=1 | ||
end | ||
return | ||
end | ||
|
||
ChainRulesCore.@non_differentiable check_use_cuda() | ||
|
||
include("functor.jl") | ||
|
||
function __init__() | ||
Flux.CUDA_LOADED[] = true | ||
|
||
try | ||
Base.require(Main, :cuDNN) | ||
catch | ||
@warn """Package cuDNN not found in current path. | ||
- Run `import Pkg; Pkg.add(\"cuDNN\")` to install the cuDNN package, then restart julia. | ||
- If cuDNN is not installed, some Flux functionalities will not be available when running on the GPU. | ||
""" | ||
end | ||
end | ||
|
||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
adapt_storage(to::FluxCUDAAdaptor, x) = CUDA.cu(x) | ||
adapt_storage(to::FluxCUDAAdaptor, x::Zygote.FillArrays.AbstractFill) = CUDA.cu(collect(x)) | ||
adapt_storage(to::FluxCUDAAdaptor, x::Random.TaskLocalRNG) = CUDA.default_rng() | ||
adapt_storage(to::FluxCUDAAdaptor, x::CUDA.RNG) = x | ||
adapt_storage(to::FluxCUDAAdaptor, x::AbstractRNG) = | ||
error("Cannot map RNG of type $(typeof(x)) to GPU. GPU execution only supports Random.default_rng().") | ||
|
||
# TODO: figure out the correct design for OneElement | ||
adapt_storage(to::FluxCUDAAdaptor, x::Zygote.OneElement) = CUDA.cu(collect(x)) | ||
|
||
adapt_storage(to::FluxCPUAdaptor, x::T) where T <: CUDA.CUSPARSE.CUDA.CUSPARSE.AbstractCuSparseMatrix = adapt(Array, x) | ||
adapt_storage(to::FluxCPUAdaptor, x::CUDA.RNG) = Random.default_rng() | ||
|
||
function ChainRulesCore.rrule(::typeof(Adapt.adapt_storage), to::FluxCPUAdaptor, x::CUDA.AbstractGPUArray) | ||
adapt_storage(to, x), dx -> (NoTangent(), NoTangent(), adapt_storage(FluxCUDAAdaptor(), unthunk(dx))) | ||
end | ||
|
||
ChainRulesCore.rrule(::typeof(adapt), a::FluxCPUAdaptor, x::AnyCuArray) = | ||
adapt(a, x), Δ -> (NoTangent(), NoTangent(), adapt(FluxCUDAAdaptor(), unthunk(Δ))) | ||
|
||
ChainRulesCore.rrule(::typeof(adapt), a::FluxCUDAAdaptor, x::AnyCuArray) = | ||
adapt(a, x), Δ -> (NoTangent(), NoTangent(), Δ) | ||
|
||
ChainRulesCore.rrule(::typeof(adapt), a::FluxCUDAAdaptor, x::AbstractArray) = | ||
adapt(a, x), Δ -> (NoTangent(), NoTangent(), adapt(FluxCPUAdaptor(), unthunk(Δ))) | ||
|
||
function _cuda(x) | ||
check_use_cuda() | ||
USE_CUDA[] || return x | ||
fmap(x -> Adapt.adapt(FluxCUDAAdaptor(), x), x; exclude=Flux._isleaf) | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rng_from_array(::CuArray) = CUDA.default_rng() |
34 changes: 25 additions & 9 deletions
34
src/cuda/cudnn.jl → ext/FluxCUDAcuDNNExt/FluxCUDAcuDNNExt.jl
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,37 @@ | ||
import NNlibCUDA: batchnorm, ∇batchnorm | ||
module FluxCUDAcuDNNExt | ||
|
||
using Flux | ||
using CUDA, cuDNN | ||
using NNlib | ||
|
||
const USE_CUDNN = Ref{Union{Nothing, Bool}}(nothing) | ||
|
||
function check_use_cudnn() | ||
if !isnothing(USE_CUDNN[]) | ||
return | ||
end | ||
|
||
USE_CUDNN[] = cuDNN.has_cudnn() | ||
if !USE_CUDNN[] | ||
@warn """ | ||
cuDNN.jl didn't found libcudnn, some Flux functionality will not be available. | ||
""" maxlog=1 | ||
end | ||
return | ||
end | ||
|
||
function (BN::Flux.BatchNorm)(x::Union{CuArray{T,2},CuArray{T,4},CuArray{T,5}}, | ||
cache=nothing) where T<:Union{Float32, Float64} | ||
|
||
@assert BN.affine "BatchNorm: only affine=true supported on gpu" | ||
@assert BN.track_stats "BatchNorm: only track_stats=true supported on gpu" | ||
@assert length(BN.β) == size(x, ndims(x)-1) "BatchNorm: input has wrong number of channels" | ||
return BN.λ.(batchnorm(BN.γ, BN.β, x, BN.μ, BN.σ², BN.momentum; | ||
|
||
return BN.λ.(NNlib.batchnorm(BN.γ, BN.β, x, BN.μ, BN.σ², BN.momentum; | ||
cache=cache, alpha=1, beta=0, eps=BN.ϵ, | ||
training=Flux._isactive(BN, x))) | ||
end | ||
|
||
function ChainRulesCore.rrule(::typeof(batchnorm), g, b, x, running_mean, running_var, momentum; kw...) | ||
y = batchnorm(g, b, x, running_mean, running_var, momentum; kw...) | ||
function batchnorm_pullback(Δ) | ||
grad = ∇batchnorm(g, b, x, unthunk(Δ), running_mean, running_var, momentum; kw...) | ||
(NoTangent(), grad..., NoTangent(), NoTangent(), NoTangent()) | ||
end | ||
y, batchnorm_pullback | ||
|
||
|
||
end |
This file contains 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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.