Nonlinear diff: skip evaluator rebuild when structure is unchanged#370
Open
yeabbratz wants to merge 1 commit into
Open
Nonlinear diff: skip evaluator rebuild when structure is unchanged#370yeabbratz wants to merge 1 commit into
yeabbratz wants to merge 1 commit into
Conversation
_cache_evaluator! rebuilt the MOI.Nonlinear evaluator (AD tapes, Jacobian/Hessian sparsity detection) and the index mappings on every forward_differentiate!/reverse_differentiate! call, although the Cache is a pure function of the Form's structure: parameter values are read live from form.model.parameters by the evaluator, and the primal/dual point lives in model.x/y/s. Return the existing model.cache when there is one, and invalidate it on every structural mutation of the model (add_variable(s), add_constraint, objective function/sense changes; MOI.empty! already reset it). Results are bit-identical to the rebuild path; the per-differentiation setup cost is removed.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #370 +/- ##
==========================================
+ Coverage 91.88% 91.93% +0.05%
==========================================
Files 17 17
Lines 2722 2739 +17
==========================================
+ Hits 2501 2518 +17
Misses 221 221 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
NonLinearProgram._cache_evaluator!rebuilds theMOI.Nonlinearevaluator — AD tapeconstruction, Jacobian/Hessian sparsity detection, coloring — plus all the index mappings on
every
forward_differentiate!/reverse_differentiate!call. But theCacheis a purefunction of the structure of the
Form: parameter values do not live in it (they enter thecomputation through the primal point / the
Form's parameter storage), and the primal/dualpoint lives in
model.x/model.y/model.s. Rebuilding it on a model whose structure isunchanged produces an identical evaluator and identical mappings, so the rebuild is pure
per-call overhead — paid, e.g., by every reverse-after-forward on the same solve, and by every
backward in a parametric training loop once the diff model is instantiated.
This PR makes
_cache_evaluator!return the existingmodel.cachewhen there is one, andinvalidates the cache on every structural mutation of the model (
add_variable(s),add_constraint, objective function/sense changes;MOI.empty!already reset it). InsideDiffOpt's own lifecycle the cache could in fact never go stale — the
Formis populatedexactly once by
MOI.copy_to, and any structural edit at theDiffOpt.Optimizerleveldiscards the whole diff model (
diff = nothing) — but the invalidation hooks make the modelsafe as a standalone
MOI.ModelLiketoo.Results are bit-identical to the rebuild path (same tapes, same sparsity order, same values in,
same FLOP order); only the per-differentiation setup cost is removed. No API change; behavior
is preserved by construction.
Tests
test_evaluator_cache_reused_across_differentiations: repeated reverse (new seeds) andforward calls on one solve return the same
Cacheobject, and the results are==(not≈) to a fresh model differentiated once in the same final state.test_evaluator_cache_invalidated_on_structural_change: objective-sense set andadd_variableon the inner model drop the cache; differentiation rebuilds it.test_evaluator_cache_after_public_structural_edit: adding a JuMP constraint after abackward rebuilds the diff model, and the post-edit gradient matches a fresh model built
directly in the final state.
Benchmark
Median of 7 (after warmup) of a repeated
reverse_differentiate!on the same solve — thediff model is already instantiated, so the evaluator rebuild is exactly the residual this PR
removes. Parameterized NLP with P parameters and P variables (script below), Ipopt, M4 Pro,
1 thread, Julia 1.12.6:
At large P the remaining cost is the dense sensitivity contraction, which is what #369
removes — the two compose.
bench_persistent_evaluator.jl
Context
Same residual-removal series as #369 (single adjoint solve for the nonlinear reverse). In a
downstream differentiable-ACOPF training loop, evaluator reuse was validated bitwise against
the rebuild path across a case14→case2000 ladder and composes with #369 and with keeping the
diff model across parameter-only re-solves (companion
PreserveDiffModelPR) for ×12.6cumulative per-backward at 2000-bus scale.
🤖 Generated with Claude Code