deps.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import importlib.metadata
  15. import importlib.util
  16. import inspect
  17. import re
  18. from collections import defaultdict
  19. from functools import lru_cache, wraps
  20. from packaging.requirements import Requirement
  21. from packaging.version import Version
  22. from . import logging
  23. _EXTRA_PATTERN = re.compile(
  24. r"(?:;|and)*[ \t]*extra[ \t]*==[ \t]*['\"]([a-z0-9]+(?:-[a-z0-9]+)*)['\"]"
  25. )
  26. _COLLECTIVE_EXTRA_NAMES = {"base", "plugins", "all"}
  27. class DependencyError(Exception):
  28. pass
  29. def _get_extra_name_and_remove_extra_marker(dep_spec):
  30. # XXX: Not sure if this is correct
  31. m = _EXTRA_PATTERN.search(dep_spec)
  32. if m:
  33. return m.group(1), dep_spec[: m.start()] + dep_spec[m.end() :]
  34. else:
  35. return None, dep_spec
  36. def _get_extras():
  37. metadata = importlib.metadata.metadata("paddlex")
  38. extras = {}
  39. # XXX: The `metadata.get_all` used here is not well documented.
  40. for name in metadata.get_all("Provides-Extra", []):
  41. if name not in _COLLECTIVE_EXTRA_NAMES:
  42. extras[name] = defaultdict(list)
  43. for dep_spec in importlib.metadata.requires("paddlex"):
  44. extra_name, dep_spec = _get_extra_name_and_remove_extra_marker(dep_spec)
  45. if extra_name is not None and extra_name not in _COLLECTIVE_EXTRA_NAMES:
  46. dep_spec = dep_spec.rstrip()
  47. req = Requirement(dep_spec)
  48. assert extra_name in extras, extra_name
  49. extras[extra_name][req.name].append(dep_spec)
  50. return extras
  51. EXTRAS = _get_extras()
  52. def _get_dep_specs():
  53. dep_specs = defaultdict(list)
  54. for dep_spec in importlib.metadata.requires("paddlex"):
  55. extra_name, dep_spec = _get_extra_name_and_remove_extra_marker(dep_spec)
  56. if extra_name is None or extra_name == "all":
  57. dep_spec = dep_spec.rstrip()
  58. req = Requirement(dep_spec)
  59. dep_specs[req.name].append(dep_spec)
  60. return dep_specs
  61. DEP_SPECS = _get_dep_specs()
  62. def get_dep_version(dep):
  63. try:
  64. return importlib.metadata.version(dep)
  65. except importlib.metadata.PackageNotFoundError:
  66. return None
  67. @lru_cache()
  68. def is_dep_available(dep, /, check_version=None):
  69. # Currently for several special deps we check if the import packages exist.
  70. if dep in ("paddlepaddle", "paddle-custom-device", "ultra-infer") and check_version:
  71. raise ValueError(
  72. "Currently, `check_version` is not allowed to be `True` for `paddlepaddle`, `paddle-custom-device`, and `ultra-infer`."
  73. )
  74. if dep == "paddlepaddle":
  75. return importlib.util.find_spec("paddle") is not None
  76. elif dep == "paddle-custom-device":
  77. return importlib.util.find_spec("paddle_custom_device") is not None
  78. elif dep == "ultra-infer":
  79. return importlib.util.find_spec("ultra_infer") is not None
  80. else:
  81. if dep != "paddle2onnx" and dep not in DEP_SPECS:
  82. raise ValueError("Unknown dependency")
  83. if check_version is None:
  84. if dep == "paddle2onnx":
  85. check_version = True
  86. else:
  87. check_version = False
  88. version = get_dep_version(dep)
  89. if version is None:
  90. return False
  91. if check_version:
  92. if dep == "paddle2onnx":
  93. return Version(version) in Requirement(get_paddle2onnx_spec()).specifier
  94. for dep_spec in DEP_SPECS[dep]:
  95. if Version(version) in Requirement(dep_spec).specifier:
  96. return True
  97. else:
  98. return True
  99. def require_deps(*deps, obj_name=None):
  100. unavailable_deps = [dep for dep in deps if not is_dep_available(dep)]
  101. if len(unavailable_deps) > 0:
  102. if obj_name is not None:
  103. msg = f"`{obj_name}` is not ready for use, because the"
  104. else:
  105. msg = "The"
  106. msg += " following dependencies are not available:\n" + "\n".join(
  107. unavailable_deps
  108. )
  109. raise DependencyError(msg)
  110. def function_requires_deps(*deps):
  111. def _deco(func):
  112. @wraps(func)
  113. def _wrapper(*args, **kwargs):
  114. require_deps(*func._deps_, obj_name=func.__name__)
  115. return func(*args, **kwargs)
  116. func._deps_ = set(deps)
  117. return _wrapper
  118. return _deco
  119. def class_requires_deps(*deps):
  120. def _deco(cls):
  121. @wraps(cls.__init__)
  122. def _wrapper(self, *args, **kwargs):
  123. require_deps(*cls._deps_, obj_name=cls.__name__)
  124. return old_init_func(self, *args, **kwargs)
  125. cls._deps_ = set(deps)
  126. for base_cls in inspect.getmro(cls)[1:-1]:
  127. if hasattr(base_cls, "_deps_"):
  128. cls._deps_.update(base_cls._deps_)
  129. if "__init__" in cls.__dict__:
  130. old_init_func = cls.__init__
  131. else:
  132. def _forward(self, *args, **kwargs):
  133. return super(cls, self).__init__(*args, **kwargs)
  134. old_init_func = _forward
  135. cls.__init__ = _wrapper
  136. return cls
  137. return _deco
  138. @lru_cache()
  139. def is_extra_available(extra):
  140. flags = [is_dep_available(dep) for dep in EXTRAS[extra]]
  141. if all(flags):
  142. return True
  143. logging.debug(
  144. "These dependencies are not available: %s",
  145. [d for d, f in zip(EXTRAS[extra], flags) if not f],
  146. )
  147. return False
  148. def require_extra(extra, *, obj_name=None, alt=None):
  149. if is_extra_available(extra) or (alt is not None and is_extra_available(alt)):
  150. return
  151. if obj_name is not None:
  152. msg = f"`{obj_name}` requires additional dependencies."
  153. else:
  154. msg = "Additional dependencies are required."
  155. msg += f' To install them, run `pip install "paddlex[{extra}]==<PADDLEX_VERSION>"` if you’re installing `paddlex` from an index, or `pip install -e "/path/to/PaddleX[{extra}]"` if you’re installing `paddlex` locally.'
  156. if alt is not None:
  157. msg += f" Alternatively, you can install `paddlex[{alt}]` instead."
  158. raise DependencyError(msg)
  159. def pipeline_requires_extra(extra, *, alt=None):
  160. def _deco(pipeline_cls):
  161. @wraps(pipeline_cls.__init__)
  162. def _wrapper(self, *args, **kwargs):
  163. require_extra(extra, obj_name=pipeline_name, alt=alt)
  164. return old_init_func(self, *args, **kwargs)
  165. old_init_func = pipeline_cls.__init__
  166. pipeline_name = pipeline_cls.entities
  167. if isinstance(pipeline_name, list):
  168. assert len(pipeline_name) == 1, pipeline_name
  169. pipeline_name = pipeline_name[0]
  170. pipeline_cls.__init__ = _wrapper
  171. return pipeline_cls
  172. return _deco
  173. def is_hpip_available():
  174. return is_dep_available("ultra-infer")
  175. def require_hpip():
  176. if not is_hpip_available():
  177. raise DependencyError(
  178. "The high-performance inference plugin is not available. Please install it properly."
  179. )
  180. def is_serving_plugin_available():
  181. return is_extra_available("serving")
  182. def require_serving_plugin():
  183. if not is_serving_plugin_available():
  184. raise DependencyError(
  185. "The serving plugin is not available. Please install it properly."
  186. )
  187. def get_serving_dep_specs():
  188. dep_specs = []
  189. for item in EXTRAS["serving"].values():
  190. dep_specs += item
  191. return dep_specs
  192. def is_paddle2onnx_plugin_available():
  193. return is_dep_available("paddle2onnx")
  194. def require_paddle2onnx_plugin():
  195. if not is_paddle2onnx_plugin_available():
  196. raise DependencyError(
  197. "The Paddle2ONNX plugin is not available. Please install it properly."
  198. )
  199. def get_paddle2onnx_spec():
  200. return "paddle2onnx == 2.0.2rc3"