Skip to content

Commit 8e640b1

Browse files
committed
Skip cupy test when cupy not installed
Signed-off-by: Ziheng Deng <zihengd@nvidia.com>
1 parent 8c92bba commit 8e640b1

5 files changed

Lines changed: 27 additions & 18 deletions

File tree

test/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,16 @@ def numba_cuda():
218218
pytest.xfail(f"Numba smoke test failed {result.returncode}. Skip.")
219219
import numba
220220
return numba.cuda
221+
222+
223+
def get_cupy_or_skip():
224+
try:
225+
import cupy as cupy
226+
except ImportError:
227+
pytest.skip("Cupy not installed. Skip test.")
228+
return cupy
229+
230+
231+
@pytest.fixture(scope="session")
232+
def cupy():
233+
return get_cupy_or_skip()

test/test_frontpage_example.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#example-begin
88
import cuda.tile as ct
9-
import cupy
109

1110
TILE_SIZE = 16
1211

@@ -18,18 +17,17 @@ def vector_add_kernel(a, b, result):
1817
b_tile = ct.load(b, index=(block_id,), shape=(TILE_SIZE,))
1918
result_tile = a_tile + b_tile
2019
ct.store(result, index=(block_id,), tile=result_tile)
21-
22-
# Host-side function that launches the above kernel.
23-
def vector_add(a: cupy.ndarray, b: cupy.ndarray, result: cupy.ndarray):
24-
assert a.shape == b.shape == result.shape
25-
grid = (ct.cdiv(a.shape[0], TILE_SIZE), 1, 1)
26-
ct.launch(cupy.cuda.get_current_stream(), grid, vector_add_kernel, (a, b, result))
2720
#example-end
2821

29-
3022
import numpy as np
3123

32-
def test_vector_add():
24+
def test_vector_add(cupy):
25+
# Host-side function that launches the above kernel.
26+
def vector_add(a: cupy.ndarray, b: cupy.ndarray, result: cupy.ndarray):
27+
assert a.shape == b.shape == result.shape
28+
grid = (ct.cdiv(a.shape[0], TILE_SIZE), 1, 1)
29+
ct.launch(cupy.cuda.get_current_stream(), grid, vector_add_kernel, (a, b, result))
30+
3331
rng = cupy.random.default_rng()
3432
a = rng.random(128)
3533
b = rng.random(128)

test/test_full.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
import numpy as np
66
import pytest
77
import torch
8-
import cupy as cp
98
# Move cutile types to the top level?
109
import cuda.tile as ct
1110

1211
from pathlib import Path
1312
from math import ceil
1413
from util import assert_equal, jit_kernel
15-
from conftest import float_dtypes, int_dtypes, bool_dtypes, dtype_id
14+
from conftest import float_dtypes, int_dtypes, bool_dtypes, dtype_id, get_cupy_or_skip
1615
from cuda.tile._exception import TileTypeError
1716
from dataclasses import dataclass
1817

@@ -65,7 +64,7 @@ def test_full_np_value_call(dtype, value, use_cupy, tmp_path: Path):
6564
dtype_str = torch_to_dtype_str[dtype].numpy
6665
if use_cupy:
6766
dtype_str = dtype_str.replace("np.", "cp.")
68-
globals = {"cp": cp}
67+
globals = {"cp": get_cupy_or_skip()}
6968
else:
7069
globals = {"np": np}
7170
value_str = str(value) if value is not None else ""
@@ -157,7 +156,7 @@ def test_full_np_dtype(value_dtype, use_cupy: bool, tmp_path: Path):
157156
dtype_str = dtype_str.replace("np.", "cp.")
158157
if value_str == "np.inf":
159158
value_str = "cp.inf"
160-
globals = {"cp": cp}
159+
globals = {"cp": get_cupy_or_skip()}
161160
else:
162161
globals = {"np": np}
163162
kernel = create_full_kernel("create_full_np_dtype", value_str, dtype_str,

test/test_readme_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
file_path = os.path.realpath(__file__)
1515

1616

17-
def test_readme():
17+
def test_readme(cupy):
1818
readme_path = os.path.join(os.path.dirname(file_path), "..", "README.md")
1919
readme_txt = open(readme_path, 'r').read()
2020
header = "Example\n-------\n```python"

test/test_stream.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
import torch
66
from torch.testing import make_tensor
7-
import cupy
87
import cuda.tile as ct
98
from ctypes import c_void_p
10-
from cuda.bindings.driver import CUstream
119

1210

1311
@ct.kernel
@@ -38,12 +36,12 @@ def test_torch_pass_stream_ptr():
3836

3937

4038
# -- Test CuPy Stream --
41-
def test_cupy_pass_stream():
39+
def test_cupy_pass_stream(cupy):
4240
stream = cupy.cuda.Stream()
4341
_test_stream(stream, stream.synchronize)
4442

4543

46-
def test_cupy_pass_stream_ptr():
44+
def test_cupy_pass_stream_ptr(cupy):
4745
stream = cupy.cuda.Stream()
4846
_test_stream(stream.ptr, stream.synchronize)
4947

@@ -59,6 +57,7 @@ def test_numba_pass_stream_ptr(numba_cuda):
5957
handle = stream.handle
6058
# numba-cuda < 0.30: handle is ctypes c_void_p
6159
# numba-cuda >= 0.30: handle is cuda.bindings.driver.CUstream
60+
from cuda.bindings.driver import CUstream
6261
if isinstance(handle, c_void_p):
6362
stream_ptr = handle.value
6463
elif isinstance(handle, CUstream):

0 commit comments

Comments
 (0)