diff options
author | Rasmus Munk Larsen <rmlarsen@google.com> | 2017-01-24 13:32:50 -0800 |
---|---|---|
committer | Rasmus Munk Larsen <rmlarsen@google.com> | 2017-01-24 13:32:50 -0800 |
commit | 5e144bbaa454eb2af7f6751376051fe16d143276 (patch) | |
tree | c29d354739cd64a0ad260cb177b33aedeee31e58 /unsupported/test/cxx11_tensor_expr.cpp | |
parent | 156e6234f1921987ab63321dbea885b75e6ae70b (diff) |
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.
Diffstat (limited to 'unsupported/test/cxx11_tensor_expr.cpp')
-rw-r--r-- | unsupported/test/cxx11_tensor_expr.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/unsupported/test/cxx11_tensor_expr.cpp b/unsupported/test/cxx11_tensor_expr.cpp index 77e24cb67..129b4e659 100644 --- a/unsupported/test/cxx11_tensor_expr.cpp +++ b/unsupported/test/cxx11_tensor_expr.cpp @@ -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()); } |