Skip to content
Open
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
14 changes: 11 additions & 3 deletions lib/OptimizationBase/ext/OptimizationEnzymeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,13 @@ function OptimizationBase.instantiate_function(
function hess(res, θ, p = p)
Enzyme.make_zero!(bθ)
Enzyme.make_zero!.(vdbθ)
θ_arr = θ isa Array ? θ : Array(θ)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't correct, it's coercing into an array which for example would not be the correct behavior on GPUArrays.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the existing behavior here:

        vdθ = Tuple((Array(r) for r in eachrow(I(length(x)) * one(eltype(x)))))

already does the conversion [we just need to match this for both primal+shadow]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well then that's the incorrect part that should get fixed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how? you cannot guarantee the primal/shadow are the same type [and index offsets] in a nice way that I can think of offhand otherwise (ofc longer term you can make a helper to adapt the array type, but that's a follow up)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make_zero, Adapt.jl, ArrayInterface.restructure

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think any of those work here, but I'll let you continue it


Enzyme.autodiff(
fmode,
inner_grad,
Const(rmode),
Enzyme.BatchDuplicated(θ, vdθ),
Enzyme.BatchDuplicated(θ_arr, vdθ),
Enzyme.BatchDuplicatedNoNeed(bθ, vdbθ),
Const(f.f),
Const(p)
Expand All @@ -193,17 +194,24 @@ function OptimizationBase.instantiate_function(
function fgh!(G, H, θ, p = p)
vdθ = Tuple((Array(r) for r in eachrow(I(length(θ)) * one(eltype(θ)))))
vdbθ = Tuple(zeros(eltype(θ), length(θ)) for i in eachindex(θ))
θ_arr = θ isa Array ? θ : Array(θ)
G_arr = G isa Array ? G : Array(G)
Enzyme.make_zero!(G_arr)

Enzyme.autodiff(
fmode,
inner_grad,
Const(rmode),
Enzyme.BatchDuplicated(θ, vdθ),
Enzyme.BatchDuplicatedNoNeed(G, vdbθ),
Enzyme.BatchDuplicated(θ_arr, vdθ),
Enzyme.BatchDuplicatedNoNeed(G_arr, vdbθ),
Const(f.f),
Const(p)
)

if !(G isa Array)
copyto!(G, G_arr)
end

for i in eachindex(θ)
H[i, :] .= vdbθ[i]
end
Expand Down
14 changes: 14 additions & 0 deletions lib/OptimizationBase/test/AD/adtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ optprob.cons_h(H3, x0)
optprob.lag_h(H4, x0, σ, μ)
@test H4 ≈ σ * H2 + μ[1] * H3[1] rtol = 1.0e-6

# Test non-Vector AbstractVector (e.g. SubArray) for AutoEnzyme hess and fgh!
x_view = @view zeros(4)[1:2]
optprob_view = OptimizationBase.instantiate_function(
OptimizationFunction(rosenbrock, OptimizationBase.AutoEnzyme()), x_view,
OptimizationBase.AutoEnzyme(), nothing, 0, h = true, fgh = true
)
H_view = Array{Float64}(undef, 2, 2)
G_view = Array{Float64}(undef, 2)
optprob_view.hess(H_view, x_view)
@test H1 == H_view
optprob_view.fgh(G_view, H_view, x_view)
@test G1 == G_view
@test H1 == H_view

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should test other things like GPUArrays, StaticArrays, ComponentArrays, etc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll let you take over this PR then

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's fine.

G2 = Array{Float64}(undef, 2)
H2 = Array{Float64}(undef, 2, 2)

Expand Down
Loading