Repository navigation
ModelingToolkitTearing: fix shared-RHS inline solves and make the solve cache trim-safe - #192
Open
asinghvi17 wants to merge 2 commits into
Open
asinghvi17 wants to merge 2 commits into
asinghvi17 wants to merge 2 commits into
Conversation
…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>
ambujone
reviewed
Dec 1, 2026
ambujone
left a comment
There was a problem hiding this comment.
Verified this on its own branch: with the shared-RHS test from this PR, a build that keeps copyto!(b, ...) and has no copy of b in safe_ldiv (that is #196's head) gives wrong values and wrong ForwardDiff derivatives, so the aliasing is real and the fix addresses it. Full ModelingToolkitTearing suite passes on Julia 1.13 on my side too.
Two things I'd like to see:
- The trim assertion in
get_inline_linsolve_cacheis only exercised forMatrix{T}/Vector{T}. The generated code passes diffcache views asAandb. Can the@inferredtest use the view types thatget_linear_scc_linsolactually produces? Ifpromote_opreturns something abstract there, the::assertion becomes a no-op and the dynamic dispatch comes back without any test failing. - A comment saying why the copy lives in
safe_ldivand not insolve_into!: the Enzyme rules callnumeric_ldiv!and rely on it overwritingb, so this placement is what keeps them correct. It is easy for someone to "simplify" it later.
Not blocking: the root cause (CSE merging buffer-allocating terms that point at different diffcache slots) is left open. If there is no issue for it yet, one would help, since the copy(b) here costs an allocation per solve for every inlined SCC.
This was referenced Dec 1, 2026
ambujone
pushed a commit
to ambujone/StateSelection.jl
that referenced
this pull request
Dec 1, 2026
…ar SCCs Builds on JuliaComputing#192. Key the task local LinearCache by array types, size, and a solve-site id, so inline SCCs of the same shape neither clobber each other's cache nor refactorize when A is unchanged (JuliaComputing#178). The cache entry keeps an A_prev copy and skips reassigning cache.A when A is identical to it. The site id is the SCC's index in the system, not a global counter, so compiling the same system twice gives isequal observed equations. The cache lookup keeps JuliaComputing#192's typed key and promote_op assertion, applied to the entry type, and safe_ldiv keeps its copy of b so numeric_ldiv! still overwrites b for the Enzyme rules, which now take the site id. License: Apache-2.0 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
🤖 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 intob. 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_ldivnow solves a copy ofb(one small allocation per solve; still inferred).Ais only read, since the LinearSolve cache copies it, so it is not copied. A new test with two SCCs sharingb = [x, x]checks values and ForwardDiff derivatives against a hand solve; it fails onmain.Trim safety.
numeric_ldiv!gets itsLinearCachefrom a task-localDict{…, Any}, so thesolve_into!call after the lookup was a runtime dispatch.juliac --trim=safereported it as unresolved for bothFloat64andForwardDiff.Dual, and a binary built with--trim=unsafe-warnthrewMethodError: solve_into!on the first solve. The lookup now asserts the type a new cache infers to (Base.promote_opof the constructor), and the cache key includes the type ofbso the assertion always holds. The new test checks the RHS and its derivative against a hand solve and that the lookup is@inferredforFloat64andDual; that check fails onmain.The MTKTearing suite passes on Julia 1.13.
Note:
--trim=safenow reaches LinearSolve'ssolve!, where two sites still fail verification and need their own fix: the generated__setfield!onDefaultLinearSolverInit(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)