Skip to content

fix(ModelingToolkitTearing): use per-site LinearCache for inline linear SCCs - #196

Closed
ambujone wants to merge 3 commits into
JuliaComputing:mainfrom
ambujone:fix/inline-scc-per-site-cache
Closed

ambujone wants to merge 3 commits into
JuliaComputing:mainfrom
ambujone:fix/inline-scc-per-site-cache

Conversation

@ambujone

@ambujone ambujone commented Nov 30, 2026 •

Copy link
Copy Markdown

Fixes #178

Built on #192, so until #192 merges the diff includes its two commits. I'll rebase onto main after it lands.

The inline-SCC LinearCache is keyed by array types and size, so two inline SCCs of the same shape share one cache, overwrite each other's matrix and refactorize on every solve. This adds a site id, the SCC's index in the system, to the cache key. The id is passed through safe_ldiv, numeric_ldiv!, numeric_ldiv_again!, inline_linsolve_fell_back and the Enzyme rules. The last A is stored next to the cache, so an unchanged (===) A is not refactorized. Because the id is deterministic, compiling a system twice still gives isequal observed equations. The cost is one cache plus one copy of A per block per task, which I have not measured.

Tests cover no clobbering between sites, zero allocations when A is unchanged, an @inferred lookup for Float64, Dual and view arguments, repeated-compile isequal, and #192's shared-RHS testset. The ModelingToolkitTearing suite, including enzyme.jl, passes locally on Julia 1.13.4.

Prepared with Claude Code.

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>
@ambujone

ambujone commented Dec 1, 2026

Copy link
Copy Markdown
Author

Moving this back to draft. While reviewing the neighbouring PRs I found two problems here:

  1. It does not fix the shared-RHS wrong-result bug. Running the "right-hand sides CSE to one buffer" testset from ModelingToolkitTearing: fix shared-RHS inline solves and make the solve cache trim-safe #192 against this branch fails: wrong values and wrong ForwardDiff derivatives, because safe_ldiv still solves into a b that two inline SCCs can share. ModelingToolkitTearing: fix shared-RHS inline solves and make the solve cache trim-safe #192 fixes that by solving a copy.
  2. The per-site id comes from a global counter and ends up in the safe_ldiv term. Compiling the same system twice then gives observed equations that are not isequal (they are on main).

I will rebase this onto #192 once it lands, or onto its branch if that is preferred, and derive the site id deterministically from the block instead of from a counter. I will also add the same inferred-type assertion #192 uses to the per-site cache entry. Until then, please review #192 first.

…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>
@ambujone
ambujone force-pushed the fix/inline-scc-per-site-cache branch from 09c32a2 to 350be5e Compare December 1, 2026 05:34
@ambujone

ambujone commented Dec 1, 2026

Copy link
Copy Markdown
Author

Updated. This branch now sits on top of #192, so until #192 merges the diff includes its two commits. On top of that:

The full ModelingToolkitTearing suite, including enzyme.jl, passes locally on Julia 1.13.4. I'll keep this as a draft until #192 lands, then rebase onto main.

@ambujone ambujone closed this Dec 1, 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.

Inline linear SCC solves share one LinearCache per matrix size, so every call refactorizes

3 participants