trt_config.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 collections import defaultdict
  15. import lazy_paddle
  16. from ...utils.flags import USE_PIR_TRT
  17. class LazyLoadDict(dict):
  18. def __init__(self, *args, **kwargs):
  19. self._initialized = False
  20. super().__init__(*args, **kwargs)
  21. def _initialize(self):
  22. if not self._initialized:
  23. self.update(self._load())
  24. self._initialized = True
  25. def __getitem__(self, key):
  26. self._initialize()
  27. return super().__getitem__(key)
  28. def __contains__(self, key):
  29. self._initialize()
  30. return super().__contains__(key)
  31. def _load(self):
  32. raise NotImplementedError
  33. class OLD_IR_TRT_PRECISION_MAP_CLASS(LazyLoadDict):
  34. def _load(self):
  35. from lazy_paddle.inference import PrecisionType
  36. return {
  37. "trt_int8": PrecisionType.Int8,
  38. "trt_fp32": PrecisionType.Float32,
  39. "trt_fp16": PrecisionType.Half,
  40. }
  41. class PIR_TRT_PRECISION_MAP_CLASS(LazyLoadDict):
  42. def _load(self):
  43. from lazy_paddle.tensorrt.export import PrecisionMode
  44. return {
  45. "trt_int8": PrecisionMode.INT8,
  46. "trt_fp32": PrecisionMode.FP32,
  47. "trt_fp16": PrecisionMode.FP16,
  48. }
  49. ############ old ir trt ############
  50. OLD_IR_TRT_PRECISION_MAP = OLD_IR_TRT_PRECISION_MAP_CLASS()
  51. OLD_IR_TRT_CFG_DEFAULT_SETTING = {
  52. "workspace_size": 1 << 30,
  53. "max_batch_size": 32,
  54. "min_subgraph_size": 3,
  55. "use_static": True,
  56. "use_calib_mode": False,
  57. }
  58. OLD_IR_TRT_CFG_SETTING = {
  59. "SegFormer-B3": {
  60. "enable_tensorrt_engine": {
  61. **OLD_IR_TRT_CFG_DEFAULT_SETTING,
  62. "workspace_size": 1 << 31,
  63. }
  64. },
  65. "SegFormer-B4": {
  66. "enable_tensorrt_engine": {
  67. **OLD_IR_TRT_CFG_DEFAULT_SETTING,
  68. "workspace_size": 1 << 31,
  69. }
  70. },
  71. "SegFormer-B5": {
  72. "enable_tensorrt_engine": {
  73. **OLD_IR_TRT_CFG_DEFAULT_SETTING,
  74. "workspace_size": 1 << 31,
  75. }
  76. },
  77. "SLANeXt_wired": {
  78. "enable_tensorrt_engine": OLD_IR_TRT_CFG_DEFAULT_SETTING,
  79. # the exp_disable_tensorrt_ops() func don't support to be pass argument by keyword
  80. # therefore, using list instead of dict
  81. "exp_disable_tensorrt_ops": [
  82. [
  83. "linear_0.tmp_0",
  84. "linear_4.tmp_0",
  85. "linear_12.tmp_0",
  86. "linear_16.tmp_0",
  87. "linear_24.tmp_0",
  88. "linear_28.tmp_0",
  89. "linear_36.tmp_0",
  90. "linear_40.tmp_0",
  91. ]
  92. ],
  93. },
  94. "SLANeXt_wireless": {
  95. "enable_tensorrt_engine": OLD_IR_TRT_CFG_DEFAULT_SETTING,
  96. "exp_disable_tensorrt_ops": [
  97. [
  98. "linear_0.tmp_0",
  99. "linear_4.tmp_0",
  100. "linear_12.tmp_0",
  101. "linear_16.tmp_0",
  102. "linear_24.tmp_0",
  103. "linear_28.tmp_0",
  104. "linear_36.tmp_0",
  105. "linear_40.tmp_0",
  106. ]
  107. ],
  108. },
  109. "PP-YOLOE_seg-S": {
  110. "enable_tensorrt_engine": OLD_IR_TRT_CFG_DEFAULT_SETTING,
  111. "exp_disable_tensorrt_ops": [
  112. ["bilinear_interp_v2_1.tmp_0", "bilinear_interp_v2_1.tmp_0_slice_0"]
  113. ],
  114. },
  115. }
  116. ############ pir trt ############
  117. PIR_TRT_PRECISION_MAP = PIR_TRT_PRECISION_MAP_CLASS()
  118. PIR_TRT_CFG_SETTING = {
  119. "DETR-R50": {"optimization_level": 4, "workspace_size": 1 << 32},
  120. "SegFormer-B0": {"optimization_level": 4, "workspace_size": 1 << 32},
  121. "SegFormer-B1": {"optimization_level": 4, "workspace_size": 1 << 32},
  122. "SegFormer-B2": {"optimization_level": 4, "workspace_size": 1 << 32},
  123. "SegFormer-B3": {"optimization_level": 4, "workspace_size": 1 << 32},
  124. "SegFormer-B4": {"optimization_level": 4, "workspace_size": 1 << 32},
  125. "SegFormer-B5": {"optimization_level": 4, "workspace_size": 1 << 32},
  126. "LaTeX_OCR_rec": {"disable_ops": ["pd_op.slice"]},
  127. "PP-YOLOE_seg-S": {"disable_ops": ["pd_op.slice", "pd_op.bilinear_interp"]},
  128. "PP-FormulaNet-L": {
  129. "disable_ops": ["pd_op.full_with_tensor"],
  130. "workspace_size": 1 << 32,
  131. },
  132. "PP-FormulaNet-S": {
  133. "disable_ops": ["pd_op.full_with_tensor"],
  134. "workspace_size": 1 << 32,
  135. },
  136. }
  137. if USE_PIR_TRT:
  138. TRT_PRECISION_MAP = PIR_TRT_PRECISION_MAP
  139. TRT_CFG_SETTING = defaultdict(dict, PIR_TRT_CFG_SETTING)
  140. else:
  141. TRT_PRECISION_MAP = OLD_IR_TRT_PRECISION_MAP
  142. TRT_CFG_SETTING = defaultdict(
  143. lambda: {"enable_tensorrt_engine": OLD_IR_TRT_CFG_DEFAULT_SETTING},
  144. OLD_IR_TRT_CFG_SETTING,
  145. )