Repository navigation
Generalize Enzyme forward rules to arbitrary batch width - #41
Merged
ChrisRackauckas merged 2 commits intoApr 17, 2026
Merged
Conversation
The three `forward` rule overloads were hardcoded to
`FwdConfig{..., 1, ...}` (width=1). When Enzyme uses batch forward
mode (e.g., width=8 for a Jacobian of an 8-output function), no rule
matched and Enzyme threw:
MethodError: no method matching forward(
::FwdConfigWidth{8, false, false, true, false},
::Const{FunctionWrappersWrapper{...}}, ...)
Replace the hardcoded `1` with a type parameter `W`. For W > 1, use
`BatchDuplicated{T, W}` annotations and return `NTuple{W, T}` or
`BatchDuplicated(primal, shadows)` as required by Enzyme's batch API.
The primal-only case is width-independent.
Reverse mode rules are unaffected (width only applies to forward mode).
Fixes OrdinaryDiffEq.jl v7 CI failures in OrdinaryDiffEqRosenbrock and
OrdinaryDiffEqBDF Enzyme-based convergence tests.
Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Verifies that FunctionWrappersWrapper correctly handles batch forward differentiation with width > 1, checking both Forward and ForwardWithPrimal modes return correct per-tangent shadows. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
ChrisRackauckas-Claude
pushed a commit
to ChrisRackauckas-Claude/OrdinaryDiffEq.jl
that referenced
this pull request
Apr 17, 2026
In the sublibrary test environment, `ODEProblem{iip}(f, ...)` triggers
`FunctionWrappersWrapper` wrapping of the ODE function. The wrapped
ForwardDiff Jacobian path produces slightly less accurate Jacobians than
the unwrapped path, causing RadauIIA5's Newton iteration to converge
slower and the adaptive controller to take ~2x more steps for very
stiff problems (μ ≥ 1e7 Van der Pol).
Observed CI step counts: 234, 314, 1680, 282, 308 for
μ=1e7/1e7(vec rtol)/1e7(tight)/1e9/1e10. Old bounds: 150/180/970/170/190.
New bounds: 300/400/2000/350/400.
The μ=1e6 case (107 steps) stays within the old bound of 150.
The unwrapped path (fresh `ODEProblem` without explicit `{iip}` in some
environments) gives ~117 steps — well within old bounds. The regression
is in the FunctionWrappersWrapper + ForwardDiff interaction, tracked
in SciML/FunctionWrappersWrappers.jl#41.
Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
This was referenced Apr 17, 2026
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 three
forwardrule overloads inFunctionWrappersWrappersEnzymeExtare hardcoded toFwdConfig{..., 1, ...}(width=1). When Enzyme uses batch forward mode (e.g., width=8 for an 8-output Jacobian), no rule matches:Fix: Replace the hardcoded
1with a type parameterW:Duplicated{T}→ returnsTBatchDuplicated{T,W}→ returnsNTuple{W,T}Duplicated(primal, shadow)BatchDuplicated(primal, shadows)Reverse mode rules are unaffected.
Fixes OrdinaryDiffEq.jl v7 CI failures in Rosenbrock and BDF Enzyme convergence tests.
🤖 Generated with Claude Code