def set_backend(name, gpu_id): global CONFIG if name == "jax": set_current_ops(JaxOps()) CONFIG = CONFIG.replace("PyTorch", "") else: if gpu_id == -1: set_current_ops(NumpyOps()) else: set_current_ops(CupyOps()) CONFIG = CONFIG.replace("LSTM.v1", "PyTorchLSTM.v1")
def set_backend(name, gpu_id): global CONFIG if name == "generic": set_current_ops(Ops()) else: if gpu_id == -1: set_current_ops(NumpyOps(use_blis=True)) else: set_current_ops(CupyOps()) if name == "pytorch": import torch torch.set_num_threads(1) CONFIG = CONFIG.replace("LSTM.v1", "PyTorchLSTM.v1")
def test_large_seq2col_gpu_against_cpu(nW): cupy_ops = CupyOps() numpy_ops = NumpyOps() # Use array with a large enough batch to require multiple # CUDA grids. batch_size = 128 * 128 * 2 # threads per block * blocks * 2 X = numpy_ops.xp.random.randn(batch_size * 2).astype("float32").reshape( -1, 2) X_gpu = cupy_ops.asarray2f(X) # Use somewhat interesting sequence lengths. lengths = numpy_ops.asarray1i([1, 4, 2, 1] * (batch_size // 8)) lengths_gpu = cupy_ops.asarray1i(lengths) cols = numpy_ops.seq2col(X, nW=nW, lengths=lengths) cols_gpu = cupy_ops.seq2col(X_gpu, nW=nW, lengths=lengths_gpu) assert_allclose(cols, cols_gpu.get())
def set_backend(name, gpu_id): if gpu_id == -1: set_current_ops(NumpyOps()) else: set_current_ops(CupyOps()) CONFIG = CONFIG.replace("LSTM.v1", "PyTorchLSTM.v1")
from thinc.api import LSTM import inspect from .. import strategies from ..strategies import ndarrays_of_shape MAX_EXAMPLES = 10 VANILLA_OPS = Ops(numpy) NUMPY_OPS = NumpyOps() BLIS_OPS = NumpyOps(use_blis=True) CPU_OPS = [NUMPY_OPS, VANILLA_OPS] XP_OPS = [NUMPY_OPS] if CupyOps.xp is not None: XP_OPS.append(CupyOps()) ALL_OPS = XP_OPS + [VANILLA_OPS] @pytest.mark.parametrize("op", [NumpyOps, CupyOps]) def test_ops_consistency(op): """Test that specific ops don't define any methods that are not on the Ops base class and that all ops methods define the exact same arguments.""" attrs = [m for m in dir(op) if not m.startswith("_")] for attr in attrs: assert hasattr(Ops, attr) method = getattr(op, attr) if hasattr(method, "__call__"): sig = inspect.signature(method) params = [p for p in sig.parameters][1:] base_sig = inspect.signature(getattr(Ops, attr))