[XLA] Add ErrorSpec::fewer_infs_ok knob.

This allows us to accept actual results that overflow to inf when the correct
answer is a large, finite value.

PiperOrigin-RevId: 230810756
This commit is contained in:
Justin Lebar 2019-01-24 16:18:05 -08:00 committed by TensorFlower Gardener
parent b682bdd374
commit d058a78ac3
2 changed files with 27 additions and 0 deletions
tensorflow/compiler/xla

View File

@ -30,6 +30,19 @@ struct ErrorSpec {
// In effect, this allows the tested operation to produce incorrect results
// for inputs outside its mathematical domain.
bool relaxed_nans;
// If this is true, then we treat each +/-inf in the actual result as
// equivalent to our choice of either +/-inf or the min/max floating-point
// value.
//
// If the expected result is +/-inf, the actual result must still be +/-inf.
//
// In effect, this allows the tested operation to overflow, so long as it's
// overflowing on "large" values.
//
// (We could have a symmetric more_infs_ok flag if necessary; right now it
// appears not to be.)
bool fewer_infs_ok = false;
};
} // namespace xla

View File

@ -389,6 +389,20 @@ class NearComparator {
abs_error = 0;
rel_error = 0;
}
} else if (IsInf(actual) && !IsInf(expected) && error_.fewer_infs_ok) {
// `fewer_infs_ok` gives us the option of comparing as though `actual`
// were float_max/min rather than inf.
T actual_finite = actual > T{0} ? std::numeric_limits<T>::max()
: std::numeric_limits<T>::lowest();
abs_error = FpAbsoluteValue(actual_finite - expected);
// Avoid division by 0 even though it's well-defined because ubsan can be
// configured to treat this as a fatal error.
if (expected != T{0}) {
rel_error = abs_error / FpAbsoluteValue(expected);
} else {
rel_error = std::numeric_limits<float>::infinity();
}
} else if (IsInf(expected) || IsInf(actual)) {
// If either the expected or actual value is infinity but not both,
// then both absolute and relative error are regarded as inifity.