base_predictor.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. import yaml
  15. import codecs
  16. from pathlib import Path
  17. from abc import abstractmethod
  18. from ....utils.subclass_register import AutoRegisterABCMetaClass
  19. from ....utils.func_register import FuncRegister
  20. from ....utils import logging
  21. from ...utils.device import constr_device
  22. from ...components.base import BaseComponent, ComponentsEngine
  23. from ...utils.pp_option import PaddlePredictorOption
  24. from ...utils.process_hook import generatorable_method
  25. from ..utils.predict_set import DeviceSetMixin, PPOptionSetMixin
  26. class BasePredictor(BaseComponent):
  27. KEEP_INPUT = False
  28. YIELD_BATCH = False
  29. INPUT_KEYS = "x"
  30. DEAULT_INPUTS = {"x": "x"}
  31. OUTPUT_KEYS = "result"
  32. DEAULT_OUTPUTS = {"result": "result"}
  33. MODEL_FILE_PREFIX = "inference"
  34. def __init__(self, model_dir, config=None):
  35. super().__init__()
  36. self.model_dir = Path(model_dir)
  37. self.config = config if config else self.load_config(self.model_dir)
  38. self._pred_set_func_map = {}
  39. self._pred_set_register = FuncRegister(self._pred_set_func_map)
  40. # alias predict() to the __call__()
  41. self.predict = self.__call__
  42. def __call__(self, input, **kwargs):
  43. self._set_predict(**kwargs)
  44. for res in super().__call__(input):
  45. yield res["result"]
  46. @property
  47. def config_path(self):
  48. return self.get_config_path(self.model_dir)
  49. @property
  50. def model_name(self) -> str:
  51. return self.config["Global"]["model_name"]
  52. @abstractmethod
  53. def apply(self, x):
  54. raise NotImplementedError
  55. @classmethod
  56. def get_config_path(cls, model_dir):
  57. return model_dir / f"{cls.MODEL_FILE_PREFIX}.yml"
  58. @classmethod
  59. def load_config(cls, model_dir):
  60. config_path = cls.get_config_path(model_dir)
  61. with codecs.open(config_path, "r", "utf-8") as file:
  62. dic = yaml.load(file, Loader=yaml.FullLoader)
  63. return dic
  64. def _set_predict(self, **kwargs):
  65. for k in kwargs:
  66. self._pred_set_func_map[k](kwargs[k])
  67. class BasicPredictor(
  68. BasePredictor, DeviceSetMixin, PPOptionSetMixin, metaclass=AutoRegisterABCMetaClass
  69. ):
  70. __is_base = True
  71. def __init__(self, model_dir, config=None):
  72. super().__init__(model_dir=model_dir, config=config)
  73. self._pred_set_register("device")(self.set_device)
  74. self._pred_set_register("pp_option")(self.set_pp_option)
  75. self.pp_option = PaddlePredictorOption()
  76. self.components = {}
  77. self._build_components()
  78. self.engine = ComponentsEngine(self.components)
  79. logging.debug(
  80. f"-------------------- {self.__class__.__name__} --------------------\nModel: {self.model_dir}"
  81. )
  82. def apply(self, x):
  83. """predict"""
  84. yield from self._generate_res(self.engine(x))
  85. @generatorable_method
  86. def _generate_res(self, batch_data):
  87. return [{"result": self._pack_res(data)} for data in batch_data]
  88. def _add_component(self, cmps):
  89. if not isinstance(cmps, list):
  90. cmps = [cmps]
  91. for cmp in cmps:
  92. if not isinstance(cmp, (list, tuple)):
  93. key = cmp.__class__.__name__
  94. else:
  95. assert len(cmp) == 2
  96. key = cmp[0]
  97. cmp = cmp[1]
  98. assert isinstance(key, str)
  99. assert isinstance(cmp, BaseComponent)
  100. assert (
  101. key not in self.components
  102. ), f"The key ({key}) has been used: {self.components}!"
  103. self.components[key] = cmp
  104. @abstractmethod
  105. def _build_components(self):
  106. raise NotImplementedError
  107. @abstractmethod
  108. def _pack_res(self, data):
  109. raise NotImplementedError