MTKBase: FunctionProperties extension exempting MTKParameters indexing#4701
Closed
ChrisRackauckas-Claude wants to merge 1 commit into
Closed
Conversation
`hasbranching` (FunctionProperties.jl) is used to decide whether a ReverseDiff
tape can be compiled for an ODE right-hand side. For a split (default) MTK
system the parameter object is an `MTKParameters`, and the generated RHS reads
parameters through `getindex(::MTKParameters, ::Int)` — a `@generated`
`if idx == k` chain. That branch is on the integer index, which is a literal
constant at every real call site (so it constant-folds away), but the
`hasbranching` recursion sees only the widened `Int` and reports it. The result
is that even a branch-free split-mode RHS is flagged as branching.
This weakdep extension marks the indexing as value-independent plumbing via the
new `FunctionProperties.is_leaf_sig` hook:
FunctionProperties.is_leaf_sig(
::Type{<:Tuple{typeof(getindex), <:MTKParameters, Vararg}}) = true
Loaded only when FunctionProperties is present; no new hard dependency. Added an
Extensions-group test building a split-mode `ODEProblem` and checking the
exemption.
Stacked on SciML/FunctionProperties.jl#62 (provides `is_leaf_sig`); compat set
to that release. Completes the split-mode case alongside the SciMLBase unwrap
extension (SciML/SciMLBase.jl#1413).
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Member
|
The optimized IR should have all of this constant-folded, right? Why does FunctionProperties pass |
Contributor
Author
|
Superseded. FunctionProperties #62 now folds the value-independent |
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
hasbranching(FunctionProperties.jl) decides whether a ReverseDiff tape can be compiled for an ODE RHS. For a split (default) MTK system the parameter object is anMTKParameters, and the generated RHS reads parameters throughgetindex(::MTKParameters, ::Int)— a@generatedif idx == k … elseif …chain.That branch is on the integer index, which is a literal constant at every real call site (so it constant-folds away in the compiled RHS), but the
hasbranchingrecursion only sees the widenedIntand reports it. Net effect: even a branch-free split-mode RHS such asẋ = a·xis flagged as branching, needlessly disabling ReverseDiff compilation.Change (
lib/ModelingToolkitBase)A weakdep extension marking the indexing as value-independent plumbing via the new
is_leaf_sighook from FunctionProperties#62:Loaded only when FunctionProperties is present — no new hard dependency. Follows the existing
ext/MTK*Ext.jlconvention.Tests
test/extensions/function_properties.jl(Extensions group): builds a split-modeODEProblem, confirmsprob.p isa MTKParameters, that theis_leaf_sigexemption is active forMTKParametersindexing (and does not over-reach to ordinaryVector), and that a function which only indexes the parameter container has no value-dependent branch. Verified locally (4/4) by loading the extension against a realMTKParameters. Runic clean.Stack
is_leaf_sig+ value-independent-branch detection (foundation).AbstractSciMLFunctionto its RHS.MTKParametersinteger indexing, completing the split-mode case.Semver
Additive weakdep extension, non-breaking: MTKBase
1.48.1→1.48.2.