Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Add update_forward_refs with local models #494

Merged
merged 2 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ wasabi>=0.8.1,<1.1.0
catalogue>=2.0.3,<2.1.0
ml_datasets>=0.2.0,<0.3.0
# Third-party dependencies
pydantic>=1.7.1,<1.8.0
pydantic>=1.7.1,<1.8.2
numpy>=1.15.0
# Backports of modern Python features
dataclasses>=0.6,<1.0; python_version < "3.7"
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ install_requires =
# Third-party dependencies
setuptools
numpy>=1.15.0
pydantic>=1.7.1,<1.8.0
pydantic>=1.7.1,<1.8.2
# Backports of modern Python features
dataclasses>=0.6,<1.0; python_version < "3.7"
typing_extensions>=3.7.4.1,<4.0.0.0; python_version < "3.8"
Expand Down
13 changes: 8 additions & 5 deletions thinc/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
except ImportError: # pragma: no cover
has_mxnet = False

from .types import ArrayXd, ArgsKwargs, Ragged, Padded, FloatsXd, IntsXd

from .types import ArrayXd, ArgsKwargs, Ragged, Padded, FloatsXd, IntsXd # noqa: E402
from . import types # noqa: E402

def get_array_module(arr): # pragma: no cover
if is_cupy_array(arr):
Expand Down Expand Up @@ -82,7 +82,7 @@ def fix_random_seed(seed: int = 0) -> None: # pragma: no cover

def is_xp_array(obj: Any) -> bool:
"""Check whether an object is a numpy or cupy array."""
return is_numpy_array(obj) or is_cupy_array(obj)
return is_numpy_array(obj) or is_cupy_array(obj)


def is_cupy_array(obj: Any) -> bool: # pragma: no cover
Expand Down Expand Up @@ -207,7 +207,7 @@ def to_categorical(Y: IntsXd, n_classes: Optional[int] = None) -> FloatsXd:
if xp is cupy: # pragma: no cover
Y = Y.get()
keep_shapes: List[int] = list(Y.shape)
Y = numpy.array(Y, dtype="int").ravel() # type: ignore
Y = numpy.array(Y, dtype="int").ravel() # type: ignore
if n_classes is None:
n_classes = int(numpy.max(Y) + 1)
keep_shapes.append(n_classes)
Expand Down Expand Up @@ -320,7 +320,7 @@ def xp2tensorflow(
"""Convert a numpy or cupy tensor to a TensorFlow Tensor or Variable"""
assert_tensorflow_installed()
if hasattr(xp_tensor, "toDlpack"):
dlpack_tensor = xp_tensor.toDlpack() # type: ignore
dlpack_tensor = xp_tensor.toDlpack() # type: ignore
tf_tensor = tensorflow.experimental.dlpack.from_dlpack(dlpack_tensor)
else:
tf_tensor = tf.convert_to_tensor(xp_tensor)
Expand Down Expand Up @@ -437,6 +437,9 @@ def validate_fwd_input_output(
sig_args["Y"] = (annot_y, ...)
args["Y"] = (Y, lambda x: x)
ArgModel = create_model("ArgModel", **sig_args)
# Make sure the forward refs are resolved and the types used by them are
# available in the correct scope. See #494 for details.
ArgModel.update_forward_refs(**types.__dict__)
try:
ArgModel.parse_obj(args)
except ValidationError as e:
Expand Down