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.2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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
  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 run a graph of the session with specific session id
  69. /// @param [in] session_id session id
  70. /// @param [in] graph_id graph id
  71. /// @param [in] inputs input data
  72. /// @param [out] outputs output data
  73. /// @return Status result of function
  74. ///
  75. Status RunGraph(SessionId session_id, uint32_t graph_id, const std::vector<Tensor> &inputs,
  76. std::vector<Tensor> &outputs);
  77. ///
  78. /// @ingroup ge_session
  79. /// @brief remove a graph from the session with specific session id
  80. /// @param [in] session_id session id
  81. /// @param [in] graph_id graph id
  82. /// @return Status result of function
  83. ///
  84. Status RemoveGraph(SessionId session_id, uint32_t graph_id);
  85. ///
  86. /// @ingroup ge_session
  87. /// @brief get variable value from the session with specific session id
  88. /// @param [in] session_id session id
  89. /// @param [in] name op name
  90. /// @param [out] val out value tensor
  91. /// @return Status result of function
  92. ///
  93. Status GetVariable(SessionId session_id, const std::string &name, Tensor &val);
  94. ///
  95. /// @ingroup ge_session
  96. /// @brief build a graph of the session with specific session id
  97. /// @param [in] session_id session id
  98. /// @param [in] graph_id graph id
  99. /// @param [in] inputs input data
  100. /// @return Status result of function
  101. ///
  102. Status BuildGraph(SessionId session_id, uint32_t graph_id, const std::vector<InputTensorInfo> &inputs);
  103. ///
  104. /// @ingroup ge_session
  105. /// @brief run a graph of the session with specific session id for train asynchronously
  106. /// @param [in] session_id session id
  107. /// @param [in] graph_id graph id
  108. /// @param [in] inputs input data
  109. /// @return Status result of function
  110. ///
  111. Status RunGraphAsync(SessionId session_id, uint32_t graph_id, const std::vector<InputTensorInfo> &inputs,
  112. RunAsyncCallback callback);
  113. ///
  114. /// @ingroup ge_graph
  115. /// @brief get variables in the session with specific session id
  116. /// @param [in] session_id: sssion id
  117. /// @param [in] var_names: variable names
  118. /// @param [out] var_values: variable values
  119. /// @return Status result of function
  120. ///
  121. Status GetVariables(SessionId session_id, const std::vector<std::string> &var_names,
  122. std::vector<Tensor> &var_values);
  123. ///
  124. /// @ingroup ge_graph
  125. /// @brief me register the callback function to get the result of summary or checkpoin
  126. /// @param [in] session_id session id
  127. /// @param [in] key: summary or checkpoint
  128. /// @param [in] callbak: The real callback object of me
  129. /// @return Status result of function
  130. ///
  131. Status RegisterCallBackFunc(
  132. SessionId session_id, const std::string &key,
  133. const std::function<Status(uint32_t, const std::map<std::string, ge::Tensor> &)> &callback);
  134. Status RegisterCallBackFunc(
  135. SessionId session_id, const std::string &key,
  136. const std::function<Status(uint32_t, const std::map<ge::AscendString, ge::Tensor> &)> &callback);
  137. bool IsGraphNeedRebuild(SessionId session_id, uint32_t graph_id);
  138. private:
  139. SessionManager() = default;
  140. ~SessionManager() = default;
  141. ///
  142. /// @ingroup ge_session
  143. /// @brief initialize session manager
  144. /// @param [in] options session manager config options
  145. /// @return Status result of function
  146. ///
  147. Status Initialize(const std::map<std::string, std::string> &options);
  148. ///
  149. /// @ingroup ge_session
  150. /// @brief finalize session manager
  151. /// @return Status result of function
  152. ///
  153. Status Finalize();
  154. bool HasSession(SessionId session_id);
  155. Status GetNextSessionId(SessionId &next_session_id);
  156. Status SetRtContext(SessionId session_id, rtContext_t rtContext);
  157. std::map<SessionId, SessionPtr> session_manager_map_;
  158. std::mutex mutex_;
  159. bool init_flag_ = false;
  160. };
  161. } // namespace ge
  162. #endif // GE_SESSION_SESSION_MANAGER_H_

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