PR #123195

tensorflow/tensorflow · Grappler · Merged · 2 files changed

Excluded Reciprocal from Grappler's IsInvolution optimization pass so adjacent 1/(1/x) nodes are preserved, protecting IEEE 754 rounding, subnormals, and overflow semantics.

Checkout Pull Request

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

$gh pr checkout 123195 --repo tensorflow/tensorflow
Causal & File Impact Graph (PR #123195)Grappler / Merged
INPUT / TRIGGERGrappler Edge CaseROOT CAUSEMissing InvariantPATCHED (2 FILES)op_types.ccarithmetic_optimiz...OUTCOMEMerged

Excluded Reciprocal from Grappler's IsInvolution optimization pass so adjacent 1/(1/x) nodes are preserved, protecting IEEE 754 rounding, subnormals, and overflow semantics.

File-by-File Modification Breakdown (2)

  • tensorflow/core/grappler/op_types.cc

    Grappler C++ graph optimizer pass and algebraic rewrite rules

    +6 / -1
  • tensorflow/core/grappler/optimizers/arithmetic_optimizer_test.cc

    C++ unit and regression test suite verifying the fix

    +45 / -14

Changed Files (2)

tensorflow/core/grappler/op_types.cc15 lines
@@ -797,9 +797,14 @@ OPDEF_PROPERTY_HELPER(Aggregate, aggregate)
 OPDEF_PROPERTY_HELPER(Commutative, commutative)
 
 bool IsInvolution(const NodeDef& node) {
+  // An involution f satisfies f(f(x)) == x exactly, which lets a pair of them
+  // be cancelled without changing the result. Reciprocal is intentionally
+  // excluded: in floating point 1 / (1 / x) does not round-trip to x because
+  // the first reciprocal is already rounded, so eliminating the pair would
+  // alter the numerical output.
   static const gtl::FlatSet<std::string>* const kInvolutionOps =
       CHECK_NOTNULL((new gtl::FlatSet<std::string>{
-          "Conj", "Reciprocal", "Invert", "Neg", "LogicalNot"}));
+          "Conj", "Invert", "Neg", "LogicalNot"}));
   return kInvolutionOps->count(node.op()) > 0;
 }

1. Introduction & Motivation

Grappler's ArithmeticOptimizer simplifies computation graphs by querying IsInvolution(node) and eliminating consecutive self-inverse operators f(f(x))→xf(f(x)) \to x [1]. While exact for bitwise or sign-flipping operations like Neg or LogicalNot, treating floating-point Reciprocal as an involution is mathematically unsound under IEEE 754 arithmetic [2].

Before: x -> Reciprocal -> Reciprocal -> y   ==>  rewritten to  x -> y  (UNSOUND)
After:  IsInvolution("Reciprocal") == false  ==>  preserves IEEE 754 rounding & overflow

2. ULP & Subnormal Collapse Proof

For normal float32 values, two rounded divisions can deviate by up to 11 ULP from xx. More severely, for subnormal x=10−40x = 10^{-40}, fl⁡(1/x)=+∞\operatorname{fl}(1/x) = +\infty because 1040>FLT_MAX≈3.4×103810^{40} > \text{FLT\_MAX} \approx 3.4 \times 10^{38}, so:

fl⁡ ⁣(1fl⁡(1/10−40))=fl⁡ ⁣(1+∞)=0≠10−40(1)\operatorname{fl}\!\left(\frac{1}{\operatorname{fl}(1 / 10^{-40})}\right) = \operatorname{fl}\!\left(\frac{1}{+\infty}\right) = 0 \neq 10^{-40} \tag{1}
OperatorExact Involution in binary32?In kInvolutionOps?
Neg, Conj, LogicalNot, InvertYes (exact bit/sign flip)Yes
ReciprocalNo (rounding & subnormal overflow)Excluded (Fixed)

3. Implementation & Verification

// In tensorflow/core/grappler/op_types.cc:
static const gtl::FlatSet<std::string>* const kInvolutionOps =
    CHECK_NOTNULL((new gtl::FlatSet<std::string>{
        "Conj", "Invert", "Neg", "LogicalNot"}));

Updated arithmetic_optimizer_test.cc (RemoveInvolutionAdjacentNodes) to verify that Neg pairs still fold while Reciprocal pairs remain intact.

4. References

  1. TensorFlow Grappler Graph Optimization Framework, tensorflow/core/grappler/optimizers/arithmetic_optimizer.cc.
  2. D. Goldberg, What Every Computer Scientist Should Know About Floating-Point Arithmetic, ACM Computing Surveys, 1991.
  3. Anandhu P Shaji, Hunting Compiler Bugs and Numerical Traps in TensorFlow, anandhupshaji.me/posts/tensorflow-stories.html#unsound-algebraic-rewrites.
View PR #123195 on GitHubRead technical story

The soul becomes dyed with the color of its thoughts.

Marcus Aurelius

Made with <3 and lots of coffee.