basic_predictor.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 typing import Dict, Any, Iterator
  15. from abc import abstractmethod
  16. from .....utils.subclass_register import AutoRegisterABCMetaClass
  17. from .....utils.flags import (
  18. INFER_BENCHMARK,
  19. INFER_BENCHMARK_WARMUP,
  20. )
  21. from .....utils import logging
  22. from ....utils.pp_option import PaddlePredictorOption
  23. from ....utils.benchmark import benchmark
  24. from .base_predictor import BasePredictor
  25. class BasicPredictor(
  26. BasePredictor,
  27. metaclass=AutoRegisterABCMetaClass,
  28. ):
  29. """BasicPredictor."""
  30. __is_base = True
  31. def __init__(
  32. self,
  33. model_dir: str,
  34. config: Dict[str, Any] = None,
  35. device: str = None,
  36. pp_option: PaddlePredictorOption = None,
  37. ) -> None:
  38. """Initializes the BasicPredictor.
  39. Args:
  40. model_dir (str): The directory where the model files are stored.
  41. config (Dict[str, Any], optional): The configuration dictionary. Defaults to None.
  42. device (str, optional): The device to run the inference engine on. Defaults to None.
  43. pp_option (PaddlePredictorOption, optional): The inference engine options. Defaults to None.
  44. """
  45. super().__init__(model_dir=model_dir, config=config)
  46. if not pp_option:
  47. pp_option = PaddlePredictorOption(model_name=self.model_name)
  48. if device:
  49. pp_option.device = device
  50. trt_dynamic_shapes = (
  51. self.config.get("Hpi", {})
  52. .get("backend_configs", {})
  53. .get("paddle_infer", {})
  54. .get("trt_dynamic_shapes", None)
  55. )
  56. if trt_dynamic_shapes:
  57. pp_option.trt_dynamic_shapes = trt_dynamic_shapes
  58. self.pp_option = pp_option
  59. logging.debug(f"{self.__class__.__name__}: {self.model_dir}")
  60. self.benchmark = benchmark
  61. def __call__(
  62. self,
  63. input: Any,
  64. batch_size: int = None,
  65. device: str = None,
  66. pp_option: PaddlePredictorOption = None,
  67. **kwargs: Dict[str, Any],
  68. ) -> Iterator[Any]:
  69. """
  70. Predict with the input data.
  71. Args:
  72. input (Any): The input data to be predicted.
  73. batch_size (int, optional): The batch size to use. Defaults to None.
  74. device (str, optional): The device to run the predictor on. Defaults to None.
  75. pp_option (PaddlePredictorOption, optional): The predictor options to set. Defaults to None.
  76. **kwargs (Dict[str, Any]): Additional keyword arguments to set up predictor.
  77. Returns:
  78. Iterator[Any]: An iterator yielding the prediction output.
  79. """
  80. self.set_predictor(batch_size, device, pp_option)
  81. if self.benchmark:
  82. self.benchmark.start()
  83. if INFER_BENCHMARK_WARMUP > 0:
  84. output = self.apply(input, **kwargs)
  85. warmup_num = 0
  86. for _ in range(INFER_BENCHMARK_WARMUP):
  87. try:
  88. next(output)
  89. warmup_num += 1
  90. except StopIteration:
  91. logging.warning(
  92. f"There are only {warmup_num} batches in input data, but `INFER_BENCHMARK_WARMUP` has been set to {INFER_BENCHMARK_WARMUP}."
  93. )
  94. break
  95. self.benchmark.warmup_stop(warmup_num)
  96. output = list(self.apply(input, **kwargs))
  97. self.benchmark.collect(len(output))
  98. else:
  99. yield from self.apply(input, **kwargs)
  100. def set_predictor(
  101. self,
  102. batch_size: int = None,
  103. device: str = None,
  104. pp_option: PaddlePredictorOption = None,
  105. ) -> None:
  106. """
  107. Sets the predictor configuration.
  108. Args:
  109. batch_size (int, optional): The batch size to use. Defaults to None.
  110. device (str, optional): The device to run the predictor on. Defaults to None.
  111. pp_option (PaddlePredictorOption, optional): The predictor options to set. Defaults to None.
  112. Returns:
  113. None
  114. """
  115. if batch_size:
  116. self.batch_sampler.batch_size = batch_size
  117. self.pp_option.batch_size = batch_size
  118. if device and device != self.pp_option.device:
  119. self.pp_option.device = device
  120. if pp_option and pp_option != self.pp_option:
  121. self.pp_option = pp_option