Repository navigation
Preserve contiguous array derivative views in implicit residual codegen - #5242
Draft
ChrisRackauckas-Claude wants to merge 4 commits into
Draft
ChrisRackauckas-Claude wants to merge 4 commits into
ChrisRackauckas-Claude wants to merge 4 commits into
Conversation
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> Co-Authored-By: Codex <noreply@openai.com> Agent-Harness: Codex CLI 0.153.4 Agent-Model: gpt-6-astra Agent-Session: local session 01a08018-78ff-7e42-a964-f5174f3f763b
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> Co-Authored-By: Codex <noreply@openai.com> Agent-Harness: Codex CLI 0.153.4 Agent-Model: gpt-6-astra Agent-Session: local session 01a08018-78ff-7e42-a964-f5174f3f763b
AayushSabharwal
requested changes
Oct 6, 2026
AayushSabharwal
left a comment
Member
There was a problem hiding this comment.
Some more comments inside array_derivative_arguments! would be nice too.
Comment on lines
+44
to
+47
| terms = Set{SymbolicT}() | ||
| for expressions in (rhss, extra_expressions), rhs in expressions | ||
| Symbolics.get_variables!(terms, rhs; is_atomic = array_derivative_is_atomic) | ||
| end |
Member
There was a problem hiding this comment.
Suggested change
| terms = Set{SymbolicT}() | |
| for expressions in (rhss, extra_expressions), rhs in expressions | |
| Symbolics.get_variables!(terms, rhs; is_atomic = array_derivative_is_atomic) | |
| end | |
| terms = Set{SymbolicT}() | |
| buffer = SU.IRStructureSearchBuffer(ir, terms) | |
| for expressions in (rhss, extra_expressions), rhs in expressions | |
| Symbolics.get_variables!(buffer, rhs; is_atomic = array_derivative_is_atomic) | |
| end |
| if length(positions) == length(indices) && | ||
| positions == collect(first(positions):last(positions)) && | ||
| all(isequal(dvs[i], parent[j]) for (i, j) in zip(positions, indices)) | ||
| arrays[parent] = Symbolics.term(reshape, du[first(positions):last(positions)], size(parent); type = symtype(parent), shape = SU.shape(parent)) |
Member
There was a problem hiding this comment.
Suggested change
| arrays[parent] = Symbolics.term(reshape, du[first(positions):last(positions)], size(parent); type = symtype(parent), shape = SU.shape(parent)) | |
| arrays[parent] = Symbolics.STerm(reshape, Symbolics.SArgsT((du[first(positions):last(positions)], size(parent))); type = symtype(parent), shape = SU.shape(parent)) |
Less compilation
| subs[term] = unwrap(Symbolics.substitute(wrap(only(expanded)), subs)) | ||
| end | ||
| end | ||
| subber = ex -> unwrap(Symbolics.substitute(wrap(ex), subs)) |
Member
There was a problem hiding this comment.
Suggested change
| subber = ex -> unwrap(Symbolics.substitute(wrap(ex), subs)) | |
| subber = SU.IRSubstituter{false}(ir, subs) |
Use IRStructureSearchBuffer for term collection, construct reshape via STerm/SArgsT, and apply substitutions with IRSubstituter. Document why contiguous du views are safe, when the scalar fallback runs, and what the sentinel collision check guards. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> Co-Authored-By: Cursor Agent <noreply@cursor.com> Agent-Harness: Cursor Agent CLI 2026.10.01-14929f9 Agent-Model: auto Agent-Session: local session, transcript at /home/crackauc/sandbox/goals/issue-backlog/orch-ibmtk/jobs/5097-cursor/log.txt on amdci2.julia.csail.mit.edu
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> Co-Authored-By: Cursor Agent <noreply@cursor.com> Agent-Harness: Cursor Agent CLI 2026.10.01-14929f9 Agent-Model: auto Agent-Session: local session, transcript at /home/crackauc/sandbox/goals/issue-backlog/orch-ibmtk/jobs/5097-cursor/log.txt on amdci2.julia.csail.mit.edu
This branch has not been deployed
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.
Please ignore until reviewed by @ChrisRackauckas.
Summary
Adopts stalled #5121 onto current master. Implicit DAE residuals previously expanded each array derivative into one scalar
duread per element viaSymbolicUtils.array_literal, so residual expression size grew with state dimension (D(x) ~ -xAST nodes 623→1631 for n=24→96). Contiguous derivative blocks are now bound to reshaped views of theduargument, keeping generated residual code size independent ofnwhen the state layout is contiguous.Cherry-picked from #5121 with authorship preserved (
7bb83a3,e19beae). The DiffEqBase7.18.1bump commit was skipped: master already requires it. Conflict resolution kept master's existingarray_equation_dae.jlcoverage and master's element-wisedervarshandling inbuild_explicit_observed_function(from the #5148 array-equation work); #5121's regression tests for code size / observations / interleaved layouts / sentinel collision were retained.Review follow-up (AayushSabharwal)
Addressed the requested changes in
array_derivative_arguments!(96afb6e):IRStructureSearchBuffer+Symbolics.get_variables!(as suggested).Symbolics.STerm/SArgsTfor the reshape binding, andSU.IRSubstituter{false}for residual substitution.duviews, the element-wise fallback, and the__mtk_dae_dusentinel collision check.isempty(terms) || array unknowns) with no behaviour change.Relation to #5101 / #5121
ODEProblemfrom array differential equations withcompleteonly #5101 (closed, unmerged): ODEProblem-from-array-DEs viacomplete. Superseded by merged Unify array-equation support behind a capability trait; fold in #5101 ODE support and MultiObjectiveOptimizationFunction #5148; that ODE path is already on master and is orthogonal to this DAE residual codegen size fix.Fixes #5097.
Verification
Negative control (src fix reverted to master
codegen.jl, new tests kept)Unchanged residual-size assertion; still fails on master:
With fix after review follow-up (
include("lib/ModelingToolkitBase/test/array_equation_dae.jl"))All 14 testsets in that file passed.
Owning group (after review follow-up)
Local InterfaceI was run against Symbolics v7.42.0. Current CI reds in InterfaceI (
variable_parsing.jl:66— "Variable x must have default of matching size") and Extended (ccompile.jl:11CTarget) come from Symbolics v7.44.1 (released 2026-10-06); they reproduce on master and are unrelated to this PR.Runic
--check --diffandtyposclean onlib/ModelingToolkitBase/src/systems/codegen.jl.Not verified
What a reviewer should push back on
__mtk_dae_duvs generating a different stable name when it collides with a user symbol (collision currently throwsArgumentError).Risk assessment
generate_rhs(...; implicit_dae=true)/DAEFunction/DAEProblem; observed equations that mention derivatives; assertion expressions in that path.array_equation_dae.jl; InterfaceI 1794/5 broken green locally on Symbolics 7.42.0 after the review follow-up.Please ignore this draft until reviewed by @ChrisRackauckas.
Harness: Cursor Agent CLI 2026.10.01-14929f9 · Model: auto · Transcript: /home/crackauc/sandbox/goals/issue-backlog/orch-ibmtk/jobs/5097-cursor/log.txt on amdci2.julia.csail.mit.edu