Skip to content

Commit b6b336d

Browse files
Review comments & cupy tests
1 parent 74ab8f3 commit b6b336d

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

dpnp/tests/helper.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ def generate_random_numpy_array(
185185
return a
186186

187187

188+
def factor_to_tol(dtype, factor):
189+
"""
190+
Calculate the tolerance for comparing floating point and complex arrays.
191+
The tolerance is based on the maximum resolution of the input dtype multiplied by the factor.
192+
"""
193+
194+
tol = 0
195+
if numpy.issubdtype(dtype, numpy.inexact):
196+
tol = numpy.finfo(dtype).resolution
197+
198+
return factor * tol
199+
200+
188201
def get_abs_array(data, dtype=None):
189202
if numpy.issubdtype(dtype, numpy.unsignedinteger):
190203
data = numpy.abs(data)

dpnp/tests/test_statistics.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from .helper import (
1515
assert_dtype_allclose,
16+
factor_to_tol,
1617
generate_random_numpy_array,
1718
get_all_dtypes,
1819
get_complex_dtypes,
@@ -210,30 +211,30 @@ def test_convolve_random(self, a_size, v_size, mode, dtype, method):
210211

211212
result = result.astype(rdtype)
212213

213-
rtol = 1e-3
214-
atol = 1e-10
215-
216-
if rdtype == dpnp.float64 or rdtype == dpnp.complex128:
217-
rtol = 1e-6
218-
atol = 1e-12
219-
elif rdtype == dpnp.bool:
214+
if rdtype == dpnp.bool:
220215
result = result.astype(dpnp.int32)
221216
rdtype = result.dtype
222217

223218
expected = expected.astype(rdtype)
224219

225-
diff = numpy.abs(result.asnumpy() - expected)
226-
invalid = diff > atol + rtol * numpy.abs(expected)
220+
factor = 1000
221+
rtol = atol = factor_to_tol(rdtype, factor)
222+
invalid = numpy.logical_not(
223+
numpy.isclose(
224+
result.asnumpy(), expected, rtol=rtol, atol=atol
225+
)
226+
)
227227

228228
# When using the 'fft' method, we might encounter outliers.
229229
# This usually happens when the resulting array contains values close to zero.
230230
# For these outliers, the relative error can be significant.
231231
# We can tolerate a few such outliers.
232-
max_outliers = 8 if expected.size > 1 else 0
232+
# max_outliers = 10 if expected.size > 1 else 0
233+
max_outliers = 10
233234
if invalid.sum() > max_outliers:
234235
# we already failed check,
235236
# call assert_dtype_allclose just to report error nicely
236-
assert_dtype_allclose(result, expected, factor=1000)
237+
assert_dtype_allclose(result, expected, factor=factor)
237238

238239
def test_convolve_mode_error(self):
239240
a = dpnp.arange(5)
@@ -446,30 +447,30 @@ def test_correlate_random(self, a_size, v_size, mode, dtype, method):
446447

447448
result = result.astype(rdtype)
448449

449-
rtol = 1e-3
450-
atol = 1e-3
451-
452-
if rdtype == dpnp.float64 or rdtype == dpnp.complex128:
453-
rtol = 1e-6
454-
atol = 1e-6
455-
elif rdtype == dpnp.bool:
450+
if rdtype == dpnp.bool:
456451
result = result.astype(dpnp.int32)
457452
rdtype = result.dtype
458453

459454
expected = expected.astype(rdtype)
460455

461-
diff = numpy.abs(result.asnumpy() - expected)
462-
invalid = diff > atol + rtol * numpy.abs(expected)
456+
factor = 1000
457+
rtol = atol = factor_to_tol(rdtype, factor)
458+
invalid = numpy.logical_not(
459+
numpy.isclose(
460+
result.asnumpy(), expected, rtol=rtol, atol=atol
461+
)
462+
)
463463

464464
# When using the 'fft' method, we might encounter outliers.
465465
# This usually happens when the resulting array contains values close to zero.
466466
# For these outliers, the relative error can be significant.
467467
# We can tolerate a few such outliers.
468-
max_outliers = 10 if expected.size > 1 else 0
468+
# max_outliers = 10 if expected.size > 1 else 0
469+
max_outliers = 10
469470
if invalid.sum() > max_outliers:
470471
# we already failed check,
471472
# call assert_dtype_allclose just to report error nicely
472-
assert_dtype_allclose(result, expected, factor=1000)
473+
assert_dtype_allclose(result, expected, factor=factor)
473474

474475
def test_correlate_mode_error(self):
475476
a = dpnp.arange(5)

dpnp/tests/third_party/cupy/math_tests/test_misc.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ def test_heaviside_nan_inf(self, xp, dtype_1, dtype_2):
531531
}
532532
)
533533
)
534-
@pytest.mark.skip("convolve() is not implemented yet")
534+
# @pytest.mark.skip("convolve() is not implemented yet")
535535
class TestConvolveShapeCombination:
536536

537537
@testing.for_all_dtypes(no_float16=True)
@@ -542,40 +542,39 @@ def test_convolve(self, xp, dtype):
542542
return xp.convolve(a, b, mode=self.mode)
543543

544544

545-
@pytest.mark.skip("convolve() is not implemented yet")
545+
# @pytest.mark.skip("convolve() is not implemented yet")
546546
@pytest.mark.parametrize("mode", ["valid", "same", "full"])
547547
class TestConvolve:
548548

549549
@testing.for_all_dtypes(no_float16=True)
550-
@testing.numpy_cupy_allclose(rtol=1e-6)
550+
@testing.numpy_cupy_allclose(rtol=1e-6, type_check=has_support_aspect64())
551551
def test_convolve_non_contiguous(self, xp, dtype, mode):
552552
a = testing.shaped_arange((300,), xp, dtype)
553553
b = testing.shaped_arange((100,), xp, dtype)
554554
return xp.convolve(a[::200], b[10::70], mode=mode)
555555

556556
@testing.for_all_dtypes(no_float16=True)
557-
@testing.numpy_cupy_allclose(rtol=5e-4)
557+
@testing.numpy_cupy_allclose(rtol=5e-4, type_check=has_support_aspect64())
558558
def test_convolve_large_non_contiguous(self, xp, dtype, mode):
559559
a = testing.shaped_arange((10000,), xp, dtype)
560560
b = testing.shaped_arange((100,), xp, dtype)
561561
return xp.convolve(a[200::], b[10::70], mode=mode)
562562

563563
@testing.for_all_dtypes_combination(names=["dtype1", "dtype2"])
564-
@testing.numpy_cupy_allclose(rtol=1e-2)
564+
@testing.numpy_cupy_allclose(rtol=1e-2, type_check=has_support_aspect64())
565565
def test_convolve_diff_types(self, xp, dtype1, dtype2, mode):
566566
a = testing.shaped_random((200,), xp, dtype1)
567567
b = testing.shaped_random((100,), xp, dtype2)
568568
return xp.convolve(a, b, mode=mode)
569569

570570

571-
@pytest.mark.skip("convolve() is not implemented yet")
572571
@testing.parameterize(*testing.product({"mode": ["valid", "same", "full"]}))
573572
class TestConvolveInvalid:
574573

575574
@testing.for_all_dtypes()
576575
def test_convolve_empty(self, dtype):
577576
for xp in (numpy, cupy):
578-
a = xp.zeros((0,), dtype)
577+
a = xp.zeros((0,), dtype=dtype)
579578
with pytest.raises(ValueError):
580579
xp.convolve(a, a, mode=self.mode)
581580

0 commit comments

Comments
 (0)