pp_option.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. from ...utils.func_register import FuncRegister
  15. from ...utils.device import parse_device, set_env_for_device, get_default_device
  16. from ...utils import logging
  17. from .new_ir_blacklist import NEWIR_BLOCKLIST
  18. class PaddlePredictorOption(object):
  19. """Paddle Inference Engine Option"""
  20. SUPPORT_RUN_MODE = (
  21. "paddle",
  22. "trt_fp32",
  23. "trt_fp16",
  24. "trt_int8",
  25. "mkldnn",
  26. "mkldnn_bf16",
  27. )
  28. SUPPORT_DEVICE = ("gpu", "cpu", "npu", "xpu", "mlu")
  29. _FUNC_MAP = {}
  30. register = FuncRegister(_FUNC_MAP)
  31. def __init__(self, model_name=None, **kwargs):
  32. super().__init__()
  33. self.model_name = model_name
  34. self._cfg = {}
  35. self._init_option(**kwargs)
  36. def _init_option(self, **kwargs):
  37. for k, v in kwargs.items():
  38. if k not in self._FUNC_MAP:
  39. raise Exception(
  40. f"{k} is not supported to set! The supported option is: \
  41. {list(self._FUNC_MAP.keys())}"
  42. )
  43. self._FUNC_MAP.get(k)(self, v)
  44. for k, v in self._get_default_config().items():
  45. self._cfg.setdefault(k, v)
  46. def _get_default_config(self):
  47. """get default config"""
  48. return {
  49. "run_mode": "paddle",
  50. "device": get_default_device(),
  51. "device_id": 0,
  52. "min_subgraph_size": 3,
  53. "shape_info_filename": None,
  54. "trt_calib_mode": False,
  55. "cpu_threads": 1,
  56. "trt_use_static": False,
  57. "delete_pass": [],
  58. "enable_new_ir": True if self.model_name not in NEWIR_BLOCKLIST else False,
  59. }
  60. @register("run_mode")
  61. def set_run_mode(self, run_mode: str):
  62. """set run mode"""
  63. if run_mode not in self.SUPPORT_RUN_MODE:
  64. support_run_mode_str = ", ".join(self.SUPPORT_RUN_MODE)
  65. raise ValueError(
  66. f"`run_mode` must be {support_run_mode_str}, but received {repr(run_mode)}."
  67. )
  68. self._cfg["run_mode"] = run_mode
  69. @register("device")
  70. def set_device(self, device: str):
  71. """set device"""
  72. if not device:
  73. return
  74. device_type, device_ids = parse_device(device)
  75. self._cfg["device"] = device_type
  76. if device_type not in self.SUPPORT_DEVICE:
  77. support_run_mode_str = ", ".join(self.SUPPORT_DEVICE)
  78. raise ValueError(
  79. f"The device type must be one of {support_run_mode_str}, but received {repr(device_type)}."
  80. )
  81. device_id = device_ids[0] if device_ids is not None else 0
  82. self._cfg["device_id"] = device_id
  83. set_env_for_device(device)
  84. if device_type not in ("cpu"):
  85. if device_ids is None or len(device_ids) > 1:
  86. logging.warning(f"The device ID has been set to {device_id}.")
  87. @register("min_subgraph_size")
  88. def set_min_subgraph_size(self, min_subgraph_size: int):
  89. """set min subgraph size"""
  90. if not isinstance(min_subgraph_size, int):
  91. raise Exception()
  92. self._cfg["min_subgraph_size"] = min_subgraph_size
  93. @register("shape_info_filename")
  94. def set_shape_info_filename(self, shape_info_filename: str):
  95. """set shape info filename"""
  96. self._cfg["shape_info_filename"] = shape_info_filename
  97. @register("trt_calib_mode")
  98. def set_trt_calib_mode(self, trt_calib_mode):
  99. """set trt calib mode"""
  100. self._cfg["trt_calib_mode"] = trt_calib_mode
  101. @register("cpu_threads")
  102. def set_cpu_threads(self, cpu_threads):
  103. """set cpu threads"""
  104. if not isinstance(cpu_threads, int) or cpu_threads < 1:
  105. raise Exception()
  106. self._cfg["cpu_threads"] = cpu_threads
  107. @register("trt_use_static")
  108. def set_trt_use_static(self, trt_use_static):
  109. """set trt use static"""
  110. self._cfg["trt_use_static"] = trt_use_static
  111. @register("delete_pass")
  112. def set_delete_pass(self, delete_pass):
  113. self._cfg["delete_pass"] = delete_pass
  114. @register("enable_new_ir")
  115. def set_enable_new_ir(self, enable_new_ir: bool):
  116. """set run mode"""
  117. self._cfg["enable_new_ir"] = enable_new_ir
  118. def get_support_run_mode(self):
  119. """get supported run mode"""
  120. return self.SUPPORT_RUN_MODE
  121. def get_support_device(self):
  122. """get supported device"""
  123. return self.SUPPORT_DEVICE
  124. def get_device(self):
  125. """get device"""
  126. return f"{self._cfg['device']}:{self._cfg['device_id']}"
  127. def __str__(self):
  128. return ", ".join([f"{k}: {v}" for k, v in self._cfg.items()])
  129. def __getattr__(self, key):
  130. if key not in self._cfg:
  131. raise Exception(f"The key ({key}) is not found in cfg: \n {self._cfg}")
  132. return self._cfg.get(key)
  133. def __eq__(self, obj):
  134. if isinstance(obj, PaddlePredictorOption):
  135. return obj._cfg == self._cfg
  136. return False