model.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. from __future__ import absolute_import
  15. import abc
  16. from . import c_lib_wrap as C
  17. class BaseUltraInferModel(metaclass=abc.ABCMeta):
  18. @abc.abstractmethod
  19. def model_name(self):
  20. raise NotImplementedError
  21. @abc.abstractmethod
  22. def num_inputs_of_runtime(self):
  23. raise NotImplementedError
  24. @abc.abstractmethod
  25. def num_outputs_of_runtime(self):
  26. raise NotImplementedError
  27. class UltraInferModel(BaseUltraInferModel):
  28. def __init__(self, option):
  29. self._model = None
  30. if option is None:
  31. self._runtime_option = C.RuntimeOption()
  32. else:
  33. self._runtime_option = option._option
  34. def model_name(self):
  35. return self._model.model_name()
  36. def num_inputs_of_runtime(self):
  37. return self._model.num_inputs_of_runtime()
  38. def num_outputs_of_runtime(self):
  39. return self._model.num_outputs_of_runtime()
  40. def input_info_of_runtime(self, index):
  41. assert (
  42. index < self.num_inputs_of_runtime()
  43. ), "The index:{} must be less than number of inputs:{}.".format(
  44. index, self.num_inputs_of_runtime()
  45. )
  46. return self._model.input_info_of_runtime(index)
  47. def output_info_of_runtime(self, index):
  48. assert (
  49. index < self.num_outputs_of_runtime()
  50. ), "The index:{} must be less than number of outputs:{}.".format(
  51. index, self.num_outputs_of_runtime()
  52. )
  53. return self._model.output_info_of_runtime(index)
  54. def enable_record_time_of_runtime(self):
  55. self._model.enable_record_time_of_runtime()
  56. def disable_record_time_of_runtime(self):
  57. self._model.disable_record_time_of_runtime()
  58. def print_statis_info_of_runtime(self):
  59. return self._model.print_statis_info_of_runtime()
  60. def get_profile_time(self):
  61. """Get profile time of Runtime after the profile process is done."""
  62. return self._model.get_profile_time()
  63. @property
  64. def runtime_option(self):
  65. return self._model.runtime_option if self._model is not None else None
  66. @property
  67. def initialized(self):
  68. if self._model is None:
  69. return False
  70. return self._model.initialized()