PR #115554

tensorflow/tensorflow · Core Math · Merged · 2 files changed

Prevented +inf accumulators in tf.math.cumulative_logsumexp from triggering NaN cascades by guarding stabilized max-subtraction in the C++ scan kernel.

Checkout Pull Request

Inspect the full commit history and diff locally from upstream tensorflow/tensorflow.

$gh pr checkout 115554 --repo tensorflow/tensorflow
Causal & File Impact Graph (PR #115554)Core Math / Merged
INPUT / TRIGGERCore Math Edge CaseROOT CAUSEMissing InvariantPATCHED (2 FILES)scan_ops.hcumulative_logsume...OUTCOMEMerged

Prevented +inf accumulators in tf.math.cumulative_logsumexp from triggering NaN cascades by guarding stabilized max-subtraction in the C++ scan kernel.

File-by-File Modification Breakdown (2)

  • tensorflow/core/kernels/scan_ops.h

    Core C++ Eigen kernel implementation and boundary guards

    +11 / -3
  • tensorflow/python/kernel_tests/math_ops/cumulative_logsumexp_test.py

    Python/XLA regression test suite verifying edge-case behavior

    +12 / -0

Changed Files (2)

tensorflow/core/kernels/scan_ops.h31 lines
@@ -60,7 +60,12 @@ struct LogSumExp {
         Eigen::internal::scalar_cmp_op<T, T, Eigen::internal::cmp_LT>();
 
     auto logsumexp = add(log1p(exp(sub(mi, ma))), ma);
-    return cmp_lt(ma, Eigen::NumTraits<T>::lowest()) ? ma : logsumexp;
+    // Return ma directly if it is -inf (all inputs -inf) or +inf
+    // (avoids inf - inf = NaN in the subtraction above).
+    return (cmp_lt(ma, Eigen::NumTraits<T>::lowest()) ||
+            cmp_lt(Eigen::NumTraits<T>::highest(), ma))
+               ? ma
+               : logsumexp;
   }
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T packetOp(const T& a,
                                                    const T& b) const {
@@ -70,12 +75,15 @@ struct LogSumExp {
     using Eigen::internal::pcmp_lt;
     using Eigen::internal::pexp;
     using Eigen::internal::plog1p;
+    using Eigen::internal::por;
     using Eigen::internal::pset1;
     using Eigen::internal::psub;
 
     auto logsumexp = padd(plog1p(pexp(psub(mi, ma))), ma);
-    return pselect(pcmp_lt(ma, pset1(Eigen::NumTraits<T>::lowest())), ma,
-                   logsumexp);
+    // Select ma directly if it is -inf or +inf.
+    auto is_inf = por(pcmp_lt(ma, pset1(Eigen::NumTraits<T>::lowest())),
+                      pcmp_lt(pset1(Eigen::NumTraits<T>::highest()), ma));
+    return pselect(is_inf, ma, logsumexp);
   }
 };

1. Introduction & Motivation

tf.math.cumulative_logsumexp computes running log-space sums in sequential models, particle filters, and CTC decoding. To avoid exponential overflow, the kernel uses the classic max-subtraction trick [1]. However, when any prefix element equals +∞+\infty, the running maximum becomes +∞+\infty, causing subsequent terms to evaluate (+∞)−(+∞)=NaN(+\infty) - (+\infty) = \text{NaN} and poison all remaining outputs.

2. Numerical Stability Proof

Standard stabilized LogSumExp evaluates:

LSE⁡(x1,…,xn)=m+log⁡∑i=1nexp⁡(xi−m),m=max⁡1≤i≤nxi(1)\operatorname{LSE}(x_1, \dots, x_n) = m + \log \sum_{i=1}^{n} \exp(x_i - m), \qquad m = \max_{1 \le i \le n} x_i \tag{1}

If xk=+∞x_k = +\infty, then m=+∞m = +\infty, so every subsequent scan step evaluates:

xi−m=(+∞)−(+∞)=NaN  ⟹  exp⁡(NaN)=NaN(2)x_i - m = (+\infty) - (+\infty) = \text{NaN} \implies \exp(\text{NaN}) = \text{NaN} \tag{2}

Guarding m=+∞m = +\infty explicitly in the scan accumulator preserves LSE⁡=+∞\operatorname{LSE} = +\infty without generating NaN.

Prefix InputBefore FixAfter Fix
Finite valuesStable LSE⁡\operatorname{LSE}Stable LSE⁡\operatorname{LSE}
Contains +∞+\inftyNaN cascadeExact +∞+\infty

3. Implementation & Verification

// In tensorflow/core/kernels/scan_ops.h:
if (Eigen::numext::isinf(curr_max) && curr_max > T(0)) return curr_max;

4. References

  1. Blanchard, Higham & Higham, Accurately Computing the Log-Sum-Exp and Softmax Functions, IMA Journal of Numerical Analysis, 2021.
View PR #115554 on GitHubRead technical story

The soul becomes dyed with the color of its thoughts.

Marcus Aurelius

Made with <3 and lots of coffee.