-
-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix and refactor normalization layers
- Loading branch information
1 parent
eddc9cb
commit ff18866
Showing
14 changed files
with
584 additions
and
370 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
import CUDA.CUDNN: batchnorm, ∇batchnorm | ||
|
||
(BN::Flux.BatchNorm)(x::Union{CuArray{T,2},CuArray{T,4},CuArray{T,5}}, cache = nothing) where T<:Union{Float32, Float64} = | ||
BN.λ.(batchnorm(BN.γ, BN.β, x, BN.μ, BN.σ², BN.momentum; cache = cache, alpha = 1, beta = 0, eps = BN.ϵ, training = Flux.istraining())) | ||
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 wronng number of channels" | ||
return BN.λ.(batchnorm(BN.γ, BN.β, x, BN.μ, BN.σ², BN.momentum; | ||
cache=cache, alpha=1, beta=0, eps=BN.ϵ, | ||
training=Flux._isactive(BN))) | ||
end | ||
|
||
@adjoint batchnorm(g, b, x, running_mean, running_var, momentum; kw...) = | ||
batchnorm(g, b, x, running_mean, running_var, momentum; kw...), Δ -> (∇batchnorm(g, b, x, Δ, running_mean, running_var, momentum; kw...)..., nothing, nothing, nothing) | ||
@adjoint function batchnorm(g, b, x, running_mean, running_var, momentum; kw...) | ||
y = batchnorm(g, b, x, running_mean, running_var, momentum; kw...) | ||
function batchnorm_pullback(Δ) | ||
∇batchnorm(g, b, x, Δ, running_mean, running_var, momentum; kw...)..., nothing, nothing, nothing | ||
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 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
Oops, something went wrong.