Skip to content

Add Mooncake extension for FunctionWrappersWrapper#39

Merged
ChrisRackauckas merged 1 commit into
SciML:mainfrom
ChrisRackauckas-Claude:mooncake-extension
Apr 8, 2026
Merged

Add Mooncake extension for FunctionWrappersWrapper#39
ChrisRackauckas merged 1 commit into
SciML:mainfrom
ChrisRackauckas-Claude:mooncake-extension

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Contributor

Summary

Add a Mooncake extension that makes FunctionWrappersWrapper calls a Mooncake primitive, using unwrap to differentiate through the original function directly.

Problem

When Mooncake differentiates through code calling a FunctionWrappersWrapper, it tries to create tangent types for each FunctionWrapper variant in the internal tuple. These have different type parameters (for different ForwardDiff Dual combinations), producing incompatible FunctionWrapperTangent types that fail with:

MethodError: Cannot convert MooncakeFunctionWrappersExt.FunctionWrapperTangent{...} to MooncakeFunctionWrappersExt.FunctionWrapperTangent{...}

Fix

Same pattern as the existing Enzyme extension: make the call a primitive, unwrap to get the original function, differentiate through that. tangent_type(::Type{<:FunctionWrappersWrapper}) = NoTangent since the wrapper is dispatch infrastructure.

Context

Needed for Mooncake AD through NonlinearProblem solves with AutoSpecialize (which wraps functions in FunctionWrappersWrapper), particularly for SCCNonlinearProblem in SciML/SciMLSensitivity.jl#1358.

🤖 Generated with Claude Code

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>
@ChrisRackauckas ChrisRackauckas merged commit 77592ef into SciML:main Apr 8, 2026
5 of 7 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