PR #123282

tensorflow/tensorflow · MLIR · Merged · 4 files changed

Removed the unsound TF_Involution trait from tf.Reciprocal in MLIR TableGen definitions so MLIR canonicalization never cancels nested 1/(1/x) pairs and silently alters IEEE 754 precision.

Checkout Pull Request

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

$gh pr checkout 123282 --repo tensorflow/tensorflow
Causal & File Impact Graph (PR #123282)MLIR / Merged
INPUT / TRIGGERMLIR Edge CaseROOT CAUSEMissing InvariantPATCHED (4 FILES)tf_generated_ops.tdtf_ops.tdcanonicalize.mlirtf_trait_folds.mlirOUTCOMEMerged

Removed the unsound TF_Involution trait from tf.Reciprocal in MLIR TableGen definitions so MLIR canonicalization never cancels nested 1/(1/x) pairs and silently alters IEEE 754 precision.

File-by-File Modification Breakdown (4)

  • tensorflow/compiler/mlir/tensorflow/ir/tf_generated_ops.td

    MLIR TableGen operator trait and canonicalization definition

    +0 / -24
  • tensorflow/compiler/mlir/tensorflow/ir/tf_ops.td

    MLIR TableGen operator trait and canonicalization definition

    +29 / -0
  • tensorflow/compiler/mlir/tensorflow/tests/canonicalize.mlir

    Python/XLA regression test suite verifying edge-case behavior

    +17 / -1
  • tensorflow/compiler/mlir/tensorflow/tests/tf_trait_folds.mlir

    Python/XLA regression test suite verifying edge-case behavior

    +21 / -3

Changed Files (4)

tensorflow/compiler/mlir/tensorflow/ir/tf_generated_ops.td30 lines
@@ -12716,30 +12716,6 @@ If `x` and `y` are reals, this will return the floating-point division.
   let hasFolder = 1;
 }
 
-def TF_ReciprocalOp : TF_Op<"Reciprocal", [Pure, TF_Involution, TF_SameOperandsAndResultTypeResolveRef]> {
-  let summary = "Computes the reciprocal of x element-wise.";
-
-  let description = [{
-I.e., \\(y = 1 / x\\).
-  }];
-
-  let arguments = (ins
-    TensorOf<[TF_Bfloat16, TF_Complex128, TF_Complex64, TF_Float16, TF_Float32, TF_Float64, TF_Int16, TF_Int32, TF_Int64, TF_Int8]>:$x
-  );
-
-  let results = (outs
-    TensorOf<[TF_Bfloat16, TF_Complex128, TF_Complex64, TF_Float16, TF_Float32, TF_Float64, TF_Int16, TF_Int32, TF_Int64, TF_Int8]>:$y
-  );
-
-  TF_DerivedOperandTypeAttr T = TF_DerivedOperandTypeAttr<0>;
-
-  let extraClassDeclaration = [{
-    static bool isCompatibleReturnTypes(TypeRange inferred, TypeRange actual) {
-      return ArraysAreCastCompatible(inferred, actual);
-    }
-  }];
-}
-
 def TF_ReciprocalGradOp : TF_Op<"ReciprocalGrad", [Pure, TF_SameOperandsAndResultTypeResolveRef]> {
   let summary = "Computes the gradient for the inverse of `x` wrt its input.";

1. Introduction & Motivation

Compiler canonicalizers rely on algebraic traits declared in MLIR TableGen (.td) files to simplify redundant operator chains [1]. Because tf.Reciprocal carried the TF_Involution trait, MLIR treated Reciprocal(Reciprocal(x))\text{Reciprocal}(\text{Reciprocal}(x)) as an exact identity and eliminated both ops, silently breaking numerical reproducibility on subnormals and rounding boundaries.

2. Finite-Precision Proof Against Involution

An operator ff is a true involution iff f(f(x))=xf(f(x)) = x for every xx in its domain. In IEEE 754 binary32 with machine epsilon ϵmach=2−24\epsilon_{\text{mach}} = 2^{-24}, each division incurs rounding fl⁡(u)=u(1+δ)\operatorname{fl}(u) = u(1 + \delta) with ∣δ∣≤ϵmach|\delta| \le \epsilon_{\text{mach}} [2]:

fl⁡ ⁣(1fl⁡(1/x))=11x(1+δ1)(1+δ2)≈x(1−δ1+δ2)≠x(1)\operatorname{fl}\!\left(\frac{1}{\operatorname{fl}(1/x)}\right) = \frac{1}{\frac{1}{x}(1 + \delta_1)}(1 + \delta_2) \approx x(1 - \delta_1 + \delta_2) \neq x \tag{1}

Furthermore, for any subnormal xx with ∣x∣<2−126|x| < 2^{-126} (such as x=10−40x = 10^{-40}), fl⁡(1/x)\operatorname{fl}(1/x) overflows FLT_MAX to +∞+\infty, yielding:

fl⁡ ⁣(1+∞)=0≠10−40(2)\operatorname{fl}\!\left(\frac{1}{+\infty}\right) = 0 \neq 10^{-40} \tag{2}
Input xx (float32)Exact 1/(1/x)Simplified x (Before)Unfolded (After)
10−4010^{-40} (subnormal)0.00.010−4010^{-40} (wrong)0.00.0 (exact)
Normal float±1\pm 1 ULP driftxxPreserves IEEE 754

3. References

  1. Lattner et al., MLIR: Scaling Compiler Infrastructure for Domain Specific Computation, CGO 2021.
  2. IEEE Computer Society, IEEE Std 754-2019, Binary Floating-Point Rounding & Subnormals.
View PR #123282 on GitHubRead technical story

The soul becomes dyed with the color of its thoughts.

Marcus Aurelius

Made with <3 and lots of coffee.