Fix gpu special function tests.

Some checks used incorrect values, partly from copy-paste errors,
partly from the change in behaviour introduced in !398.

Modified results to match scipy, simplified tests by updating
`VERIFY_IS_CWISE_APPROX` to work for scalars.
This commit is contained in:
Antonio Sanchez 2021-10-01 10:20:50 -07:00
parent f0f1d7938b
commit 701f5d1c91
2 changed files with 32 additions and 30 deletions

View File

@ -370,8 +370,8 @@ inline void verify_impl(bool condition, const char *testname, const char *file,
#define VERIFY_IS_NOT_MUCH_SMALLER_THAN(a, b) VERIFY(!test_isMuchSmallerThan(a, b))
#define VERIFY_IS_APPROX_OR_LESS_THAN(a, b) VERIFY(test_isApproxOrLessThan(a, b))
#define VERIFY_IS_NOT_APPROX_OR_LESS_THAN(a, b) VERIFY(!test_isApproxOrLessThan(a, b))
#define VERIFY_IS_CWISE_EQUAL(a, b) VERIFY(test_isCwiseApprox(a, b, true))
#define VERIFY_IS_CWISE_APPROX(a, b) VERIFY(test_isCwiseApprox(a, b, false))
#define VERIFY_IS_CWISE_EQUAL(a, b) VERIFY(verifyIsCwiseApprox(a, b, true))
#define VERIFY_IS_CWISE_APPROX(a, b) VERIFY(verifyIsCwiseApprox(a, b, false))
#define VERIFY_IS_UNITARY(a) VERIFY(test_isUnitary(a))
@ -419,6 +419,9 @@ template<> inline long double test_precision<std::complex<long double> >() { ret
#define EIGEN_TEST_SCALAR_TEST_OVERLOAD(TYPE) \
inline bool test_isApprox(TYPE a, TYPE b) \
{ return internal::isApprox(a, b, test_precision<TYPE>()); } \
inline bool test_isCwiseApprox(TYPE a, TYPE b, bool exact) \
{ return a == b || ((numext::isnan)(a) && (numext::isnan)(b)) || \
(!exact && internal::isApprox(a, b, test_precision<TYPE>())); } \
inline bool test_isMuchSmallerThan(TYPE a, TYPE b) \
{ return internal::isMuchSmallerThan(a, b, test_precision<TYPE>()); } \
inline bool test_isApproxOrLessThan(TYPE a, TYPE b) \
@ -588,6 +591,22 @@ inline bool verifyIsApprox(const Type1& a, const Type2& b)
return ret;
}
// verifyIsCwiseApprox is a wrapper to test_isCwiseApprox that outputs the relative difference magnitude if the test fails.
template<typename Type1, typename Type2>
inline bool verifyIsCwiseApprox(const Type1& a, const Type2& b, bool exact)
{
bool ret = test_isCwiseApprox(a,b,exact);
if(!ret) {
if (exact) {
std::cerr << "Values are not an exact match";
} else {
std::cerr << "Difference too large wrt tolerance " << get_test_precision(a);
}
std::cerr << ", relative error is: " << test_relative_error(a,b) << std::endl;
}
return ret;
}
// The idea behind this function is to compare the two scalars a and b where
// the scalar ref is a hint about the expected order of magnitude of a and b.
// WARNING: the scalar a and b must be positive

View File

@ -681,8 +681,8 @@ void test_gpu_digamma()
expected_out(2) = Scalar(1.2561176684318);
expected_out(3) = Scalar(2.398239129535781);
expected_out(4) = Scalar(9.210340372392849);
expected_out(5) = std::numeric_limits<Scalar>::infinity();
expected_out(6) = std::numeric_limits<Scalar>::infinity();
expected_out(5) = std::numeric_limits<Scalar>::quiet_NaN();
expected_out(6) = std::numeric_limits<Scalar>::quiet_NaN();
std::size_t bytes = in.size() * sizeof(Scalar);
@ -704,11 +704,8 @@ void test_gpu_digamma()
assert(gpuMemcpyAsync(out.data(), d_out, bytes, gpuMemcpyDeviceToHost, gpu_device.stream()) == gpuSuccess);
assert(gpuStreamSynchronize(gpu_device.stream()) == gpuSuccess);
for (int i = 0; i < 5; ++i) {
VERIFY_IS_APPROX(out(i), expected_out(i));
}
for (int i = 5; i < 7; ++i) {
VERIFY_IS_EQUAL(out(i), expected_out(i));
for (int i = 0; i < 7; ++i) {
VERIFY_IS_CWISE_APPROX(out(i), expected_out(i));
}
gpuFree(d_in);
@ -741,7 +738,7 @@ void test_gpu_zeta()
expected_out(0) = std::numeric_limits<Scalar>::infinity();
expected_out(1) = Scalar(1.61237534869);
expected_out(2) = Scalar(0.234848505667);
expected_out(3) = Scalar(1.03086757337e-5);
expected_out(3) = std::numeric_limits<Scalar>::quiet_NaN();
expected_out(4) = Scalar(0.367879440865);
expected_out(5) = Scalar(0.054102025820864097);
@ -769,13 +766,8 @@ void test_gpu_zeta()
assert(gpuMemcpyAsync(out.data(), d_out, bytes, gpuMemcpyDeviceToHost, gpu_device.stream()) == gpuSuccess);
assert(gpuStreamSynchronize(gpu_device.stream()) == gpuSuccess);
VERIFY_IS_EQUAL(out(0), expected_out(0));
VERIFY((std::isnan)(out(3)));
for (int i = 1; i < 6; ++i) {
if (i != 3) {
VERIFY_IS_APPROX(out(i), expected_out(i));
}
for (int i = 0; i < 6; ++i) {
VERIFY_IS_CWISE_APPROX(out(i), expected_out(i));
}
gpuFree(d_in_x);
@ -1117,13 +1109,8 @@ void test_gpu_ndtri()
assert(gpuMemcpyAsync(out.data(), d_out, bytes, gpuMemcpyDeviceToHost, gpu_device.stream()) == gpuSuccess);
assert(gpuStreamSynchronize(gpu_device.stream()) == gpuSuccess);
VERIFY_IS_EQUAL(out(0), expected_out(0));
VERIFY((std::isnan)(out(3)));
for (int i = 1; i < 6; ++i) {
if (i != 3) {
VERIFY_IS_APPROX(out(i), expected_out(i));
}
for (int i = 0; i < 6; ++i) {
VERIFY_IS_CWISE_APPROX(out(i), expected_out(i));
}
gpuFree(d_in_x);
@ -1262,12 +1249,8 @@ void test_gpu_betainc()
assert(gpuMemcpyAsync(out.data(), d_out, bytes, gpuMemcpyDeviceToHost, gpu_device.stream()) == gpuSuccess);
assert(gpuStreamSynchronize(gpu_device.stream()) == gpuSuccess);
for (int i = 1; i < 125; ++i) {
if ((std::isnan)(expected_out(i))) {
VERIFY((std::isnan)(out(i)));
} else {
VERIFY_IS_APPROX(out(i), expected_out(i));
}
for (int i = 0; i < 125; ++i) {
VERIFY_IS_CWISE_APPROX(out(i), expected_out(i));
}
gpuFree(d_in_x);