Re-apply: Widen UJacobianWrapper.p for nested ForwardDiff through Rosenbrock (#3381)#3587
Open
ChrisRackauckas-Claude wants to merge 4 commits into
Conversation
7494130 to
67d66cb
Compare
3 tasks
…ciML#3381) Re-applies the changes from PR SciML#3414 on top of current master. The original PR was reverted by SciML#3586; this restores it now that SciML#3488 (JacReuseState dtgamma fix) has landed and the two changes coexist without conflict (they touch disjoint files). Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Move the nested-Dual `p` widening from per-`jacobian!`-call to once at cache build time. The inner Dual eltype is fixed once `JacobianConfig` is built, so the widened `uf` can be cached in the Rosenbrock cache struct directly. `jacobian!` still calls `_widen_uf_p_for_jac`, but the fast-path (`Tp === inner_T && return f`) returns the cached `uf` unchanged with zero allocations. Measured on the issue SciML#3381 nested-Dual MWE: - Before: 240 bytes/jacobian! call (one Vector{nested_dual} convert + one UJacobianWrapper struct rebuild via `@set f.p`). - After: 0 bytes/jacobian! call. Non-nested case is unchanged (already 0 bytes/call via the `Tp <: ForwardDiff.Dual || return f` fast-path). Patches all four IIP cache builders in `rosenbrock_caches.jl`: Rosenbrock23, Rosenbrock32, RodasTableauAlgorithms, HybridExplicitImplicitRK. Other algorithms (BDF, SDIRK, FIRK, …) keep the per-call widen for now; the same pattern can be lifted into their caches as a follow-up. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Asserts the cache-time pre-widen invariant from the previous commit: - `eltype(integ.cache.uf.p)` must equal the inner nested-Dual eltype. - `_widen_uf_p_for_jac` on the cached uf must allocate zero bytes (fast-path returns `f` unchanged). Catches regressions in either direction: a missing pre-widen line in a cache builder fails the eltype check; a broken fast-path in the helper fails the @allocated check. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
67d66cb to
89bb3f7
Compare
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.
Reopens the fix from #3414, which was reverted by #3586. Rebased onto current master.
Why now
Per @ChrisRackauckas's comment on #3381 (2026-04-30), this is the intended fix for the tag-ordering issue (
UJacobianWrapper.pcarrying the outerNonlinearSolveTagDuals while the inner Rosenbrock FD needs to wrap them again). #3488 has since merged and fixes a different nested-FD path (JacReuseStatedtgamma storage, #3486) — the two changes touch disjoint files and coexist cleanly.Diff vs. master
Same as the original #3414 — 4 files, 157 insertions / 11 deletions. The revert in #3586 only touched these 4 files; reverting that revert produced no conflicts against #3488's changes (
derivative_utils.jl,rosenbrock_caches.jl).lib/DiffEqBase/ext/DiffEqBaseForwardDiffExt.jllib/OrdinaryDiffEqDifferentiation/src/derivative_wrappers.jllib/OrdinaryDiffEqDifferentiation/test/nested_forwarddiff_tests.jl(new)lib/OrdinaryDiffEqDifferentiation/test/runtests.jlTest plan
FirstAutodiffJacError.🤖 Generated with Claude Code