Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions dpnp/tests/third_party/cupy/core_tests/test_cub_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,22 @@ def _test_can_use(self, i_shape, o_shape, r_axis, o_axis, order, expected):
assert result is expected


@pytest.mark.parametrize("shape", [(2,), (2, 3), (2, 3, 4), (2, 3, 4, 5)])
@pytest.mark.parametrize("order", ["C", "F"])
_MIN_SIZE = cupy._core._cub_reduction._CUB_REDUCE_SIZE_THRESHOLD


@pytest.mark.parametrize(
"shape",
[
(_MIN_SIZE,),
(_MIN_SIZE, _MIN_SIZE + 1),
(_MIN_SIZE, 3, _MIN_SIZE + 1),
(_MIN_SIZE, 3, 4, _MIN_SIZE + 1),
],
)
@pytest.mark.parametrize(
"order",
["C", "F"],
)
class TestSimpleCubReductionKernelContiguity(CubReductionTestBase):

@testing.for_contiguous_axes()
Expand Down Expand Up @@ -139,15 +153,15 @@ def test_can_use_cub_oversize_input4(self):
b = cupy.empty((), dtype=cupy.int8)
assert self.can_use([a], [b], (1,), (0,)) is None

# thread_unsafe marker requires pytest-run-parallel, not used by dpnp
# @pytest.mark.thread_unsafe(
# reason="AssertFunctionIsCalled and accelerate mutation.")
@pytest.mark.thread_unsafe(
reason="AssertFunctionIsCalled and accelerate mutation."
)
def test_can_use_accelerator_set_unset(self):
# ensure we use CUB block reduction and not CUB device reduction
old_routine_accelerators = _accelerator.get_routine_accelerators()
_accelerator.set_routine_accelerators([])

a = cupy.random.random((10, 10))
a = cupy.random.random((10, _cub_reduction._CUB_REDUCE_SIZE_THRESHOLD))
# this is the only function we can mock; the rest is cdef'd
func_name = "".join(
(
Expand Down
78 changes: 71 additions & 7 deletions dpnp/tests/third_party/cupy/core_tests/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,55 @@ class UserNdarray(cupy.ndarray):
b.custom_attr = 100


@testing.parameterize(
*testing.product(
{
"np_order": ["C", "F"],
"np_pinned": [False], # no pinned memory
"pinned_alloc_fails": [False], # no pinned memory
"cp_setup": [
("C", False, (48, 16, 4)),
("C", True, (16, 4)),
("F", False, (4, 8, 24)),
("F", True, (4, 8)),
],
}
)
)
class TestAsarray(unittest.TestCase):
def test_asarray(self):
cp_order, view, strides = self.cp_setup
shape = (2, 3, 4)
if self.np_pinned:
count = numpy.prod(shape)
dtype = numpy.float32()
pinned_ptr = cupy.cuda.alloc_pinned_memory(count * dtype.itemsize)
a_cpu = numpy.frombuffer(
pinned_ptr, dtype=dtype, count=count
).reshape(shape)
else:
a_cpu = numpy.ndarray(
shape, dtype=numpy.float32, order=self.np_order
)
a_cpu[...] = numpy.arange(a_cpu.size).reshape(a_cpu.shape)
if view:
a_cpu = a_cpu[:, 1, :]
try:
if self.pinned_alloc_fails:
cupy.cuda.set_pinned_memory_allocator(lambda _: None)
a = cupy.asarray(a_cpu, order=cp_order)
finally:
# None means "no pool", not "the default pool"
# cupy.cuda.set_pinned_memory_allocator(
# cupy.get_default_pinned_memory_pool().malloc
# )
pass
assert a.flags.c_contiguous == (cp_order == "C")
assert a.flags.f_contiguous == (cp_order == "F")
assert a.strides == strides
testing.assert_array_equal(a_cpu, a)


@testing.parameterize(
*testing.product(
{
Expand Down Expand Up @@ -278,14 +327,16 @@ def test_copy_multi_device_with_stream(self):
)


@pytest.mark.filterwarnings(
# Shape setting is deprecated starting NumPy 2.5
"ignore::DeprecationWarning"
)
class TestNdarrayShape(unittest.TestCase):

@testing.with_requires("numpy>=2.5")
@testing.numpy_cupy_array_equal()
def test_shape_set(self, xp):
arr = xp.ndarray((2, 3))
with testing.assert_warns(DeprecationWarning):
arr.shape = (3, 2)
arr.shape = (3, 2)
return xp.array(arr.shape)

@pytest.mark.skip(
Expand All @@ -298,15 +349,12 @@ def test_shape_set_infer(self, xp):
arr.shape = (3, -1)
return xp.array(arr.shape)

@testing.with_requires("numpy>=2.5")
@testing.numpy_cupy_array_equal()
def test_shape_set_int(self, xp):
arr = xp.ndarray((2, 3))
with testing.assert_warns(DeprecationWarning):
arr.shape = 6
arr.shape = 6
return xp.array(arr.shape)

@pytest.mark.filterwarnings("ignore::DeprecationWarning")
def test_shape_need_copy(self):
# from cupy/cupy#5470
for xp in (numpy, cupy):
Expand Down Expand Up @@ -563,6 +611,22 @@ def test_shape_mismatch(self):
wrap_take(a, i, out=o)


@testing.parameterize(
{"shape": (3, 4, 5), "indices": (2, 3), "out_shape": (2, 3)},
{"shape": (), "indices": (), "out_shape": ()},
)
@pytest.mark.skip("no exception since NumPy 2.5")
class TestNdarrayTakeErrorTypeMismatch(unittest.TestCase):

def test_output_type_mismatch(self):
for xp in (numpy, cupy):
a = testing.shaped_arange(self.shape, xp, numpy.int32)
i = testing.shaped_arange(self.indices, xp, numpy.int32) % 3
o = testing.shaped_arange(self.out_shape, xp, numpy.float32)
with pytest.raises(TypeError):
wrap_take(a, i, out=o)


@testing.parameterize(
{"shape": (0,), "indices": (0,), "axis": None},
{"shape": (0,), "indices": (0, 1), "axis": None},
Expand Down
27 changes: 12 additions & 15 deletions dpnp/tests/third_party/cupy/core_tests/test_ndarray_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import pytest

import dpnp as cupy

# import cupy._core._accelerator as _acc
# from cupy._core import _cub_reduction
from dpnp.tests.third_party.cupy import testing


Expand Down Expand Up @@ -34,11 +37,10 @@ def exclude_cutensor(cls):
# pass
# _acc.set_reduction_accelerators(red_acc)

# yield
yield

# _acc.set_routine_accelerators(old_routine_accelerators)
# _acc.set_reduction_accelerators(old_reduction_accelerators)
pass

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose(contiguous_check=False)
Expand Down Expand Up @@ -298,15 +300,7 @@ def _axes_for_shape(shape):
"shape,axis",
[
(shape, axis)
for shape in [
(),
(0,),
(0, 2),
(2, 0),
(0, 2, 3),
(2, 0, 3),
(2, 3, 0),
]
for shape in [(), (0,), (0, 2), (2, 0), (0, 2, 3), (2, 0, 3), (2, 3, 0)]
for axis in _axes_for_shape(shape)
],
)
Expand All @@ -331,13 +325,16 @@ def test_zero_size(self, xp, shape, axis, order, func):

# This class compares CUB results against NumPy's. ("fallback" is CuPy's
# original kernel, also tested here to reduce code duplication.)
# Non-empty shapes keep both the first and last axis >= 128 so the
# contiguous reduction stays on the CUB block-reduction path rather than
# the short-axis fallback.
@pytest.mark.parametrize(
"shape",
[
(10,),
(10, 20),
(10, 20, 30),
(10, 20, 30, 40),
(128,),
(128, 128),
(128, 2, 128),
(128, 2, 2, 128),
# skip (2, 3, 0) because it would not hit the CUB code path
(0,),
(2, 0),
Expand Down
Loading
Loading