Skip to content

Preserve Vector container eltype in scc_solve_up - #1328

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_solve_up rebuilt the block vector with map(_concrete_scc_problem, prob.probs), which narrows the output eltype to the common concrete block
type. _scc_solve therefore sees a different SCCNonlinearProblem type for
homogeneous block vectors (Vector{NonlinearProblem{...}}) than for
heterogeneous ones (Vector{AbstractSciMLProblem}) — 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 this path.

The fix uses map! into similar(prob.probs) unless the input is a Tuple
or has a concrete eltype, in which cases map is kept (Tuple storage is
stable by construction; a concretely typed input must keep working even if a
remake changes the element type).

Note: this touches the same file as
#1321 (probvec fix) but a
different hunk (this PR: scc_solve_up line ~70; #1321: probvec line ~81).
A trial git merge-tree of that branch into this one reports zero conflict
markers — expected clean merge in either order.

Verification

New test test/core_tests__item7.jl (registered in core_tests.jl):
homogeneous NonlinearProblem blocks in Vector{Any}, solved with
SCCAlg(nlalg = NewtonRaphson()); asserts the solve succeeds, the solution
is correct, and the solution's problem (sol.prob, i.e. exactly what
_scc_solve received) still has a Vector{Any} block container. Before
(fix stashed; correctness asserts pass, only the eltype assert fails):

Test Failed at .../core_tests__item7.jl:33
  Expression: _sol.prob.probs isa Vector{Any}
   Evaluated: NonlinearProblem{...}[...] isa Vector{Any}

After: item7 passes (5/5), and the full SCCNonlinearSolve core group passes
locally (exit 0): Manual SCC 14/14, #758 3/3, Residuals Transfer 6/6,
FunctionWrappers 13/13, Homotopy 5/5, AutoDespecialize 21/21, new item 5/5.
Runic --check and typos clean on all 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
SciMLBase companion (scc_update_subproblems); the 39 vanished lines are
SCC-remake-path codegen, and the user _scc_solve instance now matches the
workload's (Vector{Any} container in both). 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). Only the SCCNonlinearSolve core group was run, not the
whole NonlinearSolve suite or downstream packages.

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

Round 2 re-review of #1328 at head 4d42a74. Companion PR checked: SciML/SciMLBase.jl#1615 at d544b614.

Blocking findings

None. Round-1 blocking finding 1 is resolved:

  • scc_solve_up now runs map first. It copies into similar(prob.probs) only when probs isa AbstractVector and every rebuilt block isa eltype(prob.probs); otherwise it keeps the narrowed map result (src/SCCNonlinearSolve.jl:70-75). SciMLBase#1615's scc_update_subproblems uses the same guarded form.
  • Regression tests were added to core_tests__item7.jl:28-37: the literal [_nfs, _nad] (one AutoSpecialize block, one AutoDespecialize block) and a Union{typeof(_nfs), typeof(_nad)} vector. Both assert a successful retcode and the correct u. Confirmed by running: with the src reverted to the unguarded ff78745, item7 errors at line 34 with MethodError: Cannot convert … DespecializedParameters …. So the new test discriminates the round-1 bug.

Non-blocking findings

  • The PR body is stale. Update it before merge. It still describes the ff78745 approach ("uses map! into similar(prob.probs) unless the input is a Tuple or has a concrete eltype"). It also says "item7 passes (5/5)", but item7 now has 11 assertions. The "75 → 55 compiled methods" claim still has no command or output attached. I did not reproduce it.
  • Round-1 non-blocking items are addressed:
    • Comments: 5 of the ~55 added lines (about 9%). The src comment states the invariant.
    • The duplicate using SCCNonlinearSolve is gone.
    • typeof(_sol.prob) === typeof(_shet.prob) is asserted, homogeneous Any[nl, nl] vs heterogeneous Any[nad, lin]. Confirmed by running: with master src it fails at line 47, and line 25 (isa Vector{Any}) fails too.
  • Minor (from reading): when the eltype is concrete and nothing changes type, copyto!(similar(...)) makes a second allocation and copy of a vector that map had already built with the same eltype. The cost is negligible per solve, but isconcretetype(eltype(prob.probs)) could skip it.
  • SciMLBase#1615's test never exercises the fallback branch, i.e. a remake that changes a block's type under a narrow eltype. The fallback does work (see below). Adding the U[prob1, probd] + u0 = Float32[...] case to that test would pin it.
  • The guard keeps the eltype only when the container is wide enough (Any, AbstractSciMLProblem). Narrow Union/UnionAll containers that hold AutoDespecialize blocks still narrow, as they do on master. That is correct behavior. It just means the TTFX benefit does not apply to those containers.
  • Empty probs (Any[], U2[]) throw BoundsError both on master and on the PR. This is pre-existing and not introduced by this PR.

What I ran

(julia +1.12; SciMLBase 3.56.0, LinearSolve 5.18.1; NonlinearSolve sublibs dev'd from a scratch clone at /Users/chrisrackauckas/sandbox/master/r1328; logs are in r1328/logs/.)

  • gh pr view/diff/checks 1328, gh pr diff 1615 -R SciML/SciMLBase.jl.
  • GROUP=Core Pkg.test() in lib/SCCNonlinearSolve:
    • at head: all 7 testsets pass, item7 11/11 (core_head.log);
    • src at ff78745: item7 has 4 passes and 1 error at line 34 (core_ff78.log);
    • src at merge-base c76316a: item7 has 9 passes and 2 fails at lines 25 and 47. The mixed/Union loop passes on master (core_master.log).
  • Edge-case probe (r1328/probe_nls.jl) against head and master src. On both, every case solves with Success and the correct u: literal mixed, Union{nfs,nad}, Union{lin,nad}, partial UnionAll NonlinearProblem{V,true,V}, concrete [nad,nad] and [nfs,nfs], Any[…], AbstractSciMLProblem[lin,nad], Tuples (nfs,nad) and (lin,nad), view(Any[…],1:2), view(U2[…],1:2), reversed view(Any[…],2:-1:1). The eltype is kept only on head, and only for the Any/AbstractSciMLProblem/view(Any) containers. Views come back as Vector via similar, which is fine. Tuples take the map path unchanged.
  • SciMLBase#1615 probe (r1328/probe_scimlbase.jl):
    • On the PR, the eltype is kept for Any containers and for U when the remake fits.
    • When the remake changes the type (U or a concrete container, with Float32/BigFloat u0; partial NonlinearProblem{Vector{Float64}} with Float32), it falls back to narrowing, with correct state and sub-u0 types.
    • On the merge-base it narrows in every case.
    • SciMLBase test/problem_building_test.jl on pr1615 passes in full.
  • CI at 4d42a74: the SCCNonlinearSolve Core runs (Julia 1 / lts / pre), QA, Runic, typos, docs and the OrdinaryDiffEq/DiffEqCallbacks downstream jobs pass. Still pending when read: the ModelingToolkit downstream jobs (Initialization/InterfaceI/InterfaceII), BoundaryValueDiffEq, and downgrade NonlinearSolveFirstOrder.

What I did not verify

  • The TTFX / compile-count claim, and the runtime cost of per-block dynamic dispatch over Vector{Any}.
  • Pending ModelingToolkit/BoundaryValueDiffEq downstream CI results.
  • Running this PR and SciMLBase#1615 together in one environment. I ran each against the registry version of the other.
  • AD (Zygote/Enzyme) through a vector SCC. I checked this from reading only in round 1.

🤖 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-1/REVIEW-1328-r2.md

ChrisRackauckas and others added 2 commits September 23, 2026 22:45
scc_solve_up rebuilt the block vector with map, which narrows the output
eltype to the common concrete block type, so _scc_solve sees a different
SCCNonlinearProblem type for homogeneous vs heterogeneous block vectors.
Fill a vector of the same eltype instead, unless the input eltype is
concrete. Tuple storage is untouched.

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
map! into similar(probs) throws when concretization changes a block's type
under a narrow abstract eltype (e.g. a literal mixing specialization levels
or a Union of block types). Compute with map first and only copy into the
caller's container when every rebuilt block fits its eltype.

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 192b11f into SciML:master Sep 24, 2026
33 of 34 checks passed
ChrisRackauckas referenced this pull request Sep 25, 2026
Agent-Harness: Devin CLI 3000.11.3
Agent-Model: fusion-claude-opus-5-5-medium-sidekick-swe-2-medium
Agent-Session: local Devin CLI fleet-master session on Chris's Mac (no URL)
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