Grappler & Optimizer

tensorflow/tensorflow · C++, Graph Rewrites

Excluded tf.Reciprocal from unsound involution simplification where 1/(1/x) -> x violated IEEE 754 float rules on subnormals and overflows, and corrected unsound ArgMax/ArgMin graph rewrites on saturated float32 boundaries.

Verify with Bazel

Run the upstream regression suite in tensorflow/tensorflow for this subsystem.

$bazel test //tensorflow/core/grappler/optimizers:arithmetic_optimizer_test

Changed Files (1)

tensorflow/core/grappler/op_types.cc12 lines
// PR #123195 — Exclude Reciprocal from Grappler involution simplification
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", "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.
Read technical story

The soul becomes dyed with the color of its thoughts.

Marcus Aurelius

Made with <3 and lots of coffee.