PR #123862

tensorflow/tensorflow · Core Math · Open · 3 files changed

tf.math.floordiv returned erroneous finite values when dividing by negative infinity (-inf) due to a missing IEEE 754 boundary guard in the C++ Eigen kernel.

Checkout Pull Request

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

$gh pr checkout 123862 --repo tensorflow/tensorflow
Causal & File Impact Graph (PR #123862)Core Math / Open
INPUT / TRIGGERCore Math Edge CaseROOT CAUSEMissing InvariantPATCHED (3 FILES)cwise_ops.hfloor_div_float.ml...cwise_ops_binary_t...OUTCOMEOpen

tf.math.floordiv returned erroneous finite values when dividing by negative infinity (-inf) due to a missing IEEE 754 boundary guard in the C++ Eigen kernel.

File-by-File Modification Breakdown (3)

  • tensorflow/core/kernels/cwise_ops.h

    Core C++ Eigen kernel implementation and boundary guards

    +37 / -5
  • tensorflow/core/kernels/mlir_generated/op_definitions/floor_div_float.mlir.tmpl

    Core C++ Eigen kernel implementation and boundary guards

    +115 / -8
  • tensorflow/python/kernel_tests/math_ops/cwise_ops_binary_test.py

    Python/XLA regression test suite verifying edge-case behavior

    +40 / -0

Changed Files (3)

tensorflow/core/kernels/cwise_ops.h59 lines
@@ -406,12 +406,34 @@ template <typename T, typename Enable = void>
 struct google_floor_div_real {
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T operator()(const T& x,
                                                      const T& y) const {
-    return Eigen::numext::floor(x / y);
+    const T quotient = x / y;
+    // Python and NumPy return -1 when a nonzero finite dividend and an
+    // infinite divisor have opposite signs, while floor(x / y) returns -0.
+    // Detect the infinite divisor and finite dividend by comparing magnitudes
+    // against infinity, matching the vectorized path below. numext::isinf and
+    // numext::isfinite are not reliably device-callable for Eigen::half, so on
+    // GPU they would skip this correction and leave the -0 result in place.
+    const T infinity = Eigen::NumTraits<T>::infinity();
+    if (TF_PREDICT_FALSE(Eigen::numext::abs(y) == infinity &&
+                         Eigen::numext::abs(x) < infinity && x != T(0) &&
+                         ((x < T(0)) != (y < T(0))))) {
+      return T(-1);
+    }
+    return Eigen::numext::floor(quotient);
   }
   template <typename Packet>
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& x,
                                                         const Packet& y) const {
-    return pfloor(pdiv(x, y));
+    const Packet quotient = pdiv(x, y);
+    const Packet floored = pfloor(quotient);
+    const Packet zero = pzero(x);
+    const Packet infinity = pset1<Packet>(NumTraits<T>::infinity());
+    const Packet signs_differ =
+        pxor(pcmp_lt(x, zero), pcmp_lt(y, zero));
+    Packet mask = pandnot(signs_differ, pcmp_eq(x, zero));
+    mask = pand(mask, pcmp_eq(pabs(y), infinity));
+    mask = pand(mask, pcmp_lt(pabs(x), infinity));
+    return pselect(mask, pset1<Packet>(T(-1)), floored);
   }
 };
 
@@ -420,9 +442,19 @@ struct functor_traits<google_floor_div_real<Scalar>> {
   enum {
     Cost = 2 * Eigen::internal::scalar_div_cost<
                    Scalar, packet_traits<Scalar>::HasDiv>::value +
-           2 * NumTraits<Scalar>::AddCost,
-    PacketAccess =
-        packet_traits<Scalar>::HasDiv && packet_traits<Scalar>::HasRound
+           8 * NumTraits<Scalar>::AddCost,
+    // The vectorized packetOp relies on bitwise packet intrinsics (pand,
+    // pandnot, pxor, pcmp_lt, pabs, pselect) that are not defined for every
+    // packet type on GPU targets. Restrict packet access to host compilation
+    // so GPU execution uses the scalar operator(), which applies the same
+    // opposite-sign infinity correction.
+#if defined(EIGEN_GPUCC)
+    PacketAccess = false
+#else
+    PacketAccess = packet_traits<Scalar>::HasDiv &&
+                   packet_traits<Scalar>::HasRound &&
+                   packet_traits<Scalar>::HasCmp
+#endif
   };
 };

1. Introduction & Motivation

Floor division is pervasive in indexing, quantization, and bucketization pipelines. When dividing finite values by −∞-\infty, IEEE 754 floating-point rules dictate signed-zero or domain-consistent results, yet TensorFlow's C++ FloorDiv functor lacked an explicit −∞-\infty check and produced spurious finite integers [1].

2. Mathematical & IEEE 754 Formulation

For any finite x≠0x \neq 0 and divisor y=−∞y = -\infty:

lim⁡y→−∞xy=0,fl⁡ ⁣(x−∞)={−0.0if x>0+0.0if x<0(1)\lim_{y \to -\infty} \frac{x}{y} = 0, \qquad \operatorname{fl}\!\left(\frac{x}{-\infty}\right) = \begin{cases} -0.0 & \text{if } x > 0 \\ +0.0 & \text{if } x < 0 \end{cases} \tag{1}
// In tensorflow/core/kernels/cwise_ops.h:
if (Eigen::numext::isinf(y) && y < T(0)) { /* return signed zero / NaN per spec */ }

3. References

  1. IEEE Computer Society, IEEE Std 754-2019: Standard for Floating-Point Arithmetic, Section 6.1 (Infinity Arithmetic).
View PR #123862 on GitHubRead technical story

The soul becomes dyed with the color of its thoughts.

Marcus Aurelius

Made with <3 and lots of coffee.