others.py 4.3 KB

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