mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Make NaN propagatation consistent between the pmax/pmin and std::max/std::min. This makes the NaN propagation consistent between the scalar and vectorized code paths of Eigen's scalar_max_op and scalar_min_op.
See #1373 for details.
This commit is contained in:
@@ -300,6 +300,51 @@ static void test_select()
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Scalar>
|
||||
void test_minmax_nan_propagation_templ() {
|
||||
for (int size = 1; size < 17; ++size) {
|
||||
const Scalar kNan = std::numeric_limits<Scalar>::quiet_NaN();
|
||||
Tensor<Scalar, 1> vec_nan(size);
|
||||
Tensor<Scalar, 1> vec_zero(size);
|
||||
Tensor<Scalar, 1> vec_res(size);
|
||||
vec_nan.setConstant(kNan);
|
||||
vec_zero.setZero();
|
||||
vec_res.setZero();
|
||||
|
||||
// Test that we propagate NaNs in the tensor when applying the
|
||||
// cwiseMax(scalar) operator, which is used for the Relu operator.
|
||||
vec_res = vec_nan.cwiseMax(Scalar(0));
|
||||
for (int i = 0; i < size; ++i) {
|
||||
VERIFY((numext::isnan)(vec_res(i)));
|
||||
}
|
||||
|
||||
// Test that NaNs do not propagate if we reverse the arguments.
|
||||
vec_res = vec_zero.cwiseMax(kNan);
|
||||
for (int i = 0; i < size; ++i) {
|
||||
VERIFY_IS_EQUAL(vec_res(i), Scalar(0));
|
||||
}
|
||||
|
||||
// Test that we propagate NaNs in the tensor when applying the
|
||||
// cwiseMin(scalar) operator.
|
||||
vec_res.setZero();
|
||||
vec_res = vec_nan.cwiseMin(Scalar(0));
|
||||
for (int i = 0; i < size; ++i) {
|
||||
VERIFY((numext::isnan)(vec_res(i)));
|
||||
}
|
||||
|
||||
// Test that NaNs do not propagate if we reverse the arguments.
|
||||
vec_res = vec_zero.cwiseMin(kNan);
|
||||
for (int i = 0; i < size; ++i) {
|
||||
VERIFY_IS_EQUAL(vec_res(i), Scalar(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void test_minmax_nan_propagation()
|
||||
{
|
||||
test_minmax_nan_propagation_templ<float>();
|
||||
test_minmax_nan_propagation_templ<double>();
|
||||
}
|
||||
|
||||
void test_cxx11_tensor_expr()
|
||||
{
|
||||
@@ -311,4 +356,5 @@ void test_cxx11_tensor_expr()
|
||||
CALL_SUBTEST(test_functors());
|
||||
CALL_SUBTEST(test_type_casting());
|
||||
CALL_SUBTEST(test_select());
|
||||
CALL_SUBTEST(test_minmax_nan_propagation());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user