Repository navigation
Description
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
Kayyuri commentedon Sep 15, 2026
Hi @ALinrunrun, Thank you for reporting this issue!
The issue you've identified, where
tf.linalg.expmreturns an incorrect second derivative under nestedtf.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!
google-ml-butler commentedon Sep 21, 2026
Are you satisfied with the resolution of your issue?
Yes
No
Fix tf.linalg.expm second derivative error under nested forward autodiff