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
2 changes: 1 addition & 1 deletion benchmarks/benchmarks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_xr_dataarray(
if type == "numpy":
pass
elif type == "cupy":
if not has_cuda_and_cupy:
if not has_cuda_and_cupy():
raise NotImplementedError()
import cupy
z = cupy.asarray(z)
Expand Down
40 changes: 40 additions & 0 deletions benchmarks/tests/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sys
from unittest.mock import Mock

import pytest

from benchmarks.benchmarks import common


@pytest.mark.parametrize("backend", ["cupy", "dask+cupy"])
@pytest.mark.parametrize("cupy_installed", [False, True])
def test_gpu_benchmark_skips_when_cuda_unavailable(
monkeypatch, backend, cupy_installed
):
available = Mock(return_value=False)
monkeypatch.setattr(common, "has_cuda_and_cupy", available)
cupy = Mock() if cupy_installed else None
if cupy_installed:
cupy.asarray.side_effect = AssertionError("must skip GPU conversion")
monkeypatch.setitem(sys.modules, "cupy", cupy)

with pytest.raises(NotImplementedError):
common.get_xr_dataarray((2, 3), backend)

available.assert_called_once_with()
if cupy_installed:
cupy.asarray.assert_not_called()


def test_cupy_benchmark_reaches_conversion_when_cuda_available(monkeypatch):
available = Mock(return_value=True)
monkeypatch.setattr(common, "has_cuda_and_cupy", available)
cupy = Mock()
cupy.asarray.side_effect = RuntimeError("conversion reached")
monkeypatch.setitem(sys.modules, "cupy", cupy)

with pytest.raises(RuntimeError, match="conversion reached"):
common.get_xr_dataarray((2, 3), "cupy")

available.assert_called_once_with()
cupy.asarray.assert_called_once()
Loading