Commit 4d3f0d6
Widen UJacobianWrapper.p for nested ForwardDiff (#3381)
When a Rosenbrock integrator runs inside an outer ForwardDiff layer, the
stored `p` in `UJacobianWrapper` is a `Vector{<:Dual}` at the outer Dual
level, but the inner Jacobian computation widens `u` into a deeper
nested-Dual type via its prepared `JacobianConfig`. The user
`f(du, u, p, t)` body then multiplies `p[i] * u[i]` across two different
Dual nesting levels, which dispatches through ForwardDiff's
`tagcount`-based tag precedence. That precedence is a `@generated`
function whose literal value is baked at first compile, so its ordering
depends on which package's precompile cache first instantiated a given
tag type — in nested scenarios this can invert nesting and produce a
triple-nested `Dual` that crashes `setindex!(du, ...)` with
`Float64(::nested_dual)`.
Fix: in `jacobian!`, inspect the prepared `ForwardDiff.JacobianConfig`
for the inner xdual buffer's element type and lift `uf.p` into that
nested-Dual type ahead of time via a fresh `UJacobianWrapper`. The
widened `p` carries zero inner partials (correct — `p` does not depend
on `u`), and the fast path is a single type-stable dispatch returning
`f` unchanged so there is no overhead for non-nested calls.
Add a regression test exercising the precise call graph
(outer-Dual `p`, inner Rosenbrock Jacobian) that crashes pre-fix with a
`FirstAutodiffJacError(MethodError(Float64, (nested_dual,)))` and
succeeds post-fix.
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent dff8c62 commit 4d3f0d6
4 files changed
Lines changed: 106 additions & 3 deletions
File tree
- lib/OrdinaryDiffEqDifferentiation
- src
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
Lines changed: 43 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
185 | 185 | | |
186 | 186 | | |
187 | 187 | | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
188 | 227 | | |
189 | 228 | | |
190 | 229 | | |
| |||
240 | 279 | | |
241 | 280 | | |
242 | 281 | | |
| 282 | + | |
| 283 | + | |
243 | 284 | | |
244 | 285 | | |
245 | | - | |
| 286 | + | |
246 | 287 | | |
247 | 288 | | |
248 | 289 | | |
249 | 290 | | |
250 | | - | |
| 291 | + | |
251 | 292 | | |
252 | 293 | | |
253 | 294 | | |
| |||
Lines changed: 61 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
0 commit comments