Repository navigation
Description
Issue type
Bug
Have you reproduced the bug with TensorFlow Nightly?
Yes
Source
binary
TensorFlow version
tf 2.21.0, also reproduced on tf-nightly 2.22.0-dev20260920
Custom code
Yes
OS platform and distribution
macOS 15.7.2 (arm64)
Mobile device
No response
Python version
3.11.7
Bazel version
No response
GCC/compiler version
No response
CUDA/cuDNN version
No response
GPU model and memory
No response
Current behavior?
In eager mode, ForwardAccumulator nested over ForwardAccumulator returns a wrong (finite, no error) second derivative when an intermediate tensor is used as an operand of a Mul both directly and through another op.
Smallest case I could find: m = tf.identity(x) followed by (m * m) * m. This is x**3, so the second derivative at x = 0.7 is 6x = 4.2. Nested forward mode returns 2.4. Reverse-over-reverse returns 4.2.
What I checked while narrowing it down (all float64, x = 0.7, eager, nested forward vs reverse-over-reverse):
- The same nested-forward code wrapped in
tf.functiongives the right answer. - Forward-over-reverse and reverse-over-forward both give the right answer, so it is specific to forward-over-forward.
- It is not specific to
MatMul.a = tf.sin(x); (a * a) * aalso mismatches (0.656 vs 1.459). With 2x2 matrices,(m @ m) @ mform = [[x, 1], [-1, x]]gives 4.8 vs 8.4 (summed entries). - Reusing the intermediate is what matters.
a = x * x; b = a * a; b * ais wrong, butb * bandb + aare right, and so is plain(x * x) * xwhere the reused operand is the watched input itself. - float32 shows the same discrepancy, so it is not a precision effect.
I have not traced the cause into the accumulator code, so I can't say more than that. I ran into this while working on #127224 (tf.linalg.expm under nested forward mode, fixed in #127627). Padé-9 and Padé-13 build m6 = m4 @ m2 from m4 = m2 @ m2, which has exactly this shape, and after that fix the nested-forward second derivative of expm is still about 9% off (2.376 vs the exact 2.176). The regression test added there keeps a loose tolerance on that one assertion for this reason.
Expected: forward-over-forward should agree with reverse-over-reverse, as it does in the graph-mode and mixed-order cases above.
Standalone code to reproduce the issue
import tensorflow as tf
print("TF", tf.version.VERSION)
def f(x):
m = tf.identity(x)
return (m * m) * m # x**3, so f''(x) = 6x
def forward_over_forward(fn, x):
with tf.autodiff.ForwardAccumulator(x, tf.ones_like(x)) as outer:
with tf.autodiff.ForwardAccumulator(x, tf.ones_like(x)) as inner:
y = fn(x)
dy = inner.jvp(y)
return outer.jvp(dy)
def reverse_over_reverse(fn, x):
with tf.GradientTape() as t2:
t2.watch(x)
with tf.GradientTape() as t1:
t1.watch(x)
y = fn(x)
dy = t1.gradient(y, x)
return t2.gradient(dy, x)
x = tf.constant(0.7, tf.float64)
print("expected 6x :", 6 * float(x))
print("reverse-over-reverse :", float(reverse_over_reverse(f, x)))
print("forward-over-forward (eager) :", float(forward_over_forward(f, x)))
print("forward-over-forward (tf.function) :",
float(tf.function(lambda v: forward_over_forward(f, v))(x)))Relevant log output
TF 2.21.0
expected 6x : 4.199999999999999
reverse-over-reverse : 4.2
forward-over-forward (eager) : 2.4
forward-over-forward (tf.function) : 4.199999999999999Same output on tf-nightly 2.22.0-dev20260920.
Activity
google-ml-butler commentedon Sep 28, 2026
Are you satisfied with the resolution of your issue?
Yes
No