Repository navigation
Conversation
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>
Member
|
@AJ0070 @DhairyaLGandhi you're fixing that type instability though? If so, close this. |
Member
|
This doesn't seem like a proper solution |
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. |
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.
With
inline_linear_sccs = true, the runtime solve of an inlined linear SCC goes throughnumeric_ldiv!, which looks up aLinearSolvecache 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. Turninginline_linear_sccsoff isn't always an option, because some models only initialize with the linear blocks inlined.This adds an opt-in field to
DefaultReassembleAlgorithm:With it, the solve is emitted as
safe_ldiv(A, b, GenericInlineLU()), which runsgeneric_lu_ldiv!on copies ofAandb.generic_lu_ldiv!is Gaussian elimination with partial pivoting in plain Julia, generic in the element type, soForwardDiff.Duals go through it. It keeps no state between calls.safe_ldiv(A, b)andnumeric_ldiv!is untouched. The op is the same function in both cases, soinline_linear_systemsandInlineLinsolveTransformationtreat both forms alike.Aandbbecause codegen can hand two inline solves the same buffer. Two blocks with the sameAand differentbgave a wrong result when the elimination overwroteAin place. This has a test.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 + 1plus a 3×3 linear block whoseAdepends onxand whosebdepends ont, withD(q)reading the block. The reference is the same block written out by hand and solved with\. Derivatives come from the implicit-function formulasdX/dx = -A⁻¹ (∂A/∂x) XanddX/dt = A⁻¹ ∂b/∂t.mainJET.report_optof the generated RHSruntime dispatch detected: solve_into!(%44::Any, A::Matrix{Float64}, b::Vector{Float64})du[q]vs analytic −8.9∂du[q]/∂x(ForwardDiff) vs analytic 68.55∂du[q]/∂t(ForwardDiff) vs analytic 7.0The 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 ofAis checked against-A⁻¹ E x, and a singular input gives non-finite output.A, and two blocks sharingb, with the flag on, checked against the analytic solutions. The shared-Acase 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
inline_linear_sccs = trueandinline_linear_scc_generic_lu = true. The default emitted code andnumeric_ldiv!are unchanged.mainand head; shared-buffer regression; BenchmarkTools on the default path atmainvs head (198.8 vs 197.7 ns). Julia 1.13.3.🤖 Generated with Claude Code