Skip to content

Commit 16fc9f6

Browse files
committed
test readonly with numpy; rename use_stream parameter
1 parent 6af4da3 commit 16fc9f6

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

cuda_core/examples/strided_memory_view.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
# Below, as a user we want to perform the said in-place operation on either CPU
108108
# or GPU, by calling the corresponding function implemented "elsewhere" (done above).
109109

110+
110111
# We assume the 0-th argument supports either DLPack or CUDA Array Interface (both
111112
# of which are supported by StridedMemoryView).
112113
@args_viewable_as_strided_memory((0,))

cuda_core/tests/test_utils.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ def convert_strides_to_counts(strides, itemsize):
2727
np.empty(3, dtype=np.int32),
2828
np.empty((6, 6), dtype=np.float64)[::2, ::2],
2929
np.empty((3, 4), order="F"),
30+
np.empty((), dtype=np.float16),
31+
# readonly is fixed recently (numpy/numpy#26501)
32+
pytest.param(
33+
np.frombuffer(b""),
34+
marks=pytest.mark.skipif(
35+
tuple(int(i) for i in np.__version__.split(".")[:2]) < (2, 1), reason="need numpy 2.1.0+"
36+
),
37+
),
3038
),
3139
)
3240
class TestViewCPU:
@@ -57,22 +65,23 @@ def _check_view(self, view, in_arr):
5765
assert view.device_id == -1
5866
assert view.is_device_accessible is False
5967
assert view.exporting_obj is in_arr
68+
assert view.readonly is not in_arr.flags.writeable
6069

6170

6271
def gpu_array_samples():
6372
# TODO: this function would initialize the device at test collection time
6473
samples = []
6574
if cp is not None:
6675
samples += [
67-
(cp.empty(3, dtype=cp.complex64), None),
76+
(cp.empty(3, dtype=cp.complex64), False),
6877
(cp.empty((6, 6), dtype=cp.float64)[::2, ::2], True),
6978
(cp.empty((3, 4), order="F"), True),
7079
]
7180
# Numba's device_array is the only known array container that does not
7281
# support DLPack (so that we get to test the CAI coverage).
7382
if numba_cuda is not None:
7483
samples += [
75-
(numba_cuda.device_array((2,), dtype=np.int8), None),
84+
(numba_cuda.device_array((2,), dtype=np.int8), False),
7685
(numba_cuda.device_array((4, 2), dtype=np.float32), True),
7786
]
7887
return samples
@@ -86,14 +95,14 @@ def gpu_array_ptr(arr):
8695
raise NotImplementedError(f"{arr=}")
8796

8897

89-
@pytest.mark.parametrize("in_arr,stream", (*gpu_array_samples(),))
98+
@pytest.mark.parametrize("in_arr,use_stream", (*gpu_array_samples(),))
9099
class TestViewGPU:
91-
def test_args_viewable_as_strided_memory_gpu(self, in_arr, stream):
100+
def test_args_viewable_as_strided_memory_gpu(self, in_arr, use_stream):
92101
# TODO: use the device fixture?
93102
dev = Device()
94103
dev.set_current()
95104
# This is the consumer stream
96-
s = dev.create_stream() if stream else None
105+
s = dev.create_stream() if use_stream else None
97106

98107
@args_viewable_as_strided_memory((0,))
99108
def my_func(arr):
@@ -102,12 +111,12 @@ def my_func(arr):
102111

103112
my_func(in_arr)
104113

105-
def test_strided_memory_view_cpu(self, in_arr, stream):
114+
def test_strided_memory_view_cpu(self, in_arr, use_stream):
106115
# TODO: use the device fixture?
107116
dev = Device()
108117
dev.set_current()
109118
# This is the consumer stream
110-
s = dev.create_stream() if stream else None
119+
s = dev.create_stream() if use_stream else None
111120

112121
view = StridedMemoryView(in_arr, stream_ptr=s.handle if s else -1)
113122
self._check_view(view, in_arr, dev)
@@ -125,3 +134,4 @@ def _check_view(self, view, in_arr, dev):
125134
assert view.device_id == dev.device_id
126135
assert view.is_device_accessible is True
127136
assert view.exporting_obj is in_arr
137+
# can't test view.readonly with CuPy or Numba...

0 commit comments

Comments
 (0)