Add MixedDuplicated(::T, ::T) shim for Enzyme#3126 workaround#1459
Draft
ChrisRackauckas-Claude wants to merge 2 commits into
Draft
Add MixedDuplicated(::T, ::T) shim for Enzyme#3126 workaround#1459ChrisRackauckas-Claude wants to merge 2 commits into
ChrisRackauckas-Claude wants to merge 2 commits into
Conversation
…vity The `EnzymeOriginator` overload at concrete_solve.jl:377 called `Enzyme.gradient(Enzyme.Reverse, Const(f), tunables)` without `set_runtime_activity`. The outer user-level call's `set_runtime_activity(Reverse)` flag does not propagate into this nested gradient, so the inner Enzyme call raised `EnzymeRuntimeActivityError` at MTK's init `remake` even when the caller used the documented Enzyme pattern. Wrap with `set_runtime_activity(Reverse)` so the nested gradient respects the same activity relaxation as the outer call. Unblocks the `@test_broken` Enzyme block in `test/parameter_initialization.jl` (SciML#1455). Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Enzyme's `runtime_generic_augfwd` (`create_activity_wrapper` in
`jitrules.jl`) constructs `MixedDuplicated(primarg, shadowarg)` with the
shadow as a bare `T` rather than `Base.RefValue{T}`. The only constructor
defined in `EnzymeCore.MixedDuplicated` requires `Base.RefValue{T}` for
the shadow, so the bare-`T` call raises a MethodError, breaking
reverse-mode AD chains that capture `ODESolution` (e.g. the MTK
parameter-init path).
Add a type-piracy shim in `src/enzyme_rules.jl` that wraps the shadow in
`Base.RefValue` before delegating to the upstream constructor. The
existing constructor's `check::Bool` parameter is forwarded but is
currently unused by EnzymeCore, so the shim is invariant-preserving.
Flip the two `@test_broken` blocks that were blocked specifically on this
MethodError back to `@test`:
- `test/parameter_initialization.jl` (Adjoint through Prob via Enzyme)
- `test/mtk.jl` (MTK Forward Mode Enzyme block, line 204)
Remove the shim once EnzymeAD/Enzyme.jl#3126 lands upstream.
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
wsmoses
requested changes
May 26, 2026
Contributor
wsmoses
left a comment
There was a problem hiding this comment.
This is absolutely wrong and type piracy and should not be done
Member
|
I know it's just testing CI to see if it would make the test pass. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Please ignore until reviewed by @ChrisRackauckas.
Summary
Enzyme's
runtime_generic_augfwd→create_activity_wrapper(Enzyme/src/rules/jitrules.jl line 14) constructsMixedDuplicated(primarg, shadowarg)withshadowarg::Trather thanBase.RefValue{T}.EnzymeCore.MixedDuplicatedonly definesMixedDuplicated(::T, ::Base.RefValue{T}), so the bare-Tcall raises a MethodError. This blocks reverse-mode AD chains that captureODESolution(e.g. the MTK parameter-init path) — exactly the residual blocker tracked in EnzymeAD/Enzyme.jl#3126.Until #3126 lands upstream, this PR adds a workaround:
src/enzyme_rules.jlthat interceptsMixedDuplicated(x::T, dx::T, check::Bool=true)and delegates to the existing constructor by wrapping the shadow inBase.RefValue. The constructor'scheck::Boolflag is currently unused byEnzymeCore, so the shim is invariant-preserving.@test_brokenblocks blocked specifically on this MethodError back to@test:test/parameter_initialization.jl: "Adjoint through Prob (Enzyme)"test/mtk.jl: MTK Forward Mode Enzyme block (line 204)@test_brokenintest/mtk.jl(line 230) is unrelated and left alone.Risk acknowledgment (type piracy)
This is type piracy on
EnzymeCore. Concerns:EnzymeCore.MixedDuplicatedthat fires for any(::T, ::T)call site. IfEnzyme(or downstream) ever callsMixedDuplicated(::T, ::T)with a legitimate intent (e.g. aT = Base.RefValue{S}primal whose shadow really is a bareRefValue{S}), this shim silently double-wraps and the result is wrong. I have not found such a call site in the current Enzyme tree — every otherMixedDuplicatedconstruction either explicitly wraps inBase.RefValueor goes through the brokencreate_activity_wrapperpath — but the surface is wide.@test_brokenflips, return to@test_broken, and wait for Enzyme#3126 to land upstream.CI will surface any breakage of other Enzyme code paths via the full test matrix.
Verification
I attempted to run
Pkg.test()on Julia 1.12 withGROUP=Core8locally.SciMLSensitivityprecompiles cleanly with the shim. The actualparameter_initialization.jltest entered the Enzyme reverse-mode compilation phase and was still grinding through code generation past 25 minutes elapsed (with 4+ GB RSS) when I had to push for CI verification. I did NOT see a final pass/fail locally. CI here is the authoritative verification — please review CI results before merging.Removal plan
When EnzymeAD/Enzyme.jl#3126 lands upstream, delete:
MixedDuplicated(x::T, dx::T, ...)method insrc/enzyme_rules.jlimport Enzyme.EnzymeCore: MixedDuplicatedlineTests should continue to pass on the fixed Enzyme version.
🤖 Generated with Claude Code