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>
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)