Skip to content

Generalize Enzyme forward rules to arbitrary batch width - #41

Merged
ChrisRackauckas merged 2 commits into
SciML:mainfrom
ChrisRackauckas-Claude:fix-enzyme-batch-width
Apr 17, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:mainfrom
ChrisRackauckas-Claude:fix-enzyme-batch-width

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Summary

The three forward rule overloads in FunctionWrappersWrappersEnzymeExt are hardcoded to FwdConfig{..., 1, ...} (width=1). When Enzyme uses batch forward mode (e.g., width=8 for an 8-output Jacobian), no rule matches:

MethodError: no method matching forward(
    ::FwdConfigWidth{8, false, false, true, false},
    ::Const{FunctionWrappersWrapper{...}}, ...)

Fix: Replace the hardcoded 1 with a type parameter W:

  • Shadow-only (W=1): Duplicated{T} → returns T
  • Shadow-only (W>1): BatchDuplicated{T,W} → returns NTuple{W,T}
  • Primal+shadow (W=1): returns Duplicated(primal, shadow)
  • Primal+shadow (W>1): returns BatchDuplicated(primal, shadows)
  • Primal-only: width-independent, unchanged

Reverse mode rules are unaffected.

Fixes OrdinaryDiffEq.jl v7 CI failures in Rosenbrock and BDF Enzyme convergence tests.

🤖 Generated with Claude Code

ChrisRackauckas and others added 2 commits April 17, 2026 09:45
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
ChrisRackauckas merged commit 5845c78 into SciML:main Apr 17, 2026
6 of 7 checks passed
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]>
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