Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_array_module with non-array input returns None #703

Merged
merged 8 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
28 changes: 23 additions & 5 deletions thinc/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

from . import strategies

ALL_XP = [numpy]
try:
import cupy
ALL_XP.append(cupy)
except ImportError:
pass


@pytest.mark.parametrize(
"obj,width",
Expand Down Expand Up @@ -39,11 +46,22 @@ def test_get_width_fail(obj):
get_width(obj)


def test_array_module_cpu_gpu_helpers():
xp = get_array_module(0)
assert hasattr(xp, "ndarray")
assert is_numpy_array(numpy.zeros((1, 2)))
assert not is_numpy_array((1, 2))
@pytest.mark.parametrize("xp", ALL_XP)
def test_array_module_cpu_gpu_helpers(xp):
error = ("Input was neither a numpy or cupy array"
f", but was of type <class 'int'>")
with pytest.raises(ValueError, match=error):
get_array_module(0)
zeros = xp.zeros((1, 2))
xp_ = get_array_module(zeros)
assert hasattr(xp_, "ndarray")
kadarakos marked this conversation as resolved.
Show resolved Hide resolved
if xp == numpy:
assert is_numpy_array(zeros)
assert not is_numpy_array((1, 2))
else:
from thinc.util import is_cupy_array
svlandeg marked this conversation as resolved.
Show resolved Hide resolved
assert is_cupy_array(zeros)
assert not is_cupy_array((1, 2))


@given(
Expand Down
9 changes: 7 additions & 2 deletions thinc/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,15 @@ def get_torch_default_device() -> "torch.device":


def get_array_module(arr): # pragma: no cover
if is_cupy_array(arr):
if is_numpy_array(arr):
return numpy
elif is_cupy_array(arr):
return cupy
else:
return numpy
raise ValueError(
"Input was neither a numpy or cupy array"
f", but was of type {type(arr)}."
kadarakos marked this conversation as resolved.
Show resolved Hide resolved
)


def gpu_is_available():
Expand Down