You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

session_manager.h 7.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  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. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef GE_SESSION_SESSION_MANAGER_H_
  17. #define GE_SESSION_SESSION_MANAGER_H_
  18. #include <iostream>
  19. #include <map>
  20. #include <memory>
  21. #include <string>
  22. #include <vector>
  23. #include "framework/common/ge_inner_error_codes.h"
  24. #include "external/ge/ge_api_types.h"
  25. #include "session/inner_session.h"
  26. #include "runtime/base.h"
  27. namespace ge {
  28. using SessionPtr = std::shared_ptr<InnerSession>;
  29. class SessionManager {
  30. public:
  31. SessionManager() = default;
  32. ~SessionManager() = default;
  33. ///
  34. /// @ingroup ge_session
  35. /// @brief initialize session manager
  36. /// @param [in] options session manager config options
  37. /// @return Status result of function
  38. ///
  39. Status Initialize(const std::map<std::string, std::string> &options);
  40. ///
  41. /// @ingroup ge_session
  42. /// @brief finalize session manager
  43. /// @return Status result of function
  44. ///
  45. Status Finalize();
  46. ///
  47. /// @ingroup ge_session
  48. /// @brief create session
  49. /// @param [in] options session config options
  50. /// @param [out] session_id session id
  51. /// @return Status result of function
  52. ///
  53. Status CreateSession(const std::map<std::string, std::string> &options, SessionId &session_id);
  54. ///
  55. /// @ingroup ge_session
  56. /// @brief destroy the session with specific session id
  57. /// @param [in] session_id session id
  58. /// @return Status result of function
  59. ///
  60. Status DestroySession(SessionId session_id);
  61. ///
  62. /// @ingroup ge_session
  63. /// @brief add a graph to the session with specific session id
  64. /// @param [in] session_id session id
  65. /// @param [in] graph_id graph id
  66. /// @param [in] graph the graph to add
  67. /// @return Status result of function
  68. ///
  69. Status AddGraph(SessionId session_id, uint32_t graph_id, const ge::Graph &graph);
  70. ///
  71. /// @ingroup ge_session
  72. /// @brief add a graph to the session with specific session id and graphOptions
  73. /// @param [in] session_id session id
  74. /// @param [in] graph_id graph id
  75. /// @param [in] graph the graph to add
  76. /// @param [in] options graph level options
  77. /// @return Status result of function
  78. ///
  79. Status AddGraph(SessionId session_id, uint32_t graph_id, const Graph &graph,
  80. const std::map<std::string, std::string> &options);
  81. ///
  82. /// @ingroup ge_session
  83. /// @brief add a copy graph to the session with specific session id and graphOptions
  84. /// @param [in] session_id session id
  85. /// @param [in] graph_id graph id
  86. /// @param [in] graph the graph to add
  87. /// @param [in] options graph level options
  88. /// @return Status result of function
  89. ///
  90. Status AddGraphWithCopy(SessionId session_id, uint32_t graph_id, const Graph &graph,
  91. const std::map<std::string, std::string> &options);
  92. ///
  93. /// @ingroup ge_session
  94. /// @brief run a graph of the session with specific session id
  95. /// @param [in] session_id session id
  96. /// @param [in] graph_id graph id
  97. /// @param [in] inputs input data
  98. /// @param [out] outputs output data
  99. /// @return Status result of function
  100. ///
  101. Status RunGraph(SessionId session_id, uint32_t graph_id, const std::vector<Tensor> &inputs,
  102. std::vector<Tensor> &outputs);
  103. ///
  104. /// @ingroup ge_session
  105. /// @brief run a graph of the session with specific stream asynchronously
  106. /// @param [in] session_id session id
  107. /// @param [in] graph_id graph id
  108. /// @param [in] stream specific stream
  109. /// @param [in] inputs input data
  110. /// @param [out] outputs output data
  111. /// @return Status result of function
  112. ///
  113. Status RunGraphWithStreamAsync(SessionId session_id, uint32_t graph_id, rtStream_t stream,
  114. const std::vector<Tensor> &inputs, std::vector<Tensor> &outputs);
  115. ///
  116. /// @ingroup ge_session
  117. /// @brief remove a graph from the session with specific session id
  118. /// @param [in] session_id session id
  119. /// @param [in] graph_id graph id
  120. /// @return Status result of function
  121. ///
  122. Status RemoveGraph(SessionId session_id, uint32_t graph_id);
  123. ///
  124. /// @ingroup ge_session
  125. /// @brief get variable value from the session with specific session id
  126. /// @param [in] session_id session id
  127. /// @param [in] name op name
  128. /// @param [out] val out value tensor
  129. /// @return Status result of function
  130. ///
  131. Status GetVariable(SessionId session_id, const std::string &name, Tensor &val);
  132. ///
  133. /// @ingroup ge_session
  134. /// @brief build a graph of the session with specific session id
  135. /// @param [in] session_id session id
  136. /// @param [in] graph_id graph id
  137. /// @param [in] inputs input data
  138. /// @return Status result of function
  139. ///
  140. Status BuildGraph(SessionId session_id, uint32_t graph_id, const std::vector<InputTensorInfo> &inputs);
  141. Status BuildGraph(SessionId session_id, uint32_t graph_id, const std::vector<ge::Tensor> &inputs);
  142. ///
  143. /// @ingroup ge_session
  144. /// @brief run a graph of the session with specific session id for train asynchronously
  145. /// @param [in] session_id session id
  146. /// @param [in] graph_id graph id
  147. /// @param [in] inputs input data
  148. /// @return Status result of function
  149. ///
  150. Status RunGraphAsync(SessionId session_id, uint32_t graph_id, const std::vector<ge::Tensor> &inputs,
  151. RunAsyncCallback callback);
  152. ///
  153. /// @ingroup ge_graph
  154. /// @brief get variables in the session with specific session id
  155. /// @param [in] session_id: sssion id
  156. /// @param [in] var_names: variable names
  157. /// @param [out] var_values: variable values
  158. /// @return Status result of function
  159. ///
  160. Status GetVariables(SessionId session_id, const std::vector<std::string> &var_names,
  161. std::vector<Tensor> &var_values);
  162. ///
  163. /// @ingroup ge_graph
  164. /// @brief me register the callback function to get the result of summary or checkpoin
  165. /// @param [in] session_id session id
  166. /// @param [in] key: summary or checkpoint
  167. /// @param [in] callbak: The real callback object of me
  168. /// @return Status result of function
  169. ///
  170. Status RegisterCallBackFunc(
  171. SessionId session_id, const std::string &key,
  172. const std::function<Status(uint32_t, const std::map<std::string, ge::Tensor> &)> &callback);
  173. Status RegisterCallBackFunc(
  174. SessionId session_id, const std::string &key,
  175. const std::function<Status(uint32_t, const std::map<AscendString, ge::Tensor> &)> &callback);
  176. bool IsGraphNeedRebuild(SessionId session_id, uint32_t graph_id);
  177. private:
  178. bool HasSession(SessionId session_id);
  179. Status GetNextSessionId(SessionId &next_session_id);
  180. Status SetRtContext(SessionId session_id, rtContext_t rtContext);
  181. std::map<SessionId, SessionPtr> session_manager_map_;
  182. std::mutex mutex_;
  183. bool init_flag_ = false;
  184. };
  185. } // namespace ge
  186. #endif // GE_SESSION_SESSION_MANAGER_H_

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示