Skip to content

Fix DI prep type-mismatch fallbacks for hess/hv/fg/fgh/lag_h - #1408

Draft
ChrisRackauckas-Claude wants to merge 3 commits into
SciML:masterfrom
ChrisRackauckas-Claude:opt-prep-fallbacks
Draft

ChrisRackauckas-Claude wants to merge 3 commits into
SciML:masterfrom
ChrisRackauckas-Claude:opt-prep-fallbacks

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

Summary

Collapses the DI prep-validity fallbacks in OptimizationDIExt.jl onto shared Type-parameter helper closures (including grad!/cons_j!/lag_h!/_ad_cons_jac), fixing the duplicated NullParameters branches. Also switches AD-generated fgh! from DI's scalar value_derivative_and_second_derivative(!) to value_gradient_and_hessian(!) so the matching-type fast path works, gates hv! on the tangent v type (built from zero(x) so SVector tangents hit the prepared path), and adds foreign-type coverage for IIP packed-vector and OOP lag_h.

Out of scope (still broken, not addressed here): Dual p with second-order ops raises DualMismatchError (ForwardDiff tag nesting, even with a fresh Dual-p prep); OOP SVector solves through OptimJL/Optimisers remain wrapper bugs beyond the unit-level instantiate_function fallback.

Verification

Regression (dual_tolerant_tests.jl):

  • before (src stashed): matching-type fgh! → MethodError on value_derivative_and_second_derivative!
  • after: matching-type fgh and foreign-type hess/hv/fg/fgh/lag_h (incl. IIP packed-vector + OOP lag_h) pass

Matching-type path unchanged (n=10, min ns, master vs PR; identical allocations):

op master PR
iip grad! 137.7 139.1
iip hess! 2528 2553
iip fg! 147.4 146.8
iip hv! 561 565
oop hess 2797 2751
oop fg 170.3 169.4
oop hv 604 606

Test groups (Julia 1, OPTIMIZATION_TEST_GROUP):

  • AD: 1045 Pass
  • QA: 20 Pass + 1 Broken

Probes: Float32/BigFloat/SVector/hv-with-mismatched-v and matching fgh! match fresh-prep results; Dual-p second-order still DualMismatchError (out of scope).

Not verified

  • Full end-to-end a4_generic.jl Optim/Optimisers SVector solve matrix (unit-level covered; wrapper bugs out of scope).
  • OptimizationDISparseExt second-order gates (still only cons_j-style).

Reviewer pushback

  • Whether Dual-p second-order / Optim SVector wrappers should be in-scope follow-ups.
  • Whether the Type-param helper surface is the right long-term shape vs a macro.

Please ignore until reviewed by @ChrisRackauckas.

Risk assessment

Independent review: Claude Code (claude-opus-5-5), 3 rounds; round 3 MERGE, risk low (confidence medium-high): matching-type path identical to master (prepared calls, allocations, timings), 46 foreign-type probe cases match a fresh prep, AD 1045/1045 and QA pass; remaining CI failures are known on master (MTK downstream, Reactant downgrade, Manopt Core) or Python-setup infrastructure (PyCMA, SciPy).

🤖 Generated with Cursor Agent 2026.10.01-14929f9 (model: unknown (Cursor auto)), transcript /home/crackauc/sandbox/goals/performance/jobs/opt/opt-prep/log.txt on amdci2; orchestrated by Claude Code (claude-opus-5-5[1m]) https://claude.ai/code/session_01LPHREnnonfLg1VcE1EJovv

ChrisRackauckas and others added 3 commits October 10, 2026 02:30
Extend the existing `_prep_valid` gate beyond grad!/cons_j! so second-order
and combined DI operators fall back to prep-free calls when θ/p eltypes or
array types differ from construction, avoiding PreparationMismatchError.

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Cursor Agent <noreply@cursor.com>
Agent-Harness: Cursor Agent 2026.10.01-14929f9
Agent-Model: unknown (Cursor auto)
Agent-Session: local session, transcript /home/crackauc/sandbox/goals/performance/jobs/opt/opt-prep/log.txt on amdci2
Share Type-param helper closures (incl. grad!/cons_j!/lag_h!), drop
duplicated NullParameters branches, use value_gradient_and_hessian(!)
for fgh!, and gate hv! on the tangent type.

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Cursor Agent <noreply@cursor.com>
Agent-Harness: Cursor Agent 2026.10.01-14929f9
Agent-Model: unknown (Cursor auto)
Agent-Session: local session, transcript /home/crackauc/sandbox/goals/performance/jobs/opt/opt-prep/log.txt on amdci2
Named _iip/_oop_ad_cons_jac_clos match the other Type-param factories,
build hv prep tangents from zero(x) so SVector v hits the prepared path,
and cover IIP packed-vector plus OOP lag_h foreign types.

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Cursor Agent <noreply@cursor.com>
Agent-Harness: Cursor Agent 2026.10.01-14929f9
Agent-Model: unknown (Cursor auto)
Agent-Session: local session, transcript /home/crackauc/sandbox/goals/performance/jobs/opt/opt-prep/log.txt on amdci2

This branch has not been deployed

No deployments
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