kernel_option.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 functools import wraps, partial
  15. def register(register_map, key):
  16. """register the option setting func
  17. """
  18. def decorator(func):
  19. register_map[key] = func
  20. @wraps(func)
  21. def wrapper(self, *args, **kwargs):
  22. return func(self, *args, **kwargs)
  23. return wrapper
  24. return decorator
  25. class PaddleInferenceOption(object):
  26. """Paddle Inference Engine Option
  27. """
  28. SUPPORT_RUN_MODE = ('paddle', 'trt_fp32', 'trt_fp16', 'trt_int8', 'mkldnn',
  29. 'mkldnn_bf16')
  30. SUPPORT_DEVICE = ('gpu', 'cpu', 'npu', 'xpu', 'mlu')
  31. _REGISTER_MAP = {}
  32. register2self = partial(register, _REGISTER_MAP)
  33. def __init__(self, **kwargs):
  34. super().__init__()
  35. self._cfg = {}
  36. self._init_option(**kwargs)
  37. def _init_option(self, **kwargs):
  38. for k, v in kwargs.items():
  39. if k not in self._REGISTER_MAP:
  40. raise Exception(
  41. f"{k} is not supported to set! The supported option is: \
  42. {list(self._REGISTER_MAP.keys())}")
  43. self._REGISTER_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(cls):
  47. """ get default config """
  48. return {
  49. 'run_mode': 'paddle',
  50. 'batch_size': 1,
  51. 'device': 'gpu',
  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. }
  58. @register2self('run_mode')
  59. def set_run_mode(self, run_mode: str):
  60. """set run mode
  61. """
  62. if run_mode not in self.SUPPORT_RUN_MODE:
  63. support_run_mode_str = ", ".join(self.SUPPORT_RUN_MODE)
  64. raise ValueError(
  65. f"`run_mode` must be {support_run_mode_str}, but received {repr(run_mode)}."
  66. )
  67. self._cfg['run_mode'] = run_mode
  68. @register2self('batch_size')
  69. def set_batch_size(self, batch_size: int):
  70. """set batch size
  71. """
  72. if not isinstance(batch_size, int) or batch_size < 1:
  73. raise Exception()
  74. self._cfg['batch_size'] = batch_size
  75. @register2self('device')
  76. def set_device(self, device: str):
  77. """set device
  78. """
  79. device = device.split(":")[0]
  80. if device.lower() not in self.SUPPORT_DEVICE:
  81. support_run_mode_str = ", ".join(self.SUPPORT_DEVICE)
  82. raise ValueError(
  83. f"`device` must be {support_run_mode_str}, but received {repr(device)}."
  84. )
  85. self._cfg['device'] = device.lower()
  86. @register2self('min_subgraph_size')
  87. def set_min_subgraph_size(self, min_subgraph_size: int):
  88. """set min subgraph size
  89. """
  90. if not isinstance(min_subgraph_size, int):
  91. raise Exception()
  92. self._cfg['min_subgraph_size'] = min_subgraph_size
  93. @register2self('shape_info_filename')
  94. def set_shape_info_filename(self, shape_info_filename: str):
  95. """set shape info filename
  96. """
  97. self._cfg['shape_info_filename'] = shape_info_filename
  98. @register2self('trt_calib_mode')
  99. def set_trt_calib_mode(self, trt_calib_mode):
  100. """set trt calib mode
  101. """
  102. self._cfg['trt_calib_mode'] = trt_calib_mode
  103. @register2self('cpu_threads')
  104. def set_cpu_threads(self, cpu_threads):
  105. """set cpu threads
  106. """
  107. if not isinstance(cpu_threads, int) or cpu_threads < 1:
  108. raise Exception()
  109. self._cfg['cpu_threads'] = cpu_threads
  110. @register2self('trt_use_static')
  111. def set_trt_use_static(self, trt_use_static):
  112. """set trt use static
  113. """
  114. self._cfg['trt_use_static'] = trt_use_static
  115. def get_support_run_mode(self):
  116. """get supported run mode
  117. """
  118. return self.SUPPORT_RUN_MODE
  119. def get_support_device(self):
  120. """get supported device
  121. """
  122. return self.SUPPORT_DEVICE
  123. def __str__(self):
  124. return ", ".join([f"{k}: {v}" for k, v in self._cfg.items()])
  125. def __getattr__(self, key):
  126. if key not in self._cfg:
  127. raise Exception()
  128. return self._cfg.get(key)