Skip to content

Add forward mode rules for calling a FunctionWrappersWrapper - #84

Merged
ChrisRackauckas merged 4 commits into
SciML:mainfrom
AstitvaAggarwal:Astitva/fix-1427-functionwrapperswrapper-forward
Aug 18, 2026
Merged

Add forward mode rules for calling a FunctionWrappersWrapper#84
ChrisRackauckas merged 4 commits into
SciML:mainfrom
AstitvaAggarwal:Astitva/fix-1427-functionwrapperswrapper-forward

Conversation

@AstitvaAggarwal

@AstitvaAggarwal AstitvaAggarwal commented Aug 10, 2026

Copy link
Copy Markdown
Member

Mooncake had a reverse mode rule for calling a FunctionWrappersWrapper, but no forward mode rule, so forward mode differentiation through code that calls one (for example, second derivatives) failed.

This adds a forward mode rule for calling a wrapper, plus rules for unwrap, which reaches into a nested FunctionWrapper's internal field in a way the generic getfield rule cannot handle since FunctionWrapperTangent is a custom tangent type.

Every rule here builds a fresh zero tangent for the unwrapped function rather than threading through any real tangent it might carry. That is only correct if the wrapped function itself has nothing to differentiate; if it does (for example, a struct holding a learnable matrix), it now errors clearly instead of silently returning a too-small gradient. Derived rules are cached by signature too, since the wrapped function is typically called once per ODE timestep, and rebuilding a rule from scratch on every call would be wasted work.

Needed for SciML/SciMLSensitivity.jl#1427.

build_frule(f_orig_dual, args...) is an existing convenience method that
extracts the interpreter/signature internally, same as build_rrule(sig)
already does for the reverse-mode rule above. Removes the need to import
ForwardMode/get_interpreter at all, cutting the Mooncake QA ignore list
from 15 to 13 symbols.
@ChrisRackauckas
ChrisRackauckas merged commit 425ac57 into SciML:main Aug 18, 2026
16 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