others.py 4.2 KB

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