trt_config.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_DEFAULT_CFG = {
  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 = {
  59. "SegFormer-B3": {
  60. "enable_tensorrt_engine": {**OLD_IR_TRT_DEFAULT_CFG, "workspace_size": 1 << 31}
  61. },
  62. "SegFormer-B4": {
  63. "enable_tensorrt_engine": {**OLD_IR_TRT_DEFAULT_CFG, "workspace_size": 1 << 31}
  64. },
  65. "SegFormer-B5": {
  66. "enable_tensorrt_engine": {**OLD_IR_TRT_DEFAULT_CFG, "workspace_size": 1 << 31}
  67. },
  68. "SLANeXt_wired": {
  69. "enable_tensorrt_engine": OLD_IR_TRT_DEFAULT_CFG,
  70. "exp_disable_tensorrt_ops": {
  71. "ops": [
  72. "linear_0.tmp_0",
  73. "linear_4.tmp_0",
  74. "linear_12.tmp_0",
  75. "linear_16.tmp_0",
  76. "linear_24.tmp_0",
  77. "linear_28.tmp_0",
  78. "linear_36.tmp_0",
  79. "linear_40.tmp_0",
  80. ]
  81. },
  82. },
  83. "SLANeXt_wireless": {
  84. "enable_tensorrt_engine": OLD_IR_TRT_DEFAULT_CFG,
  85. "exp_disable_tensorrt_ops": {
  86. "ops": [
  87. "linear_0.tmp_0",
  88. "linear_4.tmp_0",
  89. "linear_12.tmp_0",
  90. "linear_16.tmp_0",
  91. "linear_24.tmp_0",
  92. "linear_28.tmp_0",
  93. "linear_36.tmp_0",
  94. "linear_40.tmp_0",
  95. ]
  96. },
  97. },
  98. "PP-YOLOE_seg-S": {
  99. "enable_tensorrt_engine": OLD_IR_TRT_DEFAULT_CFG,
  100. "exp_disable_tensorrt_ops": {
  101. "ops": ["bilinear_interp_v2_1.tmp_0", "bilinear_interp_v2_1.tmp_0_slice_0"]
  102. },
  103. },
  104. }
  105. ############ pir trt ############
  106. PIR_TRT_PRECISION_MAP = PIR_TRT_PRECISION_MAP_CLASS()
  107. PIR_TRT_CFG = {
  108. "DETR-R50": {"optimization_level": 4, "workspace_size": 1 << 32},
  109. "SegFormer-B0": {"optimization_level": 4, "workspace_size": 1 << 32},
  110. "SegFormer-B1": {"optimization_level": 4, "workspace_size": 1 << 32},
  111. "SegFormer-B2": {"optimization_level": 4, "workspace_size": 1 << 32},
  112. "SegFormer-B3": {"optimization_level": 4, "workspace_size": 1 << 32},
  113. "SegFormer-B4": {"optimization_level": 4, "workspace_size": 1 << 32},
  114. "SegFormer-B5": {"optimization_level": 4, "workspace_size": 1 << 32},
  115. "LaTeX_OCR_rec": {"disable_ops": ["pd_op.slice"]},
  116. "PP-YOLOE_seg-S": {"disable_ops": ["pd_op.slice", "pd_op.bilinear_interp"]},
  117. "PP-FormulaNet-L": {
  118. "disable_ops": ["pd_op.full_with_tensor"],
  119. "workspace_size": 1 << 32,
  120. },
  121. "PP-FormulaNet-S": {
  122. "disable_ops": ["pd_op.full_with_tensor"],
  123. "workspace_size": 1 << 32,
  124. },
  125. }
  126. if USE_PIR_TRT:
  127. TRT_PRECISION_MAP = PIR_TRT_PRECISION_MAP
  128. TRT_CFG = defaultdict(dict, PIR_TRT_CFG)
  129. else:
  130. TRT_PRECISION_MAP = OLD_IR_TRT_PRECISION_MAP
  131. TRT_CFG = defaultdict(
  132. lambda: {"enable_tensorrt_engine": OLD_IR_TRT_DEFAULT_CFG}, OLD_IR_TRT_CFG
  133. )