From 336d77ea19be48efad6025f824a58f89a87ce097 Mon Sep 17 00:00:00 2001 From: Justin Lebar Date: Fri, 25 May 2018 20:23:31 -0700 Subject: [PATCH] [XLA] Don't compute relative error when the expected value is 0. 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 | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tensorflow/compiler/xla/literal_comparison.cc b/tensorflow/compiler/xla/literal_comparison.cc index bf9679c..a588f4a 100644 --- a/tensorflow/compiler/xla/literal_comparison.cc +++ b/tensorflow/compiler/xla/literal_comparison.cc @@ -317,7 +317,15 @@ class NearComparator { rel_error = std::numeric_limits::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; -- 2.7.4