Skip to content

_init_originator_gradient: wrap nested Enzyme.gradient with set_runtime_activity#1458

Draft
ChrisRackauckas-Claude wants to merge 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:enzyme-init-originator-runtime-activity
Draft

_init_originator_gradient: wrap nested Enzyme.gradient with set_runtime_activity#1458
ChrisRackauckas-Claude wants to merge 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:enzyme-init-originator-runtime-activity

Conversation

@ChrisRackauckas-Claude
Copy link
Copy Markdown
Contributor

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented May 26, 2026

Please ignore this PR until reviewed by @ChrisRackauckas.

Summary

The EnzymeOriginator overload of _init_originator_gradient (src/concrete_solve.jl:377) called

Enzyme.gradient(Enzyme.Reverse, Enzyme.Const(f), tunables)

without set_runtime_activity. The outer user-level Enzyme.autodiff(set_runtime_activity(Reverse), ...) call's runtime-activity relaxation 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 pattern (see PR #1455).

This patch wraps the inner gradient with set_runtime_activity(Reverse) so the nested gradient inherits the same activity relaxation as the outer call.

Diff

function _init_originator_gradient(::SciMLBase.EnzymeOriginator, f, tunables)
    return Enzyme.gradient(
        Enzyme.set_runtime_activity(Enzyme.Reverse),
        Enzyme.Const(f), tunables,
    )[1]
end

Effect on #1455

After this patch the EnzymeRuntimeActivityError is gone, but the @test_broken Enzyme block in test/parameter_initialization.jl still trips a different downstream error: MethodError: no method matching MixedDuplicated(::ODESolution{...}) inside Enzyme's runtime-activity wrapping of the ODESolution type. That is a separate Enzyme/MTK problem unrelated to the inner-gradient-activity issue this PR fixes. So #1455 stays @test_broken for now; this PR is independently useful for anyone hitting the runtime-activity error from a clean Enzyme call path.

Test plan

…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>
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