Skip to content

Commit 21c5f6d

Browse files
authored
3541 has cupy check (#3544)
* has cupy check Signed-off-by: Wenqi Li <[email protected]> * update based on comments Signed-off-by: Wenqi Li <[email protected]>
1 parent e655b4e commit 21c5f6d

13 files changed

+56
-38
lines changed

tests/test_cast_to_type.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
from monai.transforms import CastToType
1919
from monai.utils import optional_import
2020
from monai.utils.type_conversion import get_equivalent_dtype
21-
from tests.utils import TEST_NDARRAYS
21+
from tests.utils import HAS_CUPY, TEST_NDARRAYS
2222

23-
cp, has_cp = optional_import("cupy")
23+
cp, _ = optional_import("cupy")
2424

2525
TESTS = []
2626
for p in TEST_NDARRAYS:
@@ -45,7 +45,7 @@ def test_type(self, out_dtype, input_data, expected_type):
4545
self.assertEqual(result.dtype, get_equivalent_dtype(expected_type, type(result)))
4646

4747
@parameterized.expand(TESTS_CUPY)
48-
@unittest.skipUnless(has_cp, "Requires CuPy")
48+
@unittest.skipUnless(HAS_CUPY, "Requires CuPy")
4949
def test_type_cupy(self, out_dtype, input_data, expected_type):
5050
input_data = cp.asarray(input_data)
5151

tests/test_cast_to_typed.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818
from monai.transforms import CastToTyped
1919
from monai.utils import optional_import
20+
from tests.utils import HAS_CUPY
2021

21-
cp, has_cp = optional_import("cupy")
22+
cp, _ = optional_import("cupy")
2223

2324
TEST_CASE_1 = [
2425
{"keys": ["img"], "dtype": np.float64},
@@ -58,7 +59,7 @@ def test_type(self, input_param, input_data, expected_type):
5859
self.assertEqual(v.dtype, expected_type[k])
5960

6061
@parameterized.expand(TESTS_CUPY)
61-
@unittest.skipUnless(has_cp, "Requires CuPy")
62+
@unittest.skipUnless(HAS_CUPY, "Requires CuPy")
6263
def test_type_cupy(self, input_param, input_data, expected_type):
6364
input_data = {k: cp.asarray(v) for k, v in input_data.items()}
6465

tests/test_cucim_dict_transform.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
from monai.transforms import CuCIMd
1818
from monai.utils import optional_import, set_determinism
19-
from tests.utils import skip_if_no_cuda
19+
from tests.utils import HAS_CUPY, skip_if_no_cuda
2020

2121
_, has_cut = optional_import("cucim.core.operations.expose.transform")
22-
cp, has_cp = optional_import("cupy")
22+
cp, _ = optional_import("cupy")
2323

2424
set_determinism(seed=0)
2525

@@ -62,7 +62,7 @@
6262

6363

6464
@skip_if_no_cuda
65-
@unittest.skipUnless(has_cp, "CuPy is required.")
65+
@unittest.skipUnless(HAS_CUPY, "CuPy is required.")
6666
@unittest.skipUnless(has_cut, "cuCIM transforms are required.")
6767
class TestCuCIMDict(unittest.TestCase):
6868
@parameterized.expand(

tests/test_cucim_transform.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
from monai.transforms import CuCIM
1818
from monai.utils import optional_import, set_determinism
19-
from tests.utils import skip_if_no_cuda
19+
from tests.utils import HAS_CUPY, skip_if_no_cuda
2020

2121
_, has_cut = optional_import("cucim.core.operations.expose.transform")
22-
cp, has_cp = optional_import("cupy")
22+
cp, _ = optional_import("cupy")
2323

2424
set_determinism(seed=0)
2525

@@ -62,7 +62,7 @@
6262

6363

6464
@skip_if_no_cuda
65-
@unittest.skipUnless(has_cp, "CuPy is required.")
65+
@unittest.skipUnless(HAS_CUPY, "CuPy is required.")
6666
@unittest.skipUnless(has_cut, "cuCIM transforms are required.")
6767
class TestCuCIM(unittest.TestCase):
6868
@parameterized.expand(

tests/test_nvtx_decorator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
ToTensorD,
3232
)
3333
from monai.utils import Range, optional_import
34+
from tests.utils import HAS_CUPY
3435

3536
_, has_nvtx = optional_import("torch._C._nvtx", descriptor="NVTX is not installed. Are you sure you have a CUDA build?")
36-
_, has_cp = optional_import("cupy")
3737
_, has_tvt = optional_import("torchvision.transforms")
3838
_, has_cut = optional_import("cucim.core.operations.expose.transform")
3939

@@ -103,7 +103,7 @@ def test_tranform_dict(self, input):
103103
np.testing.assert_equal(output.numpy(), output3.numpy())
104104

105105
@parameterized.expand([TEST_CASE_WRAPPER])
106-
@unittest.skipUnless(has_cp, "Requires CuPy.")
106+
@unittest.skipUnless(HAS_CUPY, "Requires CuPy.")
107107
@unittest.skipUnless(has_cut, "Requires cuCIM transforms.")
108108
@unittest.skipUnless(has_tvt, "Requires torchvision transforms.")
109109
def test_wrapper_tranforms(self, input):

tests/test_rand_cucim_dict_transform.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
from monai.transforms import RandCuCIMd
1818
from monai.utils import optional_import, set_determinism
19-
from tests.utils import skip_if_no_cuda
19+
from tests.utils import HAS_CUPY, skip_if_no_cuda
2020

2121
_, has_cut = optional_import("cucim.core.operations.expose.transform")
22-
cp, has_cp = optional_import("cupy")
22+
cp, _ = optional_import("cupy")
2323

2424
set_determinism(seed=0)
2525

@@ -74,7 +74,7 @@
7474

7575

7676
@skip_if_no_cuda
77-
@unittest.skipUnless(has_cp, "CuPy is required.")
77+
@unittest.skipUnless(HAS_CUPY, "CuPy is required.")
7878
@unittest.skipUnless(has_cut, "cuCIM transforms are required.")
7979
class TestRandCuCIMDict(unittest.TestCase):
8080
@parameterized.expand(

tests/test_rand_cucim_transform.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
from monai.transforms import RandCuCIM
1818
from monai.utils import optional_import, set_determinism
19-
from tests.utils import skip_if_no_cuda
19+
from tests.utils import HAS_CUPY, skip_if_no_cuda
2020

2121
_, has_cut = optional_import("cucim.core.operations.expose.transform")
22-
cp, has_cp = optional_import("cupy")
22+
cp, _ = optional_import("cupy")
2323

2424
set_determinism(seed=0)
2525

@@ -74,7 +74,7 @@
7474

7575

7676
@skip_if_no_cuda
77-
@unittest.skipUnless(has_cp, "CuPy is required.")
77+
@unittest.skipUnless(HAS_CUPY, "CuPy is required.")
7878
@unittest.skipUnless(has_cut, "cuCIM transforms are required.")
7979
class TestRandCuCIM(unittest.TestCase):
8080
@parameterized.expand(

tests/test_to_cupy.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
from monai.transforms import ToCupy
1919
from monai.utils import optional_import
20-
from tests.utils import skip_if_no_cuda
20+
from tests.utils import HAS_CUPY, skip_if_no_cuda
2121

22-
cp, has_cp = optional_import("cupy")
22+
cp, _ = optional_import("cupy")
2323

2424

25-
@skipUnless(has_cp, "CuPy is required.")
25+
@skipUnless(HAS_CUPY, "CuPy is required.")
2626
class TestToCupy(unittest.TestCase):
2727
def test_cupy_input(self):
2828
test_data = cp.array([[1, 2], [3, 4]], dtype=cp.float32)

tests/test_to_cupyd.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
from monai.transforms import ToCupyd
1919
from monai.utils import optional_import
20-
from tests.utils import skip_if_no_cuda
20+
from tests.utils import HAS_CUPY, skip_if_no_cuda
2121

22-
cp, has_cp = optional_import("cupy")
22+
cp, _ = optional_import("cupy")
2323

2424

25+
@skipUnless(HAS_CUPY, "CuPy is required.")
2526
class TestToCupyd(unittest.TestCase):
26-
@skipUnless(has_cp, "CuPy is required.")
2727
def test_cupy_input(self):
2828
test_data = cp.array([[1, 2], [3, 4]])
2929
test_data = cp.rot90(test_data)
@@ -33,7 +33,6 @@ def test_cupy_input(self):
3333
self.assertTrue(result.flags["C_CONTIGUOUS"])
3434
cp.testing.assert_allclose(result, test_data)
3535

36-
@skipUnless(has_cp, "CuPy is required.")
3736
def test_numpy_input(self):
3837
test_data = np.array([[1, 2], [3, 4]])
3938
test_data = np.rot90(test_data)
@@ -43,7 +42,6 @@ def test_numpy_input(self):
4342
self.assertTrue(result.flags["C_CONTIGUOUS"])
4443
cp.testing.assert_allclose(result, test_data)
4544

46-
@skipUnless(has_cp, "CuPy is required.")
4745
def test_tensor_input(self):
4846
test_data = torch.tensor([[1, 2], [3, 4]])
4947
test_data = test_data.rot90()
@@ -53,7 +51,6 @@ def test_tensor_input(self):
5351
self.assertTrue(result.flags["C_CONTIGUOUS"])
5452
cp.testing.assert_allclose(result, test_data.numpy())
5553

56-
@skipUnless(has_cp, "CuPy is required.")
5754
@skip_if_no_cuda
5855
def test_tensor_cuda_input(self):
5956
test_data = torch.tensor([[1, 2], [3, 4]]).cuda()
@@ -64,7 +61,6 @@ def test_tensor_cuda_input(self):
6461
self.assertTrue(result.flags["C_CONTIGUOUS"])
6562
cp.testing.assert_allclose(result, test_data.cpu().numpy())
6663

67-
@skipUnless(has_cp, "CuPy is required.")
6864
def test_list_tuple(self):
6965
test_data = [[1, 2], [3, 4]]
7066
result = ToCupyd(keys="img", wrap_sequence=True)({"img": test_data})["img"]

tests/test_to_numpy.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
from monai.transforms import ToNumpy
1919
from monai.utils import optional_import
20-
from tests.utils import assert_allclose, skip_if_no_cuda
20+
from tests.utils import HAS_CUPY, assert_allclose, skip_if_no_cuda
2121

22-
cp, has_cp = optional_import("cupy")
22+
cp, _ = optional_import("cupy")
2323

2424

2525
class TestToNumpy(unittest.TestCase):
26-
@skipUnless(has_cp, "CuPy is required.")
26+
@skipUnless(HAS_CUPY, "CuPy is required.")
2727
def test_cupy_input(self):
2828
test_data = cp.array([[1, 2], [3, 4]])
2929
test_data = cp.rot90(test_data)

0 commit comments

Comments
 (0)