Skip to content

Treat DiffEqBase.promote_f as an opaque Mooncake primitive - #4201

Merged
ChrisRackauckas merged 3 commits into
SciML:masterfrom
AstitvaAggarwal:Astitva/fix-1427-promote_f-forward-rule
Aug 10, 2026
Merged

ChrisRackauckas merged 3 commits into
SciML:masterfrom
AstitvaAggarwal:Astitva/fix-1427-promote_f-forward-rule

Conversation

@AstitvaAggarwal

@AstitvaAggarwal AstitvaAggarwal commented Aug 10, 2026 •

Copy link
Copy Markdown
Member

promote_f can wrap f in a FunctionWrappersWrapper for performance. Constructing that wrapper involves raw cfunction IR, which Mooncake's forward mode interpreter does not support, so forward mode differentiation through a real solve (for example, Tsit5) crashes with "Expressions of type :cfunction are not yet supported in forward mode".

This marks promote_f itself as a Mooncake primitive with a hand written rrule!!/frule!! pair, so Mooncake never traces into the FunctionWrappersWrapper construction. The rare AutoDePSpecialize opaque-p branch is not handled, it errors clearly instead of silently returning a wrong gradient.

promote_f can also return a new (non-identical) f object even when nothing gets wrapped, for example when it rebuilds f to promote a jac_prototype's element type. Zeroing f's tangent is only correct if f itself has nothing to differentiate, so that case is guarded the same way and errors clearly instead of silently dropping a real gradient.

Only covers the Val{false} method (algorithms that do not need ForwardDiff internally, e.g. Tsit5/Verner). The Val{true} method used by algorithms like Rosenbrock/BDF wraps jac/tgrad/f separately and is not covered here.

Fixes the promote_f part of SciML/SciMLSensitivity.jl#1427.

@AstitvaAggarwal
AstitvaAggarwal marked this pull request as draft August 10, 2026 12:36
@AstitvaAggarwal AstitvaAggarwal changed the title Treat DiffEqBase.promote_f as an opaque Mooncake primitive Treat DiffEqBase.promote_f as an opaque Mooncake primitive Aug 10, 2026
@AstitvaAggarwal
AstitvaAggarwal marked this pull request as ready for review August 10, 2026 14:19
@ChrisRackauckas
ChrisRackauckas merged commit ca6e75f into SciML:master Aug 10, 2026
87 of 136 checks passed
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.

2 participants