From d196dc949088fc27c5641497c9f477e2aee684c9 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 13 Sep 2024 14:38:36 +0200 Subject: [PATCH 1/2] Enable third party tests for dpnp.size --- .../third_party/cupy/core_tests/test_core.py | 118 ++++++++++++++++++ .../cupy/core_tests/test_ndarray.py | 10 +- 2 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 tests/third_party/cupy/core_tests/test_core.py diff --git a/tests/third_party/cupy/core_tests/test_core.py b/tests/third_party/cupy/core_tests/test_core.py new file mode 100644 index 000000000000..06a63580e4ae --- /dev/null +++ b/tests/third_party/cupy/core_tests/test_core.py @@ -0,0 +1,118 @@ +import unittest +import sys + +import numpy +import pytest + +import dpnp as cupy +from tests.third_party.cupy import testing + + +class TestSize(unittest.TestCase): + # def tearDown(self): + # # Free huge memory for slow test + # cupy.get_default_memory_pool().free_all_blocks() + + @testing.for_all_dtypes() + @testing.numpy_cupy_equal() + def test_size(self, xp, dtype): + a = xp.ndarray((2, 3), dtype=dtype) + return xp.size(a) + + @testing.for_all_dtypes() + @testing.numpy_cupy_equal() + def test_size_axis(self, xp, dtype): + a = xp.ndarray((2, 3), dtype=dtype) + return xp.size(a, axis=1) + + @testing.for_all_dtypes() + def test_size_axis_error(self, dtype): + for xp in (numpy, cupy): + a = xp.ndarray((2, 3), dtype=dtype) + with pytest.raises(IndexError): + return xp.size(a, axis=3) + + @testing.numpy_cupy_equal() + @testing.slow + def test_size_huge(self, xp): + a = xp.ndarray(2 ** 32, 'b') # 4 GiB + return xp.size(a) + + +_orders = { + order_arg: order_expect + for order_expect, order_args in [ + ('C', ['C', 'c', 'CONTIGUOUS', '', None]), + ('F', ['F', 'f', 'FORTRAN']), + ] + for order_arg in order_args +} + + +@pytest.mark.skip("no cupy._core submodule") +class TestOrder(unittest.TestCase): + @testing.for_orders(_orders.keys()) + def test_ndarray(self, order): + order_expect = _orders[order] + a = core.ndarray((2, 3), order=order) + expect_c = order_expect == 'C' + expect_f = order_expect == 'F' + assert a.flags.c_contiguous == expect_c + assert a.flags.f_contiguous == expect_f + + +@pytest.mark.skip("min_scalar_type() is not supported") +class TestMinScalarType: + def test_scalar(self): + for v in (-129, -128, 0, 1.2, numpy.inf): + assert cupy.min_scalar_type(v) is numpy.min_scalar_type(v) + + @testing.for_all_dtypes() + def test_numpy_scalar(self, dtype): + sc = dtype(1) + for v in (sc, [sc, sc]): + assert cupy.min_scalar_type(v) is numpy.min_scalar_type(v) + + @testing.for_all_dtypes() + def test_cupy_scalar(self, dtype): + sc = cupy.array(-1).astype(dtype) + for v in (sc, [sc, sc]): + assert cupy.min_scalar_type(v) is sc.dtype + + @testing.for_all_dtypes() + def test_numpy_ndarray(self, dtype): + arr = numpy.array([[-1, 1]]).astype(dtype) + for v in (arr, (arr, arr)): + assert cupy.min_scalar_type(v) is numpy.min_scalar_type(v) + + @testing.for_all_dtypes() + def test_cupy_ndarray(self, dtype): + arr = cupy.array([[-1, 1]]).astype(dtype) + for v in (arr, (arr, arr)): + assert cupy.min_scalar_type(v) is arr.dtype + + +@testing.parameterize(*testing.product({ + 'cxx': (None, '--std=c++11'), +})) +@pytest.mark.skip("compiling cupy headers are not supported") +class TestCuPyHeaders(unittest.TestCase): + def setUp(self): + self.temporary_cache_dir_context = test_raw.use_temporary_cache_dir() + self.cache_dir = self.temporary_cache_dir_context.__enter__() + self.header = '\n'.join(['#include <' + h + '>' + for h in core._cupy_header_list]) + + def tearDown(self): + self.temporary_cache_dir_context.__exit__(*sys.exc_info()) + + def test_compiling_core_header(self): + code = r''' + extern "C" __global__ void _test_ker_() { } + ''' + code = self.header + code + options = () if self.cxx is None else (self.cxx,) + ker = cupy.RawKernel(code, '_test_ker_', + options=options, backend='nvrtc') + ker((1,), (1,), ()) + cupy.cuda.Device().synchronize() diff --git a/tests/third_party/cupy/core_tests/test_ndarray.py b/tests/third_party/cupy/core_tests/test_ndarray.py index 09ade3b29074..975e75e12506 100644 --- a/tests/third_party/cupy/core_tests/test_ndarray.py +++ b/tests/third_party/cupy/core_tests/test_ndarray.py @@ -154,11 +154,18 @@ def test_unsupported_type(self): with pytest.raises(TypeError): cupy.array(arr) + @pytest.mark.skip("no ndim limit") + @testing.with_requires("numpy>=2.0") + @testing.numpy_cupy_array_equal() + def test_upper_limit_ndim(self, xp): + shape = [1 for i in range(64)] + return xp.zeros(shape, dtype=xp.int8) + @pytest.mark.skip("no ndim limit") def test_excessive_ndim(self): for xp in (numpy, cupy): with pytest.raises(ValueError): - xp.ndarray(shape=[1 for i in range(33)], dtype=xp.int8) + xp.ndarray(shape=[1 for i in range(65)], dtype=xp.int8) @testing.parameterize( @@ -588,7 +595,6 @@ def test_output_type_mismatch(self): wrap_take(a, i) -@pytest.mark.skip("size() is not supported") class TestSize(unittest.TestCase): @testing.numpy_cupy_equal() def test_size_without_axis(self, xp): From 03b7b77b15e87681a163a663e2785f302be975e1 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 13 Sep 2024 14:54:00 +0200 Subject: [PATCH 2/2] Applied pre-commit hooks --- .../third_party/cupy/core_tests/test_core.py | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/tests/third_party/cupy/core_tests/test_core.py b/tests/third_party/cupy/core_tests/test_core.py index 06a63580e4ae..42512322bac9 100644 --- a/tests/third_party/cupy/core_tests/test_core.py +++ b/tests/third_party/cupy/core_tests/test_core.py @@ -1,5 +1,5 @@ -import unittest import sys +import unittest import numpy import pytest @@ -35,15 +35,15 @@ def test_size_axis_error(self, dtype): @testing.numpy_cupy_equal() @testing.slow def test_size_huge(self, xp): - a = xp.ndarray(2 ** 32, 'b') # 4 GiB + a = xp.ndarray(2**32, "b") # 4 GiB return xp.size(a) _orders = { order_arg: order_expect for order_expect, order_args in [ - ('C', ['C', 'c', 'CONTIGUOUS', '', None]), - ('F', ['F', 'f', 'FORTRAN']), + ("C", ["C", "c", "CONTIGUOUS", "", None]), + ("F", ["F", "f", "FORTRAN"]), ] for order_arg in order_args } @@ -55,8 +55,8 @@ class TestOrder(unittest.TestCase): def test_ndarray(self, order): order_expect = _orders[order] a = core.ndarray((2, 3), order=order) - expect_c = order_expect == 'C' - expect_f = order_expect == 'F' + expect_c = order_expect == "C" + expect_f = order_expect == "F" assert a.flags.c_contiguous == expect_c assert a.flags.f_contiguous == expect_f @@ -92,27 +92,33 @@ def test_cupy_ndarray(self, dtype): assert cupy.min_scalar_type(v) is arr.dtype -@testing.parameterize(*testing.product({ - 'cxx': (None, '--std=c++11'), -})) +@testing.parameterize( + *testing.product( + { + "cxx": (None, "--std=c++11"), + } + ) +) @pytest.mark.skip("compiling cupy headers are not supported") class TestCuPyHeaders(unittest.TestCase): def setUp(self): self.temporary_cache_dir_context = test_raw.use_temporary_cache_dir() self.cache_dir = self.temporary_cache_dir_context.__enter__() - self.header = '\n'.join(['#include <' + h + '>' - for h in core._cupy_header_list]) + self.header = "\n".join( + ["#include <" + h + ">" for h in core._cupy_header_list] + ) def tearDown(self): self.temporary_cache_dir_context.__exit__(*sys.exc_info()) def test_compiling_core_header(self): - code = r''' + code = r""" extern "C" __global__ void _test_ker_() { } - ''' + """ code = self.header + code options = () if self.cxx is None else (self.cxx,) - ker = cupy.RawKernel(code, '_test_ker_', - options=options, backend='nvrtc') + ker = cupy.RawKernel( + code, "_test_ker_", options=options, backend="nvrtc" + ) ker((1,), (1,), ()) cupy.cuda.Device().synchronize()