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_basic.h 10 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
5 years ago
adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * Copyright 2019-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 MINDSPORE_CCSRC_BACKEND_SESSION_SESSION_BASIC_H
  17. #define MINDSPORE_CCSRC_BACKEND_SESSION_SESSION_BASIC_H
  18. #include <vector>
  19. #include <string>
  20. #include <unordered_map>
  21. #include <utility>
  22. #include <memory>
  23. #include <map>
  24. #include "backend/session/session_context.h"
  25. #include "backend/session/kernel_graph.h"
  26. #include "backend/session/anf_runtime_algorithm.h"
  27. #include "ir/anf.h"
  28. #include "ir/tensor.h"
  29. #include "utils/any.h"
  30. #include "utils/contract.h"
  31. #include "runtime/device/kernel_info.h"
  32. #include "utils/ms_context.h"
  33. #if !defined(_WIN32) && !defined(_WIN64)
  34. #include "debug/debugger/debugger.h"
  35. #endif
  36. namespace mindspore {
  37. using GraphId = uint32_t;
  38. using GraphInfo = std::string;
  39. namespace session {
  40. void ClearPythonParasMap();
  41. using CallBackFunc = uint32_t (*)(uint32_t graph_id,
  42. const std::map<std::string, mindspore::tensor::TensorPtr> &params_list);
  43. using AnyList = std::vector<Any>;
  44. using AnyListPtr = std::shared_ptr<AnyList>;
  45. struct OpRunInfo {
  46. std::string op_name;
  47. PrimitivePtr primitive;
  48. AbstractBasePtr abstract;
  49. ValuePtr value = nullptr;
  50. bool is_dynamic_shape = false;
  51. bool is_auto_mixed_precision = false;
  52. std::string next_op_name = "";
  53. size_t next_input_index = 0;
  54. };
  55. using OpRunInfoPtr = std::shared_ptr<OpRunInfo>;
  56. class Executor;
  57. class SessionBasic : public std::enable_shared_from_this<SessionBasic> {
  58. public:
  59. SessionBasic() : context_(nullptr), summary_callback_(nullptr), device_id_(0) {
  60. #if !defined(_WIN32) && !defined(_WIN64)
  61. debugger_ = nullptr;
  62. #endif
  63. }
  64. virtual void Init(uint32_t device_id) { device_id_ = device_id; }
  65. void InitExecutor(const std::string &device_name, uint32_t device_id);
  66. virtual ~SessionBasic() { summary_callback_ = nullptr; }
  67. GraphId CompileGraph(const GraphSegmentPtr &segment, const AnfNodePtrList &outputs);
  68. GraphId CompileGraph(NotNull<FuncGraphPtr> func_graph);
  69. void BuildGraph(GraphId graphId);
  70. void RunGraph(const GraphId &graph_id, const std::vector<tensor::TensorPtr> &inputs, VectorRef *outputs);
  71. void RunGraphAsync(const GraphId &graph_id, const std::vector<tensor::TensorPtr> &inputs, VectorRef *outputs);
  72. void BuildOp(OpRunInfo *, const GraphInfo &, const std::vector<tensor::TensorPtr> &input_tensors,
  73. const std::vector<int64_t> &tensors_mask);
  74. void RunOp(OpRunInfo *, const GraphInfo &, const std::vector<tensor::TensorPtr> &input_tensors, VectorRef *outputs);
  75. virtual void RegisterSummaryCallBackFunc(const CallBackFunc &callback);
  76. bool CreateCNodeOfKernelGraph(const AnfNodePtr &node, KernelGraph *graph);
  77. std::shared_ptr<KernelGraph> ConstructKernelGraph(const AnfNodePtrList &lst, const AnfNodePtrList &outputs);
  78. std::shared_ptr<KernelGraph> ConstructKernelGraph(const FuncGraphPtr &func_graph,
  79. std::vector<KernelGraphPtr> *all_out_graph);
  80. CNodePtr CreateNewCNode(const CNodePtr &cnode, KernelGraph *graph,
  81. std::unordered_map<AnfNodePtr, AnfNodePtr> *other_graph_cnode);
  82. CNodePtr CreateNewCNode(CNodePtr cnode, KernelGraph *graph);
  83. // get graph id in child graphs by ME front anf node pointer
  84. virtual GraphId GetGraphIdByNode(const AnfNodePtr &) const;
  85. virtual GraphId GetFinalRunGraph() const { return kInvalidGraphId; }
  86. void CheckPSModeConsistence(const KernelGraphPtr &Kernel_graph);
  87. void AssignParamKey(const KernelGraphPtr &kernel_graph);
  88. void InitPSParamAndOptim(const KernelGraphPtr &kernel_graph, const std::vector<tensor::TensorPtr> &inputs_const);
  89. virtual bool CheckModelInputs(uint32_t graph_id, const std::vector<tensor::TensorPtr> &inputs,
  90. std::string *error_msg) const {
  91. return true;
  92. }
  93. void GetModelInputsInfo(uint32_t graph_id, std::vector<tensor::TensorPtr> *inputs,
  94. std::vector<std::string> *inputs_name) const;
  95. void GetModelOutputsInfo(uint32_t graph_id, std::vector<tensor::TensorPtr> *outputs,
  96. std::vector<std::string> *outputs_name) const;
  97. std::vector<tensor::TensorPtr> GetInputNeedLockTensors(const GraphId &graph_id,
  98. const std::vector<tensor::TensorPtr> &inputs);
  99. // Get graph by graph id, if not exist return null ptr
  100. KernelGraphPtr GetGraph(GraphId graph_id) const;
  101. #ifdef ENABLE_DEBUGGER
  102. // set debugger
  103. void SetDebugger() {
  104. debugger_ = Debugger::GetInstance();
  105. auto ms_context = MsContext::GetInstance();
  106. MS_EXCEPTION_IF_NULL(ms_context);
  107. debugger_->Init(device_id_, ms_context->get_param<std::string>(MS_CTX_DEVICE_TARGET));
  108. }
  109. #endif
  110. private:
  111. CNodePtr CreateSwitchInput(const AnfNodePtr &node_input, KernelGraph *graph);
  112. std::vector<AnfNodePtr> CreateSwitchOrPartialNode(const CNodePtr &cnode, KernelGraph *graph);
  113. std::vector<AnfNodePtr> CreateValueNode(const CNodePtr &cnode, KernelGraph *graph);
  114. void CreateCNodeInputs(const CNodePtr &cnode, KernelGraph *graph, std::vector<AnfNodePtr> *cnode_inputs);
  115. std::vector<AnfNodePtr> CreateCallSwitchInputs(const CNodePtr &cnode, KernelGraph *graph);
  116. void GetCNodeInfo(const CNodePtr &cnode, std::vector<AnfNodePtr> *cnode_inputs);
  117. void GetNewCNodeInputs(const CNodePtr &cnode, KernelGraph *graph, std::vector<AnfNodePtr> *cnode_inputs,
  118. std::unordered_map<AnfNodePtr, AnfNodePtr> *other_graph_cnode);
  119. protected:
  120. friend class Executor;
  121. friend class CompileNodesTask;
  122. friend class CompileGraphTask;
  123. friend class BuildGraphTask;
  124. friend class RunGraphTask;
  125. friend class BuildOpTask;
  126. friend class RunOpTask;
  127. virtual bool IsSupportSummary() { return true; }
  128. virtual void CreateOutputTensors(const GraphId &graph_id, const std::vector<tensor::TensorPtr> &input_tensors,
  129. VectorRef *outputs,
  130. std::map<tensor::TensorPtr, session::KernelWithIndex> *tensor_to_node);
  131. virtual GraphId CompileGraphImpl(const AnfNodePtrList &lst, const AnfNodePtrList &outputs) = 0;
  132. virtual GraphId CompileGraphImpl(NotNull<FuncGraphPtr> func_graph) { return kInvalidGraphId; }
  133. virtual GraphId CompileGraphImpl(NotNull<FuncGraphPtr> func_graph, const std::vector<tensor::TensorPtr> &inputs) {
  134. MS_EXCEPTION(NotExistsError) << "Call an empty function";
  135. }
  136. virtual void BuildGraphImpl(GraphId) {}
  137. virtual void RunGraphImpl(const GraphId &graph_id, const std::vector<tensor::TensorPtr> &inputs,
  138. VectorRef *outputs) = 0;
  139. virtual void BuildOpImpl(const OpRunInfo &op_run_info, const GraphInfo &graph_info,
  140. const std::vector<tensor::TensorPtr> &input_tensors,
  141. const std::vector<int64_t> &tensors_mask) {}
  142. virtual void RunOpImpl(const OpRunInfo &op_run_info, const GraphInfo &graph_info,
  143. const std::vector<tensor::TensorPtr> &input_tensors, VectorRef *outputs) {}
  144. void RunInfer(NotNull<FuncGraphPtr> func_graph, const std::vector<tensor::TensorPtr> &inputs);
  145. virtual void SetSummaryNodes(KernelGraph *graph);
  146. virtual void LoadInputData(const std::shared_ptr<KernelGraph> &kernel_graph,
  147. const std::vector<tensor::TensorPtr> &inputs_const) const;
  148. void UpdateOutputs(const std::shared_ptr<KernelGraph> &kernel_graph, VectorRef *const outputs,
  149. const std::vector<tensor::TensorPtr> &input_tensors) const;
  150. void Reorder(std::vector<CNodePtr> *node_list);
  151. void Summary(KernelGraph *graph);
  152. // create graph output for RunOp
  153. void CreateOutputNode(const CNodePtr &cnode, const std::shared_ptr<KernelGraph> &graph);
  154. CNodePtr ConstructOutput(const AnfNodePtrList &outputs, const std::shared_ptr<KernelGraph> &graph);
  155. // create a single run op graph
  156. std::shared_ptr<KernelGraph> ConstructSingleOpGraph(const OpRunInfo &op_run_info,
  157. const std::vector<tensor::TensorPtr> &input_tensors,
  158. const std::vector<int64_t> &tensors_mask);
  159. // create a new kernel graph and update the graph sum
  160. KernelGraphPtr NewKernelGraph();
  161. std::vector<AnfNodePtr> CreateParameterFromTuple(const AnfNodePtr &node, KernelGraph *graph);
  162. virtual ParameterPtr CreateNewParameterFromParameter(const AnfNodePtr &anf, KernelGraph *graph);
  163. ValueNodePtr CreateValueNodeKernelGraph(const AnfNodePtr &anf, KernelGraph *graph);
  164. ParameterPtr CreateNewParameter(const AnfNodePtr &anf, KernelGraph *graph);
  165. AnfNodePtr CreateNewParameterFromCNode(const AnfNodePtr &anf, KernelGraph *graph);
  166. void AddParameterToGraphInputs(const std::vector<AnfNodePtr> &parameters, KernelGraph *graph);
  167. void InitInternalOutputParameter(const AnfNodePtr &out_node, const AnfNodePtr &parameter);
  168. AnfNodePtr FindPullNode(const AnfNodePtr &push_node, const std::vector<AnfNodePtr> &node_list);
  169. void UpdateGraphDynamicShapeAttr(const NotNull<KernelGraphPtr> &root_graph);
  170. void UpdateAllGraphDynamicShapeAttr(const std::vector<KernelGraphPtr> &all_graphs);
  171. std::unordered_map<GraphId, std::shared_ptr<KernelGraph>> graphs_;
  172. std::unordered_map<GraphInfo, std::shared_ptr<KernelGraph>> run_op_graphs_;
  173. std::unordered_map<FuncGraphPtr, KernelGraphPtr> front_backend_graph_map_;
  174. std::shared_ptr<Context> context_;
  175. CallBackFunc summary_callback_;
  176. static GraphId graph_sum_;
  177. uint32_t device_id_;
  178. std::shared_ptr<Executor> executor_;
  179. #if !defined(_WIN32) && !defined(_WIN64)
  180. std::shared_ptr<Debugger> debugger_;
  181. #endif
  182. };
  183. using SessionPtr = std::shared_ptr<session::SessionBasic>;
  184. using NamedSummaryOutputs = std::map<std::string, std::pair<AnfNodePtr, int>>;
  185. } // namespace session
  186. } // namespace mindspore
  187. #endif // MINDSPORE_CCSRC_BACKEND_SESSION_SESSION_BASIC_H