Skip to content

tf.linalg.expm returns incorrect second derivative under nested forward-mode autodiff #127224

Closed

Description

@ALinrunrun

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

source

TensorFlow version

tf 2.21.0, also reproduced on tf 2.22.0-dev20260904

Custom code

Yes

OS platform and distribution

Linux Ubuntu 22.04

Mobile device

No response

Python version

Python 3.13.5

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

tf.linalg.expm returns an incorrect second derivative under nested forward-mode autodiff.

The reproducer builds a 2x2 matrix of the form t * I + A, where A is a fixed rotation generator, and evaluates the sum of its matrix exponential. For this input, the scalar function is exactly 2 * exp(t) * cos(1), so its first and second derivatives are equal to the same expression.

At t = 0.7, the expected second derivative is approximately 2.1760704625890157. However, nested tf.autodiff.ForwardAccumulator returns 357356221.1760303, which is many orders of magnitude larger than the expected value.

The forward value itself is correct up to normal floating-point error, so the discrepancy appears in the order-2 forward-mode autodiff result.

Expected behavior?

The second derivative computed by nested forward-mode autodiff should be close to the mathematical reference value 2.1760704625890157, not 357356221.1760303.

Standalone code to reproduce the issue

import os
os.environ["CUDA_VISIBLE_DEVICES"] = ""
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["TF_NUM_INTRAOP_THREADS"] = "1"
os.environ["TF_NUM_INTEROP_THREADS"] = "1"

import tensorflow as tf

def target(t):
    one = tf.constant(1, dtype=t.dtype)
    a = tf.stack([
        tf.stack([t, one]),
        tf.stack([-one, t]),
    ])
    return tf.reduce_sum(tf.linalg.expm(a))

def forward_ad(fn):
    def jvp(x):
        with tf.autodiff.ForwardAccumulator(x, tf.ones_like(x)) as acc:
            y = fn(x)
        return acc.jvp(y)
    return jvp

x = tf.constant(0.7, dtype=tf.float64)

forward = target(x)
actual = forward_ad(forward_ad(target))(x)
expected = 2.1760704625890157

print("forward:", forward.numpy())
print("actual:", actual.numpy())
print("expected:", expected)

if abs(float(actual) - expected) > 1e-9:
    print("BUG REPRODUCED: tf.linalg.expm returns incorrect second derivative under nested forward AD")
else:
    print("not reproduced")

Relevant log output

forward: 2.1760704625890153
actual: 357356221.1760303
expected: 2.1760704625890157
BUG REPRODUCED: tf.linalg.expm returns incorrect second derivative under nested forward AD

Activity

  1. added
    comp:core
    2.21.0
    on Sep 11, 2026
  2. Kayyuri commented on Sep 15, 2026

    @Kayyuri

    Hi @ALinrunrun, Thank you for reporting this issue!

    The issue you've identified, where tf.linalg.expm returns an incorrect second derivative under nested tf.autodiff.ForwardAccumulator, appears to be a bug in how higher-order derivatives are computed for this operation in forward mode.

    We’ll investigate the issue on our end and provide an update. Thanks!

  3. added
    stat:awaiting response
    comp:eager
    comp:ops
    subtype: ubuntu/linux
    subtype:bazel
    on Sep 15, 2026
  4. removed
    stat:awaiting response
    on Sep 15, 2026
  5. removed
    comp:eager
    comp:ops
    subtype: ubuntu/linux
    subtype:bazel
    on Sep 15, 2026
  6. added
    stat:awaiting tensorflower
    on Sep 16, 2026
  7. google-ml-butler commented on Sep 21, 2026

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

  8. added a commit that references this issue on Sep 21, 2026
    1cb44dc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

2.21.0comp:corestat:awaiting tensorflowertype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions