Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.
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
20 changes: 13 additions & 7 deletions ext/OptimizationEnzymeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function set_runtime_activity2(
Enzyme.set_runtime_activity(a, RTA)
end
function_annotation(::Nothing) = Nothing
function_annotation(::AutoEnzyme{<:Any, A}) where A = A
function_annotation(::AutoEnzyme{<:Any, A}) where {A} = A
function OptimizationBase.instantiate_function(f::OptimizationFunction{true}, x,
adtype::AutoEnzyme, p, num_cons = 0;
g = false, h = false, hv = false, fg = false, fgh = false,
Expand Down Expand Up @@ -225,9 +225,12 @@ function OptimizationBase.instantiate_function(f::OptimizationFunction{true}, x,
if func_annot <: Enzyme.Const
basefunc = Enzyme.Const(basefunc)
elseif func_annot <: Enzyme.Duplicated || func_annot <: Enzyme.BatchDuplicated
basefunc = Enzyme.BatchDuplicated(basefunc, Tuple(make_zero(basefunc) for i in 1:length(x)))
elseif func_annot <: Enzyme.DuplicatedNoNeed || func_annot <: Enzyme.BatchDuplicatedNoNeed
basefunc = Enzyme.BatchDuplicatedNoNeed(basefunc, Tuple(make_zero(basefunc) for i in 1:length(x)))
basefunc = Enzyme.BatchDuplicated(basefunc, Tuple(make_zero(basefunc)
for i in 1:length(x)))
elseif func_annot <: Enzyme.DuplicatedNoNeed ||
func_annot <: Enzyme.BatchDuplicatedNoNeed
basefunc = Enzyme.BatchDuplicatedNoNeed(basefunc, Tuple(make_zero(basefunc)
for i in 1:length(x)))
end
# else
# seeds = Enzyme.onehot(zeros(eltype(x), num_cons))
Expand All @@ -241,12 +244,14 @@ function OptimizationBase.instantiate_function(f::OptimizationFunction{true}, x,
Enzyme.make_zero!(jc)
end
Enzyme.make_zero!(y)
if func_annot <: Enzyme.Duplicated || func_annot <: Enzyme.BatchDuplicated || func_annot <: Enzyme.DuplicatedNoNeed || func_annot <: Enzyme.BatchDuplicatedNoNeed
if func_annot <: Enzyme.Duplicated || func_annot <: Enzyme.BatchDuplicated ||
func_annot <: Enzyme.DuplicatedNoNeed ||
func_annot <: Enzyme.BatchDuplicatedNoNeed
for bf in basefunc.dval
Enzyme.make_zero!(bf)
end
end
Enzyme.autodiff(fmode, basefunc , BatchDuplicated(y, Jaccache),
Enzyme.autodiff(fmode, basefunc, BatchDuplicated(y, Jaccache),
BatchDuplicated(θ, seeds), Const(p))
for i in eachindex(θ)
if J isa Vector
Expand Down Expand Up @@ -575,7 +580,8 @@ function OptimizationBase.instantiate_function(f::OptimizationFunction{false}, x
for i in eachindex(Jaccache)
Enzyme.make_zero!(Jaccache[i])
end
Jaccache, y = Enzyme.autodiff(WithPrimal(fmode), f.cons, Duplicated,
Jaccache,
y = Enzyme.autodiff(WithPrimal(fmode), f.cons, Duplicated,
BatchDuplicated(θ, seeds), Const(p))
if size(y, 1) == 1
return reduce(vcat, Jaccache)
Expand Down
53 changes: 34 additions & 19 deletions ext/OptimizationZygoteExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function OptimizationBase.instantiate_function(
adtype, soadtype = OptimizationBase.generate_adtype(adtype)

if g == true && f.grad === nothing
prep_grad = prepare_gradient(f.f, adtype, x, Constant(p), strict=Val(false))
prep_grad = prepare_gradient(f.f, adtype, x, Constant(p), strict = Val(false))
function grad(res, θ)
gradient!(f.f, res, prep_grad, adtype, θ, Constant(p))
end
Expand All @@ -47,7 +47,7 @@ function OptimizationBase.instantiate_function(

if fg == true && f.fg === nothing
if g == false
prep_grad = prepare_gradient(f.f, adtype, x, Constant(p), strict=Val(false))
prep_grad = prepare_gradient(f.f, adtype, x, Constant(p), strict = Val(false))
end
function fg!(res, θ)
(y, _) = value_and_gradient!(f.f, res, prep_grad, adtype, θ, Constant(p))
Expand All @@ -68,7 +68,7 @@ function OptimizationBase.instantiate_function(
hess_sparsity = f.hess_prototype
hess_colors = f.hess_colorvec
if h == true && f.hess === nothing
prep_hess = prepare_hessian(f.f, soadtype, x, Constant(p), strict=Val(false))
prep_hess = prepare_hessian(f.f, soadtype, x, Constant(p), strict = Val(false))
function hess(res, θ)
hessian!(f.f, res, prep_hess, soadtype, θ, Constant(p))
end
Expand All @@ -85,13 +85,17 @@ function OptimizationBase.instantiate_function(

if fgh == true && f.fgh === nothing
function fgh!(G, H, θ)
(y, _, _) = value_derivative_and_second_derivative!(
(y,
_,
_) = value_derivative_and_second_derivative!(
f.f, G, H, prep_hess, soadtype, θ, Constant(p))
return y
end
if p !== SciMLBase.NullParameters() && p !== nothing
function fgh!(G, H, θ, p)
(y, _, _) = value_derivative_and_second_derivative!(
(y,
_,
_) = value_derivative_and_second_derivative!(
f.f, G, H, prep_hess, soadtype, θ, Constant(p))
return y
end
Expand Down Expand Up @@ -143,7 +147,7 @@ function OptimizationBase.instantiate_function(
cons_jac_prototype = f.cons_jac_prototype
cons_jac_colorvec = f.cons_jac_colorvec
if cons !== nothing && cons_j == true && f.cons_j === nothing
prep_jac = prepare_jacobian(cons_oop, adtype, x, strict=Val(false))
prep_jac = prepare_jacobian(cons_oop, adtype, x, strict = Val(false))
function cons_j!(J, θ)
jacobian!(cons_oop, J, prep_jac, adtype, θ)
if size(J, 1) == 1
Expand All @@ -157,7 +161,8 @@ function OptimizationBase.instantiate_function(
end

if f.cons_vjp === nothing && cons_vjp == true && cons !== nothing
prep_pullback = prepare_pullback(cons_oop, adtype, x, (ones(eltype(x), num_cons),), strict=Val(false))
prep_pullback = prepare_pullback(
cons_oop, adtype, x, (ones(eltype(x), num_cons),), strict = Val(false))
function cons_vjp!(J, θ, v)
pullback!(cons_oop, (J,), prep_pullback, adtype, θ, (v,))
end
Expand All @@ -169,7 +174,7 @@ function OptimizationBase.instantiate_function(

if cons !== nothing && f.cons_jvp === nothing && cons_jvp == true
prep_pushforward = prepare_pushforward(
cons_oop, adtype, x, (ones(eltype(x), length(x)),), strict=Val(false))
cons_oop, adtype, x, (ones(eltype(x), length(x)),), strict = Val(false))
function cons_jvp!(J, θ, v)
pushforward!(cons_oop, (J,), prep_pushforward, adtype, θ, (v,))
end
Expand All @@ -182,7 +187,8 @@ function OptimizationBase.instantiate_function(
conshess_sparsity = f.cons_hess_prototype
conshess_colors = f.cons_hess_colorvec
if cons !== nothing && cons_h == true && f.cons_h === nothing
prep_cons_hess = [prepare_hessian(cons_oop, soadtype, x, Constant(i), strict=Val(false))
prep_cons_hess = [prepare_hessian(
cons_oop, soadtype, x, Constant(i), strict = Val(false))
for i in 1:num_cons]

function cons_h!(H, θ)
Expand All @@ -201,7 +207,7 @@ function OptimizationBase.instantiate_function(
if f.lag_h === nothing && cons !== nothing && lag_h == true
lag_extras = prepare_hessian(
lagrangian, soadtype, x, Constant(one(eltype(x))),
Constant(ones(eltype(x), num_cons)), Constant(p), strict=Val(false))
Constant(ones(eltype(x), num_cons)), Constant(p), strict = Val(false))
lag_hess_prototype = zeros(Bool, num_cons, length(x))

function lag_h!(H::AbstractMatrix, θ, σ, λ)
Expand Down Expand Up @@ -294,7 +300,8 @@ function OptimizationBase.instantiate_function(
adtype, soadtype = OptimizationBase.generate_sparse_adtype(adtype)

if g == true && f.grad === nothing
extras_grad = prepare_gradient(f.f, adtype.dense_ad, x, Constant(p), strict=Val(false))
extras_grad = prepare_gradient(
f.f, adtype.dense_ad, x, Constant(p), strict = Val(false))
function grad(res, θ)
gradient!(f.f, res, extras_grad, adtype.dense_ad, θ, Constant(p))
end
Expand All @@ -311,16 +318,19 @@ function OptimizationBase.instantiate_function(

if fg == true && f.fg === nothing
if g == false
extras_grad = prepare_gradient(f.f, adtype.dense_ad, x, Constant(p), strict=Val(false))
extras_grad = prepare_gradient(
f.f, adtype.dense_ad, x, Constant(p), strict = Val(false))
end
function fg!(res, θ)
(y, _) = value_and_gradient!(
(y,
_) = value_and_gradient!(
f.f, res, extras_grad, adtype.dense_ad, θ, Constant(p))
return y
end
if p !== SciMLBase.NullParameters() && p !== nothing
function fg!(res, θ, p)
(y, _) = value_and_gradient!(
(y,
_) = value_and_gradient!(
f.f, res, extras_grad, adtype.dense_ad, θ, Constant(p))
return y
end
Expand All @@ -334,7 +344,7 @@ function OptimizationBase.instantiate_function(
hess_sparsity = f.hess_prototype
hess_colors = f.hess_colorvec
if h == true && f.hess === nothing
prep_hess = prepare_hessian(f.f, soadtype, x, Constant(p), strict=Val(false))
prep_hess = prepare_hessian(f.f, soadtype, x, Constant(p), strict = Val(false))
function hess(res, θ)
hessian!(f.f, res, prep_hess, soadtype, θ, Constant(p))
end
Expand All @@ -354,14 +364,18 @@ function OptimizationBase.instantiate_function(

if fgh == true && f.fgh === nothing
function fgh!(G, H, θ)
(y, _, _) = value_derivative_and_second_derivative!(
(y,
_,
_) = value_derivative_and_second_derivative!(
f.f, G, H, θ, prep_hess, soadtype, Constant(p))
return y
end

if p !== SciMLBase.NullParameters() && p !== nothing
function fgh!(G, H, θ, p)
(y, _, _) = value_derivative_and_second_derivative!(
(y,
_,
_) = value_derivative_and_second_derivative!(
f.f, G, H, θ, prep_hess, soadtype, Constant(p))
return y
end
Expand Down Expand Up @@ -458,7 +472,8 @@ function OptimizationBase.instantiate_function(
conshess_sparsity = f.cons_hess_prototype
conshess_colors = f.cons_hess_colorvec
if cons !== nothing && f.cons_h === nothing && cons_h == true
prep_cons_hess = [prepare_hessian(cons_oop, soadtype, x, Constant(i), strict=Val(false))
prep_cons_hess = [prepare_hessian(
cons_oop, soadtype, x, Constant(i), strict = Val(false))
for i in 1:num_cons]
colores = getfield.(prep_cons_hess, :coloring_result)
conshess_sparsity = getfield.(colores, :A)
Expand All @@ -479,7 +494,7 @@ function OptimizationBase.instantiate_function(
if cons !== nothing && f.lag_h === nothing && lag_h == true
lag_extras = prepare_hessian(
lagrangian, soadtype, x, Constant(one(eltype(x))),
Constant(ones(eltype(x), num_cons)), Constant(p), strict=Val(false))
Constant(ones(eltype(x), num_cons)), Constant(p), strict = Val(false))
lag_hess_prototype = lag_extras.coloring_result.A
lag_hess_colors = lag_extras.coloring_result.color

Expand Down
16 changes: 12 additions & 4 deletions src/OptimizationDIExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,17 @@ function instantiate_function(

if fgh == true && f.fgh === nothing
function fgh!(G, H, θ)
(y, _, _) = value_derivative_and_second_derivative!(
(y,
_,
_) = value_derivative_and_second_derivative!(
f.f, G, H, prep_hess, soadtype, θ, Constant(p))
return y
end
if p !== SciMLBase.NullParameters() && p !== nothing
function fgh!(G, H, θ, p)
(y, _, _) = value_derivative_and_second_derivative!(
(y,
_,
_) = value_derivative_and_second_derivative!(
f.f, G, H, prep_hess, soadtype, θ, Constant(p))
return y
end
Expand Down Expand Up @@ -338,13 +342,17 @@ function instantiate_function(

if fgh == true && f.fgh === nothing
function fgh!(θ)
(y, G, H) = value_derivative_and_second_derivative(
(y,
G,
H) = value_derivative_and_second_derivative(
f.f, prep_hess, adtype, θ, Constant(p))
return y, G, H
end
if p !== SciMLBase.NullParameters() && p !== nothing
function fgh!(θ, p)
(y, G, H) = value_derivative_and_second_derivative(
(y,
G,
H) = value_derivative_and_second_derivative(
f.f, prep_hess, adtype, θ, Constant(p))
return y, G, H
end
Expand Down
22 changes: 16 additions & 6 deletions src/OptimizationDISparseExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ function instantiate_function(
prep_grad = prepare_gradient(f.f, adtype.dense_ad, x, Constant(p))
end
function fg!(res, θ)
(y, _) = value_and_gradient!(
(y,
_) = value_and_gradient!(
f.f, res, prep_grad, adtype.dense_ad, θ, Constant(p))
return y
end
if p !== SciMLBase.NullParameters()
function fg!(res, θ, p)
(y, _) = value_and_gradient!(
(y,
_) = value_and_gradient!(
f.f, res, prep_grad, adtype.dense_ad, θ, Constant(p))
return y
end
Expand Down Expand Up @@ -81,13 +83,17 @@ function instantiate_function(

if fgh == true && f.fgh === nothing
function fgh!(G, H, θ)
(y, _, _) = value_derivative_and_second_derivative!(
(y,
_,
_) = value_derivative_and_second_derivative!(
f.f, G, H, prep_hess, soadtype.dense_ad, θ, Constant(p))
return y
end
if p !== SciMLBase.NullParameters() && p !== nothing
function fgh!(G, H, θ, p)
(y, _, _) = value_derivative_and_second_derivative!(
(y,
_,
_) = value_derivative_and_second_derivative!(
f.f, G, H, prep_hess, soadtype.dense_ad, θ, Constant(p))
return y
end
Expand Down Expand Up @@ -336,14 +342,18 @@ function instantiate_function(

if fgh == true && f.fgh === nothing
function fgh!(θ)
(y, G, H) = value_derivative_and_second_derivative(
(y,
G,
H) = value_derivative_and_second_derivative(
f.f, prep_hess, soadtype, θ, Constant(p))
return y, G, H
end

if p !== SciMLBase.NullParameters() && p !== nothing
function fgh!(θ, p)
(y, G, H) = value_derivative_and_second_derivative(
(y,
G,
H) = value_derivative_and_second_derivative(
f.f, prep_hess, soadtype, θ, Constant(p))
return y, G, H
end
Expand Down
3 changes: 2 additions & 1 deletion test/matrixvalued.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ using Test, ReverseDiff
# 1. Matrix Factorization
@show adtype
function matrix_factorization_objective(X, A)
U, V = @view(X[1:size(A, 1), 1:Int(size(A, 2) / 2)]),
U,
V = @view(X[1:size(A, 1), 1:Int(size(A, 2) / 2)]),
@view(X[1:size(A, 1), (Int(size(A, 2) / 2) + 1):size(A, 2)])
return norm(A - U * V')
end
Expand Down
Loading