Skip to content

perf(evaluator): cache Jacobian values so J^T w under L-BFGS reuses them - #164

Open
aarontrowbridge wants to merge 2 commits into
mainfrom
opencode/issue-163-jtprod-cache
Open

aarontrowbridge wants to merge 2 commits into
mainfrom
opencode/issue-163-jtprod-cache

Conversation

@aarontrowbridge

Copy link
Copy Markdown
Member

Closes #163.

What

Under MadNLP's hessian_approximation = "compact_lbfgs", the L-BFGS update asks for $J^\top l$ via MOI.eval_constraint_jacobian_transpose_product on every iteration, and the evaluator answered each request by re-evaluating the full constraint Jacobian. On spline-integrator problems that is ~45% of wall time, booked by MadNLP as "solver (w/o init./fun./lin. alg.)" rather than as function evaluations (profiled: 328 s of 714 s over 4 iterations on a 30-knot ket spline problem; 22,046 s of a 44,335 s production solve).

The evaluator now keeps a two-slot FIFO cache of (exact copy of the packed iterate, Jacobian values), filled by eval_constraint_jacobian and by any product that had to recompute. J·w / Jᵀw at a bit-identical iterate (elementwise isequal, no tolerance) reuse the stored values with zero allocation.

Why two slots: MadNLP 0.9 requests $J^\top l$ at the previous iterate (qn.last_x) right after evaluating $J$ at the current one. A single same-iterate slot gets 1 hit in 15 iterations; LRU gets 2; FIFO with two slots gets 15/15.

Correctness

Returned values are unchanged. A seeded 15-iteration compact_lbfgs solve on the standard test problem has a bit-identical per-iteration trace (k, obj, inf_pr, inf_du), objective, iteration count, and final iterate with the cache on vs off, with hits ≥ iterations − 1. evaluator._jacobian_cache_enabled = false restores the old path (used as the test baseline).

Tests

7 new testitems (hit after eval for both products, zero-allocation hit path, recompute + refresh at a different iterate including a one-ulp change, in-place mutation of the caller's buffer is not a stale hit, cold cache, L-BFGS access pattern, the bit-identical MadNLP solve). Targeted local run: 48/48 pass. Full suite: this PR's CI.

Found along the way (not fixed here)

MadNLPMOI's eval_constraint_jacobian_transpose_product(::Optimizer, …) writes the qp_data (linear-constraint) contribution into Jtv before calling the NLP evaluator, and our evaluator starts with fill!(y, 0), erasing it. For problems with affine MOI constraints the L-BFGS $y_k$ may be missing $-A_\text{lin}^\top l$. Pre-existing; will file separately once confirmed.

Aaron Trowbridge added 2 commits October 24, 2026 13:11
…it-identical iterate (#163)

Under compact_lbfgs MadNLP calls the transpose product every iteration (at
the previous iterate), and each call re-evaluated the full constraint
Jacobian. Keep a two-slot FIFO cache of (copied packed iterate, values),
filled by eval_constraint_jacobian and by recomputing products; hits are
exact (isequal), allocation-free, and counted in _jacobian_cache_hits.
Correct the seam comment: Jᵀw is a per-iteration caller, not only
restoration/robust.
@codecov

codecov Bot commented Oct 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@aarontrowbridge

Copy link
Copy Markdown
Member Author

Production-problem A/B (bosonic GKP |0_L⟩ prep, 30-knot ket spline, state dim 200, 6,083 variables; Modal 8 cores each, same image except DirectTrajOpt): resume from the same checkpoint, 4 MadNLP compact_lbfgs iterations.

v0.11.0 this PR
MadNLP iteration table (k, obj, inf_pr, inf_du, …) — identical, all 5 rows
final F (independent rollout, 1e-12) 0.987249158 0.987249158
NLP function evals 388.8 s 264.6 s
solver (w/o init./fun./lin. alg.) 333.2 s 66.8 s
total 723.9 s 332.9 s (2.17×)

@aarontrowbridge
aarontrowbridge marked this pull request as ready for review October 24, 2026 17:52

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.

perf: cache constraint-Jacobian values so J^T w under L-BFGS does not re-evaluate the Jacobian

1 participant