Skip to content

Commit 54a2550

Browse files
authored
Merge pull request #16 from explosion/register-entropypoint-without-torch
Do not fail to register entry points when Torch is not installed
2 parents f69dabc + 9b2e20f commit 54a2550

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

spacy_experimental/biaffine_parser/bilinear.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
from thinc.shims.pytorch_grad_scaler import PyTorchGradScaler
88
from thinc.types import ArgsKwargs, Floats2d, Ints1d
99

10-
from .pytorch_bilinear import BilinearModel as PyTorchBilinearModel
10+
11+
# Ensure that the spacy-experimental package can register entry points without
12+
# Torch installed.
13+
try:
14+
from .pytorch_bilinear import BilinearModel as PyTorchBilinearModel
15+
except ImportError:
16+
PyTorchBilinearModel = None
1117

1218

1319
def build_bilinear(
@@ -19,6 +25,9 @@ def build_bilinear(
1925
mixed_precision: bool = False,
2026
grad_scaler: Optional[PyTorchGradScaler] = None
2127
) -> Model[Tuple[List[Doc], Ints1d], Floats2d]:
28+
if PyTorchBilinearModel is None:
29+
raise ImportError("BiLinear layer requires PyTorch: pip install thinc[torch]")
30+
2231
nI = None
2332
if tok2vec.has_dim("nO") is True:
2433
nI = tok2vec.get_dim("nO")

spacy_experimental/biaffine_parser/pairwise_bilinear.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77
from thinc.shims.pytorch_grad_scaler import PyTorchGradScaler
88
from thinc.types import ArgsKwargs, Floats2d, Floats3d, Floats4d, Ints1d
99

10-
from .pytorch_pairwise_bilinear import (
11-
PairwiseBilinearModel as PyTorchPairwiseBilinearModel,
12-
)
10+
# Ensure that the spacy-experimental package can register entry points without
11+
# Torch installed.
12+
try:
13+
from .pytorch_pairwise_bilinear import (
14+
PairwiseBilinearModel as PyTorchPairwiseBilinearModel,
15+
)
16+
except ImportError:
17+
PyTorchPairwiseBilinearModel = None
1318

1419

1520
def build_pairwise_bilinear(
@@ -21,6 +26,11 @@ def build_pairwise_bilinear(
2126
mixed_precision: bool = False,
2227
grad_scaler: Optional[PyTorchGradScaler] = None
2328
) -> Model[Tuple[List[Doc], Ints1d], Floats2d]:
29+
if PyTorchPairwiseBilinearModel is None:
30+
raise ImportError(
31+
"PairwiseBiLinear layer requires PyTorch: pip install thinc[torch]"
32+
)
33+
2434
nI = None
2535
if tok2vec.has_dim("nO") is True:
2636
nI = tok2vec.get_dim("nO")

0 commit comments

Comments
 (0)