Skip to content

Preserve Vector container eltype when remaking SCCNonlinearProblem - #1615

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:scc-preserve-container-eltype
Sep 24, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:scc-preserve-container-eltype

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Please ignore until reviewed by @ChrisRackauckas.

What changed and why

scc_update_subproblems(probs::Vector, ...) rebuilt the block vector with
map, which narrows the output eltype to the common concrete block type.
Remaking an SCCNonlinearProblem with homogeneous blocks (e.g. MTK
initialization problems with all-NonlinearProblem blocks) therefore yields
a different problem type than remaking one with heterogeneous blocks — and a
different type than the problem had at construction (Vector{Any}).
Solve-time compiled code splits by block homogeneity, so MTK's multi-block
SCC precompile workload (nonlinear + linear blocks, see
SciML/ModelingToolkit.jl#5202) cannot cover
all-nonlinear user models on the remake/_scc_solve path.

The fix fills a vector of the same eltype (map! into similar(probs))
unless the input eltype is concrete, in which case map is kept so
element-type-changing remakes keep working. Tuple storage is untouched
(handled by the separate Tuple method).

Verification

New testset in test/problem_building_test.jl (homogeneous NonlinearProblem
blocks in Vector{Any}; remake must keep Vector{Any}; concrete-typed
container still remakes correctly). Full problem_building_test.jl run
locally. Before (fix stashed):

SCCNonlinearProblem remake preserves Vector container eltype |    3     1      4  3.0s
Test Failed at .../test/problem_building_test.jl:372
  Expression: sccprob2.probs isa Vector{Any}
   Evaluated: NonlinearProblem{...}[...] isa Vector{Any}

After:

SCCNonlinearProblem remake preserves Vector container eltype |    4      4  0.5s

Whole file green (exit 0), including the pre-existing SCC testset (7/7).
Runic --check and typos clean on both changed files.

Attribution evidence (MTK TTFX residual analysis, --trace-compile diffs of
first vs second user model, Snoop-free): native-codegen residual for a user
DAE first solve drops 75 → 55 compiled methods (−27%) with this fix and its
SCCNonlinearSolve companion; the 39 vanished lines are SCC-remake-path
codegen. Load caveat: trace batteries ran at loads 23–47; single-run
wall-clock numbers are not comparable across runs, so no wall-clock claim is
made here.

What was NOT verified

Wall-clock improvement, and the per-block dynamic-dispatch cost of iterating
a Vector{Any} instead of a narrowed vector (expected small vs a Newton
solve; unmeasured). Full SciMLBase suite not run (only the file containing
the new test); downstream packages not tested.

Part of the MTK TTFX effort; related to SciML/ModelingToolkit.jl#5202.

🤖 Generated with opencode 1.18.31 (model: opencode/muse-spark-1.3-contributor-free); transcript at /home/crackauc/sandbox/agent-jobs/mtk-ttfx/jobs/residual/log.txt on amdci2.julia.csail.mit.edu

Risk assessment

  • Risk: low
  • Independent review: Devin CLI 3000.11.3 / fusion-claude-opus-5-5-high-sidekick-swe-2-medium (author: opencode muse-spark) — VERDICT: MERGE, no blocking findings. Full review below.
  • Merge: merged at Chris's direction (low risk); remaining red CI jobs also fail on master / unrelated PRs.
Review

VERDICT: MERGE

Review of #1615 at head d544b61 (branch scc-preserve-container-eltype, base b7076b5).

Risk assessment

Low risk, high confidence. The change is 6 lines in scc_update_subproblems(probs::Vector, ...). It still builds the blocks with map, which gives the same result as master. It copies into similar(probs) only when every rebuilt block isa eltype(probs), so the copy cannot throw. When a block changes type, the function returns the narrowed map result, exactly as master does. The Tuple method is untouched. The guard matches SciML/NonlinearSolve.jl#1328 scc_solve_up line for line. #1328 has an extra probs isa AbstractVector check because its prob.probs can be a Tuple; here dispatch on ::Vector already rules that out. I ran before/after probes and they behave as intended. Every red CI job also fails on unrelated PRs from the same day.

Blocking findings

None.

Non-blocking findings

  • The PR body is stale. It describes the first commit ("map! into similar(probs) unless the input eltype is concrete, in which case map is kept"). With d544b61, concrete containers also go through the all(isa) guard and copyto!, and the rebuild is map followed by a conditional copyto!, not map!. The "before" output (3 1 4) comes from the 4-assertion version of the test; the testset now has 6 assertions. The body should be updated before merge.
  • The fallback branch is not covered by the new testset. No test remakes a block so that it changes type under a narrow abstract eltype, which is the hazard the second commit fixes. I ran that case (probe (a) below) and it works. Adding remake(sccprob5; u0 = Float32[1.5, 0.5]) with a state check would pin it.
  • Redundant copy for concrete eltypes (minor, from reading). When eltype(probs) is concrete and the remake keeps the type, map already returns a Vector{T}, and the guard allocates and copies a second one. The cost is negligible, and [v2-backport] Fix integer time-step indexing under AD (#1325) #1328 has the same redundancy. An isconcretetype(eltype(probs)) short-circuit would skip it.
  • Comment density is slightly high. 5 of 40 added lines are comments (about 12%, against the ~10% guideline). None of them narrate the change. The test comment "map would narrow homogeneous blocks…" explains why the assertion exists, which is acceptable.
  • The attribution format is non-standard. The body opens with "Please ignore until reviewed by @ChrisRackauckas." instead of the standard UNREVIEWED banner. Harness, model and transcript path are present in the trailing line.

Checklist answers

  1. Same guarded pattern as [v2-backport] Fix integer time-step indexing under AD (#1325) #1328? Yes. Both use map first, then if all(Base.Fix2(isa, eltype(probs)), out); out = copyto!(similar(probs), out); end. Under a narrow abstract eltype, a block whose type changes makes the guard false, so nothing is copied and there is no MethodError. Confirmed by running (probe (a)).
  2. Does the new testset fail on master and pass on the branch? Yes, confirmed by running.
  3. Edge cases, all confirmed by running unless marked:
    • Union narrower than Any: the eltype is kept when the blocks fit. With Float32 u0 the result falls back to narrowing, with Float32 sub-states.
    • AbstractSciMLProblem[...]: kept on the PR, narrowed on master.
    • Concrete container with a type-changing Float32 remake: works, and the type is identical to master's.
    • Tuple: identical to master.
    • View inputs: the method is restricted to probs::Vector, so views never reach this code. That is unchanged from master; from reading only, not run.
    • Empty Vector: all over no elements is true and copyto! of an empty vector is fine. Other paths were already reported to throw BoundsError on empty probs before this PR; from reading only.
  4. Test weakening, scope creep, expected values:
    • No existing test was modified, skipped, or loosened.
    • The expected values ([1.5, 0.5] and container types) are the requested inputs and invariants, not values copied from code output.
    • The diff touches 2 files, all in scope.

CI

All failures are pre-existing or infrastructure:

What I ran

  • gh pr view/diff/checks 1615, gh run view --log-failed on the QA, SII, Catalyst and Core jobs, gh pr diff 1328 -R SciML/NonlinearSolve.jl. I also read /Users/chrisrackauckas/sandbox/fleet-master/jobs/918-1/REVIEW-1328-r2.md.
  • Scratch clone at /Users/chrisrackauckas/sandbox/master/r1615 (pr1615 = d544b61) and a master worktree r1615-master (b7076b5), each dev'd into its own env. Julia 1.12.7.
  • jobs/918-2/probe.jl: the new testset's body, plus cases (a) U with a Float32 remake, (b) concrete container with a Float32 remake, (d) AbstractSciMLProblem[...], and (e) Tuple. Each testset is wrapped so that every case runs on master.
    • Logs: probe_pr.log and probe_master.log.
    • Results: the PR passes every case. Master fails the two eltype assertions in the testset and case (d). Nothing throws on either.
  • test/problem_building_test.jl on the branch: exit 0, new testset 6/6 (problem_building_pr.log).

What I did not verify

  • The TTFX claim (75 → 55 compiled methods) and the dispatch cost at runtime of iterating over Vector{Any}.
  • The full SciMLBase test suite locally (I relied on CI Core), and the SciMLSensitivity failure logs beyond comparing job names.
  • This PR and [v2-backport] Fix integer time-step indexing under AD (#1325) #1328 together in one environment.
  • View-typed probs input (reasoned from dispatch only).

🤖 Risk assessment added by an AI agent — harness: Devin CLI 3000.11.3 · model: fusion-claude-opus-5-5-medium-sidekick-swe-2-medium (fleet master); reviewer: fusion-claude-opus-5-5-high-sidekick-swe-2-medium
Conversation: local Devin CLI fleet-master session on Chris's Mac (no session URL); review output at ~/sandbox/fleet-master/jobs/918-2/REVIEW-1615.md

ChrisRackauckas and others added 2 commits September 23, 2026 22:45
scc_update_subproblems used map, which narrows the output eltype to the
common concrete block type. Remaking a problem with homogeneous blocks then
yields a different SCCNonlinearProblem type than remaking one with
heterogeneous blocks, splitting solve-time compiled code by block
homogeneity. Fill a vector of the same eltype instead, unless the input
eltype is concrete.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: opencode <[email protected]>
Agent-Harness: opencode 1.18.31
Agent-Model: opencode/muse-spark-1.3-contributor-free
Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/mtk-ttfx/jobs/residual/log.txt on amdci2.julia.csail.mit.edu
Copying into similar(probs) throws when a remake changes a block's type
under a narrow abstract eltype. Compute with map first and only copy into
the caller's container when every rebuilt block fits its eltype, so
concretely typed and narrow abstract containers keep working as before.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: opencode <[email protected]>
Agent-Harness: opencode 1.18.31
Agent-Model: opencode/muse-spark-1.3-contributor-free
Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/mtk-ttfx/jobs/residual/log.txt on amdci2.julia.csail.mit.edu
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review September 24, 2026 17:42
@ChrisRackauckas
ChrisRackauckas merged commit 4fa7f7f into SciML:master Sep 24, 2026
64 of 73 checks passed
ChrisRackauckas referenced this pull request Sep 26, 2026
Agent-Harness: Claude Code
Agent-Model: claude-opus-5-5[1m]
Agent-Session: local Claude Code session 3cd6500a-1f81-46b5-ac0b-c466e15b6a53 on Chris's Mac (session ID, no URL)

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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