[XLA] Don't compute relative error when the expected value is 0.
authorJustin Lebar <jlebar@google.com>
Sat, 26 May 2018 03:23:31 +0000 (20:23 -0700)
committerTensorFlower Gardener <gardener@tensorflow.org>
Sat, 26 May 2018 03:26:13 +0000 (20:26 -0700)
In literal_comparison, don't try to compute a relative error when the
expected value is 0, because doing so would mean that the only
acceptable value *is* zero, which probably isn't what you mean.

PiperOrigin-RevId: 198137414

tensorflow/compiler/xla/literal_comparison.cc

index bf9679c..a588f4a 100644 (file)
@@ -317,7 +317,15 @@ class NearComparator {
       rel_error = std::numeric_limits<float>::infinity();
     } else {
       abs_error = FpAbsoluteValue(actual - expected);
-      rel_error = abs_error / FpAbsoluteValue(expected);
+      // If the expected result is exactly zero, don't compute relative error;
+      // that's meaningless.
+      //
+      // TODO(b/80321728): Come up with a better way to handle this case.
+      if (expected == NativeT{}) {
+        rel_error = 0;
+      } else {
+        rel_error = abs_error / FpAbsoluteValue(expected);
+      }
     }
     const bool is_abs_mismatch = abs_error > error_.abs;
     const bool is_rel_mismatch = rel_error > error_.rel;