Add Mooncake extension for FunctionWrappersWrapper#39
Merged
ChrisRackauckas merged 1 commit intoApr 8, 2026
Conversation
When Mooncake differentiates through code that calls a FunctionWrappersWrapper, it tries to create tangent types for each FunctionWrapper variant in the internal tuple. These variants have different type parameters (for different ForwardDiff Dual combinations), producing incompatible FunctionWrapperTangent types that can't be stored in a typed tuple — causing a convert error. The fix: make FunctionWrappersWrapper calls a Mooncake primitive that unwraps to the original function (via `unwrap`) and differentiates through that directly. This mirrors the existing Enzyme extension pattern. The FunctionWrappersWrapper itself gets NoTangent since it's runtime dispatch infrastructure, not differentiable data — the original function's derivatives are handled in the rrule. This enables Mooncake to differentiate through NonlinearProblem solves that use AutoSpecialize (FunctionWrappers), which is needed for SCCNonlinearProblem AD support in SciMLSensitivity.jl (#1358). Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
5bf8b41 to
b943a46
Compare
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
Add a Mooncake extension that makes
FunctionWrappersWrappercalls a Mooncake primitive, usingunwrapto differentiate through the original function directly.Problem
When Mooncake differentiates through code calling a
FunctionWrappersWrapper, it tries to create tangent types for eachFunctionWrappervariant in the internal tuple. These have different type parameters (for different ForwardDiff Dual combinations), producing incompatibleFunctionWrapperTangenttypes that fail with:Fix
Same pattern as the existing Enzyme extension: make the call a primitive,
unwrapto get the original function, differentiate through that.tangent_type(::Type{<:FunctionWrappersWrapper}) = NoTangentsince the wrapper is dispatch infrastructure.Context
Needed for Mooncake AD through
NonlinearProblemsolves withAutoSpecialize(which wraps functions inFunctionWrappersWrapper), particularly forSCCNonlinearProblemin SciML/SciMLSensitivity.jl#1358.🤖 Generated with Claude Code