Fix NaN second derivative in cumulative_logsumexp #127551

Stale Open
Venkat6871 opened 11:17pm on September 16, 2026 wants to merge 118 Ξ” into tensorflow/tensorflow master from
fix/127243-cumulative-logsumexp-nested-jvp

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.math.cumulative_logsumexp produces nan for a second derivative computed with nested forward-mode autodiff.

The reproducer builds a finite float64 tensor from a scalar t, applies tf.math.cumulative_logsumexp with axis=-1, exclusive=False, and reverse=True, then reduces selected output entries into a scalar. With reverse=True, the selected outputs are smooth suffix log-sum-exp expressions, and their second derivatives are finite.

At t = -40.0, the forward value is computed correctly as 395.0000000000019, but nested tf.autodiff.ForwardAccumulator returns nan for the second derivative. The expected second derivative is approximately 1.0572349592992632e-12.

Expected behavior?

The second derivative of the weighted tf.math.cumulative_logsumexp output at t = -40.0 should be close to 1.0572349592992632e-12, not nan.

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):
    x = tf.reshape(
        tf.stack([t, -t + 1, t / 2 - 2, -2 * t, t + 3, t / 4]),
        [2, 3],
    )
    y = tf.math.cumulative_logsumexp(
        x,
        axis=-1,
        exclusive=False,
        reverse=True,
    )
    return y[0, 0] + 2 * y[0, 1] - y[0, 2] + 3 * y[1, 0] + y[1, 1] - 2 * y[1, 2]

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(-40.0, dtype=tf.float64)

forward = target(x)
actual = forward_ad(forward_ad(target))(x)
expected = 1.0572349592992632e-12

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

if tf.math.is_nan(actual):
    print("BUG REPRODUCED: tf.math.cumulative_logsumexp produces NaN under nested forward AD")
else:
    print("not reproduced")

Relevant log output

forward: 395.0000000000019
actual: nan
expected: 1.0572349592992632e-12
BUG REPRODUCED: tf.math.cumulative_logsumexp produces NaN under nested forward AD
Diff Delta:
118
About 252 Diff Delta/hour
Classified as:  Bug Fix

Venkat6871's Description of Work #127243 tf.math.cumulative_logsumexp produces NaN second derivative under nested forward-mode autodiff

Fixes #127243

tf.math.cumulative_logsumexp produces NaN for second derivatives under nested tf.autodiff.ForwardAccumulator.

The issue is caused by two problems in _CumulativeLogsumexpGrad:


  1. Unselected where_v2 branches evaluate log(0), which can result in 0.0 * inf = NaN during higher-order differentiation.

  2. Using cached op.outputs[0] prevents the outer ForwardAccumulator from tracking its tangent correctly.

This change:


  • Safely masks inputs to log().

  • Handles non-finite cumulative_logsumexp values safely.

  • Recomputes cumulative_logsumexp from op.inputs[0] for correct nested forward-mode autodiff.

  • Adds regression coverage for the nested ForwardAccumulator case.

Tests:



  • cumulative_logsumexp_test.py passes on tf-nightly.

  • Focused regression test passes on TensorFlow 2.21.0.

  • Relevant higher-order forwardprop tests pass.

  • Original reproduction no longer produces NaN.

2 total changed files
Loading changes...
PR terminus reached:
No more diffs beyond this point