Lower computed initialization maps via symbolic tracing - #516
Closed
ChrisRackauckas wants to merge 3 commits into
Closed
Lower computed initialization maps via symbolic tracing#516ChrisRackauckas wants to merge 3 commits into
ChrisRackauckas wants to merge 3 commits into
Conversation
Trace MTK initialization state and parameter maps with Symbolics variables instead of integer index tokens. Entries copied from a source slot stay static gather recipes; literal outputs become constants; computed entries (for example ODE states that are observed variables of the torn initialization system) compile through `build_function` into isbits generated functions evaluated against the same source vector on the device. The integer-token scheme could not represent computed entries, and worse, an integer-valued computed output could alias a valid source index and initialize with wrong values while reporting success. The symbolic trace removes that aliasing by construction. Raw homotopy initialization problems now fail with a clear error, and the tutorial documents rewriting `homotopy(actual, simplified)` nodes to `actual` to obtain an equivalent supported initialization. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Older ModelingToolkit versions in the downgrade CI lower homotopy nodes without producing a HomotopyProblem, so there is no rejection to assert. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> # Conflicts: # docs/src/tutorials/modelingtoolkit.md
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.
Summary
InitializationSourceIndex), a literal becomes anInitializationConstant, and a computed symbolic expression compiles throughSymbolics.build_functioninto a generated function (InitializationScalarExpression/InitializationArrayExpression) evaluated on-device against the same flat source vector. Arrays with any computed entry compile as one static-output function.RuntimeGeneratedFunctions.drop_exprstrips the storedbody::Exprso compiled recipes are isbits and fit inside GPU kernel problems (the same mechanism MTKBase codegen uses).SymbolicsandRuntimeGeneratedFunctionsjoin theModelingToolkitBaseExttrigger list — both always load with ModelingToolkitBase, so extension activation is unchanged.HomotopyProbleminitialization now fails with a clearArgumentErrorinstead of aMethodError, and the tutorial documents thehomotopy(actual, simplified) → actualrewrite (exact by the operator's own numeric semantics:homotopy(a, s) = a) that yields an equivalent supported initialization, with a runnable recipe.The bug this also fixes
The integer-token trace was not merely limited — it was unsound. A computed map output like
2·token + 1is integer-valued; when it happens to land in1:source_countit is indistinguishable from a legitimate index, and initialization silently gathers wrong values while reporting success. Reproducer (plain ODE,initialization_eqs = [mx^3 + mx ~ 2, my ~ 2mx + 1], correct initmx = 1, my = 3):master (silent wrong answer):
this PR:
Whether master errors or silently mis-initializes depends only on where the aliased value lands: the same equations under different variable naming produce the out-of-range case, which errors. The new
Computed initialization mapstestset hits that shape on master:With this PR both new testsets pass:
Testing
GROUP=JLArraysDAE/MTK suite: all 16 testsets pass, including MTK pendulum (19/19) and the SCC testsets from Run MTK SCC initialization sequentially on GPU kernels #511 — the pure-gather fast path is preserved under symbolic tracing.Testing DiffEqGPU tests passed.@exampleblocks in untouched pages (Pascalcode 801limitation, as on Bump to 3.20.0 for the SCC initialization feature #515) — Documentation CI is the arbiter.Review notes: the map-tracing rewrite touches every recipe path, so the pendulum + Test-3 suites doubling as regression coverage for pure-gather maps is the main safety net. Judgment call worth scrutiny: arrays with any computed entry compile the whole array into one generated function (bare entries become direct indexing) rather than mixing per-element recipes.
This PR should be ignored until reviewed by @ChrisRackauckas.
🤖 Generated with Claude Code