From f6c8cc0e99c48cd8ebe3c5a94fad07273c0e91ea Mon Sep 17 00:00:00 2001 From: Antonio Sanchez Date: Fri, 29 Oct 2021 22:03:44 +0000 Subject: [PATCH] Fix TensorReduction warnings and error bound for sum accuracy test. The sum accuracy test currently uses the default test precision for the given scalar type. However, scalars are generated via a normal distribution, and given a large enough count and strong enough random generator, the expected sum is zero. This causes the test to periodically fail. Here we estimate an upper-bound for the error as `sqrt(N) * prec` for summing N values, with each having an approximate epsilon of `prec`. Also fixed a few warnings generated by MSVC when compiling the reduction test. --- unsupported/Eigen/CXX11/src/Tensor/Tensor.h | 2 +- unsupported/Eigen/CXX11/src/Tensor/TensorReduction.h | 4 ++-- unsupported/test/cxx11_tensor_reduction.cpp | 6 +++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/unsupported/Eigen/CXX11/src/Tensor/Tensor.h b/unsupported/Eigen/CXX11/src/Tensor/Tensor.h index 76e97cd1a..26358d545 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/Tensor.h +++ b/unsupported/Eigen/CXX11/src/Tensor/Tensor.h @@ -76,7 +76,7 @@ class Tensor : public TensorBase0) & !(Options_&DontAlign), + IsAligned = (EIGEN_MAX_ALIGN_BYTES>0) && !(Options_&DontAlign), Layout = Options_ & RowMajor ? RowMajor : ColMajor, CoordAccess = true, RawAccess = true diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorReduction.h b/unsupported/Eigen/CXX11/src/Tensor/TensorReduction.h index b5922477a..2939b9862 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorReduction.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorReduction.h @@ -633,7 +633,7 @@ static const bool RunningOnGPU = false; m_fastOutputStrides[i] = internal::TensorIntDivisor(m_outputStrides[i]); } } else { - m_outputStrides[NumOutputDims - 1] = 1; + m_outputStrides[static_cast(NumOutputDims - 1)] = 1; for (int i = NumOutputDims - 2; i >= 0; --i) { m_outputStrides[i] = m_outputStrides[i + 1] * m_dimensions[i + 1]; m_fastOutputStrides[i] = internal::TensorIntDivisor(m_outputStrides[i]); @@ -680,7 +680,7 @@ static const bool RunningOnGPU = false; ? internal::array_prod(input_dims) : (static_cast(Layout) == static_cast(ColMajor)) ? m_preservedStrides[0] - : m_preservedStrides[NumOutputDims - 1]; + : m_preservedStrides[static_cast(NumOutputDims - 1)]; } EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; } diff --git a/unsupported/test/cxx11_tensor_reduction.cpp b/unsupported/test/cxx11_tensor_reduction.cpp index b7611d7b0..afcb79570 100644 --- a/unsupported/test/cxx11_tensor_reduction.cpp +++ b/unsupported/test/cxx11_tensor_reduction.cpp @@ -504,7 +504,11 @@ void test_sum_accuracy() { for (int i = 0; i < num_elements; ++i) { expected_sum += static_cast(tensor(i)); } - VERIFY_IS_APPROX(sum(), static_cast(expected_sum)); + // Scale tolerance to account for # elements. Otherwise, we periodically fail, since + // E[sum] == prescribed_mean == 0 for the first iteration. + double err = Eigen::numext::abs(static_cast(sum()) - expected_sum); + double tol = Eigen::numext::sqrt(num_elements) * static_cast(test_precision()) * numext::maxi(1.0, prescribed_mean); + VERIFY(err < tol); } }