ultra_infer_model.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright (c) 2022 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. #pragma once
  15. #include "ultra_infer/runtime.h"
  16. namespace ultra_infer {
  17. /*! @brief Base model object for all the vision models
  18. */
  19. class ULTRAINFER_DECL UltraInferModel {
  20. public:
  21. /// Get model's name
  22. virtual std::string ModelName() const { return "NameUndefined"; }
  23. /** \brief Inference the model by the runtime. This interface is included in
  24. * the `Predict()` function, so we don't call `Infer()` directly in most
  25. * common situation
  26. */
  27. virtual bool Infer(std::vector<FDTensor> &input_tensors,
  28. std::vector<FDTensor> *output_tensors);
  29. /** \brief Inference the model by the runtime. This interface is using class
  30. * member reused_input_tensors_ to do inference and writing results to
  31. * reused_output_tensors_
  32. */
  33. virtual bool Infer();
  34. RuntimeOption runtime_option;
  35. /** \brief Model's valid cpu backends. This member defined all the cpu
  36. * backends have successfully tested for the model
  37. */
  38. std::vector<Backend> valid_cpu_backends = {Backend::ORT};
  39. /** Model's valid gpu backends. This member defined all the gpu backends have
  40. * successfully tested for the model
  41. */
  42. std::vector<Backend> valid_gpu_backends = {Backend::ORT};
  43. /** Model's valid ipu backends. This member defined all the ipu backends have
  44. * successfully tested for the model
  45. */
  46. std::vector<Backend> valid_ipu_backends = {};
  47. /** Model's valid timvx backends. This member defined all the timvx backends
  48. * have successfully tested for the model
  49. */
  50. std::vector<Backend> valid_timvx_backends = {};
  51. /** Model's valid directml backends. This member defined all the onnxruntime
  52. * directml backends have successfully tested for the model
  53. */
  54. std::vector<Backend> valid_directml_backends = {};
  55. /** Model's valid ascend backends. This member defined all the cann backends
  56. * have successfully tested for the model
  57. */
  58. std::vector<Backend> valid_ascend_backends = {};
  59. /** Model's valid KunlunXin xpu backends. This member defined all the
  60. * KunlunXin xpu backends have successfully tested for the model
  61. */
  62. std::vector<Backend> valid_kunlunxin_backends = {};
  63. /** Model's valid hardware backends. This member defined all the gpu backends
  64. * have successfully tested for the model
  65. */
  66. std::vector<Backend> valid_rknpu_backends = {};
  67. /** Model's valid hardware backends. This member defined all the sophgo npu
  68. * backends have successfully tested for the model
  69. */
  70. std::vector<Backend> valid_horizon_backends = {};
  71. std::vector<Backend> valid_sophgonpu_backends = {};
  72. /// Get number of inputs for this model
  73. virtual int NumInputsOfRuntime() { return runtime_->NumInputs(); }
  74. /// Get number of outputs for this model
  75. virtual int NumOutputsOfRuntime() { return runtime_->NumOutputs(); }
  76. /// Get input information for this model
  77. virtual TensorInfo InputInfoOfRuntime(int index) {
  78. return runtime_->GetInputInfo(index);
  79. }
  80. /// Get output information for this model
  81. virtual TensorInfo OutputInfoOfRuntime(int index) {
  82. return runtime_->GetOutputInfo(index);
  83. }
  84. /// Check if the model is initialized successfully
  85. virtual bool Initialized() const {
  86. return runtime_initialized_ && initialized;
  87. }
  88. /** \brief This is a debug interface, used to record the time of runtime
  89. * (backend + h2d + d2h)
  90. *
  91. * example code @code
  92. * auto model = ultra_infer::vision::PPYOLOE("model.pdmodel",
  93. * "model.pdiparams", "infer_cfg.yml"); if (!model.Initialized()) { std::cerr
  94. * << "Failed to initialize." << std::endl; return -1;
  95. * }
  96. * model.EnableRecordTimeOfRuntime();
  97. * cv::Mat im = cv::imread("test.jpg");
  98. * for (auto i = 0; i < 1000; ++i) {
  99. * ultra_infer::vision::DetectionResult result;
  100. * model.Predict(&im, &result);
  101. * }
  102. * model.PrintStatisInfoOfRuntime();
  103. * @endcode After called the `PrintStatisInfoOfRuntime()`, the statistical
  104. * information of runtime will be printed in the console
  105. */
  106. virtual void EnableRecordTimeOfRuntime() {
  107. time_of_runtime_.clear();
  108. std::vector<double>().swap(time_of_runtime_);
  109. enable_record_time_of_runtime_ = true;
  110. }
  111. /** \brief Disable to record the time of runtime, see
  112. * `EnableRecordTimeOfRuntime()` for more detail
  113. */
  114. virtual void DisableRecordTimeOfRuntime() {
  115. enable_record_time_of_runtime_ = false;
  116. }
  117. /** \brief Print the statistic information of runtime in the console, see
  118. * function `EnableRecordTimeOfRuntime()` for more detail
  119. */
  120. virtual std::map<std::string, float> PrintStatisInfoOfRuntime();
  121. /** \brief Check if the `EnableRecordTimeOfRuntime()` method is enabled.
  122. */
  123. virtual bool EnabledRecordTimeOfRuntime() {
  124. return enable_record_time_of_runtime_;
  125. }
  126. /** \brief Get profile time of Runtime after the profile process is done.
  127. */
  128. virtual double GetProfileTime() { return runtime_->GetProfileTime(); }
  129. /** \brief Release reused input/output buffers
  130. */
  131. virtual void ReleaseReusedBuffer() {
  132. std::vector<FDTensor>().swap(reused_input_tensors_);
  133. std::vector<FDTensor>().swap(reused_output_tensors_);
  134. }
  135. virtual ultra_infer::Runtime *CloneRuntime() { return runtime_->Clone(); }
  136. virtual bool SetRuntime(ultra_infer::Runtime *clone_runtime) {
  137. runtime_ = std::unique_ptr<Runtime>(clone_runtime);
  138. return true;
  139. }
  140. virtual std::unique_ptr<UltraInferModel> Clone() {
  141. FDERROR << ModelName() << " doesn't support Cone() now." << std::endl;
  142. return nullptr;
  143. }
  144. protected:
  145. virtual bool InitRuntime();
  146. bool initialized = false;
  147. // Reused input tensors
  148. std::vector<FDTensor> reused_input_tensors_;
  149. // Reused output tensors
  150. std::vector<FDTensor> reused_output_tensors_;
  151. private:
  152. bool InitRuntimeWithSpecifiedBackend();
  153. bool InitRuntimeWithSpecifiedDevice();
  154. bool CreateCpuBackend();
  155. bool CreateGpuBackend();
  156. bool CreateIpuBackend();
  157. bool CreateRKNPUBackend();
  158. bool CreateHorizonBackend();
  159. bool CreateSophgoNPUBackend();
  160. bool CreateTimVXBackend();
  161. bool CreateKunlunXinBackend();
  162. bool CreateASCENDBackend();
  163. bool CreateDirectMLBackend();
  164. bool IsSupported(const std::vector<Backend> &backends, Backend backend);
  165. std::shared_ptr<Runtime> runtime_;
  166. bool runtime_initialized_ = false;
  167. // whether to record inference time
  168. bool enable_record_time_of_runtime_ = false;
  169. std::vector<double> time_of_runtime_;
  170. };
  171. } // namespace ultra_infer