Add inline_linear_systems diagnostic#111
Merged
Merged
Conversation
Report the inline linear-solve blocks a compiled system re-solves on every evaluation. When tearing solves a strongly-connected component of algebraic equations as one linear system, it emits, into `equations`/`observed`, a square system A x = b solved at every call (as `A \\ b`, or `inline_scc_ldiv` for the rank-tolerant aggressive reduction), with each component appearing as `vᵢ ~ (A \\ b)[i]`. `inline_linear_systems(sys)` collects those blocks and reports, for each, its size N and the N variables it solves for (returned as `InlineLinearSystem`s, with the solve expression retained so callers can inspect/evaluate A). It is a diagnostic for a model's per-step linear-algebra cost and for tracing a singularity-prone block to the physical quantities it determines. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Member
|
Looks great. However, instead of parsing equations what if the code that creates the linsolves in |
Address PR review: instead of re-parsing equations/observed to rediscover the inline `A \ b` blocks, have the reassemble pass build the `InlineLinearSystem` structs as it emits each block and store them in the simplified system metadata under `InlineLinearSystemsMetadata`. `inline_linear_systems` now just reads the metadata. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
AayushSabharwal
left a comment
Member
There was a problem hiding this comment.
Some suggestions around type-stability and redundancy.
Co-authored-by: Aayush Sabharwal <aayush.sabharwal@gmail.com>
Member
|
Hopefully I did this right :D |
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.
What
Adds
inline_linear_systems, a diagnostic that reports the inline linear-solve blocks a compiled system re-solves on every evaluation.When tearing solves a strongly-connected component of algebraic equations as one linear system, it emits — into
equations(sys)/observed(sys)— a square systemA x = bthat the generated code re-solves at every call (A \ b, orinline_scc_ldivfor the rank-tolerant aggressive reduction). Each solution component appears as an assignmentvᵢ ~ (A \ b)[i].inline_linear_systems(sys)collects these and returns, for each, anInlineLinearSystemwith:operation—\orinline_scc_ldiv,size— the dimensionNof theN×Nsystem,variables— theNvariables it solves for, in solution order,expression— the retainedA \ bterm, so callers can inspect or numerically evaluateA(e.g. to check conditioning).Why
It is a diagnostic for understanding a compiled model's per-step cost and structure: large blocks are the dominant linear-algebra work each evaluation, and a singularity-prone block can be traced to the physical quantities it determines. (It grew out of debugging the multibody car/rotorcraft models, where knowing which variables a deficient block solves for was the key to locating the model-level degeneracy.)
Notes
\andinline_scc_ldivby name, so it works whether or not the rank-tolerant op is present (stock systems emit plain\blocks).exported for discoverability — happy to switch to@public/qualified-only if that better matches the package's conventions (it currently exports nothing).Test
New testset
inline_linear_systems diagnostic: a 3-variable algebraic SCC with state/time-dependent coefficients compiles to a single 3×3 inline\solve; the diagnostic reports size 3 and the three solved variables, and an empty result for a system with no inline solves. Verified locally (9/9 pass).🤖 Generated with Claude Code