Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
refactor: remove first(batchnorm_cudnn)
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Jul 13, 2024
1 parent a251ea8 commit e53c05a
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 19 deletions.
18 changes: 0 additions & 18 deletions ext/LuxLibTrackercuDNNExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,6 @@ using LuxLib: LuxLib
using Tracker: Tracker, TrackedVector, TrackedArray

# api/batchnorm.jl
const TR_CUDNN_BN_ARRAY_TYPE = Union{
TrackedArray{<:Any, <:Any, <:CuArray{<:Union{Float32, Float64}, 2}},
TrackedArray{<:Any, <:Any, <:CuArray{<:Union{Float32, Float64}, 4}},
TrackedArray{<:Any, <:Any, <:CuArray{<:Union{Float32, Float64}, 5}}}
const TR_BNParamType = Union{
Nothing, TrackedArray{<:Any, <:Any, <:CuVector{<:Union{Float32, Float64}}},
CuVector{<:Union{Float32, Float64}}}

function LuxLib.batchnorm(
x::TR_CUDNN_BN_ARRAY_TYPE, scale::TR_BNParamType, bias::TR_BNParamType,
running_mean::TR_BNParamType, running_var::TR_BNParamType, training::Val,
σ::F=identity, momentum::Real=0.1f0, epsilon::Real=1.0f-5) where {F}
rm, rv = LuxLib._get_batchnorm_statistics(x, running_mean, running_var, training)
# NOTE: The following returns a tracked tuple so we can't do `first` on it
x_ = LuxLib.batchnorm_cudnn(rm, rv, scale, bias, x, momentum, epsilon, training)[1]
return LuxLib.fast_activation!!(σ, x_), (; running_mean=rm, running_var=rv)
end

for RM in (:TrackedVector, :Nothing, :AbstractVector),
RV in (:TrackedVector, :Nothing, :AbstractVector),
S in (:TrackedVector, :Nothing, :AbstractVector),
Expand Down
2 changes: 1 addition & 1 deletion ext/LuxLibcuDNNExt/LuxLibcuDNNExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function LuxLib.batchnorm(x::CUDNN_BN_ARRAY_TYPE, scale::BNParamType, bias::BNPa
running_mean::BNParamType, running_var::BNParamType, training::Val,
σ::F=identity, momentum::Real=0.1f0, epsilon::Real=1.0f-5) where {F}
rm, rv = LuxLib._get_batchnorm_statistics(x, running_mean, running_var, training)
x_ = first(LuxLib.batchnorm_cudnn(rm, rv, scale, bias, x, momentum, epsilon, training))
x_ = LuxLib.batchnorm_cudnn(rm, rv, scale, bias, x, momentum, epsilon, training)[1]
return LuxLib.fast_activation!!(σ, x_), (; running_mean=rm, running_var=rv)
end

Expand Down

0 comments on commit e53c05a

Please sign in to comment.