From e026c24e7626bafa025302e69dbd4a41199a58b4 Mon Sep 17 00:00:00 2001 From: "Andrew M. Sica" Date: Wed, 27 Oct 2021 10:37:14 -0400 Subject: [PATCH 1/8] add bigendian op support --- thinc/backends/__init__.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/thinc/backends/__init__.py b/thinc/backends/__init__.py index 06f7cf6c6..1324a6903 100644 --- a/thinc/backends/__init__.py +++ b/thinc/backends/__init__.py @@ -79,6 +79,10 @@ def _import_extra_cpu_backends(): from thinc_apple_ops import AppleOps except ImportError: pass + try: + from thinc_bigendian_ops import BigEndianOps + except ImportError: + pass def get_ops(name: str, **kwargs) -> Ops: @@ -91,10 +95,15 @@ def get_ops(name: str, **kwargs) -> Ops: cls: Optional[Callable[..., Ops]] = None if name == "cpu": _import_extra_cpu_backends() + cls = ops_by_name.get("apple", ops_by_name.get("numpy")) + + if "bigendian" in ops_by_name: + cls = ops_by_name.get("bigendian", ops_by_name.get("numpy")) + else: cls = ops_by_name.get(name) - + if cls is None: raise ValueError(f"Invalid backend: {name}") @@ -113,6 +122,9 @@ def get_array_ops(arr): def use_ops(name: str, **kwargs): """Change the backend to execute on for the scope of the block.""" current_ops = get_current_ops() + # avoid fallback to base NumpyOps if on big endian platform + if current_ops.name == "bigendian" and name == "numpy": + name = current_ops.name set_current_ops(get_ops(name, **kwargs)) try: yield From 5cb9989b4eacbfc9a2fadde5035b2a8b613a9236 Mon Sep 17 00:00:00 2001 From: "Andrew M. Sica" Date: Thu, 28 Oct 2021 21:26:54 -0400 Subject: [PATCH 2/8] add test to mirror appleops test --- thinc/tests/backends/test_ops.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/thinc/tests/backends/test_ops.py b/thinc/tests/backends/test_ops.py index 44752334e..975f144f5 100644 --- a/thinc/tests/backends/test_ops.py +++ b/thinc/tests/backends/test_ops.py @@ -441,6 +441,13 @@ def test_get_ops(): assert isinstance(get_ops("cpu"), AppleOps) except ImportError: assert isinstance(get_ops("cpu"), NumpyOps) + # If BigEndian ops are available, "cpu" should return BigEndianOps or + # NumpyOps otherwise. + try: + from thinc_bigendian_ops import BigEndianOps + assert isinstance(get_ops("cpu"), BigEndianOps) + except ImportError: + assert isinstance(get_ops("cpu"), NumpyOps) with pytest.raises(ValueError): get_ops("blah") ops = Ops(numpy) From 769c6499419209b82397cd9f3819a06acaafee43 Mon Sep 17 00:00:00 2001 From: "Andrew M. Sica" Date: Fri, 19 Nov 2021 13:56:59 -0500 Subject: [PATCH 3/8] removed explicit bigendian check in get_ops --- thinc/backends/__init__.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/thinc/backends/__init__.py b/thinc/backends/__init__.py index d4f64f38f..07ace1e26 100644 --- a/thinc/backends/__init__.py +++ b/thinc/backends/__init__.py @@ -96,10 +96,9 @@ def get_ops(name: str, **kwargs) -> Ops: if name == "cpu": _import_extra_cpu_backends() - cls = ops_by_name.get("apple", ops_by_name.get("numpy")) - - if "bigendian" in ops_by_name: - cls = ops_by_name.get("bigendian", ops_by_name.get("numpy")) + cls = ops_by_name.get("numpy") + cls = ops_by_name.get("apple", cls) + cls = ops_by_name.get("bigendian", cls) else: cls = ops_by_name.get(name) From 912e51247540ca32eceb20b0f464f2b154e7b313 Mon Sep 17 00:00:00 2001 From: "Andrew M. Sica" Date: Fri, 19 Nov 2021 14:24:59 -0500 Subject: [PATCH 4/8] update use_ops --- thinc/backends/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/thinc/backends/__init__.py b/thinc/backends/__init__.py index 07ace1e26..31b21b540 100644 --- a/thinc/backends/__init__.py +++ b/thinc/backends/__init__.py @@ -122,8 +122,8 @@ def use_ops(name: str, **kwargs): """Change the backend to execute on for the scope of the block.""" current_ops = get_current_ops() # avoid fallback to base NumpyOps if on big endian platform - if current_ops.name == "bigendian" and name == "numpy": - name = current_ops.name + #if current_ops.name == "bigendian" and name == "numpy": + # name = current_ops.name set_current_ops(get_ops(name, **kwargs)) try: yield From af5c1f3da3716ce0dc26418459c0cbaf6fc9dbae Mon Sep 17 00:00:00 2001 From: "Andrew M. Sica" Date: Fri, 19 Nov 2021 14:36:31 -0500 Subject: [PATCH 5/8] remove use_ops check --- thinc/backends/__init__.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/thinc/backends/__init__.py b/thinc/backends/__init__.py index 31b21b540..00f1ca5db 100644 --- a/thinc/backends/__init__.py +++ b/thinc/backends/__init__.py @@ -121,9 +121,6 @@ def get_array_ops(arr): def use_ops(name: str, **kwargs): """Change the backend to execute on for the scope of the block.""" current_ops = get_current_ops() - # avoid fallback to base NumpyOps if on big endian platform - #if current_ops.name == "bigendian" and name == "numpy": - # name = current_ops.name set_current_ops(get_ops(name, **kwargs)) try: yield From 6dc3e9e43bca4749ad859dfacb817f90b37736f4 Mon Sep 17 00:00:00 2001 From: "Andrew M. Sica" Date: Fri, 19 Nov 2021 14:38:46 -0500 Subject: [PATCH 6/8] commit suggested changes --- thinc/backends/__init__.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/thinc/backends/__init__.py b/thinc/backends/__init__.py index d4f64f38f..00f1ca5db 100644 --- a/thinc/backends/__init__.py +++ b/thinc/backends/__init__.py @@ -96,10 +96,9 @@ def get_ops(name: str, **kwargs) -> Ops: if name == "cpu": _import_extra_cpu_backends() - cls = ops_by_name.get("apple", ops_by_name.get("numpy")) - - if "bigendian" in ops_by_name: - cls = ops_by_name.get("bigendian", ops_by_name.get("numpy")) + cls = ops_by_name.get("numpy") + cls = ops_by_name.get("apple", cls) + cls = ops_by_name.get("bigendian", cls) else: cls = ops_by_name.get(name) @@ -122,9 +121,6 @@ def get_array_ops(arr): def use_ops(name: str, **kwargs): """Change the backend to execute on for the scope of the block.""" current_ops = get_current_ops() - # avoid fallback to base NumpyOps if on big endian platform - if current_ops.name == "bigendian" and name == "numpy": - name = current_ops.name set_current_ops(get_ops(name, **kwargs)) try: yield From a3ee25b8741338cd790186c186c095796ec27528 Mon Sep 17 00:00:00 2001 From: Adriane Boyd Date: Mon, 6 Dec 2021 13:52:03 +0100 Subject: [PATCH 7/8] Format --- thinc/backends/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thinc/backends/__init__.py b/thinc/backends/__init__.py index 00f1ca5db..a96ef2573 100644 --- a/thinc/backends/__init__.py +++ b/thinc/backends/__init__.py @@ -102,7 +102,7 @@ def get_ops(name: str, **kwargs) -> Ops: else: cls = ops_by_name.get(name) - + if cls is None: raise ValueError(f"Invalid backend: {name}") From 83a0273b27fc49fb8a5a656dec2298b51340ac29 Mon Sep 17 00:00:00 2001 From: Adriane Boyd Date: Mon, 6 Dec 2021 13:54:25 +0100 Subject: [PATCH 8/8] Format --- thinc/backends/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/thinc/backends/__init__.py b/thinc/backends/__init__.py index a96ef2573..ba4daaa8a 100644 --- a/thinc/backends/__init__.py +++ b/thinc/backends/__init__.py @@ -94,12 +94,10 @@ def get_ops(name: str, **kwargs) -> Ops: cls: Optional[Callable[..., Ops]] = None if name == "cpu": - _import_extra_cpu_backends() - + _import_extra_cpu_backends() cls = ops_by_name.get("numpy") cls = ops_by_name.get("apple", cls) cls = ops_by_name.get("bigendian", cls) - else: cls = ops_by_name.get(name)