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 6.7 kB

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

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