Skip to content

MTKBase: FunctionProperties extension exempting MTKParameters indexing#4701

Closed
ChrisRackauckas-Claude wants to merge 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:mtkbase-functionproperties-ext
Closed

MTKBase: FunctionProperties extension exempting MTKParameters indexing#4701
ChrisRackauckas-Claude wants to merge 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:mtkbase-functionproperties-ext

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Contributor

Please ignore until reviewed by @ChrisRackauckas. Draft opened by an agent. Stacked on SciML/FunctionProperties.jl#62 — needs the FunctionProperties 0.1.7 release to resolve (compat is pinned to it). Companion to SciML/SciMLBase.jl#1413.

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 an MTKParameters, and the generated RHS reads parameters through getindex(::MTKParameters, ::Int) — a @generated if 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 hasbranching recursion only sees the widened Int and reports it. Net effect: even a branch-free split-mode RHS such as ẋ = a·x is 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_sig hook from FunctionProperties#62:

FunctionProperties.is_leaf_sig(
    ::Type{<:Tuple{typeof(getindex), <:MTKParameters, Vararg}}) = true

Loaded only when FunctionProperties is present — no new hard dependency. Follows the existing ext/MTK*Ext.jl convention.

Tests

test/extensions/function_properties.jl (Extensions group): builds a split-mode ODEProblem, confirms prob.p isa MTKParameters, that the is_leaf_sig exemption is active for MTKParameters indexing (and does not over-reach to ordinary Vector), 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 real MTKParameters. Runic clean.

Stack

Semver

Additive weakdep extension, non-breaking: MTKBase 1.48.11.48.2.

`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>
@AayushSabharwal

Copy link
Copy Markdown
Member

The optimized IR should have all of this constant-folded, right? Why does FunctionProperties pass optimize = false?

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Contributor Author

Superseded. FunctionProperties #62 now folds the value-independent getindex(::MTKParameters, ::Int) branch generically via constant-propagation-aware analysis (the is_leaf_sig hook this extension used has been removed), so no MTK-side override is needed. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants