Skip to content

ModelingToolkitTearing: fix shared-RHS inline solves and make the solve cache trim-safe - #192

Open
asinghvi17 wants to merge 2 commits into
mainfrom
as/trim-safe-inline-linsolve
Open

asinghvi17 wants to merge 2 commits into
mainfrom
as/trim-safe-inline-linsolve

Conversation

@asinghvi17

@asinghvi17 asinghvi17 commented Nov 26, 2026 •

Copy link
Copy Markdown

🤖 Automated comment from an AI agent running as @asinghvi17 — not written or reviewed by Anshul.

Two fixes to the inline linear SCC solve (inline_linear_sccs = true).

Wrong results when right-hand sides share a buffer. numeric_ldiv! writes its solution into b. When two SCCs have the same right-hand side, CSE fills one buffer and passes it to both solves, so the second solve reads the first one's solution as its RHS. safe_ldiv now solves a copy of b (one small allocation per solve; still inferred). A is only read, since the LinearSolve cache copies it, so it is not copied. A new test with two SCCs sharing b = [x, x] checks values and ForwardDiff derivatives against a hand solve; it fails on main.

Trim safety. numeric_ldiv! gets its LinearCache from a task-local Dict{…, Any}, so the solve_into! call after the lookup was a runtime dispatch. juliac --trim=safe reported it as unresolved for both Float64 and ForwardDiff.Dual, and a binary built with --trim=unsafe-warn threw MethodError: solve_into! on the first solve. The lookup now asserts the type a new cache infers to (Base.promote_op of the constructor), and the cache key includes the type of b so the assertion always holds. The new test checks the RHS and its derivative against a hand solve and that the lookup is @inferred for Float64 and Dual; that check fails on main.

The MTKTearing suite passes on Julia 1.13.

Note: --trim=safe now reaches LinearSolve's solve!, where two sites still fail verification and need their own fix: the generated __setfield! on DefaultLinearSolverInit (default.jl), whose type-mismatched branches can never run, and the 13-argument residual-safety message (factorization.jl). The trimmed binary computes the right values despite them.

Not fixed here: CSE treats these buffer-allocating terms as equal even when they refer to different diffcache slots, which is what merges the buffers in the first place.


🤖 Posted by an AI agent — harness: Claude 2.1.316 · model: claude-opus-5-5
Conversation: factory head hd_c55d7e33 on m_4242afb3ce622d22c2c40d9e9baf1f9a (no session URL)

asinghvi17 and others added 2 commits November 26, 2026 05:11
…ache

`numeric_ldiv!` took its `LinearCache` from a `Dict` with `Any` values, so
the `solve_into!` call after the lookup was a runtime dispatch. A
`juliac --trim` build of generated code that inlines a numeric linear SCC
then fails: `--trim=safe` reports the call as unresolved, and an
`unsafe-warn` binary throws a `MethodError` on the first solve, for both
`Float64` and `ForwardDiff.Dual` arguments.

Assert the type a new cache infers to for the argument types, and key
the cache by the type of `b` as well as `A` so the assertion always holds.

License: AGPL-3.0-only
Copyright: JuliaHub, Inc.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…right-hand side

`numeric_ldiv!` writes its solution into `b`, on the assumption that `b`
belongs to its solve site alone. It does not: CSE merges the `b` buffers
of two inline linear SCCs whose right-hand sides are equal, fills the
merged buffer once, and passes it to both solves. The second solve then
reads the first one's solution as its right-hand side, and both the RHS
and its derivatives come back wrong without an error.

Have `safe_ldiv`, the operation the generated code calls, solve a copy of
`b`. That costs one allocation of the size of `b` per solve. `A` needs no
copy: CSE shares it the same way, but `numeric_ldiv!` only copies it into
the cache's own matrix and never writes to it.

License: AGPL-3.0-only
Copyright: JuliaHub, Inc.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@asinghvi17 asinghvi17 changed the title ModelingToolkitTearing: concretely type the inline linear SCC solve cache ModelingToolkitTearing: fix shared-RHS inline solves and make the solve cache trim-safe Nov 26, 2026
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.

1 participant