easy_infer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. /**
  21. * @file easy_infer.h
  22. *
  23. * This file contains a declaration of the EasyInfer class.
  24. */
  25. #ifndef EASYINFER_EASY_INFER_H_
  26. #define EASYINFER_EASY_INFER_H_
  27. #include <memory>
  28. #include "cxxutil/edk_attribute.h"
  29. #include "cxxutil/exception.h"
  30. #include "easyinfer/model_loader.h"
  31. namespace edk {
  32. struct MluTaskQueue;
  33. using MluTaskQueue_t = std::shared_ptr<MluTaskQueue>;
  34. class EasyInferPrivate;
  35. /**
  36. * @brief Inference helper class
  37. */
  38. class EasyInfer {
  39. public:
  40. /**
  41. * @brief Construct a new Easy Infer object
  42. */
  43. EasyInfer();
  44. /**
  45. * @brief Destroy the Easy Infer object
  46. */
  47. ~EasyInfer();
  48. /**
  49. * @brief Initialize the inference helper class
  50. *
  51. * @param model Model loader which contain neural network offline model and informations
  52. * @param dev_id init cninfer in device with origin id dev_id. only supported on MLU270
  53. */
  54. void Init(std::shared_ptr<ModelLoader> model, int dev_id);
  55. /**
  56. * @brief Invoke inference function
  57. *
  58. * @param input Input data in MLU
  59. * @param output Output data in MLU
  60. * @param hw_time Hardware time of inference
  61. */
  62. void Run(void** input, void** output, float* hw_time = nullptr) const;
  63. /**
  64. * @brief Async invoke inference function
  65. *
  66. * @param input Input data in MLU
  67. * @param output Output data in MLU
  68. * @param task_queue
  69. */
  70. void RunAsync(void** input, void** output, MluTaskQueue_t task_queue) const;
  71. /**
  72. * @brief Get the model loader
  73. *
  74. * @see edk::ModelLoader
  75. * @return Model loader
  76. */
  77. std::shared_ptr<ModelLoader> Model() const;
  78. /**
  79. * @brief Get the MLU task queue, used to share MLU queue with Bang kernel
  80. *
  81. * @return MluTaskQueue
  82. */
  83. MluTaskQueue_t GetMluQueue() const;
  84. private:
  85. EasyInferPrivate* d_ptr_;
  86. EasyInfer(const EasyInfer&) = delete;
  87. EasyInfer& operator=(const EasyInfer&) = delete;
  88. }; // class EasyInfer
  89. } // namespace edk
  90. #endif // CNINFER_H_