Skip to content

Return raw tangents from friendly_tangents for non-Tangent structs - #1467

Open
yebai wants to merge 1 commit into
mainfrom
friendlyraw
Open

yebai wants to merge 1 commit into
mainfrom
friendlyraw

Conversation

@yebai

@yebai yebai commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

With friendly_tangents=true, friendly_tangent_cache assumed every immutable struct with fields has a Tangent. Differentiating abs2 at 1.0 + 2.0im, or any function of a floating-point Complex, TwicePrecision or MemoryRef argument, threw an error such as type Complex has no field fields. Non-differentiable structs returned (n = NoTangent(),)-style output instead of NoTangent(), and on Julia 1.12 and later a mutable one such as a Method overflowed the stack.

The field-wise conversion now applies only when the tangent type is Tangent or MutableTangent; any other tangent is returned unchanged, as the AsRaw docstring states for custom tangent types.

CI Summary — GitHub Actions

Documentation Preview

Mooncake.jl documentation for PR #1467 is available at:
https://chalk-lab.github.io/Mooncake.jl/previews/PR1467/

Performance

Performance Ratio:
Ratio of time to compute gradient and time to compute function.
Warning: results are very approximate! See here for more context.

┌────────────────────────────┬──────────┬──────────┬─────────────┬─────────┬─────────────┬─────────┐
│                      Label │   Primal │ Mooncake │ MooncakeFwd │  Zygote │ ReverseDiff │  Enzyme │
│                     String │   String │   String │      String │  String │      String │  String │
├────────────────────────────┼──────────┼──────────┼─────────────┼─────────┼─────────────┼─────────┤
│                   sum_1000 │ 439.0 ns │     1.27 │        1.55 │    1.72 │        4.63 │ missing │
│                  _sum_1000 │ 620.0 ns │     6.77 │        1.14 │   103.0 │        45.0 │ missing │
│               sum_sin_1000 │  4.29 μs │     3.67 │        1.46 │    3.17 │        13.0 │ missing │
│              _sum_sin_1000 │  4.15 μs │     3.31 │         2.5 │    18.9 │        13.8 │ missing │
│                   kron_sum │ 358.0 μs │     2.62 │        2.57 │    4.58 │       234.0 │ missing │
│              kron_view_sum │ 358.0 μs │     2.96 │        3.42 │    8.38 │       230.0 │ missing │
│      naive_map_sin_cos_exp │  2.12 μs │     2.44 │        1.39 │ missing │        7.59 │ missing │
│            map_sin_cos_exp │  2.08 μs │     2.95 │        1.71 │     2.5 │        6.46 │ missing │
│      broadcast_sin_cos_exp │  2.37 μs │     2.55 │         1.4 │    3.54 │        1.86 │ missing │
│                 simple_mlp │ 161.0 μs │     5.81 │        2.65 │    1.81 │        12.9 │ missing │
│                     gp_lml │ 182.0 μs │     6.06 │        2.52 │    11.5 │     missing │ missing │
│ turing_broadcast_benchmark │  1.49 ms │     4.96 │        3.09 │ missing │        31.9 │ missing │
│         large_single_block │ 387.0 ns │      9.2 │        2.14 │  1920.0 │        32.6 │ missing │
└────────────────────────────┴──────────┴──────────┴─────────────┴─────────┴─────────────┴─────────┘

Friendly gradients returned `(n = NoTangent(),)` for non-differentiable
structs and raised missing-`fields` errors for floating-point `Complex`,
`TwicePrecision` and `MemoryRef` arguments. On Julia 1.12+, a `Method`
argument overflowed the stack during cache construction.

Build per-field caches only for standard `Tangent` or `MutableTangent`
representations; return other tangents unchanged.

Assisted-by: Claude Code <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

This branch has not been deployed

No deployments
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.

1 participant