enum_variables.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. };
  38. /**
  39. * @brief Get all the available inference backend in UltraInfer
  40. */
  41. ULTRAINFER_DECL std::vector<Backend> GetAvailableBackends();
  42. /**
  43. * @brief Check if the inference backend available
  44. */
  45. ULTRAINFER_DECL bool IsBackendAvailable(const Backend &backend);
  46. enum ULTRAINFER_DECL Device {
  47. CPU,
  48. GPU,
  49. RKNPU,
  50. IPU,
  51. TIMVX,
  52. KUNLUNXIN,
  53. ASCEND,
  54. SOPHGOTPUD,
  55. DIRECTML,
  56. SUNRISENPU,
  57. };
  58. /*! Deep learning model format */
  59. enum ModelFormat {
  60. AUTOREC, ///< Auto recognize the model format by model file name
  61. PADDLE, ///< Model with paddlepaddle format
  62. ONNX, ///< Model with ONNX format
  63. RKNN, ///< Model with RKNN format
  64. TORCHSCRIPT, ///< Model with TorchScript format
  65. SOPHGO, ///< Model with SOPHGO format
  66. HORIZON, ///< Model with HORIZON format
  67. TVMFormat, ///< Model with TVM format
  68. };
  69. /// Describle all the supported backends for specified model format
  70. static std::map<ModelFormat, std::vector<Backend>>
  71. s_default_backends_by_format = {
  72. {ModelFormat::PADDLE,
  73. {Backend::PDINFER, Backend::LITE, Backend::ORT, Backend::OPENVINO,
  74. Backend::TRT}},
  75. {ModelFormat::ONNX, {Backend::ORT, Backend::OPENVINO, Backend::TRT}},
  76. {ModelFormat::RKNN, {Backend::RKNPU2}},
  77. {ModelFormat::HORIZON, {Backend::HORIZONNPU}},
  78. {ModelFormat::TORCHSCRIPT, {Backend::POROS}},
  79. {ModelFormat::SOPHGO, {Backend::SOPHGOTPU}},
  80. {ModelFormat::TVMFormat, {Backend::TVM}}};
  81. /// Describle all the supported backends for specified device
  82. static std::map<Device, std::vector<Backend>> s_default_backends_by_device = {
  83. {Device::CPU,
  84. {Backend::LITE, Backend::PDINFER, Backend::ORT, Backend::OPENVINO,
  85. Backend::POROS, Backend::TVM}},
  86. {Device::GPU,
  87. {Backend::LITE, Backend::PDINFER, Backend::ORT, Backend::TRT,
  88. Backend::POROS, Backend::TVM}},
  89. {Device::RKNPU, {Backend::RKNPU2}},
  90. {Device::SUNRISENPU, {Backend::HORIZONNPU}},
  91. {Device::IPU, {Backend::PDINFER}},
  92. {Device::TIMVX, {Backend::LITE}},
  93. {Device::KUNLUNXIN, {Backend::LITE, Backend::PDINFER}},
  94. {Device::ASCEND, {Backend::LITE}},
  95. {Device::SOPHGOTPUD, {Backend::SOPHGOTPU}},
  96. {Device::DIRECTML, {Backend::ORT}}};
  97. inline bool Supported(ModelFormat format, Backend backend) {
  98. auto iter = s_default_backends_by_format.find(format);
  99. if (iter == s_default_backends_by_format.end()) {
  100. FDERROR << "Didn't find format is registered in "
  101. << "s_default_backends_by_format." << std::endl;
  102. return false;
  103. }
  104. for (size_t i = 0; i < iter->second.size(); ++i) {
  105. if (iter->second[i] == backend) {
  106. return true;
  107. }
  108. }
  109. std::string msg = Str(iter->second);
  110. FDERROR << backend << " only supports " << msg << ", but now it's " << format
  111. << "." << std::endl;
  112. return false;
  113. }
  114. inline bool Supported(Device device, Backend backend) {
  115. auto iter = s_default_backends_by_device.find(device);
  116. if (iter == s_default_backends_by_device.end()) {
  117. FDERROR << "Didn't find device is registered in "
  118. << "s_default_backends_by_device." << std::endl;
  119. return false;
  120. }
  121. for (size_t i = 0; i < iter->second.size(); ++i) {
  122. if (iter->second[i] == backend) {
  123. return true;
  124. }
  125. }
  126. std::string msg = Str(iter->second);
  127. FDERROR << backend << " only supports " << msg << ", but now it's " << device
  128. << "." << std::endl;
  129. return false;
  130. }
  131. ULTRAINFER_DECL std::ostream &operator<<(std::ostream &o, const Backend &b);
  132. ULTRAINFER_DECL std::ostream &operator<<(std::ostream &o, const Device &d);
  133. ULTRAINFER_DECL std::ostream &operator<<(std::ostream &o, const ModelFormat &f);
  134. } // namespace ultra_infer