[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:
parent
b682bdd374
commit
d058a78ac3
@ -30,6 +30,19 @@ struct ErrorSpec {
|
|||||||
// In effect, this allows the tested operation to produce incorrect results
|
// In effect, this allows the tested operation to produce incorrect results
|
||||||
// for inputs outside its mathematical domain.
|
// for inputs outside its mathematical domain.
|
||||||
bool relaxed_nans;
|
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
|
} // namespace xla
|
||||||
|
@ -389,6 +389,20 @@ class NearComparator {
|
|||||||
abs_error = 0;
|
abs_error = 0;
|
||||||
rel_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)) {
|
} else if (IsInf(expected) || IsInf(actual)) {
|
||||||
// If either the expected or actual value is infinity but not both,
|
// If either the expected or actual value is infinity but not both,
|
||||||
// then both absolute and relative error are regarded as inifity.
|
// then both absolute and relative error are regarded as inifity.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user