From 7b54f728ddec7765a1d8a5e553d4b4b90b9edaec Mon Sep 17 00:00:00 2001 From: "Andrew M. Sica" <55592496+andrewsi-z@users.noreply.github.com> Date: Tue, 21 Dec 2021 07:40:42 -0500 Subject: [PATCH] Support big endian platform by providing new ops implementation (#559) * add bigendian op support * add test to mirror appleops test * removed explicit bigendian check in get_ops * update use_ops * remove use_ops check * commit suggested changes * Format * Format Co-authored-by: Adriane Boyd --- thinc/backends/__init__.py | 10 ++++++++-- thinc/tests/backends/test_ops.py | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/thinc/backends/__init__.py b/thinc/backends/__init__.py index 3376f54e0..ba4daaa8a 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: @@ -90,8 +94,10 @@ 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")) + _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) 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)