mlu_memory_op.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 mlu_memory_op.h
  22. *
  23. * This file contains a declaration of the MluMemoryOp class.
  24. */
  25. #ifndef EASYINFER_MLU_MEMORY_OP_H_
  26. #define EASYINFER_MLU_MEMORY_OP_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. /**
  33. * @brief MluMemoryOp is a MLU memory helper class.
  34. * @note It provides a easy way to manage memory on MLU.
  35. */
  36. class MluMemoryOp {
  37. public:
  38. /**
  39. * @brief Construct a new Mlu Memory Op object
  40. */
  41. MluMemoryOp();
  42. /**
  43. * @brief Set ModelLoader
  44. *
  45. * @note model loader is used for manage model's input and output memory easily,
  46. * do not need to set if this feature is not used.
  47. * @param model Model loader
  48. * @see edk::ModelLoader
  49. */
  50. void SetModel(std::shared_ptr<ModelLoader> model);
  51. /**
  52. * @brief Get model loader
  53. *
  54. * @return Model loader
  55. */
  56. std::shared_ptr<ModelLoader> Model() const;
  57. /**
  58. * @brief Alloc memory on CPU for model input
  59. *
  60. * @note Input data shape is described by input_shapes from ModelLoader
  61. * @attention Ensure SetModel has been called once
  62. * @return Alloced CPU memory
  63. */
  64. void **AllocCpuInput() const;
  65. /**
  66. * @brief Alloc memory on CPU for model output
  67. *
  68. * @note Output data shape is described by output_shapes from ModelLoader
  69. * @attention Ensure SetModel has been called once
  70. * @return Alloced CPU memory
  71. */
  72. void **AllocCpuOutput() const;
  73. /**
  74. * @brief Alloc memory on MLU according to nBytes.
  75. *
  76. * @param nBytes Alloced memory size in bytes
  77. * @return Alloced MLU memory
  78. */
  79. static void *AllocMlu(size_t nBytes);
  80. /**
  81. * @brief Alloc memory on MLU for model input
  82. *
  83. * @note Input data shape is described by input_data_descs from ModelLoader
  84. * @attention Ensure SetModel has been called once
  85. * @return Alloced MLU memory
  86. */
  87. void **AllocMluInput() const;
  88. /**
  89. * @brief Alloc memory on MLU for model output
  90. *
  91. * @note Input data shape is described by output_data_descs from ModelLoader
  92. * @attention Ensure SetModel has been called once
  93. * @return Alloced MLU memory
  94. */
  95. void **AllocMluOutput() const;
  96. /**
  97. * @brief Free input memory on CPU.
  98. *
  99. * @attention Ensure SetModel has been called once
  100. * @param ptr CPU memory pointer
  101. */
  102. void FreeCpuInput(void **ptr) const;
  103. /**
  104. * @brief Free output memory on CPU.
  105. *
  106. * @attention Ensure SetModel has been called once
  107. * @param ptr CPU memory pointer
  108. */
  109. void FreeCpuOutput(void **ptr) const;
  110. /**
  111. * @brief Free input memory on MLU
  112. *
  113. * @param ptr Memory array on MLU
  114. * @attention Ensure SetModel has been called once
  115. */
  116. void FreeMluInput(void **ptr) const;
  117. /**
  118. * @brief Free output memory on MLU
  119. *
  120. * @param ptr Memory array on MLU
  121. * @attention Ensure SetModel has been called once
  122. */
  123. void FreeMluOutput(void **ptr) const;
  124. /**
  125. * @brief Free memory on MLU
  126. *
  127. * @param ptr MLU memory pointer
  128. */
  129. static void FreeMlu(void *ptr);
  130. /**
  131. * @brief Copy model input data, from host(CPU) to device(MLU)
  132. *
  133. * @attention Ensure SetModel has been called once
  134. * @param mlu_dst Copy destination, memory on MLU
  135. * @param cpu_src Copy source, data on CPU
  136. */
  137. void MemcpyInputH2D(void **mlu_dst, void **cpu_src) const;
  138. /**
  139. * @brief Copy model output data, from device to host
  140. *
  141. * @attention Ensure SetModel has been called once
  142. * @param cpu_dst Copy destination, memory on CPU
  143. * @param mlu_src Copy source, data on MLU
  144. */
  145. void MemcpyOutputD2H(void **cpu_dst, void **mlu_src) const;
  146. /**
  147. * @brief Copy data from host to device
  148. *
  149. * @param mlu_dst Copy destination, memory on MLU
  150. * @param cpu_src Copy source, data on CPU
  151. * @param nBytes Memory size in bytes
  152. */
  153. static void MemcpyH2D(void *mlu_dst, void *cpu_src, size_t nBytes);
  154. /**
  155. * @brief Copy data from device to host
  156. *
  157. * @param cpu_dst Copy destination, memory on CPU
  158. * @param mlu_src Copy source, data on MLU
  159. * @param nBytes Memory size in bytes
  160. */
  161. static void MemcpyD2H(void *cpu_dst, void *mlu_src, size_t nBytes);
  162. /**
  163. * @brief Copy data from device to device
  164. *
  165. * @param mlu_dst Copy destination, memory on MLU
  166. * @param mlu_src Copy source, data on MLU
  167. * @param nBytes Memory size in bytes
  168. */
  169. static void MemcpyD2D(void *mlu_dst, void *mlu_src, size_t nBytes);
  170. private:
  171. std::shared_ptr<ModelLoader> model_;
  172. }; // class MluMemoryOp
  173. } // namespace edk
  174. #endif // EASYINFER_MLU_MEMORY_OP_H_