base.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 abc
  15. import logging
  16. from ..model import BaseUltraInferModel
  17. from ..runtime import Runtime, RuntimeOption
  18. _logger = logging.getLogger(__name__)
  19. class PyOnlyUltraInferModel(BaseUltraInferModel):
  20. def __init__(self, option):
  21. super().__init__()
  22. if option is None:
  23. self._option = RuntimeOption()
  24. else:
  25. self._option = option
  26. self._update_option()
  27. self._runtime = Runtime(self._option)
  28. _logger.debug("Python-only model initialized")
  29. def num_inputs_of_runtime(self):
  30. return self._runtime.num_inputs()
  31. def num_outputs_of_runtime(self):
  32. return self._runtime.num_outputs()
  33. def get_profile_time(self):
  34. return self._runtime.get_profile_time()
  35. def _update_option(self):
  36. pass
  37. class PyOnlyProcessor(metaclass=abc.ABCMeta):
  38. @abc.abstractmethod
  39. def __call__(self, data):
  40. raise NotImplementedError
  41. class PyOnlyProcessorChain(object):
  42. def __init__(self, processors):
  43. super().__init__()
  44. self._processors = processors
  45. def __call__(self, data):
  46. for processor in self._processors:
  47. data = processor(data)
  48. return data