Skip to content

Use a package extension for Enzyme.jl#312

Merged
amontoison merged 4 commits into
mainfrom
extensions
Feb 14, 2026
Merged

Use a package extension for Enzyme.jl#312
amontoison merged 4 commits into
mainfrom
extensions

Conversation

@amontoison
Copy link
Copy Markdown
Member

@amontoison amontoison commented Nov 26, 2024

12/02/2026: I also remove all the code for Zygote, it is not anymore relevant in 2026...
I prefer to spend time on the support of DI.jl.

@tmigot
Copy link
Copy Markdown
Member

tmigot commented Nov 26, 2024

Connected to #231 ?
If you are going for this, could ou please first drop julia 1.6 for 1.10 from the Project.toml

julia = "^1.6"
(this is breaking change)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Nov 26, 2024

Package name latest stable
JSOSuite
PartiallySeparableNLPModels
PartiallySeparableSolvers
SolverTest

@amontoison
Copy link
Copy Markdown
Member Author

amontoison commented Nov 26, 2024

@tmigot it's a wip for #303 but yes I plan to do a last release for 0.8.8 before.
I will do the breaking changes after.

@amontoison amontoison marked this pull request as ready for review November 27, 2024 16:43
@amontoison amontoison changed the title Extensions Use a package extension for Enzyme.jl Feb 14, 2026
@amontoison
Copy link
Copy Markdown
Member Author

amontoison commented Feb 14, 2026

@michel2323
I try to use OptimalControl.jl with Enzyme as backend for ADNLPModels.jl but I have an error:

using OptimalControl
using NLPModelsIpopt

x0 = [ 0.
       1. ]
A = [  0. 1.
      -1. 0. ]
B = [ 0.
      1. ]
Q = [ 1. 0.
      0. 1. ]
R = 1.
tf = 3.

ocp = @def begin
    t ∈ [0, tf], time
    x ∈ R², state
    u ∈ R, control
    x(0) == x0
    ẋ(t) == A * x(t) + B * u(t)
    0.5∫( x(t)' * Q * x(t) + u(t)' * R * u(t) ) → min
end

docp, nlp = direct_transcription(ocp)

solve(ocp) # error "Cannot determine ordering of Dual tags ForwardDiff.Tag{ReverseDiff.var..."
solve(ocp; adnlp_backend = :enzyme)

The error is:

ERROR: EnzymeRuntimeActivityError: Detected potential need for runtime activity.

Constant memory is stored (or returned) to a differentiable variable and correctness cannot be guaranteed with static activity analysis.
This might be due to the use of a constant variable as temporary storage for active memory (https://enzyme.mit.edu/julia/stable/faq/#faq-runtime-activity).
If Enzyme should be able to prove this use non-differentable, open an issue!

To work around this issue, either:
   a) rewrite this variable to not be conditionally active (fastest performance, slower to setup), or
   b) set the Enzyme mode to turn on runtime activity (e.g. autodiff(set_runtime_activity(Reverse), ...) ). This will maintain correctness, but may slightly reduce performance.

Failure within method:

DOCP_objective(::Vector{Float64}, ::CTDirect.DOCP{CTDirect.Midpoint, Model{…}, CTDirect.ADNLPBackend})
     @ CTDirect ~/.julia/packages/CTDirect/DgVuH/src/docp.jl:614

Hint: catch this exception as `err` and call `code_typed(err)` to inspect the surrounding code.

Mismatched activity for:   %value_phi = phi ptr addrspace(10) [ %6, %L12 ], [ %5, %L9 ] const val:   %5 = load atomic ptr addrspace(10), ptr addrspace(11) %4 unordered, align 8, !dbg !92, !tbaa !94, !alias.scope !98, !noalias !99, !enzyme_type !100, !enzymejl_source_type_Vector\7BFloat64\7D !0, !enzymejl_byref_MUT_REF !0
 Julia value causing error:  Unknown object of type Vector{Float64}
 LLVM view of erring value:     %5 = load atomic ptr addrspace(10), ptr addrspace(11) %4 unordered, align 8, !dbg !92, !tbaa !94, !alias.scope !98, !noalias !99, !enzyme_type !100, !enzymejl_source_type_Vector\7BFloat64\7D !0, !enzymejl_byref_MUT_REF !0

Stacktrace:
 [1] getproperty
   @ ./Base_compiler.jl:54
 [2] get_OCP_variable
   @ ~/.julia/packages/CTDirect/DgVuH/src/disc/common.jl:12
 [3] DOCP_objective
   @ ~/.julia/packages/CTDirect/DgVuH/src/docp.jl:622

Stacktrace:
  [1] getproperty
    @ ./Base_compiler.jl:54 [inlined]
  [2] DOCP_objective
    @ ~/.julia/packages/CTDirect/DgVuH/src/docp.jl:620
  [3] #4
    @ ~/.julia/packages/CTDirect/DgVuH/ext/CTDirectExtADNLP.jl:30 [inlined]
  [4] augmented_julia__4_40863_inner_36wrap
    @ ~/.julia/packages/CTDirect/DgVuH/ext/CTDirectExtADNLP.jl:0
  [5] macro expansion
    @ ~/.julia/packages/Enzyme/S3nC6/src/compiler.jl:6672 [inlined]
  [6] enzyme_call
    @ ~/.julia/packages/Enzyme/S3nC6/src/compiler.jl:6151 [inlined]
  [7] AugmentedForwardThunk
    @ ~/.julia/packages/Enzyme/S3nC6/src/compiler.jl:6099 [inlined]
  [8] autodiff(mode::ReverseMode{…}, f::Const{…}, ::Type{…}, args::Duplicated{…})
    @ Enzyme ~/.julia/packages/Enzyme/S3nC6/src/Enzyme.jl:419
  [9] gradient!(::ADNLPModels.EnzymeReverseADGradient, g::Vector{Float64}, f::Function, x::Vector{Float64})
    @ ADNLPModelsEnzymeExt ~/Bureau/git/ADNLPModels.jl/ext/ADNLPModelsEnzymeExt.jl:73
 [10] grad!(nlp::ADNLPModels.ADNLPModel{Float64, Vector{Float64}, Vector{Int64}}, x::Vector{Float64}, g::Vector{Float64})

The related code is defined here.
I think the culprit is the closure.

cc @ocots @jbcaillau

@amontoison amontoison merged commit 0a2dac4 into main Feb 14, 2026
32 checks passed
@amontoison amontoison deleted the extensions branch February 14, 2026 06:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants