PR #124927

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

The C++ Eigen kernel for tf.math.igamma omitted a domain boundary check for non-positive shape parameters (a <= 0) when x == 0, returning invalid finite values instead of NaN. I restored exact mathematical domain semantics across the kernel.

Checkout Pull Request

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

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

The C++ Eigen kernel for tf.math.igamma omitted a domain boundary check for non-positive shape parameters (a <= 0) when x == 0, returning invalid finite values instead of NaN. I restored exact mathematical domain semantics across the kernel.

File-by-File Modification Breakdown (2)

  • tensorflow/core/kernels/cwise_ops.h

    Core C++ Eigen kernel implementation and boundary guards

    +37 / -1
  • tensorflow/python/kernel_tests/math_ops/cwise_ops_binary_test.py

    Python/XLA regression test suite verifying edge-case behavior

    +24 / -0

Changed Files (2)

tensorflow/core/kernels/cwise_ops.h52 lines
@@ -844,6 +844,42 @@ struct functor_traits<scalar_erfinv_op<float>> {
   };
 };
 
+// igamma(a, x) = P(a, x) is defined only for a > 0, x >= 0.  Eigen's
+// scalar_igamma_op short-circuits to 0 when x == 0 before the domain check
+// fires, so a <= 0 with x == 0 silently returns 0 instead of NaN.  The
+// wrapper restores the mathematically correct NaN for out-of-domain inputs.
+template <typename Scalar>
+struct igamma_op : binary_op_base<Scalar, Scalar> {
+  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar
+  operator()(const Scalar& a, const Scalar& x) const {
+    if (x == Scalar(0) && !(a > Scalar(0))) {
+      return Eigen::NumTraits<Scalar>::quiet_NaN();
+    }
+    return Eigen::internal::scalar_igamma_op<Scalar>()(a, x);
+  }
+  template <typename Packet>
+  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet
+  packetOp(const Packet& a, const Packet& x) const {
+    Packet zeros = pzero(x);
+    Packet x_is_zero = pcmp_eq(x, zeros);
+    Packet a_gt_zero = pcmp_lt(zeros, a);
+    Packet domain_error = pandnot(x_is_zero, a_gt_zero);
+    Packet nan = pset1<Packet>(Eigen::NumTraits<Scalar>::quiet_NaN());
+    Packet igamma_val =
+        Eigen::internal::scalar_igamma_op<Scalar>().packetOp(a, x);
+    return pselect(domain_error, nan, igamma_val);
+  }
+};
+
+template <typename Scalar>
+struct functor_traits<igamma_op<Scalar>> {
+  enum {
+    Cost = functor_traits<scalar_igamma_op<Scalar>>::Cost +
+           Eigen::NumTraits<Scalar>::AddCost,
+    PacketAccess = functor_traits<scalar_igamma_op<Scalar>>::PacketAccess,
+  };
+};
+
 }  // end namespace internal
 }  // end namespace Eigen
 
@@ -1177,7 +1213,7 @@ struct minimum
     : base<T, Eigen::internal::scalar_min_op<T, T, Eigen::PropagateNaN>> {};
 
 template <typename T>
-struct igamma : base<T, Eigen::internal::scalar_igamma_op<T>> {};
+struct igamma : base<T, Eigen::internal::igamma_op<T>> {};
 
 template <typename T>
 struct random_gamma_grad

1. Introduction & Motivation

Special functions in TensorFlow underpin probabilistic programming, survival analysis, and Bayesian inference. Because downstream models often evaluate tf.math.igamma across broad parameter sweeps or during unconstrained optimization steps, out-of-domain inputs must strictly produce NaN rather than silent finite numbers that could corrupt loss landscapes or gradients without warning [1].

2. Mathematical Formulation & Domain Analysis

The lower regularized incomplete gamma function P(a,x)P(a, x) is defined for a>0a > 0 and x≥0x \ge 0 by:

P(a,x)=γ(a,x)Γ(a)=1Γ(a)∫0xta−1e−t dt(1)P(a, x) = \frac{\gamma(a, x)}{\Gamma(a)} = \frac{1}{\Gamma(a)} \int_0^x t^{a - 1} e^{-t}\, dt \tag{1}

When a≤0a \le 0 and x=0x = 0, two singularities interact in Equation (1): Γ(a)\Gamma(a) exhibits simple poles at non-positive integers (lim⁡z→−n∣Γ(z)∣=∞\lim_{z \to -n} |\Gamma(z)| = \infty), while the integrand ta−1t^{a-1} diverges as t→0+t \to 0^+. Consequently, P(a,0)P(a, 0) for a≤0a \le 0 is mathematically indeterminate and must evaluate to NaN\text{NaN}:

∀a≤0,  x=0  ⟹  P(a,x)=NaN(2)\forall a \le 0,\; x = 0 \implies P(a, x) = \text{NaN} \tag{2}
Domain RegionAnalytical ValueKernel Output (After)
a>0,x=0a > 0, x = 0000.0
a≤0,x=0a \le 0, x = 0IndeterminateNaN (matches SciPy)

3. Implementation & Verification

// In tensorflow/core/kernels/cwise_ops.h:
if (a <= T(0) && x == T(0)) return Eigen::numext::quiet_NaN<T>();

Regression tests confirm exact agreement with SciPy scipy.special.gammainc across float32 and float64.

4. References

  1. DLMF (NIST Digital Library of Mathematical Functions), Chapter 8: Incomplete Gamma and Related Functions, Eq. 8.2.4.
  2. IEEE Computer Society, IEEE Standard for Floating-Point Arithmetic (IEEE Std 754-2019), quiet NaN propagation rules.
View PR #124927 on GitHubRead technical story

The soul becomes dyed with the color of its thoughts.

Marcus Aurelius

Made with <3 and lots of coffee.