From 134de837c3656b157c119cce3df4e4a401ac5c67 Mon Sep 17 00:00:00 2001 From: kadarakos Date: Fri, 17 Jun 2022 08:31:56 +0000 Subject: [PATCH 1/7] if not xp array module is None --- thinc/util.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/thinc/util.py b/thinc/util.py index e46c62447..109014ee9 100644 --- a/thinc/util.py +++ b/thinc/util.py @@ -47,10 +47,12 @@ 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 + return None def gpu_is_available(): From ea24ce5e5ba8d54cf9317e71b4ffe7d86740b24a Mon Sep 17 00:00:00 2001 From: kadarakos Date: Mon, 20 Jun 2022 10:54:18 +0000 Subject: [PATCH 2/7] raise error --- thinc/util.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/thinc/util.py b/thinc/util.py index 109014ee9..ed018e226 100644 --- a/thinc/util.py +++ b/thinc/util.py @@ -52,7 +52,10 @@ def get_array_module(arr): # pragma: no cover elif is_cupy_array(arr): return cupy else: - return None + raise ValueError( + "Input was neither a numpy or cupy array" + f", but was of type {type(arr)}." + ) def gpu_is_available(): From be9d18c6a87432318dcfb527a32b4d2215c1a6ab Mon Sep 17 00:00:00 2001 From: kadarakos Date: Mon, 20 Jun 2022 11:15:54 +0000 Subject: [PATCH 3/7] update test --- thinc/tests/test_util.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/thinc/tests/test_util.py b/thinc/tests/test_util.py index 715d381d5..a7a0baad0 100644 --- a/thinc/tests/test_util.py +++ b/thinc/tests/test_util.py @@ -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", @@ -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 ") + with pytest.raises(ValueError, match=error): + get_array_module(0) + zeros = xp.zeros((1, 2)) + xp_ = get_array_module(zeros) + assert hasattr(xp_, "ndarray") + if xp == numpy: + assert is_numpy_array(zeros) + assert not is_numpy_array((1, 2)) + else: + from thinc.util import is_cupy_array + assert is_cupy_array(zeros) + assert not is_cupy_array((1, 2)) @given( From b78182f8b2551eb780a8d36463fe8c1177242dd4 Mon Sep 17 00:00:00 2001 From: kadarakos Date: Mon, 20 Jun 2022 15:43:56 +0000 Subject: [PATCH 4/7] more detailed error --- thinc/tests/test_util.py | 8 +++++--- thinc/util.py | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/thinc/tests/test_util.py b/thinc/tests/test_util.py index a7a0baad0..dba59fe93 100644 --- a/thinc/tests/test_util.py +++ b/thinc/tests/test_util.py @@ -3,6 +3,7 @@ from hypothesis import given from thinc.api import get_width, Ragged, Padded from thinc.util import get_array_module, is_numpy_array, to_categorical +from thinc.util import is_cupy_array from thinc.util import convert_recursive from thinc.types import ArgsKwargs @@ -48,8 +49,10 @@ def test_get_width_fail(obj): @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 ") + error = ("Only numpy and cupy arrays are supported" + ", but found instead. If " + "get_array_module module wasn't called " + "directly, this might mean a bug in Thinc.") with pytest.raises(ValueError, match=error): get_array_module(0) zeros = xp.zeros((1, 2)) @@ -59,7 +62,6 @@ def test_array_module_cpu_gpu_helpers(xp): assert is_numpy_array(zeros) assert not is_numpy_array((1, 2)) else: - from thinc.util import is_cupy_array assert is_cupy_array(zeros) assert not is_cupy_array((1, 2)) diff --git a/thinc/util.py b/thinc/util.py index ed018e226..c0158e5a7 100644 --- a/thinc/util.py +++ b/thinc/util.py @@ -53,8 +53,10 @@ def get_array_module(arr): # pragma: no cover return cupy else: raise ValueError( - "Input was neither a numpy or cupy array" - f", but was of type {type(arr)}." + "Only numpy and cupy arrays are supported" + f", but found {type(arr)} instead. If " + "get_array_module module wasn't called " + "directly, this might mean a bug in Thinc." ) From f27a59c782a3ba2fa7c183abff3cbe36537df90c Mon Sep 17 00:00:00 2001 From: kadarakos Date: Thu, 23 Jun 2022 09:08:11 +0200 Subject: [PATCH 5/7] Update thinc/tests/test_util.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniƫl de Kok --- thinc/tests/test_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thinc/tests/test_util.py b/thinc/tests/test_util.py index dba59fe93..8b1c8c188 100644 --- a/thinc/tests/test_util.py +++ b/thinc/tests/test_util.py @@ -57,7 +57,7 @@ def test_array_module_cpu_gpu_helpers(xp): get_array_module(0) zeros = xp.zeros((1, 2)) xp_ = get_array_module(zeros) - assert hasattr(xp_, "ndarray") + assert xp_ == xp if xp == numpy: assert is_numpy_array(zeros) assert not is_numpy_array((1, 2)) From f22cbdd9a57e77643f6cb9a79c9f0b72f451884b Mon Sep 17 00:00:00 2001 From: kadarakos Date: Wed, 6 Jul 2022 17:57:59 +0200 Subject: [PATCH 6/7] Update thinc/util.py Co-authored-by: Adriane Boyd --- thinc/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thinc/util.py b/thinc/util.py index c0158e5a7..ab2680dd1 100644 --- a/thinc/util.py +++ b/thinc/util.py @@ -56,7 +56,7 @@ def get_array_module(arr): # pragma: no cover "Only numpy and cupy arrays are supported" f", but found {type(arr)} instead. If " "get_array_module module wasn't called " - "directly, this might mean a bug in Thinc." + "directly, this might indicate a bug in Thinc." ) From 917731fe2fc4a0f70130dbf07974eacbd5a1ece0 Mon Sep 17 00:00:00 2001 From: Adriane Boyd Date: Thu, 7 Jul 2022 17:34:51 +0200 Subject: [PATCH 7/7] Update thinc/tests/test_util.py --- thinc/tests/test_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thinc/tests/test_util.py b/thinc/tests/test_util.py index 8b1c8c188..76985b24b 100644 --- a/thinc/tests/test_util.py +++ b/thinc/tests/test_util.py @@ -52,7 +52,7 @@ def test_array_module_cpu_gpu_helpers(xp): error = ("Only numpy and cupy arrays are supported" ", but found instead. If " "get_array_module module wasn't called " - "directly, this might mean a bug in Thinc.") + "directly, this might indicate a bug in Thinc.") with pytest.raises(ValueError, match=error): get_array_module(0) zeros = xp.zeros((1, 2))