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.2 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
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 "common/ge_inner_error_codes.h"
  24. #include "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. friend class GELib;
  31. public:
  32. ///
  33. /// @ingroup ge_session
  34. /// @brief create session
  35. /// @param [in] options session config options
  36. /// @param [out] session_id session id
  37. /// @return Status result of function
  38. ///
  39. Status CreateSession(const std::map<std::string, std::string> &options, SessionId &session_id);
  40. ///
  41. /// @ingroup ge_session
  42. /// @brief destroy the session with specific session id
  43. /// @param [in] session_id session id
  44. /// @return Status result of function
  45. ///
  46. Status DestroySession(SessionId session_id);
  47. ///
  48. /// @ingroup ge_session
  49. /// @brief add a graph to the session with specific session id
  50. /// @param [in] session_id session id
  51. /// @param [in] graph_id graph id
  52. /// @param [in] graph the graph to add
  53. /// @return Status result of function
  54. ///
  55. Status AddGraph(SessionId session_id, uint32_t graph_id, const ge::Graph &graph);
  56. ///
  57. /// @ingroup ge_session
  58. /// @brief add a graph to the session with specific session id and graphOptions
  59. /// @param [in] session_id session id
  60. /// @param [in] graph_id graph id
  61. /// @param [in] graph the graph to add
  62. /// @param [in] options graph level options
  63. /// @return Status result of function
  64. ///
  65. Status AddGraph(SessionId session_id, uint32_t graph_id, const Graph &graph,
  66. const std::map<std::string, std::string> &options);
  67. ///
  68. /// @ingroup ge_session
  69. /// @brief add a copy graph to the session with specific session id and graphOptions
  70. /// @param [in] session_id session id
  71. /// @param [in] graph_id graph id
  72. /// @param [in] graph the graph to add
  73. /// @param [in] options graph level options
  74. /// @return Status result of function
  75. ///
  76. Status AddGraphWithCopy(SessionId session_id, uint32_t graph_id, const Graph &graph,
  77. const std::map<std::string, std::string> &options);
  78. ///
  79. /// @ingroup ge_session
  80. /// @brief run a graph of the session with specific session id
  81. /// @param [in] session_id session id
  82. /// @param [in] graph_id graph id
  83. /// @param [in] inputs input data
  84. /// @param [out] outputs output data
  85. /// @return Status result of function
  86. ///
  87. Status RunGraph(SessionId session_id, uint32_t graph_id, const std::vector<Tensor> &inputs,
  88. std::vector<Tensor> &outputs);
  89. ///
  90. /// @ingroup ge_session
  91. /// @brief run a graph of the session with specific stream asynchronously
  92. /// @param [in] session_id session id
  93. /// @param [in] graph_id graph id
  94. /// @param [in] stream specific stream
  95. /// @param [in] inputs input data
  96. /// @param [out] outputs output data
  97. /// @return Status result of function
  98. ///
  99. Status RunGraphWithStreamAsync(SessionId session_id, uint32_t graph_id, rtStream_t stream,
  100. const std::vector<Tensor> &inputs, std::vector<Tensor> &outputs);
  101. ///
  102. /// @ingroup ge_session
  103. /// @brief remove a graph from the session with specific session id
  104. /// @param [in] session_id session id
  105. /// @param [in] graph_id graph id
  106. /// @return Status result of function
  107. ///
  108. Status RemoveGraph(SessionId session_id, uint32_t graph_id);
  109. ///
  110. /// @ingroup ge_session
  111. /// @brief get variable value from the session with specific session id
  112. /// @param [in] session_id session id
  113. /// @param [in] name op name
  114. /// @param [out] val out value tensor
  115. /// @return Status result of function
  116. ///
  117. Status GetVariable(SessionId session_id, const std::string &name, Tensor &val);
  118. ///
  119. /// @ingroup ge_session
  120. /// @brief build a graph of the session with specific session id
  121. /// @param [in] session_id session id
  122. /// @param [in] graph_id graph id
  123. /// @param [in] inputs input data
  124. /// @return Status result of function
  125. ///
  126. Status BuildGraph(SessionId session_id, uint32_t graph_id, const std::vector<InputTensorInfo> &inputs);
  127. ///
  128. /// @ingroup ge_session
  129. /// @brief run a graph of the session with specific session id for train asynchronously
  130. /// @param [in] session_id session id
  131. /// @param [in] graph_id graph id
  132. /// @param [in] inputs input data
  133. /// @return Status result of function
  134. ///
  135. Status RunGraphAsync(SessionId session_id, uint32_t graph_id, const std::vector<InputTensorInfo> &inputs,
  136. RunAsyncCallback callback);
  137. ///
  138. /// @ingroup ge_graph
  139. /// @brief get variables in the session with specific session id
  140. /// @param [in] session_id: sssion id
  141. /// @param [in] var_names: variable names
  142. /// @param [out] var_values: variable values
  143. /// @return Status result of function
  144. ///
  145. Status GetVariables(SessionId session_id, const std::vector<std::string> &var_names,
  146. std::vector<Tensor> &var_values);
  147. ///
  148. /// @ingroup ge_graph
  149. /// @brief me register the callback function to get the result of summary or checkpoin
  150. /// @param [in] session_id session id
  151. /// @param [in] key: summary or checkpoint
  152. /// @param [in] callbak: The real callback object of me
  153. /// @return Status result of function
  154. ///
  155. Status RegisterCallBackFunc(
  156. SessionId session_id, const std::string &key,
  157. const std::function<Status(uint32_t, const std::map<std::string, ge::Tensor> &)> &callback);
  158. Status RegisterCallBackFunc(
  159. SessionId session_id, const std::string &key,
  160. const std::function<Status(uint32_t, const std::map<AscendString, ge::Tensor> &)> &callback);
  161. bool IsGraphNeedRebuild(SessionId session_id, uint32_t graph_id);
  162. private:
  163. SessionManager() = default;
  164. ~SessionManager() = default;
  165. ///
  166. /// @ingroup ge_session
  167. /// @brief initialize session manager
  168. /// @param [in] options session manager config options
  169. /// @return Status result of function
  170. ///
  171. Status Initialize(const std::map<std::string, std::string> &options);
  172. ///
  173. /// @ingroup ge_session
  174. /// @brief finalize session manager
  175. /// @return Status result of function
  176. ///
  177. Status Finalize();
  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两部分组成,详细的架构图如下所示