Skip to content

Opt-in cache-free generic LU for inlined linear SCC solves - #175

Closed
pankgeorg wants to merge 2 commits into
JuliaComputing:mainfrom
pankgeorg:numeric-ldiv-generic
Closed

pankgeorg wants to merge 2 commits into
JuliaComputing:mainfrom
pankgeorg:numeric-ldiv-generic

Conversation

@pankgeorg

@pankgeorg pankgeorg commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

With inline_linear_sccs = true, the runtime solve of an inlined linear SCC goes through numeric_ldiv!, which looks up a LinearSolve cache in task local storage. That lookup is type unstable by design and sits behind a function barrier, so the generated RHS makes a dynamic call. Closed-world ahead-of-time compilation (juliac --trim=safe, WebAssembly targets) can't compile that call. Turning inline_linear_sccs off isn't always an option, because some models only initialize with the linear blocks inlined.

This adds an opt-in field to DefaultReassembleAlgorithm:

DefaultReassembleAlgorithm(; inline_linear_sccs = true, inline_linear_scc_generic_lu = true)

With it, the solve is emitted as safe_ldiv(A, b, GenericInlineLU()), which runs generic_lu_ldiv! on copies of A and b. generic_lu_ldiv! is Gaussian elimination with partial pivoting in plain Julia, generic in the element type, so ForwardDiff.Duals go through it. It keeps no state between calls.

  • The default is unchanged. Without the flag, the emitted term is still the two-argument safe_ldiv(A, b) and numeric_ldiv! is untouched. The op is the same function in both cases, so inline_linear_systems and InlineLinsolveTransformation treat both forms alike.
  • The solve copies A and b because codegen can hand two inline solves the same buffer. Two blocks with the same A and different b gave a wrong result when the elimination overwrote A in place. This has a test.
  • Trade-offs of the opt-in: each solve allocates its two copies (192 B per RHS call vs 80 B on the default path in the measurement below). There is also no rank-deficient fallback. A singular block gives non-finite values, where the default's QR fallback gives the minimum-norm solution.

Found while preparing the ahead-of-time compilation work tracked in SciML/DifferentialEquations.jl#1177; independent of the PRs listed there.

Evidence (Julia 1.13.3, ModelingToolkit 11.46.1, LinearSolve 5.18.3)

Model: D(x) ~ 2t + 1 plus a 3×3 linear block whose A depends on x and whose b depends on t, with D(q) reading the block. The reference is the same block written out by hand and solved with \. Derivatives come from the implicit-function formulas dX/dx = -A⁻¹ (∂A/∂x) X and dX/dt = A⁻¹ ∂b/∂t.

main this PR, default this PR, opt-in
JET.report_opt of the generated RHS 1 report: runtime dispatch detected: solve_into!(%44::Any, A::Matrix{Float64}, b::Vector{Float64}) same 1 report 0 reports
du[q] vs analytic −8.9 match match match
∂du[q]/∂x (ForwardDiff) vs analytic 68.55 match match match
∂du[q]/∂t (ForwardDiff) vs analytic 7.0 match match match
RHS median (BenchmarkTools) 198.8 ns, 80 B 197.7 ns, 80 B 81.7 ns, 192 B

The default and opt-in Jacobians agree exactly (max abs difference 0.0). Kernels alone, on a diagonally dominant random matrix: n = 3 is 289 ns cached vs 133 ns generic; n = 8 is 621 ns vs 478 ns. Both are allocation-free.

Tests

  • generic_lu_ldiv!: a 3×3 system that needs a row swap, with a known integer solution. No allocations. The ForwardDiff derivative w.r.t. a perturbation of A is checked against -A⁻¹ E x, and a singular input gives non-finite output.
  • The model above with the flag on: values and both derivatives against the hand-derived reference, and no task local cache created in a fresh task.
  • Two blocks sharing A, and two blocks sharing b, with the flag on, checked against the analytic solutions. The shared-A case fails if the elimination works in place on the buffers.

The new testsets and the existing inline-linear-SCC testsets pass locally, 43/43. I ran them as single testsets on Julia 1.13.3, not the whole suite. Julia 1.10 is not verified locally.

Risk assessment

  • Risk: Medium-low (ML2: a non-default linear solve with independent accuracy evidence, defaults unchanged; also ML7, an additive documented and tested keyword)
  • Blast radius: Opt-in only, for models compiled with inline_linear_sccs = true and inline_linear_scc_generic_lu = true. The default emitted code and numeric_ldiv! are unchanged.
  • Evidence: JET dynamic-dispatch check, values, and ForwardDiff derivatives against analytic references, on main and head; shared-buffer regression; BenchmarkTools on the default path at main vs head (198.8 vs 197.7 ns). Julia 1.13.3.
  • Independent review: MERGE · low (L8) at 1372535 (fresh gpt-6.1-sol session, rules of JuliaComputing/ai-requests#58/build: bump SSel minor, MTKTearing minor, SSel compat #60; review file: https://gist.github.com/pankgeorg/00220bbe7ea7c6f7fd154b2005742d8f)
  • Merge: needs review

🤖 Generated with Claude Code

pankgeorg and others added 2 commits October 31, 2026 22:57
With inline_linear_scc_generic_lu = true, the runtime solve of an inlined
linear SCC is emitted as safe_ldiv(A, b, GenericInlineLU()), which runs
generic_lu_ldiv! (Gaussian elimination with partial pivoting in plain
Julia, generic in the element type) on copies of A and b. It keeps no
task local LinearSolve cache, so the generated code makes no dynamic call
for the solve. The default emitted code is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ChrisRackauckas

Copy link
Copy Markdown
Member

@AJ0070 @DhairyaLGandhi you're fixing that type instability though? If so, close this.

@ChrisRackauckas

Copy link
Copy Markdown
Member

This doesn't seem like a proper solution

@AJ0070

AJ0070 commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Opened #176 for the type instability: the cache type is now inferred and asserted on the lookup, so the solve is a static call and the cache stays.

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.

3 participants