model_loader.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 model_loader.h
  22. *
  23. * This file contains a declaration of the ModelLoader class and involved struct
  24. */
  25. #ifndef EASYINFER_MODEL_LOADER_H_
  26. #define EASYINFER_MODEL_LOADER_H_
  27. #include <memory>
  28. #include <string>
  29. #include <vector>
  30. #include "cxxutil/edk_attribute.h"
  31. #include "cxxutil/exception.h"
  32. #include "easyinfer/shape.h"
  33. namespace edk {
  34. /**
  35. * @brief Enumeration to specify data type of model input and output
  36. */
  37. enum class DataType { UINT8, FLOAT32, FLOAT16, INT16, INT32 };
  38. /**
  39. * @brief Enumeration to specify dim order of model input and output
  40. */
  41. enum class DimOrder { NCHW, NHWC, HWCN, TNC, NTC };
  42. /**
  43. * @brief Describe data layout on MLU or CPU
  44. */
  45. struct DataLayout {
  46. DataType dtype; ///< @see edk::DataType
  47. DimOrder order; ///< @see edk::DimOrder
  48. };
  49. class ModelLoaderPrivate;
  50. class ModelLoaderInternalInterface;
  51. /**
  52. * @brief A helper class to load offline model and get model infomation
  53. */
  54. class ModelLoader {
  55. public:
  56. friend class ModelLoaderInternalInterface;
  57. /**
  58. * @brief Constructor 1. Construct a new Model Loader object
  59. *
  60. * @note Delegate to constructor 2 for construct
  61. * @param model_path Inference offline model path
  62. * @param function_name Name of function in offline model
  63. */
  64. ModelLoader(const std::string& model_path, const std::string& function_name);
  65. /**
  66. * @brief Constructor 2. Construct a new Model Loader object
  67. *
  68. * @note Delegate to constructor 3 for construct
  69. * @param model_path Model path
  70. * @param function_name Function name
  71. */
  72. ModelLoader(const char* model_path, const char* function_name);
  73. /**
  74. * @brief Constructor 3. Construct a new Model Loader object
  75. *
  76. * @param mem_ptr Offline model binary stored in memory
  77. * @param function_name Function name
  78. */
  79. ModelLoader(void* mem_ptr, const char* function_name);
  80. /**
  81. * @brief Destroy the Model Loader object
  82. */
  83. ~ModelLoader();
  84. /**
  85. * @brief Set specified input data layout on CPU
  86. *
  87. * @param layout Data layout
  88. * @param data_index Data index
  89. */
  90. void SetCpuInputLayout(DataLayout layout, int data_index);
  91. /**
  92. * @brief Set specified output data layout on CPU
  93. *
  94. * @param layout Data layout
  95. * @param data_index Data index
  96. */
  97. void SetCpuOutputLayout(DataLayout layout, int data_index);
  98. /**
  99. * @brief Get specified input data layout on CPU
  100. *
  101. * @param data_index Data index
  102. * @return Data layout
  103. */
  104. DataLayout GetCpuInputLayout(int data_index) const;
  105. /**
  106. * @brief Get specified output data layout on CPU
  107. *
  108. * @param data_index Data index
  109. * @return Data layout
  110. */
  111. DataLayout GetCpuOutputLayout(int data_index) const;
  112. /**
  113. * @brief Adjust MLU stack memory according to model size
  114. *
  115. * @note Adjust MLU stack memory. Do nothing if model size is not larger than current stack memory size.
  116. * @return Return true if stack memory is adjusted.
  117. */
  118. bool AdjustStackMemory();
  119. /**
  120. * @brief Get model output number
  121. *
  122. * @return Model output number
  123. */
  124. uint32_t OutputNum() const;
  125. /**
  126. * @brief Get model input number
  127. *
  128. * @return Model input number
  129. */
  130. uint32_t InputNum() const;
  131. /**
  132. * @brief Get model input data shapes
  133. *
  134. * @deprecated use ModelLoader::InputShape(uint32_t) instead
  135. * @return Model input data shapes
  136. */
  137. attribute_deprecated const std::vector<Shape>& InputShapes() const;
  138. /**
  139. * @brief Get model output data shapes
  140. *
  141. * @deprecated use ModelLoader::OutputShape(uint32_t) instead
  142. * @return Model output data shapes
  143. */
  144. attribute_deprecated const std::vector<Shape>& OutputShapes() const;
  145. /**
  146. * @brief Get model input data shape
  147. *
  148. * @param index input index
  149. * @return Model input data shape
  150. */
  151. const ShapeEx& InputShape(uint32_t index) const;
  152. /**
  153. * @brief Get model output data shape
  154. *
  155. * @param index output index
  156. * @return Model output data shape
  157. */
  158. const ShapeEx& OutputShape(uint32_t index) const;
  159. /**
  160. * @brief Get model parallelism
  161. *
  162. * @note Not supported on MLU100, always return 1.
  163. * @return Model parallelism
  164. */
  165. int ModelParallelism() const;
  166. /**
  167. * @brief Get the input data batch align size
  168. *
  169. * @param data_index Data index
  170. * @return input data batch align size
  171. */
  172. int64_t GetInputDataBatchAlignSize(int data_index) const;
  173. /**
  174. * @brief Get the output data batch align size
  175. *
  176. * @param data_index Data index
  177. * @return output data batch align size
  178. */
  179. int64_t GetOutputDataBatchAlignSize(int data_index) const;
  180. private:
  181. std::unique_ptr<ModelLoaderPrivate> d_ptr_;
  182. ModelLoader(const ModelLoader&) = delete;
  183. ModelLoader& operator=(const ModelLoader&) = delete;
  184. ModelLoader(ModelLoader&&) = delete;
  185. ModelLoader& operator=(ModelLoader&&) = delete;
  186. }; // class ModelLoader
  187. } // namespace edk
  188. #endif // EASYINFER_MODEL_LOADER_H_