Skip to content

Bug(Mooncake): jacobian for real→complex functions returns Float64 instead of ComplexF64 #1010

Description

@bdrhill

Description

When using AutoMooncake() with functions that have real input and complex output, jacobian returns a Float64 matrix containing only partial information instead of the expected ComplexF64 matrix. The imaginary components of the derivatives are lost.

Note: This is likely a Mooncake limitation that should be upstreamed, as the native Mooncake API exhibits the same behavior.

MWE

using DifferentiationInterface
using Mooncake: Mooncake
using ADTypes: AutoMooncake

backend = AutoMooncake(; config=nothing)

# Real input → Complex output
f(x) = [complex(x[1], x[2]), complex(x[2], x[1])]
x = [1.0, 2.0]

J = jacobian(f, backend, x)

Output:

2×2 Matrix{Float64}:
 1.0  0.0
 0.0  1.0

Expected:

2×2 Matrix{ComplexF64}:
 1.0+0.0im  0.0+1.0im
 0.0+1.0im  1.0+0.0im

The jacobian should have imaginary components (the derivative of x[2] appearing in the imaginary part of y[1] is im, not 0).

Native Mooncake API Comparison

The native Mooncake API returns the same incomplete result:

using Mooncake

f(x) = [complex(x[1], x[2]), complex(x[2], x[1])]
x = [1.0, 2.0]

cache = Mooncake.prepare_pullback_cache(f, x)
dy = ComplexF64[1.0, 0.0]

y, (_, dx) = Mooncake.value_and_pullback!!(cache, dy, f, x)
# dx = [1.0, 0.0]  (Float64, not ComplexF64)
# Expected: [1.0, im] or similar complex representation

Mooncake's tangent type appears to match the input type (Float64) rather than accounting for the complex output, losing the imaginary gradient components.

Enzyme Comparison (Correct Output)

Enzyme handles this case correctly:

using DifferentiationInterface
using Enzyme: Enzyme
using ADTypes: AutoEnzyme

f(x) = [complex(x[1], x[2]), complex(x[2], x[1])]
x = [1.0, 2.0]

J = jacobian(f, AutoEnzyme(), x)
# 2×2 Matrix{ComplexF64}:
#  1.0+0.0im  0.0+1.0im
#  0.0+1.0im  1.0+0.0im

Interesting Note

Mooncake's derivative for scalar real→complex works correctly:

g(t) = complex(t, 2*t)
d = derivative(g, AutoMooncake(; config=nothing), 1.0)
# Returns: 1.0 + 2.0im ✓

Only jacobian (which uses pullbacks internally) exhibits this issue.

Recommendation

This should likely be reported upstream to Mooncake, as DI correctly calls the Mooncake API which returns limited results. Possible solutions:

  1. Mooncake could support complex tangents for real inputs when output is complex
  2. DI could document this as a known Mooncake limitation
  3. DI could detect this case and warn users

Environment

  • Julia 1.12.5
  • DifferentiationInterface v0.7.18
  • Mooncake v0.5.28

🤖 I am a robot. This is an experiment in agentic bug-catching under the supervision of @adrhill and @gdalle (#1008). Contents may be hallucinated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    botIssue or PR created automatically, wait for human review before interacting

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions