enum_variables.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. /*! \file enum_variables.h
  15. \brief A brief file description.
  16. More details
  17. */
  18. #pragma once
  19. #include "ultra_infer/utils/utils.h"
  20. #include <map>
  21. #include <ostream>
  22. namespace ultra_infer {
  23. /*! Inference backend supported in UltraInfer */
  24. enum Backend {
  25. UNKNOWN, ///< Unknown inference backend
  26. ORT, //< ONNX Runtime, support Paddle/ONNX format model,
  27. //< CPU/ Nvidia GPU DirectML
  28. TRT, ///< TensorRT, support Paddle/ONNX format model, Nvidia GPU only
  29. PDINFER, ///< Paddle Inference, support Paddle format model, CPU / Nvidia GPU
  30. POROS, ///< Poros, support TorchScript format model, CPU / Nvidia GPU
  31. OPENVINO, ///< Intel OpenVINO, support Paddle/ONNX format, CPU only
  32. LITE, ///< Paddle Lite, support Paddle format model, ARM CPU / ARM GPU
  33. RKNPU2, ///< RKNPU2, support RKNN format model, Rockchip NPU only
  34. SOPHGOTPU, ///< SOPHGOTPU, support SOPHGO format model, Sophgo TPU only
  35. HORIZONNPU, ///< HORIZONNPU, support Horizon format model, Horizon NPU
  36. TVM, ///< TVMBackend, support TVM format model, CPU / Nvidia GPU
  37. OMONNPU, ///< OMONNPU, support OM format model, OM NPU
  38. };
  39. /**
  40. * @brief Get all the available inference backend in UltraInfer
  41. */
  42. ULTRAINFER_DECL std::vector<Backend> GetAvailableBackends();
  43. /**
  44. * @brief Check if the inference backend available
  45. */
  46. ULTRAINFER_DECL bool IsBackendAvailable(const Backend &backend);
  47. enum ULTRAINFER_DECL Device {
  48. CPU,
  49. GPU,
  50. RKNPU,
  51. IPU,
  52. TIMVX,
  53. KUNLUNXIN,
  54. ASCEND,
  55. SOPHGOTPUD,
  56. DIRECTML,
  57. SUNRISENPU,
  58. };
  59. /*! Deep learning model format */
  60. enum ModelFormat {
  61. AUTOREC, ///< Auto recognize the model format by model file name
  62. PADDLE, ///< Model with paddlepaddle format
  63. ONNX, ///< Model with ONNX format
  64. RKNN, ///< Model with RKNN format
  65. TORCHSCRIPT, ///< Model with TorchScript format
  66. SOPHGO, ///< Model with SOPHGO format
  67. HORIZON, ///< Model with HORIZON format
  68. TVMFormat, ///< Model with TVM format
  69. OM, ///< Model with OM format
  70. };
  71. /// Describle all the supported backends for specified model format
  72. static std::map<ModelFormat, std::vector<Backend>>
  73. s_default_backends_by_format = {
  74. {ModelFormat::PADDLE,
  75. {Backend::PDINFER, Backend::LITE, Backend::ORT, Backend::OPENVINO,
  76. Backend::TRT}},
  77. {ModelFormat::ONNX, {Backend::ORT, Backend::OPENVINO, Backend::TRT}},
  78. {ModelFormat::RKNN, {Backend::RKNPU2}},
  79. {ModelFormat::HORIZON, {Backend::HORIZONNPU}},
  80. {ModelFormat::TORCHSCRIPT, {Backend::POROS}},
  81. {ModelFormat::SOPHGO, {Backend::SOPHGOTPU}},
  82. {ModelFormat::TVMFormat, {Backend::TVM}},
  83. {ModelFormat::OM, {Backend::OMONNPU}}};
  84. /// Describle all the supported backends for specified device
  85. static std::map<Device, std::vector<Backend>> s_default_backends_by_device = {
  86. {Device::CPU,
  87. {Backend::LITE, Backend::PDINFER, Backend::ORT, Backend::OPENVINO,
  88. Backend::POROS, Backend::TVM}},
  89. {Device::GPU,
  90. {Backend::LITE, Backend::PDINFER, Backend::ORT, Backend::TRT,
  91. Backend::POROS, Backend::TVM}},
  92. {Device::RKNPU, {Backend::RKNPU2}},
  93. {Device::SUNRISENPU, {Backend::HORIZONNPU}},
  94. {Device::IPU, {Backend::PDINFER}},
  95. {Device::TIMVX, {Backend::LITE}},
  96. {Device::KUNLUNXIN, {Backend::LITE, Backend::PDINFER}},
  97. {Device::ASCEND, {Backend::LITE}},
  98. {Device::SOPHGOTPUD, {Backend::SOPHGOTPU}},
  99. {Device::DIRECTML, {Backend::ORT}},
  100. {Device::ASCEND, {Backend::OMONNPU}}};
  101. inline bool Supported(ModelFormat format, Backend backend) {
  102. auto iter = s_default_backends_by_format.find(format);
  103. if (iter == s_default_backends_by_format.end()) {
  104. FDERROR << "Didn't find format is registered in "
  105. << "s_default_backends_by_format." << std::endl;
  106. return false;
  107. }
  108. for (size_t i = 0; i < iter->second.size(); ++i) {
  109. if (iter->second[i] == backend) {
  110. return true;
  111. }
  112. }
  113. std::string msg = Str(iter->second);
  114. FDERROR << backend << " only supports " << msg << ", but now it's " << format
  115. << "." << std::endl;
  116. return false;
  117. }
  118. inline bool Supported(Device device, Backend backend) {
  119. auto iter = s_default_backends_by_device.find(device);
  120. if (iter == s_default_backends_by_device.end()) {
  121. FDERROR << "Didn't find device is registered in "
  122. << "s_default_backends_by_device." << std::endl;
  123. return false;
  124. }
  125. for (size_t i = 0; i < iter->second.size(); ++i) {
  126. if (iter->second[i] == backend) {
  127. return true;
  128. }
  129. }
  130. std::string msg = Str(iter->second);
  131. FDERROR << backend << " only supports " << msg << ", but now it's " << device
  132. << "." << std::endl;
  133. return false;
  134. }
  135. ULTRAINFER_DECL std::ostream &operator<<(std::ostream &o, const Backend &b);
  136. ULTRAINFER_DECL std::ostream &operator<<(std::ostream &o, const Device &d);
  137. ULTRAINFER_DECL std::ostream &operator<<(std::ostream &o, const ModelFormat &f);
  138. } // namespace ultra_infer