hubconf.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (c) Facebook, Inc. and its affiliates.
  2. #
  3. # This source code is licensed under the MIT license found in the
  4. # LICENSE file in the root directory of this source tree.
  5. """isort:skip_file"""
  6. import functools
  7. import importlib
  8. dependencies = [
  9. "dataclasses",
  10. "hydra",
  11. "numpy",
  12. "omegaconf",
  13. "regex",
  14. "requests",
  15. "torch",
  16. ]
  17. # Check for required dependencies and raise a RuntimeError if any are missing.
  18. missing_deps = []
  19. for dep in dependencies:
  20. try:
  21. importlib.import_module(dep)
  22. except ImportError:
  23. # Hack: the hydra package is provided under the "hydra-core" name in
  24. # pypi. We don't want the user mistakenly calling `pip install hydra`
  25. # since that will install an unrelated package.
  26. if dep == "hydra":
  27. dep = "hydra-core"
  28. missing_deps.append(dep)
  29. if len(missing_deps) > 0:
  30. raise RuntimeError("Missing dependencies: {}".format(", ".join(missing_deps)))
  31. # only do fairseq imports after checking for dependencies
  32. from fairseq.hub_utils import ( # noqa; noqa
  33. BPEHubInterface as bpe,
  34. TokenizerHubInterface as tokenizer,
  35. )
  36. from fairseq.models import MODEL_REGISTRY # noqa
  37. # torch.hub doesn't build Cython components, so if they are not found then try
  38. # to build them here
  39. try:
  40. import fairseq.data.token_block_utils_fast # noqa
  41. except ImportError:
  42. try:
  43. import cython # noqa
  44. import os
  45. from setuptools import sandbox
  46. sandbox.run_setup(
  47. os.path.join(os.path.dirname(__file__), "setup.py"),
  48. ["build_ext", "--inplace"],
  49. )
  50. except ImportError:
  51. print(
  52. "Unable to build Cython components. Please make sure Cython is "
  53. "installed if the torch.hub model you are loading depends on it."
  54. )
  55. # automatically expose models defined in FairseqModel::hub_models
  56. for _model_type, _cls in MODEL_REGISTRY.items():
  57. for model_name in _cls.hub_models().keys():
  58. globals()[model_name] = functools.partial(
  59. _cls.from_pretrained,
  60. model_name,
  61. )