PR #122826

tensorflow/tensorflow · Grappler · Merged · 4 files changed

Corrected Grappler ArgMax/ArgMin graph rewrites on saturated float32 boundaries where non-strict monotonicity altered winning indices.

Checkout Pull Request

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

$gh pr checkout 122826 --repo tensorflow/tensorflow
Causal & File Impact Graph (PR #122826)Grappler / Merged
INPUT / TRIGGERGrappler Edge CaseROOT CAUSEMissing InvariantPATCHED (4 FILES)op_types.ccop_types.harithmetic_optimiz...arithmetic_optimiz...OUTCOMEMerged

Corrected Grappler ArgMax/ArgMin graph rewrites on saturated float32 boundaries where non-strict monotonicity altered winning indices.

File-by-File Modification Breakdown (4)

  • tensorflow/core/grappler/op_types.cc

    Grappler C++ graph optimizer pass and algebraic rewrite rules

    +36 / -0
  • tensorflow/core/grappler/op_types.h

    Grappler C++ graph optimizer pass and algebraic rewrite rules

    +1 / -0
  • tensorflow/core/grappler/optimizers/arithmetic_optimizer.cc

    Grappler C++ graph optimizer pass and algebraic rewrite rules

    +10 / -0
  • tensorflow/core/grappler/optimizers/arithmetic_optimizer_test.cc

    C++ unit and regression test suite verifying the fix

    +216 / -0

Changed Files (4)

tensorflow/core/grappler/op_types.cc43 lines
@@ -251,6 +251,42 @@ bool IsElementWiseMonotonic(const NodeDef& node, bool* is_non_decreasing) {
   return false;
 }
 
+// Returns true if node represents a monotonic unary elementwise function that
+// maps distinct floating-point inputs to distinct outputs everywhere except
+// for isolated rounding-induced ties, i.e. has no saturation plateau or
+// overflow that collapses an entire input interval to a single value. This is
+// a strictly stronger property than monotonicity: ops like Tanh or Relu are
+// monotonic but collapse whole input ranges to one constant (tanh rounds to
+// exactly 1.0 in float32 for inputs >= ~9; relu maps every negative input to
+// 0.0), so hoisting them out of index-producing reductions such as
+// ArgMax/ArgMin can change the resulting indices. Monotonic ops omitted here
+// and the float32 property that disqualifies them:
+//
+//   Tanh, Sigmoid, Erf:  saturate to a finite constant for large |x|.
+//   Exp, Expm1, Sinh:    overflow to +/-inf for |x| above ~88.
+//   Elu, Selu:           saturate toward a negative constant for large
+//                        negative inputs as exp(x) underflows to 0.
+//   Relu:                maps every negative input to 0.0.
+//   Relu6:               clamps all inputs above 6 to exactly 6.0.
+//   Softplus:            underflows to 0.0 for large negative inputs.
+//   Softsign:            x / (1 + |x|) rounds to exactly +/-1.0 once
+//                        1 + |x| == |x| in float32.
+//   Atan:                rounds to exactly +/-pi/2 for large |x|.
+//   Acos:                acos(x) = pi/2 - x + O(x^3) near zero, so every
+//                        |x| small relative to an ulp of pi/2 (~1e-8,
+//                        including all denormals) rounds to exactly acos(0).
+//   Erfc:                underflows to exactly 0.0 for x above ~10.5.
+//   Floor, Ceil, Rint:   many-to-one by definition.
+//   Sign:                three-valued.
+//   Atanh:               produces NaN outside the open interval (-1, 1).
+bool IsElementWiseStrictlyInjective(const NodeDef& node) {
+  static const gtl::FlatSet<std::string>* const kStrictlyInjectiveOps =
+      CHECK_NOTNULL((new gtl::FlatSet<std::string>{
+          "Acosh", "Asin", "Asinh", "Log", "Log1p", "Neg", "Rsqrt", "Sqrt",
+      }));
+  return kStrictlyInjectiveOps->count(node.op()) > 0;
+}
+
 bool IsElu(const NodeDef& node) { return node.op() == "Elu"; }
 
 bool IsEluGrad(const NodeDef& node) { return node.op() == "EluGrad"; }

1. Introduction & Motivation

Grappler simplifies ArgMax(f(x)) to ArgMax(x) when ff is an order-preserving transformation, saving compute on large logits tensors. However, near float32 saturation limits, finite-precision clamping collapses distinct inputs onto the same boundary value and flips tie-breaking outcomes [1].

2. Strict Monotonicity Condition

An argmax-preserving rewrite arg⁡max⁡if(xi)=arg⁡max⁡ixi\arg\max_i f(x_i) = \arg\max_i x_i is sound if and only if ff is strictly increasing over the entire representable float32 domain:

xa<xb  ⟹  fl⁡(f(xa))<fl⁡(f(xb))(1)x_a < x_b \implies \operatorname{fl}(f(x_a)) < \operatorname{fl}(f(x_b)) \tag{1}

When saturation causes fl⁡(f(xa))=fl⁡(f(xb))=FLT_MAX\operatorname{fl}(f(x_a)) = \operatorname{fl}(f(x_b)) = \text{FLT\_MAX}, Equation (1) fails and the rewrite returns a different winning index than the unoptimized graph.

3. References

  1. D. Goldberg, What Every Computer Scientist Should Know About Floating-Point Arithmetic, ACM Computing Surveys, 1991.
View PR #122826 on GitHubRead technical story

The soul becomes dyed with the color of its thoughts.

Marcus Aurelius

Made with <3 and lots of coffee.