processor.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*************************************************************************
  2. * Copyright (C) [2020] 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. #ifndef INFER_SERVER_PROCESSOR_H_
  21. #define INFER_SERVER_PROCESSOR_H_
  22. #include <memory>
  23. #include <string>
  24. #include <vector>
  25. #include "buffer.h"
  26. #include "infer_server.h"
  27. #include "shape.h"
  28. namespace infer_server {
  29. // -------------------- Predictor --------------------
  30. /**
  31. * @brief a structure storing input and output data of inference
  32. */
  33. struct ModelIO {
  34. /// input / output data
  35. std::vector<Buffer> buffers;
  36. /// shape of input / output
  37. std::vector<Shape> shapes;
  38. };
  39. struct PredictorPrivate;
  40. /**
  41. * @brief Predictor processor
  42. */
  43. class Predictor : public ProcessorForkable<Predictor> {
  44. public:
  45. /**
  46. * @brief Construct a new Predictor object
  47. */
  48. Predictor() noexcept;
  49. /**
  50. * @brief Destroy the Predictor object
  51. */
  52. ~Predictor();
  53. /**
  54. * @brief Perform predict
  55. *
  56. * @param data processed data
  57. * @retval Status::SUCCESS Succeeded
  58. * @retval Status::INVALID_PARAM Predictor get uncontinuous data
  59. * @retval Status::WRONG_TYPE Predictor get data of wrong type (bad_any_cast)
  60. * @retval Status::ERROR_BACKEND Predict failed
  61. */
  62. Status Process(PackagePtr data) noexcept override;
  63. /**
  64. * @brief Initialize predictor
  65. *
  66. * @retval Status::SUCCESS Init succeeded
  67. * @retval Status::INVALID_PARAM Predictor donot have enough params or get wrong params, @see BaseObject::SetParam
  68. * @retval Status::WRONG_TYPE Predictor get params of wrong type (bad_any_cast)
  69. */
  70. Status Init() noexcept override;
  71. /**
  72. * @brief Get backend string
  73. *
  74. * @retval magicmind Use magicmind to do inference
  75. * @retval cnrt Use cnrt model to do inference
  76. */
  77. static std::string Backend() noexcept;
  78. private:
  79. PredictorPrivate* priv_;
  80. }; // class Predictor
  81. // -------------------- Predictor END --------------------
  82. // -------------------- PreprocessorHost --------------------
  83. struct PreprocessorHostPrivate;
  84. /**
  85. * @brief PreprocessorHost processor
  86. */
  87. class PreprocessorHost : public ProcessorForkable<PreprocessorHost> {
  88. public:
  89. /**
  90. * @brief Preprocess function on single data, set by user
  91. */
  92. using ProcessFunction = std::function<bool(ModelIO*, const InferData&, const ModelInfo&)>;
  93. /**
  94. * @brief Construct a new PreprocessorHost object
  95. */
  96. PreprocessorHost() noexcept;
  97. /**
  98. * @brief Destroy the PreprocessorHost object
  99. */
  100. ~PreprocessorHost();
  101. /**
  102. * @brief Perform preprocess on host
  103. *
  104. * @param data processed data
  105. * @retval Status::SUCCESS Succeeded
  106. * @retval Status::ERROR_BACKEND Preprocess failed in transform layout or process_function set by user
  107. */
  108. Status Process(PackagePtr data) noexcept override;
  109. /**
  110. * @brief Initialize PreprocessorHost
  111. *
  112. * @retval Status::SUCCESS Init succeeded
  113. * @retval Status::INVALID_PARAM Preprocessor donot have enough params or get wrong params, @see BaseObject::SetParam
  114. * @retval Status::WRONG_TYPE Preprocessor get params of wrong type (bad_any_cast)
  115. */
  116. Status Init() noexcept override;
  117. private:
  118. PreprocessorHostPrivate* priv_;
  119. }; // class PreprocessorHost
  120. // -------------------- PreprocessorHost END --------------------
  121. // -------------------- Postprocessor --------------------
  122. struct PostprocessorPrivate;
  123. /**
  124. * @brief Postprocessor processor
  125. */
  126. class Postprocessor : public ProcessorForkable<Postprocessor> {
  127. public:
  128. /**
  129. * @brief Postprocess function on single data, set by user
  130. */
  131. using ProcessFunction = std::function<bool(InferData*, const ModelIO&, const ModelInfo&)>;
  132. /**
  133. * @brief Construct a new Postprocessor object
  134. */
  135. Postprocessor() noexcept;
  136. /**
  137. * @brief Destroy the Postprocessor object
  138. */
  139. ~Postprocessor();
  140. /**
  141. * @brief Perform postprocess
  142. *
  143. * @param data processed data
  144. * @retval Status::SUCCESS Succeeded
  145. * @retval Status::INVALID_PARAM Postprocessor get uncontinuous data
  146. * @retval Status::WRONG_TYPE Postprocessor get data of wrong type (bad_any_cast)
  147. * @retval Status::ERROR_BACKEND Postprocess failed in transform layout or process_function set by user
  148. */
  149. Status Process(PackagePtr data) noexcept override;
  150. /**
  151. * @brief Initialize PostprocessorHost
  152. *
  153. * @retval Status::SUCCESS Init succeeded
  154. * @retval Status::INVALID_PARAM Postprocessor donot have enough params or get wrong params, @see BaseObject::SetParam
  155. * @retval Status::WRONG_TYPE Postprocessor get params of wrong type (bad_any_cast)
  156. */
  157. Status Init() noexcept override;
  158. private:
  159. PostprocessorPrivate* priv_;
  160. }; // class PreprocessorHost
  161. // -------------------- PostprocessorHost END --------------------
  162. } // namespace infer_server
  163. #endif // INFER_SERVER_PROCESSOR_H_