Repository navigation
Treat DiffEqBase.promote_f as an opaque Mooncake primitive - #4201
Merged
ChrisRackauckas merged 3 commits intoAug 10, 2026
Merged
ChrisRackauckas merged 3 commits into
ChrisRackauckas merged 3 commits into
Conversation
AstitvaAggarwal
marked this pull request as draft
August 10, 2026 12:36
DiffEqBase.promote_f as an opaque Mooncake primitive
AstitvaAggarwal
marked this pull request as ready for review
August 10, 2026 14:19
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.
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.