Propagate set_runtime_activity through FWW Enzyme forward rules#45
Merged
ChrisRackauckas merged 2 commits intoApr 23, 2026
Merged
Conversation
The forward-mode rules hard-coded plain `Forward` in their delegated `Enzyme.autodiff` calls, silently dropping the outer caller's `set_runtime_activity(Forward)` / `set_strong_zero(Forward)` flags. Enzyme's IR-level activity analysis can't see through FunctionWrappersWrapper's cfunction indirection, so the inner call raised `EnzymeRuntimeActivityError` at `broadcast_unalias` / `mightalias` inside `@. du = …` — even when the user had explicitly set runtime activity on the outer `autodiff` call. Reproducer (confirmed): `Rosenbrock23(autodiff = AutoEnzyme(set_runtime_activity(Enzyme.Forward)))` on any time-dependent in-place ODE RHS that DiffEqBase's `AutoSpecialize` wraps with `wrapfun_iip`. See OrdinaryDiffEq.jl PR #3518. Fix: extract `RuntimeActivity` and `StrongZero` from `FwdConfig` / `RevConfig` type parameters and rebuild the `ForwardMode` used in the delegated `Enzyme.autodiff` call with those flags set. The reverse rules' internal forward-mode helpers are updated analogously. Also adds a test exercising the exact DiffEqBase `wrapfun_iip` shape — a 4-arg `(du, u, p, t)` IIP RHS with `@.` broadcast — under `set_runtime_activity(Forward)`, and a `set_strong_zero(Forward)` case. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
2 tasks
4 tasks
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.
Summary
The existing
FunctionWrappersWrappersEnzymeExtforward-mode ruleshard-coded plain
Forwardwhen delegating toEnzyme.autodiff,silently dropping the outer caller's
set_runtime_activity(Forward)(and
set_strong_zero) flags. Enzyme's static IR-level activityanalysis can't see through the cfunction indirection inside
FunctionWrappersWrapper, so the innerEnzyme.autodiffcall thenraised
EnzymeRuntimeActivityErroratbroadcast_unalias→mightaliasinside@. du = …— even though theuser had explicitly set runtime activity on the outer call.
Motivation
Confirmed reproducer from SciML/OrdinaryDiffEq.jl#3518:
on any time-dependent in-place ODE RHS that DiffEqBase's
AutoSpecializewraps viawrapfun_iip(a 4-argFunctionWrappersWrapperover(du, u, p, t)returningNothing).Dropping the runtime-activity flag through the extension's delegation
broke that end-to-end path.
Bare-
f!vs wrapped-f!reproducer matrix (without this fix):f!(du, u, p, t) = @. du = p*u,set_runtime_activity(Forward)→ OK,ddu == 0.f!wrapped inFunctionWrappersWrapper,set_runtime_activity(Forward)→EnzymeRuntimeActivityError.Mechanism / fix
EnzymeRules.FwdConfig{NeedsPrimal, NeedsShadow, Width, RuntimeActivity, StrongZero}carries the caller's ForwardMode flags as type parameters. The updated
rules extract
RuntimeActivityandStrongZeroand rebuild theForwardModeused for the delegatedEnzyme.autodiffcall viaEnzyme.set_runtime_activity/Enzyme.set_strong_zero. The reverserules' internal forward-mode helpers are updated analogously using
EnzymeRules.runtime_activity(config)/EnzymeRules.strong_zero(config)accessors onRevConfig.Numerical check
The added test set (test/enzyme_tests.jl) exercises the DiffEqBase
wrapfun_iipshape end-to-end:f!(du, u, p, t) = @. du = p*u(∂/∂t = 0) — wrapped +set_runtime_activity(Forward)→ddu ≈ 0, matches ForwardDiff baseline.g!(du, u, p, t) = @. du = sin(t)*u— wrapped +set_runtime_activity(Forward)→ddu ≈ cos(t) .* u, matches ForwardDiff baseline exactly.h!(du, u, p, t) = (du[1] = u[1] * t)underset_strong_zero(Forward)→ correct tangent, confirms the strong-zero flag is also propagated.Unrelated test status: the pre-existing "Enzyme batch forward mode
(width > 1)" test errors on this Julia/Enzyme stack before and after
this change — it fails the
shadow_result[1]::NTuple{W, T}typeassertbecause Enzyme now returns a
@NamedTupleforBatchDuplicated. Outof scope for this PR.
Unblocks
Once a release with this fix is cut,
Rosenbrock23(autodiff = AutoEnzyme(mode = set_runtime_activity(Enzyme.Forward)))(and thesame for other Rosenbrock / W-methods) works on any in-place ODE RHS
routed through DiffEqBase's
AutoSpecialize/wrapfun_iip.Test plan
🤖 Generated with Claude Code