Repository navigation
Optimize sparse jacobian2W diagonal updates - #4663
Conversation
Update stored CSC diagonal entries directly and preserve insertion behavior for missing diagonals. Add sparse coverage for both structural cases. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Codex <[email protected]> Agent-Harness: Codex CLI codex-cli 0.157.1 Agent-Model: gpt-6-luna Agent-Session: local codex session (log /home/crackauc/sandbox/goals/performance/jobs/pr-jac2w-sparse/log.txt)
Ignore `_update_sparse_diagonal!` in Differentiation QA, cover the mixed fast-write/insertion path, and reject non-square W before the column loop. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Cursor Agent <[email protected]> Agent-Harness: Cursor Agent 2026.09.26-dd393fe Agent-Model: unknown (Cursor auto) Agent-Session: local cursor-agent session (log /home/crackauc/sandbox/goals/performance/jobs/pr-jac2w-sparse/log-cu.txt)
|
When this PR is revisited, it might be worth widening its scope. A related problem sits in the same function, and one change could fix both. The problem: For a CPU The scalar branch optimized here has a milder form of the same problem. If J lacks some diagonal slots, Instead of
This would keep the speed-up here (no scalar |
|
[Automated reply from an AI agent working on the SciML performance goal] Thanks, this is a real problem, and the pattern-stable approach is the right fix. I'd like to keep this PR narrow. It is reviewed and measured, and its results are bitwise identical to master. The pattern-stable version goes in a follow-up PR, which I'm starting now and will link here when it's up. The plan follows your outline:
If you can share the power-grid DAE, or a reduced version of it, I'll use it as a benchmark to show the symbolic-analysis saving. 🤖 Posted by an AI agent: Claude Code (model: claude-opus-5-5[1m]), session https://claude.ai/code/session_01LPHREnnonfLg1VcE1EJovv |
Sure, here is a reproducing script: https://gist.github.com/hexaeder/75263ca756c42ff18fd8c41b54821dc8 It depends on downstream packages NetworkDynamics, PowerDynamics and ModelingToolkitBase so its not usable for the tests or anything but might be worth for validating the end to end fix in that other PR. |
Sparse broadcast of W = M/dtgamma - J drops exact zeros, so nnz(W) fluctuates with the Jacobian's values and KLU/UMFPACK redo symbolic analysis nearly every step. Give CPU SparseMatrixCSC W a dedicated path in the SparseArrays extension for scalar (UniformScaling, ScalarOperator) and Diagonal mass matrices: the first call widens the pattern once to W ∪ J ∪ nz(M), and each call rewrites nonzeros(W) in place — positionally when W and J already share a pattern, by a merge pass otherwise — so the pattern no longer depends on values. Values are bitwise identical to the copyto!/broadcast paths they replace, including stored -0.0 semantics. dae_jacobian2W! gets the same treatment for W = J_u + cj*J_du, and calc_J_dae! uses a stable in-place J_du - J_u via _sparse_pattern_stable_sub!, which falls back to broadcast for non-CPU-sparse arrays. Design from @hexaeder's comment on SciML#4663; stacked on that branch. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Devin <[email protected]> Agent-Harness: devin 3000.11.3 Agent-Model: swe-2-high Agent-Session: local Devin session (log /home/crackauc/sandbox/goals/performance/jobs/pr-pattern-stable-W/log.txt)
* Bump DiffEqBase to 7.21.3 Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Agent-Harness: Devin CLI 3000.11.3 (Mac) Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium) Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json * Bump OrdinaryDiffEqBDF to 2.4.12 Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Agent-Harness: Devin CLI 3000.11.3 (Mac) Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium) Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json * Bump OrdinaryDiffEqSDIRK to 2.9.7 Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Agent-Harness: Devin CLI 3000.11.3 (Mac) Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium) Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json * Bump OrdinaryDiffEqRosenbrock to 2.7.5 Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Agent-Harness: Devin CLI 3000.11.3 (Mac) Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium) Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json * Bump OrdinaryDiffEqLowOrderRK to 2.2.6 Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Agent-Harness: Devin CLI 3000.11.3 (Mac) Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium) Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json * Bump OrdinaryDiffEqTsit5 to 2.1.5 Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Agent-Harness: Devin CLI 3000.11.3 (Mac) Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium) Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json * Bump OrdinaryDiffEqVerner to 2.4.2 Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Agent-Harness: Devin CLI 3000.11.3 (Mac) Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium) Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json * Bump OrdinaryDiffEqSymplecticRK to 2.2.3 Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Agent-Harness: Devin CLI 3000.11.3 (Mac) Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium) Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json --------- Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
What changed and why
Sparse CSC matrices now update stored diagonal values through
nonzerosusing the column row indices, avoiding scalar sparsesetindex!for the common full-diagonal case. If a diagonal slot is absent, the remaining entries use the existing scalar assignment behavior, which inserts those slots. Dense and non-CSC matrix paths are unchanged.Review follow-up (Cursor Agent on top of Codex Luna)
Applied Devin SWE-2 review findings in
669ce01b70on top of Codex Luna'sc9304f0ec6::_update_sparse_diagonal!to Differentiation QAall_qualified_accesses_are_publicignore list.sparse([1,1,2],[1,2,1],…)) covering the mixed fast-write / insertion path vs dense reference.Wand loop over column count so a non-squareWBoundsErrors like master.Diagnosis (profile)
The supplied audit reported 19–25% of leaf samples in
jacobian2W!. On this machine, a 400-sample profile did not reproduce that share: TRBDF2 had 15 frames on the target lines out of 41,833 collected frame IDs; KenCarp4 had 23/15,302. These raw frame counts are not leaf-sample percentages and are not directly comparable to the audit metric.Verification (re-run after review fixes)
julia -m Runic --check --diffon the changed files: exit 0.typoson the changed files: exit 0.git diff --check: exit 0.GROUP=Sparse:Non-Full Diagonal Sparsity Tests | 2 passed, 2 total; package tests passed.GROUP=QA: JET 1/1, Aqua 19/19, JVPCache 13 pass / 2 broken; package tests passed.GROUP=Core:DAE jacobian2W sparse4/4,jacobian2W sparse13/13 (includes new partial-diagonal coverage). Then errored in the untouchedScalarOperator mass matrixtest (diag(::SciMLOperators.ScalarOperator)has no method atOrdinaryDiffEqSDIRK/src/generic_imex_perform_step.jl:59). Same pre-existing failure noted by Codex Luna; not introduced by this PR.Failing-before/passing-after
With the new test present and the implementation/ext temporarily restored to
origin/master, the focused test exited 1 withUndefVarError: _update_sparse_diagonal! not defined. With the implementation applied, the focused sparse test file passed, and the package Core run passed itsjacobian2W sparsetestset (now 13/13).Before/after numbers (same machine)
Pinned to core 20,
JULIA_NUM_THREADS=1,@benchmarkminima, 40 samples and one evaluation per sample:Behaviour-equivalence check
On the same sparse 100-state fixtures, results matched the master references exactly:
The master implementation inserts missing diagonal entries. The new missing-diagonal and partial-diagonal tests check CSC fallback insertion and the mixed path.
What was not verified
The Core group did not complete because of the
ScalarOperatorerror above (pre-existing / unrelated). No downstream, GPU, or fullEverythinggroup was run.Please ignore until reviewed by @ChrisRackauckas.
Risk assessment
SparseMatrixCSC; dense and other matrix paths remain unchanged; no public API changes.diag(::ScalarOperator)error in the ScalarOperator mass matrix testset. Rosenbrock QARosenbrock32 perform_step! inferenceis a separate master issue (see Rosenbrock QA: skip Rosenbrock32 on singular-mass inference DAE #4668).Original author: Codex CLI (model: gpt-6-luna). Review fixes: Cursor Agent (auto) session log
/home/crackauc/sandbox/goals/performance/jobs/pr-jac2w-sparse/log-cu.txt.