static_infer.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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 abc
  15. import subprocess
  16. from os import PathLike
  17. from pathlib import Path
  18. from typing import List, Sequence, Union
  19. import numpy as np
  20. from ....utils import logging
  21. from ....utils.deps import class_requires_deps
  22. from ....utils.device import check_supported_device_type
  23. from ....utils.flags import (
  24. DEBUG,
  25. DISABLE_MKLDNN_MODEL_BL,
  26. DISABLE_TRT_MODEL_BL,
  27. USE_PIR_TRT,
  28. )
  29. from ...utils.benchmark import benchmark, set_inference_operations
  30. from ...utils.hpi import (
  31. HPIConfig,
  32. OMConfig,
  33. ONNXRuntimeConfig,
  34. OpenVINOConfig,
  35. TensorRTConfig,
  36. suggest_inference_backend_and_config,
  37. )
  38. from ...utils.mkldnn_blocklist import MKLDNN_BLOCKLIST
  39. from ...utils.model_paths import get_model_paths
  40. from ...utils.pp_option import PaddlePredictorOption, get_default_run_mode
  41. from ...utils.trt_blocklist import TRT_BLOCKLIST
  42. from ...utils.trt_config import DISABLE_TRT_HALF_OPS_CONFIG
  43. CACHE_DIR = ".cache"
  44. INFERENCE_OPERATIONS = [
  45. "PaddleInferChainLegacy",
  46. "MultiBackendInfer",
  47. ]
  48. set_inference_operations(INFERENCE_OPERATIONS)
  49. # XXX: Better use Paddle Inference API to do this
  50. def _pd_dtype_to_np_dtype(pd_dtype):
  51. import paddle
  52. if pd_dtype == paddle.inference.DataType.FLOAT64:
  53. return np.float64
  54. elif pd_dtype == paddle.inference.DataType.FLOAT32:
  55. return np.float32
  56. elif pd_dtype == paddle.inference.DataType.INT64:
  57. return np.int64
  58. elif pd_dtype == paddle.inference.DataType.INT32:
  59. return np.int32
  60. elif pd_dtype == paddle.inference.DataType.UINT8:
  61. return np.uint8
  62. elif pd_dtype == paddle.inference.DataType.INT8:
  63. return np.int8
  64. else:
  65. raise TypeError(f"Unsupported data type: {pd_dtype}")
  66. # old trt
  67. def _collect_trt_shape_range_info(
  68. model_file,
  69. model_params,
  70. gpu_id,
  71. shape_range_info_path,
  72. dynamic_shapes,
  73. dynamic_shape_input_data,
  74. ):
  75. import paddle.inference
  76. dynamic_shape_input_data = dynamic_shape_input_data or {}
  77. config = paddle.inference.Config(model_file, model_params)
  78. config.enable_use_gpu(100, gpu_id)
  79. config.collect_shape_range_info(shape_range_info_path)
  80. # TODO: Add other needed options
  81. config.disable_glog_info()
  82. predictor = paddle.inference.create_predictor(config)
  83. input_names = predictor.get_input_names()
  84. for name in dynamic_shapes:
  85. if name not in input_names:
  86. raise ValueError(
  87. f"Invalid input name {repr(name)} found in `dynamic_shapes`"
  88. )
  89. for name in input_names:
  90. if name not in dynamic_shapes:
  91. raise ValueError(f"Input name {repr(name)} not found in `dynamic_shapes`")
  92. for name in dynamic_shape_input_data:
  93. if name not in input_names:
  94. raise ValueError(
  95. f"Invalid input name {repr(name)} found in `dynamic_shape_input_data`"
  96. )
  97. # It would be better to check if the shapes are valid.
  98. min_arrs, opt_arrs, max_arrs = {}, {}, {}
  99. for name, candidate_shapes in dynamic_shapes.items():
  100. # XXX: Currently we have no way to get the data type of the tensor
  101. # without creating an input handle.
  102. handle = predictor.get_input_handle(name)
  103. dtype = _pd_dtype_to_np_dtype(handle.type())
  104. min_shape, opt_shape, max_shape = candidate_shapes
  105. if name in dynamic_shape_input_data:
  106. min_arrs[name] = np.array(
  107. dynamic_shape_input_data[name][0], dtype=dtype
  108. ).reshape(min_shape)
  109. opt_arrs[name] = np.array(
  110. dynamic_shape_input_data[name][1], dtype=dtype
  111. ).reshape(opt_shape)
  112. max_arrs[name] = np.array(
  113. dynamic_shape_input_data[name][2], dtype=dtype
  114. ).reshape(max_shape)
  115. else:
  116. min_arrs[name] = np.ones(min_shape, dtype=dtype)
  117. opt_arrs[name] = np.ones(opt_shape, dtype=dtype)
  118. max_arrs[name] = np.ones(max_shape, dtype=dtype)
  119. # `opt_arrs` is used twice to ensure it is the most frequently used.
  120. for arrs in [min_arrs, opt_arrs, opt_arrs, max_arrs]:
  121. for name, arr in arrs.items():
  122. handle = predictor.get_input_handle(name)
  123. handle.reshape(arr.shape)
  124. handle.copy_from_cpu(arr)
  125. predictor.run()
  126. # HACK: The shape range info will be written to the file only when
  127. # `predictor` is garbage collected. It works in CPython, but it is
  128. # definitely a bad idea to count on the implementation-dependent behavior of
  129. # a garbage collector. Is there a more explicit and deterministic way to
  130. # handle this?
  131. # HACK: Manually delete the predictor to trigger its destructor, ensuring that the shape_range_info file would be saved.
  132. del predictor
  133. # pir trt
  134. def _convert_trt(
  135. trt_cfg_setting,
  136. pp_model_file,
  137. pp_params_file,
  138. trt_save_path,
  139. device_id,
  140. dynamic_shapes,
  141. dynamic_shape_input_data,
  142. ):
  143. import paddle.inference
  144. from paddle.tensorrt.export import Input, TensorRTConfig, convert
  145. def _set_trt_config():
  146. for attr_name in trt_cfg_setting:
  147. assert hasattr(
  148. trt_config, attr_name
  149. ), f"The `{type(trt_config)}` don't have the attribute `{attr_name}`!"
  150. setattr(trt_config, attr_name, trt_cfg_setting[attr_name])
  151. def _get_predictor(model_file, params_file):
  152. # HACK
  153. config = paddle.inference.Config(str(model_file), str(params_file))
  154. config.enable_use_gpu(100, device_id)
  155. # NOTE: Disable oneDNN to circumvent a bug in Paddle Inference
  156. config.disable_mkldnn()
  157. config.disable_glog_info()
  158. return paddle.inference.create_predictor(config)
  159. dynamic_shape_input_data = dynamic_shape_input_data or {}
  160. predictor = _get_predictor(pp_model_file, pp_params_file)
  161. input_names = predictor.get_input_names()
  162. for name in dynamic_shapes:
  163. if name not in input_names:
  164. raise ValueError(
  165. f"Invalid input name {repr(name)} found in `dynamic_shapes`"
  166. )
  167. for name in input_names:
  168. if name not in dynamic_shapes:
  169. raise ValueError(f"Input name {repr(name)} not found in `dynamic_shapes`")
  170. for name in dynamic_shape_input_data:
  171. if name not in input_names:
  172. raise ValueError(
  173. f"Invalid input name {repr(name)} found in `dynamic_shape_input_data`"
  174. )
  175. trt_inputs = []
  176. for name, candidate_shapes in dynamic_shapes.items():
  177. # XXX: Currently we have no way to get the data type of the tensor
  178. # without creating an input handle.
  179. handle = predictor.get_input_handle(name)
  180. dtype = _pd_dtype_to_np_dtype(handle.type())
  181. min_shape, opt_shape, max_shape = candidate_shapes
  182. if name in dynamic_shape_input_data:
  183. min_arr = np.array(dynamic_shape_input_data[name][0], dtype=dtype).reshape(
  184. min_shape
  185. )
  186. opt_arr = np.array(dynamic_shape_input_data[name][1], dtype=dtype).reshape(
  187. opt_shape
  188. )
  189. max_arr = np.array(dynamic_shape_input_data[name][2], dtype=dtype).reshape(
  190. max_shape
  191. )
  192. else:
  193. min_arr = np.ones(min_shape, dtype=dtype)
  194. opt_arr = np.ones(opt_shape, dtype=dtype)
  195. max_arr = np.ones(max_shape, dtype=dtype)
  196. # refer to: https://github.com/PolaKuma/Paddle/blob/3347f225bc09f2ec09802a2090432dd5cb5b6739/test/tensorrt/test_converter_model_resnet50.py
  197. trt_input = Input((min_arr, opt_arr, max_arr))
  198. trt_inputs.append(trt_input)
  199. # Create TensorRTConfig
  200. trt_config = TensorRTConfig(inputs=trt_inputs)
  201. _set_trt_config()
  202. trt_config.save_model_dir = str(trt_save_path)
  203. pp_model_path = str(pp_model_file.with_suffix(""))
  204. convert(pp_model_path, trt_config)
  205. def _sort_inputs(inputs, names):
  206. # NOTE: Adjust input tensors to match the sorted sequence.
  207. indices = sorted(range(len(names)), key=names.__getitem__)
  208. inputs = [inputs[indices.index(i)] for i in range(len(inputs))]
  209. return inputs
  210. # FIXME: Name might be misleading
  211. @benchmark.timeit
  212. class PaddleInferChainLegacy:
  213. def __init__(self, predictor):
  214. self.predictor = predictor
  215. input_names = self.predictor.get_input_names()
  216. self.input_handles = []
  217. self.output_handles = []
  218. for input_name in input_names:
  219. input_handle = self.predictor.get_input_handle(input_name)
  220. self.input_handles.append(input_handle)
  221. output_names = self.predictor.get_output_names()
  222. for output_name in output_names:
  223. output_handle = self.predictor.get_output_handle(output_name)
  224. self.output_handles.append(output_handle)
  225. def __call__(self, x):
  226. for input_, input_handle in zip(x, self.input_handles):
  227. input_handle.reshape(input_.shape)
  228. input_handle.copy_from_cpu(input_)
  229. self.predictor.run()
  230. outputs = [o.copy_to_cpu() for o in self.output_handles]
  231. return outputs
  232. class StaticInfer(metaclass=abc.ABCMeta):
  233. @abc.abstractmethod
  234. def __call__(self, x: Sequence[np.ndarray]) -> List[np.ndarray]:
  235. raise NotImplementedError
  236. class PaddleInfer(StaticInfer):
  237. def __init__(
  238. self,
  239. model_name: str,
  240. model_dir: Union[str, PathLike],
  241. model_file_prefix: str,
  242. option: PaddlePredictorOption,
  243. ) -> None:
  244. super().__init__()
  245. self._model_name = model_name
  246. self.model_dir = Path(model_dir)
  247. self.model_file_prefix = model_file_prefix
  248. self._option = option
  249. self.predictor = self._create()
  250. self.infer = PaddleInferChainLegacy(self.predictor)
  251. def __call__(self, x: Sequence[np.ndarray]) -> List[np.ndarray]:
  252. names = self.predictor.get_input_names()
  253. if len(names) != len(x):
  254. raise ValueError(
  255. f"The number of inputs does not match the model: {len(names)} vs {len(x)}"
  256. )
  257. # TODO:
  258. # Ensure that input tensors follow the model's input sequence without sorting.
  259. x = _sort_inputs(x, names)
  260. x = list(map(np.ascontiguousarray, x))
  261. pred = self.infer(x)
  262. return pred
  263. def _check_run_mode(self):
  264. # TODO: Check if trt is available
  265. # check avaliable for trt
  266. if (
  267. not DISABLE_TRT_MODEL_BL
  268. and self._option.run_mode.startswith("trt")
  269. and self._model_name in TRT_BLOCKLIST
  270. and self._option.device_type == "gpu"
  271. ):
  272. logging.warning(
  273. f"The model({self._model_name}) is not supported to run in trt mode! Using `paddle` instead!"
  274. )
  275. self._option.run_mode = "paddle"
  276. # check avaliable for mkldnn
  277. elif (
  278. not DISABLE_MKLDNN_MODEL_BL
  279. and self._option.run_mode.startswith("mkldnn")
  280. and self._model_name in MKLDNN_BLOCKLIST
  281. and self._option.device_type == "cpu"
  282. ):
  283. logging.warning(
  284. f"The model({self._model_name}) is not supported to run in MKLDNN mode! Using `paddle` instead!"
  285. )
  286. self._option.run_mode = "paddle"
  287. return "paddle"
  288. # check avaliable for model
  289. if self._model_name == "LaTeX_OCR_rec" and self._option.device_type == "cpu":
  290. import cpuinfo
  291. if (
  292. "GenuineIntel" in cpuinfo.get_cpu_info().get("vendor_id_raw", "")
  293. and self._option.run_mode != "mkldnn"
  294. ):
  295. logging.warning(
  296. "Now, the `LaTeX_OCR_rec` model only support `mkldnn` mode when running on Intel CPU devices. So using `mkldnn` instead."
  297. )
  298. self._option.run_mode = "mkldnn"
  299. def _create(
  300. self,
  301. ):
  302. """_create"""
  303. import paddle
  304. import paddle.inference
  305. model_paths = get_model_paths(self.model_dir, self.model_file_prefix)
  306. if "paddle" not in model_paths:
  307. raise RuntimeError("No valid PaddlePaddle model found")
  308. check_supported_device_type(self._option.device_type, self._model_name)
  309. self._check_run_mode()
  310. model_file, params_file = model_paths["paddle"]
  311. if self._option.device_type == "cpu" and self._option.device_id is not None:
  312. self._option.device_id = None
  313. logging.debug("`device_id` has been set to None")
  314. if (
  315. self._option.device_type in ("gpu", "dcu", "npu", "mlu", "gcu", "xpu")
  316. and self._option.device_id is None
  317. ):
  318. self._option.device_id = 0
  319. logging.debug("`device_id` has been set to 0")
  320. # for TRT
  321. if self._option.run_mode.startswith("trt"):
  322. assert self._option.device_type.lower() == "gpu", (
  323. f"`{self._option.run_mode}` is only available on GPU devices, "
  324. f"but got device_type='{self._option.device_type}'."
  325. )
  326. cache_dir = self.model_dir / CACHE_DIR / "paddle"
  327. config = self._configure_trt(
  328. model_file,
  329. params_file,
  330. cache_dir,
  331. )
  332. config.exp_disable_mixed_precision_ops({"feed", "fetch"})
  333. config.enable_use_gpu(100, self._option.device_id)
  334. # for Native Paddle and MKLDNN
  335. else:
  336. config = paddle.inference.Config(str(model_file), str(params_file))
  337. if self._option.device_type == "gpu":
  338. config.exp_disable_mixed_precision_ops({"feed", "fetch"})
  339. from paddle.inference import PrecisionType
  340. precision = (
  341. PrecisionType.Half
  342. if self._option.run_mode == "paddle_fp16"
  343. else PrecisionType.Float32
  344. )
  345. config.disable_mkldnn()
  346. config.enable_use_gpu(100, self._option.device_id, precision)
  347. if hasattr(config, "enable_new_ir"):
  348. config.enable_new_ir(self._option.enable_new_ir)
  349. if self._option.enable_new_ir and self._option.enable_cinn:
  350. config.enable_cinn()
  351. if hasattr(config, "enable_new_executor"):
  352. config.enable_new_executor()
  353. config.set_optimization_level(3)
  354. elif self._option.device_type == "npu":
  355. config.enable_custom_device("npu", self._option.device_id)
  356. if hasattr(config, "enable_new_ir"):
  357. config.enable_new_ir(self._option.enable_new_ir)
  358. if hasattr(config, "enable_new_executor"):
  359. config.enable_new_executor()
  360. elif self._option.device_type == "xpu":
  361. config.enable_xpu()
  362. config.set_xpu_device_id(self._option.device_id)
  363. if hasattr(config, "enable_new_ir"):
  364. config.enable_new_ir(self._option.enable_new_ir)
  365. if hasattr(config, "enable_new_executor"):
  366. config.enable_new_executor()
  367. config.delete_pass("conv2d_bn_xpu_fuse_pass")
  368. config.delete_pass("transfer_layout_pass")
  369. elif self._option.device_type == "mlu":
  370. config.enable_custom_device("mlu", self._option.device_id)
  371. if hasattr(config, "enable_new_ir"):
  372. config.enable_new_ir(self._option.enable_new_ir)
  373. if hasattr(config, "enable_new_executor"):
  374. config.enable_new_executor()
  375. elif self._option.device_type == "gcu":
  376. from paddle_custom_device.gcu import passes as gcu_passes
  377. gcu_passes.setUp()
  378. config.enable_custom_device("gcu", self._option.device_id)
  379. if hasattr(config, "enable_new_ir"):
  380. config.enable_new_ir()
  381. if hasattr(config, "enable_new_executor"):
  382. config.enable_new_executor()
  383. else:
  384. pass_builder = config.pass_builder()
  385. name = "PaddleX_" + self._option.model_name
  386. gcu_passes.append_passes_for_legacy_ir(pass_builder, name)
  387. elif self._option.device_type == "dcu":
  388. if hasattr(config, "enable_new_ir"):
  389. config.enable_new_ir(self._option.enable_new_ir)
  390. config.enable_use_gpu(100, self._option.device_id)
  391. config.disable_mkldnn()
  392. if hasattr(config, "enable_new_executor"):
  393. config.enable_new_executor()
  394. # XXX: is_compiled_with_rocm() must be True on dcu platform ?
  395. if paddle.is_compiled_with_rocm():
  396. # Delete unsupported passes in dcu
  397. config.delete_pass("conv2d_add_act_fuse_pass")
  398. config.delete_pass("conv2d_add_fuse_pass")
  399. elif self._option.device_type == "iluvatar_gpu":
  400. config.enable_custom_device("iluvatar_gpu", int(self._option.device_id))
  401. if hasattr(config, "enable_new_ir"):
  402. config.enable_new_ir(self._option.enable_new_ir)
  403. if hasattr(config, "enable_new_executor"):
  404. config.enable_new_executor()
  405. else:
  406. assert self._option.device_type == "cpu"
  407. config.disable_gpu()
  408. if "mkldnn" in self._option.run_mode:
  409. config.enable_mkldnn()
  410. if "bf16" in self._option.run_mode:
  411. config.enable_mkldnn_bfloat16()
  412. config.set_mkldnn_cache_capacity(self._option.mkldnn_cache_capacity)
  413. else:
  414. if hasattr(config, "disable_mkldnn"):
  415. config.disable_mkldnn()
  416. config.set_cpu_math_library_num_threads(self._option.cpu_threads)
  417. if hasattr(config, "enable_new_ir"):
  418. config.enable_new_ir(self._option.enable_new_ir)
  419. if hasattr(config, "enable_new_executor"):
  420. config.enable_new_executor()
  421. config.set_optimization_level(3)
  422. config.enable_memory_optim()
  423. for del_p in self._option.delete_pass:
  424. config.delete_pass(del_p)
  425. # Disable paddle inference logging
  426. if not DEBUG:
  427. config.disable_glog_info()
  428. predictor = paddle.inference.create_predictor(config)
  429. return predictor
  430. def _configure_trt(self, model_file, params_file, cache_dir):
  431. # TODO: Support calibration
  432. import paddle.inference
  433. if USE_PIR_TRT:
  434. if self._option.trt_dynamic_shapes is None:
  435. raise RuntimeError("No dynamic shape information provided")
  436. trt_save_path = cache_dir / "trt" / self.model_file_prefix
  437. trt_model_file = trt_save_path.with_suffix(".json")
  438. trt_params_file = trt_save_path.with_suffix(".pdiparams")
  439. if not trt_model_file.exists() or not trt_params_file.exists():
  440. _convert_trt(
  441. self._option.trt_cfg_setting,
  442. model_file,
  443. params_file,
  444. trt_save_path,
  445. self._option.device_id,
  446. self._option.trt_dynamic_shapes,
  447. self._option.trt_dynamic_shape_input_data,
  448. )
  449. else:
  450. logging.debug(
  451. f"Use TRT cache files(`{trt_model_file}` and `{trt_params_file}`)."
  452. )
  453. config = paddle.inference.Config(str(trt_model_file), str(trt_params_file))
  454. else:
  455. config = paddle.inference.Config(str(model_file), str(params_file))
  456. config.set_optim_cache_dir(str(cache_dir / "optim_cache"))
  457. # call enable_use_gpu() first to use TensorRT engine
  458. config.enable_use_gpu(100, self._option.device_id)
  459. for func_name in self._option.trt_cfg_setting:
  460. assert hasattr(
  461. config, func_name
  462. ), f"The `{type(config)}` don't have function `{func_name}`!"
  463. args = self._option.trt_cfg_setting[func_name]
  464. if isinstance(args, list):
  465. getattr(config, func_name)(*args)
  466. else:
  467. getattr(config, func_name)(**args)
  468. if self._option.trt_use_dynamic_shapes:
  469. if self._option.trt_dynamic_shapes is None:
  470. raise RuntimeError("No dynamic shape information provided")
  471. if self._option.trt_collect_shape_range_info:
  472. # NOTE: We always use a shape range info file.
  473. if self._option.trt_shape_range_info_path is not None:
  474. trt_shape_range_info_path = Path(
  475. self._option.trt_shape_range_info_path
  476. )
  477. else:
  478. trt_shape_range_info_path = cache_dir / "shape_range_info.pbtxt"
  479. should_collect_shape_range_info = True
  480. if not trt_shape_range_info_path.exists():
  481. trt_shape_range_info_path.parent.mkdir(
  482. parents=True, exist_ok=True
  483. )
  484. logging.info(
  485. f"Shape range info will be collected into {trt_shape_range_info_path}"
  486. )
  487. elif self._option.trt_discard_cached_shape_range_info:
  488. trt_shape_range_info_path.unlink()
  489. logging.info(
  490. f"The shape range info file ({trt_shape_range_info_path}) has been removed, and the shape range info will be re-collected."
  491. )
  492. else:
  493. logging.info(
  494. f"A shape range info file ({trt_shape_range_info_path}) already exists. There is no need to collect the info again."
  495. )
  496. should_collect_shape_range_info = False
  497. if should_collect_shape_range_info:
  498. _collect_trt_shape_range_info(
  499. str(model_file),
  500. str(params_file),
  501. self._option.device_id,
  502. str(trt_shape_range_info_path),
  503. self._option.trt_dynamic_shapes,
  504. self._option.trt_dynamic_shape_input_data,
  505. )
  506. if (
  507. self._option.model_name in DISABLE_TRT_HALF_OPS_CONFIG
  508. and self._option.run_mode == "trt_fp16"
  509. ):
  510. paddle.inference.InternalUtils.disable_tensorrt_half_ops(
  511. config, DISABLE_TRT_HALF_OPS_CONFIG[self._option.model_name]
  512. )
  513. config.enable_tuned_tensorrt_dynamic_shape(
  514. str(trt_shape_range_info_path),
  515. self._option.trt_allow_rebuild_at_runtime,
  516. )
  517. else:
  518. min_shapes, opt_shapes, max_shapes = {}, {}, {}
  519. for (
  520. key,
  521. shapes,
  522. ) in self._option.trt_dynamic_shapes.items():
  523. min_shapes[key] = shapes[0]
  524. opt_shapes[key] = shapes[1]
  525. max_shapes[key] = shapes[2]
  526. config.set_trt_dynamic_shape_info(
  527. min_shapes, max_shapes, opt_shapes
  528. )
  529. return config
  530. # FIXME: Name might be misleading
  531. @benchmark.timeit
  532. @class_requires_deps("ultra-infer")
  533. class MultiBackendInfer(object):
  534. def __init__(self, ui_runtime):
  535. super().__init__()
  536. self.ui_runtime = ui_runtime
  537. # The time consumed by the wrapper code will also be taken into account.
  538. def __call__(self, x):
  539. outputs = self.ui_runtime.infer(x)
  540. return outputs
  541. # TODO: It would be better to refactor the code to make `HPInfer` a higher-level
  542. # class that uses `PaddleInfer`.
  543. @class_requires_deps("ultra-infer")
  544. class HPInfer(StaticInfer):
  545. def __init__(
  546. self,
  547. model_dir: Union[str, PathLike],
  548. model_file_prefix: str,
  549. config: HPIConfig,
  550. ) -> None:
  551. super().__init__()
  552. self._model_dir = Path(model_dir)
  553. self._model_file_prefix = model_file_prefix
  554. self._config = config
  555. backend, backend_config = self._determine_backend_and_config()
  556. if backend == "paddle":
  557. self._use_paddle = True
  558. self._paddle_infer = self._build_paddle_infer(backend_config)
  559. else:
  560. self._use_paddle = False
  561. ui_runtime = self._build_ui_runtime(backend, backend_config)
  562. self._multi_backend_infer = MultiBackendInfer(ui_runtime)
  563. num_inputs = ui_runtime.num_inputs()
  564. self._input_names = [
  565. ui_runtime.get_input_info(i).name for i in range(num_inputs)
  566. ]
  567. @property
  568. def model_dir(self) -> Path:
  569. return self._model_dir
  570. @property
  571. def model_file_prefix(self) -> str:
  572. return self._model_file_prefix
  573. @property
  574. def config(self) -> HPIConfig:
  575. return self._config
  576. def __call__(self, x: Sequence[np.ndarray]) -> List[np.ndarray]:
  577. if self._use_paddle:
  578. return self._call_paddle_infer(x)
  579. else:
  580. return self._call_multi_backend_infer(x)
  581. def _call_paddle_infer(self, x):
  582. return self._paddle_infer(x)
  583. def _call_multi_backend_infer(self, x):
  584. num_inputs = len(self._input_names)
  585. if len(x) != num_inputs:
  586. raise ValueError(f"Expected {num_inputs} inputs but got {len(x)} instead")
  587. x = _sort_inputs(x, self._input_names)
  588. inputs = {}
  589. for name, input_ in zip(self._input_names, x):
  590. inputs[name] = np.ascontiguousarray(input_)
  591. return self._multi_backend_infer(inputs)
  592. def _determine_backend_and_config(self):
  593. if self._config.auto_config:
  594. # Should we use the strategy pattern here to allow extensible
  595. # strategies?
  596. model_paths = get_model_paths(self._model_dir, self._model_file_prefix)
  597. ret = suggest_inference_backend_and_config(
  598. self._config,
  599. model_paths,
  600. )
  601. if ret[0] is None:
  602. # Should I use a custom exception?
  603. raise RuntimeError(
  604. f"No inference backend and configuration could be suggested. Reason: {ret[1]}"
  605. )
  606. backend, backend_config = ret
  607. else:
  608. backend = self._config.backend
  609. if backend is None:
  610. raise RuntimeError(
  611. "When automatic configuration is not used, the inference backend must be specified manually."
  612. )
  613. backend_config = self._config.backend_config or {}
  614. if backend == "paddle":
  615. if not backend_config:
  616. is_default_config = True
  617. elif backend_config.keys() != {"run_mode"}:
  618. is_default_config = False
  619. else:
  620. is_default_config = backend_config["run_mode"] == get_default_run_mode(
  621. self._config.pdx_model_name, self._config.device_type
  622. )
  623. if is_default_config:
  624. logging.warning(
  625. "The Paddle Inference backend is selected with the default configuration. This may not provide optimal performance."
  626. )
  627. return backend, backend_config
  628. def _build_paddle_infer(self, backend_config):
  629. kwargs = {
  630. "device_type": self._config.device_type,
  631. "device_id": self._config.device_id,
  632. **backend_config,
  633. }
  634. # TODO: This is probably redundant. Can we reuse the code in the
  635. # predictor class?
  636. paddle_info = None
  637. if self._config.hpi_info:
  638. hpi_info = self._config.hpi_info
  639. if hpi_info.backend_configs:
  640. paddle_info = hpi_info.backend_configs.paddle_infer
  641. if paddle_info is not None:
  642. if (
  643. kwargs.get("trt_dynamic_shapes") is None
  644. and paddle_info.trt_dynamic_shapes is not None
  645. ):
  646. trt_dynamic_shapes = paddle_info.trt_dynamic_shapes
  647. logging.debug("TensorRT dynamic shapes set to %s", trt_dynamic_shapes)
  648. kwargs["trt_dynamic_shapes"] = trt_dynamic_shapes
  649. if (
  650. kwargs.get("trt_dynamic_shape_input_data") is None
  651. and paddle_info.trt_dynamic_shape_input_data is not None
  652. ):
  653. trt_dynamic_shape_input_data = paddle_info.trt_dynamic_shape_input_data
  654. logging.debug(
  655. "TensorRT dynamic shape input data set to %s",
  656. trt_dynamic_shape_input_data,
  657. )
  658. kwargs["trt_dynamic_shape_input_data"] = trt_dynamic_shape_input_data
  659. pp_option = PaddlePredictorOption(**kwargs)
  660. pp_option.setdefault_by_model_name(model_name=self._config.pdx_model_name)
  661. logging.info("Using Paddle Inference backend")
  662. logging.info("Paddle predictor option: %s", pp_option)
  663. return PaddleInfer(
  664. self._config.pdx_model_name,
  665. self._model_dir,
  666. self._model_file_prefix,
  667. option=pp_option,
  668. )
  669. def _build_ui_runtime(self, backend, backend_config, ui_option=None):
  670. # TODO: Validate the compatibility of backends with device types
  671. from ultra_infer import ModelFormat, Runtime, RuntimeOption
  672. if ui_option is None:
  673. ui_option = RuntimeOption()
  674. if self._config.device_type == "cpu":
  675. pass
  676. elif self._config.device_type == "gpu":
  677. ui_option.use_gpu(self._config.device_id or 0)
  678. elif self._config.device_type == "npu":
  679. ui_option.use_ascend(self._config.device_id or 0)
  680. else:
  681. raise RuntimeError(
  682. f"Unsupported device type {repr(self._config.device_type)}"
  683. )
  684. model_paths = get_model_paths(self._model_dir, self.model_file_prefix)
  685. if backend in ("openvino", "onnxruntime", "tensorrt"):
  686. # XXX: This introduces side effects.
  687. if "onnx" not in model_paths:
  688. if self._config.auto_paddle2onnx:
  689. if "paddle" not in model_paths:
  690. raise RuntimeError("PaddlePaddle model required")
  691. # The CLI is used here since there is currently no API.
  692. logging.info(
  693. "Automatically converting PaddlePaddle model to ONNX format"
  694. )
  695. try:
  696. subprocess.run(
  697. [
  698. "paddlex",
  699. "--paddle2onnx",
  700. "--paddle_model_dir",
  701. str(self._model_dir),
  702. "--onnx_model_dir",
  703. str(self._model_dir),
  704. ],
  705. capture_output=True,
  706. check=True,
  707. text=True,
  708. )
  709. except subprocess.CalledProcessError as e:
  710. raise RuntimeError(
  711. f"PaddlePaddle-to-ONNX conversion failed:\n{e.stderr}"
  712. ) from e
  713. model_paths = get_model_paths(
  714. self._model_dir, self.model_file_prefix
  715. )
  716. assert "onnx" in model_paths
  717. else:
  718. raise RuntimeError("ONNX model required")
  719. ui_option.set_model_path(str(model_paths["onnx"]), "", ModelFormat.ONNX)
  720. elif backend == "om":
  721. if "om" not in model_paths:
  722. raise RuntimeError("OM model required")
  723. ui_option.set_model_path(str(model_paths["om"]), "", ModelFormat.OM)
  724. else:
  725. raise ValueError(f"Unsupported inference backend {repr(backend)}")
  726. if backend == "openvino":
  727. backend_config = OpenVINOConfig.model_validate(backend_config)
  728. ui_option.use_openvino_backend()
  729. ui_option.set_cpu_thread_num(backend_config.cpu_num_threads)
  730. elif backend == "onnxruntime":
  731. backend_config = ONNXRuntimeConfig.model_validate(backend_config)
  732. ui_option.use_ort_backend()
  733. ui_option.set_cpu_thread_num(backend_config.cpu_num_threads)
  734. elif backend == "tensorrt":
  735. if (
  736. backend_config.get("use_dynamic_shapes", True)
  737. and backend_config.get("dynamic_shapes") is None
  738. ):
  739. trt_info = None
  740. if self._config.hpi_info:
  741. hpi_info = self._config.hpi_info
  742. if hpi_info.backend_configs:
  743. trt_info = hpi_info.backend_configs.tensorrt
  744. if trt_info is not None and trt_info.dynamic_shapes is not None:
  745. trt_dynamic_shapes = trt_info.dynamic_shapes
  746. logging.debug(
  747. "TensorRT dynamic shapes set to %s", trt_dynamic_shapes
  748. )
  749. backend_config = {
  750. **backend_config,
  751. "dynamic_shapes": trt_dynamic_shapes,
  752. }
  753. backend_config = TensorRTConfig.model_validate(backend_config)
  754. ui_option.use_trt_backend()
  755. cache_dir = self._model_dir / CACHE_DIR / "tensorrt"
  756. cache_dir.mkdir(parents=True, exist_ok=True)
  757. ui_option.trt_option.serialize_file = str(cache_dir / "trt_serialized.trt")
  758. if backend_config.precision == "fp16":
  759. ui_option.trt_option.enable_fp16 = True
  760. if not backend_config.use_dynamic_shapes:
  761. raise RuntimeError(
  762. "TensorRT static shape inference is currently not supported"
  763. )
  764. if backend_config.dynamic_shapes is not None:
  765. if not Path(ui_option.trt_option.serialize_file).exists():
  766. for name, shapes in backend_config.dynamic_shapes.items():
  767. ui_option.trt_option.set_shape(name, *shapes)
  768. else:
  769. logging.info(
  770. "TensorRT dynamic shapes will be loaded from the file."
  771. )
  772. elif backend == "om":
  773. backend_config = OMConfig.model_validate(backend_config)
  774. ui_option.use_om_backend()
  775. else:
  776. raise ValueError(f"Unsupported inference backend {repr(backend)}")
  777. logging.info("Inference backend: %s", backend)
  778. logging.info("Inference backend config: %s", backend_config)
  779. ui_runtime = Runtime(ui_option)
  780. return ui_runtime