base.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 abc import ABC, abstractmethod
  15. from contextvars import ContextVar, copy_context
  16. from typing import TypedDict, Type
  17. from ...utils.subclass_register import AutoRegisterABCMetaClass
  18. from ..models import create_predictor
  19. pipeline_info_list_var = ContextVar("pipeline_info_list", default=None)
  20. class _PipelineInfo(TypedDict):
  21. cls: Type["BasePipeline"]
  22. class _PipelineMetaClass(AutoRegisterABCMetaClass):
  23. def __new__(mcs, name, bases, attrs):
  24. def _patch_init_func(init_func):
  25. def _patched___init__(self, *args, **kwargs):
  26. ctx = copy_context()
  27. pipeline_info_list = [
  28. *ctx.get(pipeline_info_list_var, []),
  29. _PipelineInfo(cls=type(self)),
  30. ]
  31. ctx.run(pipeline_info_list_var.set, pipeline_info_list)
  32. ret = ctx.run(init_func, self, *args, **kwargs)
  33. return ret
  34. return _patched___init__
  35. cls = super().__new__(mcs, name, bases, attrs)
  36. cls.__init__ = _patch_init_func(cls.__init__)
  37. return cls
  38. class BasePipeline(ABC, metaclass=_PipelineMetaClass):
  39. """Base Pipeline"""
  40. __is_base = True
  41. def __init__(self, device, predictor_kwargs={}) -> None:
  42. super().__init__()
  43. self._predictor_kwargs = predictor_kwargs
  44. self._device = device
  45. @abstractmethod
  46. def set_predictor():
  47. raise NotImplementedError(
  48. "The method `set_predictor` has not been implemented yet."
  49. )
  50. # alias the __call__() to predict()
  51. def __call__(self, *args, **kwargs):
  52. yield from self.predict(*args, **kwargs)
  53. def _create(self, model=None, pipeline=None, *args, **kwargs):
  54. if model:
  55. return create_predictor(
  56. *args,
  57. model=model,
  58. device=self._device,
  59. **kwargs,
  60. **self._predictor_kwargs
  61. )
  62. elif pipeline:
  63. return pipeline(
  64. *args,
  65. device=self._device,
  66. predictor_kwargs=self._predictor_kwargs,
  67. **kwargs
  68. )
  69. else:
  70. raise Exception()