others.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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 json
  15. import signal
  16. from typing import Union
  17. from pathlib import Path
  18. __all__ = [
  19. "UnsupportedAPIError",
  20. "UnsupportedParamError",
  21. "CalledProcessError",
  22. "raise_unsupported_api_error",
  23. "raise_key_not_found_error",
  24. "raise_class_not_found_error",
  25. "raise_no_entity_registered_error",
  26. "raise_unsupported_device_error",
  27. "raise_model_not_found_error",
  28. "DuplicateRegistrationError",
  29. ]
  30. class UnsupportedAPIError(Exception):
  31. """UnsupportedAPIError"""
  32. pass
  33. class UnsupportedParamError(Exception):
  34. """UnsupportedParamError"""
  35. pass
  36. class KeyNotFoundError(Exception):
  37. """KeyNotFoundError"""
  38. pass
  39. class ClassNotFoundException(Exception):
  40. """ClassNotFoundException"""
  41. pass
  42. class NoEntityRegisteredException(Exception):
  43. """NoEntityRegisteredException"""
  44. pass
  45. class UnsupportedDeviceError(Exception):
  46. """UnsupportedDeviceError"""
  47. pass
  48. class CalledProcessError(Exception):
  49. """CalledProcessError"""
  50. def __init__(self, returncode, cmd, output=None, stderr=None):
  51. super().__init__()
  52. self.returncode = returncode
  53. self.cmd = cmd
  54. self.output = output
  55. self.stderr = stderr
  56. def __str__(self):
  57. if self.returncode and self.returncode < 0:
  58. try:
  59. return f"Command {repr(self.cmd)} died with {repr(signal.Signals(-self.returncode))}."
  60. except ValueError:
  61. return f"Command {repr(self.cmd)} died with unknown signal {-self.returncode}."
  62. else:
  63. return f"Command {repr(self.cmd)} returned non-zero exit status {self.returncode}."
  64. class DuplicateRegistrationError(Exception):
  65. """DuplicateRegistrationError"""
  66. pass
  67. class ModelNotFoundError(Exception):
  68. """Model Not Found Error"""
  69. def raise_unsupported_api_error(api_name, cls=None):
  70. """raise unsupported api error"""
  71. # TODO: Automatically extract `api_name` and `cls` from stack frame
  72. if cls is not None:
  73. name = f"{cls.__name__}.{api_name}"
  74. else:
  75. name = api_name
  76. raise UnsupportedAPIError(f"The API `{name}` is not supported.")
  77. def raise_key_not_found_error(key, config=None):
  78. """raise key not found error"""
  79. msg = f"`{key}` not found in config."
  80. if config:
  81. config_str = json.dumps(config, indent=4, ensure_ascii=False)
  82. msg += f"\nThe content of config:\n{config_str}"
  83. raise KeyNotFoundError(msg)
  84. def raise_class_not_found_error(cls_name, base_cls, all_entities=None):
  85. """raise class not found error"""
  86. base_cls_name = base_cls.__name__
  87. msg = f"`{cls_name}` is not registered on {base_cls_name}."
  88. if all_entities is not None:
  89. all_entities_str = ", ".join(all_entities)
  90. msg += f"\nThe registied entities: [{all_entities_str}]"
  91. raise ClassNotFoundException(msg)
  92. def raise_no_entity_registered_error(base_cls):
  93. """raise no entity registered error"""
  94. base_cls_name = base_cls.__name__
  95. msg = f"There no entity register on {base_cls_name}. Hint: Maybe the subclass is not imported."
  96. raise NoEntityRegisteredException(msg)
  97. def raise_unsupported_device_error(device, supported_device=None):
  98. """raise_unsupported_device_error"""
  99. msg = f"The device `{device}` is not supported! "
  100. if supported_device is not None:
  101. supported_device_str = ", ".join(supported_device)
  102. msg += f"The supported device types are: [{supported_device_str}]."
  103. raise UnsupportedDeviceError(msg)
  104. def raise_model_not_found_error(model_path: Union[str, Path]):
  105. """raise ModelNotFoundError
  106. Args:
  107. model_path (str|Path): the path to model file.
  108. """
  109. msg = f"The model file(s)(`{model_path}`) is not found."
  110. raise ModelNotFoundError(msg)