Skip to content
Merged
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
4 changes: 2 additions & 2 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ icnf = ContinuousNormalizingFlows.construct(
nn,
nvars,
naugs;
compute_mode = ContinuousNormalizingFlows.DIVecJacMatrixMode(ADTypes.AutoZygote()),
compute_mode = ContinuousNormalizingFlows.LuxVecJacMatrixMode(ADTypes.AutoZygote()),
tspan = (0.0f0, 13.0f0),
steer_rate = 1.0f-1,
λ₃ = 1.0f-2,
Expand Down Expand Up @@ -79,7 +79,7 @@ icnf2 = ContinuousNormalizingFlows.construct(
nvars,
naugs;
inplace = true,
compute_mode = ContinuousNormalizingFlows.DIVecJacMatrixMode(ADTypes.AutoZygote()),
compute_mode = ContinuousNormalizingFlows.LuxVecJacMatrixMode(ADTypes.AutoZygote()),
tspan = (0.0f0, 13.0f0),
steer_rate = 1.0f-1,
λ₃ = 1.0f-2,
Expand Down
2 changes: 2 additions & 0 deletions src/ContinuousNormalizingFlows.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export construct,
DIJacVecVectorMode,
DIVecJacMatrixMode,
DIJacVecMatrixMode,
LuxVecJacMatrixMode,
LuxJacVecMatrixMode,
ICNFModel,
CondICNFModel,
CondLayer,
Expand Down
128 changes: 128 additions & 0 deletions src/icnf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,134 @@ function augmented_f(
nothing
end

function augmented_f(
u::Any,
p::Any,
::Any,
icnf::ICNF{T, <:LuxVecJacMatrixMode, false, COND, AUGMENTED, STEER, NORM_Z, NORM_J},
mode::TrainMode,
nn::LuxCore.AbstractLuxLayer,
st::NamedTuple,
ϵ::AbstractMatrix{T},
) where {T <: AbstractFloat, COND, AUGMENTED, STEER, NORM_Z, NORM_J}
n_aug = n_augment(icnf, mode)
snn = Lux.StatefulLuxLayer{true}(nn, p, st)
z = u[begin:(end - n_aug - 1), :]
ż = snn(z)
ϵJ = Lux.vector_jacobian_product(snn, icnf.compute_mode.adback, z, ϵ)
l̇ = -sum(ϵJ .* ϵ; dims = 1)
Ė = transpose(if NORM_Z
LinearAlgebra.norm.(eachcol(ż))
else
zrs_Ė = similar(ż, size(ż, 2))
ChainRulesCore.@ignore_derivatives fill!(zrs_Ė, zero(T))
zrs_Ė
end)
ṅ = transpose(if NORM_J
LinearAlgebra.norm.(eachcol(ϵJ))
else
zrs_ṅ = similar(ż, size(ż, 2))
ChainRulesCore.@ignore_derivatives fill!(zrs_ṅ, zero(T))
zrs_ṅ
end)
vcat(ż, l̇, Ė, ṅ)
end

function augmented_f(
du::Any,
u::Any,
p::Any,
::Any,
icnf::ICNF{T, <:LuxVecJacMatrixMode, true, COND, AUGMENTED, STEER, NORM_Z, NORM_J},
mode::TrainMode,
nn::LuxCore.AbstractLuxLayer,
st::NamedTuple,
ϵ::AbstractMatrix{T},
) where {T <: AbstractFloat, COND, AUGMENTED, STEER, NORM_Z, NORM_J}
n_aug = n_augment(icnf, mode)
snn = Lux.StatefulLuxLayer{true}(nn, p, st)
z = u[begin:(end - n_aug - 1), :]
ż = snn(z)
ϵJ = Lux.vector_jacobian_product(snn, icnf.compute_mode.adback, z, ϵ)
du[begin:(end - n_aug - 1), :] .= ż
du[(end - n_aug), :] .= -vec(sum(ϵJ .* ϵ; dims = 1))
du[(end - n_aug + 1), :] .= if NORM_Z
LinearAlgebra.norm.(eachcol(ż))
else
zero(T)
end
du[(end - n_aug + 2), :] .= if NORM_J
LinearAlgebra.norm.(eachcol(ϵJ))
else
zero(T)
end
nothing
end

function augmented_f(
u::Any,
p::Any,
::Any,
icnf::ICNF{T, <:LuxJacVecMatrixMode, false, COND, AUGMENTED, STEER, NORM_Z, NORM_J},
mode::TrainMode,
nn::LuxCore.AbstractLuxLayer,
st::NamedTuple,
ϵ::AbstractMatrix{T},
) where {T <: AbstractFloat, COND, AUGMENTED, STEER, NORM_Z, NORM_J}
n_aug = n_augment(icnf, mode)
snn = Lux.StatefulLuxLayer{true}(nn, p, st)
z = u[begin:(end - n_aug - 1), :]
ż = snn(z)
Jϵ = Lux.jacobian_vector_product(snn, icnf.compute_mode.adback, z, ϵ)
l̇ = -sum(ϵ .* Jϵ; dims = 1)
Ė = transpose(if NORM_Z
LinearAlgebra.norm.(eachcol(ż))
else
zrs_Ė = similar(ż, size(ż, 2))
ChainRulesCore.@ignore_derivatives fill!(zrs_Ė, zero(T))
zrs_Ė
end)
ṅ = transpose(if NORM_J
LinearAlgebra.norm.(eachcol(Jϵ))
else
zrs_ṅ = similar(ż, size(ż, 2))
ChainRulesCore.@ignore_derivatives fill!(zrs_ṅ, zero(T))
zrs_ṅ
end)
vcat(ż, l̇, Ė, ṅ)
end

function augmented_f(
du::Any,
u::Any,
p::Any,
::Any,
icnf::ICNF{T, <:LuxJacVecMatrixMode, true, COND, AUGMENTED, STEER, NORM_Z, NORM_J},
mode::TrainMode,
nn::LuxCore.AbstractLuxLayer,
st::NamedTuple,
ϵ::AbstractMatrix{T},
) where {T <: AbstractFloat, COND, AUGMENTED, STEER, NORM_Z, NORM_J}
n_aug = n_augment(icnf, mode)
snn = Lux.StatefulLuxLayer{true}(nn, p, st)
z = u[begin:(end - n_aug - 1), :]
ż = snn(z)
Jϵ = Lux.jacobian_vector_product(snn, icnf.compute_mode.adback, z, ϵ)
du[begin:(end - n_aug - 1), :] .= ż
du[(end - n_aug), :] .= -vec(sum(ϵ .* Jϵ; dims = 1))
du[(end - n_aug + 1), :] .= if NORM_Z
LinearAlgebra.norm.(eachcol(ż))
else
zero(T)
end
du[(end - n_aug + 2), :] .= if NORM_J
LinearAlgebra.norm.(eachcol(Jϵ))
else
zero(T)
end
nothing
end

@inline function loss(
icnf::ICNF{<:AbstractFloat, <:VectorMode},
mode::TrainMode,
Expand Down
8 changes: 8 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ struct DIJacVecMatrixMode{ADBack <: ADTypes.AbstractADType} <: DIMatrixMode{ADBa
adback::ADBack
end

abstract type LuxMatrixMode{ADBack} <: MatrixMode{ADBack} end
struct LuxVecJacMatrixMode{ADBack <: ADTypes.AbstractADType} <: LuxMatrixMode{ADBack}
adback::ADBack
end
struct LuxJacVecMatrixMode{ADBack <: ADTypes.AbstractADType} <: LuxMatrixMode{ADBack}
adback::ADBack
end

abstract type AbstractICNF{
T <: AbstractFloat,
CM <: ComputeMode,
Expand Down
10 changes: 10 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ end
)
)
end

@inline function jacobian_batched(
icnf::AbstractICNF{T, <:LuxMatrixMode},
f::Lux.StatefulLuxLayer,
xs::AbstractMatrix{<:Real},
) where {T}
y = f(xs)
J = Lux.batched_jacobian(f, icnf.compute_mode.adback, xs)
y, eachslice(J; dims = 3)
end
1 change: 1 addition & 0 deletions test/call_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Test.@testset "Call Tests" begin
# ),
]
compute_modes = ContinuousNormalizingFlows.ComputeMode[
ContinuousNormalizingFlows.LuxVecJacMatrixMode(ADTypes.AutoZygote()),
ContinuousNormalizingFlows.DIVecJacVectorMode(ADTypes.AutoZygote()),
ContinuousNormalizingFlows.DIJacVecVectorMode(ADTypes.AutoZygote()),
ContinuousNormalizingFlows.DIVecJacMatrixMode(ADTypes.AutoZygote()),
Expand Down
1 change: 1 addition & 0 deletions test/fit_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Test.@testset "Fit Tests" begin
# ),
]
compute_modes = ContinuousNormalizingFlows.ComputeMode[
ContinuousNormalizingFlows.LuxVecJacMatrixMode(ADTypes.AutoZygote()),
ContinuousNormalizingFlows.DIVecJacVectorMode(ADTypes.AutoZygote()),
ContinuousNormalizingFlows.DIJacVecVectorMode(ADTypes.AutoZygote()),
ContinuousNormalizingFlows.DIVecJacMatrixMode(ADTypes.AutoZygote()),
Expand Down
2 changes: 1 addition & 1 deletion test/instability_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Test.@testset "Instability" begin
nn,
nvars,
naugs;
compute_mode = ContinuousNormalizingFlows.DIVecJacMatrixMode(ADTypes.AutoZygote()),
compute_mode = ContinuousNormalizingFlows.LuxVecJacMatrixMode(ADTypes.AutoZygote()),
tspan = (0.0f0, 13.0f0),
steer_rate = 1.0f-1,
λ₃ = 1.0f-2,
Expand Down
2 changes: 1 addition & 1 deletion test/regression_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Test.@testset "Regression Tests" begin
nn,
nvars,
naugs;
compute_mode = ContinuousNormalizingFlows.DIVecJacMatrixMode(ADTypes.AutoZygote()),
compute_mode = ContinuousNormalizingFlows.LuxVecJacMatrixMode(ADTypes.AutoZygote()),
tspan = (0.0f0, 13.0f0),
steer_rate = 1.0f-1,
λ₃ = 1.0f-2,
Expand Down
Loading