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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_SESSION_SESSION_BASIC_H
  17. #define MINDSPORE_CCSRC_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 "utils/base_ref_extends.h"
  25. #include "session/session_context.h"
  26. #include "session/kernel_graph.h"
  27. #include "ir/anf.h"
  28. #include "ir/tensor.h"
  29. #include "utils/any.h"
  30. #include "utils/contract.h"
  31. #include "pynative/pynative_execute.h"
  32. #include "device/kernel_info.h"
  33. #ifdef ENABLE_DEBUGGER
  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. using OpRunInfo = pynative::OpExecInfo;
  46. using OpRunInfoPtr = std::shared_ptr<OpRunInfo>;
  47. class SessionBasic {
  48. public:
  49. SessionBasic() : context_(nullptr), summary_callback_(nullptr), device_id_(0) {
  50. #ifdef ENABLE_DEBUGGER
  51. debugger_ = nullptr;
  52. #endif
  53. }
  54. virtual void Init(uint32_t device_id) { device_id_ = device_id; }
  55. virtual ~SessionBasic() { summary_callback_ = nullptr; }
  56. virtual GraphId CompileGraph(const AnfNodePtrList &lst, const AnfNodePtrList &outputs) = 0;
  57. virtual GraphId CompileGraph(NotNull<FuncGraphPtr> func_graph) { return kInvalidGraphId; }
  58. // build graph, used to handle multiple child graphs
  59. virtual void BuildGraph(GraphId) {}
  60. virtual void RunGraph(const GraphId &graph_id, const std::vector<tensor::TensorPtr> &inputs, VectorRef *outputs) = 0;
  61. virtual void BuildOp(const OpRunInfo &, const GraphInfo &, const std::vector<tensor::TensorPtr> &input_tensors,
  62. const std::vector<int> &tensors_mask) {}
  63. virtual py::tuple RunOp(const OpRunInfo &, const GraphInfo &, const std::vector<tensor::TensorPtr> &input_tensors) {
  64. return py::tuple();
  65. }
  66. virtual void RegisterSummaryCallBackFunc(const CallBackFunc &callback);
  67. void CreateCNodeKernelGraph(const AnfNodePtr node, KernelGraphPtr graph);
  68. std::shared_ptr<KernelGraph> ConstructKernelGraph(const AnfNodePtrList &lst, const AnfNodePtrList &outputs);
  69. std::shared_ptr<KernelGraph> ConstructKernelGraph(const FuncGraphPtr &func_graph,
  70. std::vector<KernelGraphPtr> *all_out_graph);
  71. CNodePtr CreateNewCNode(const CNodePtr &cnode, bool valid_input, KernelGraph *graph, bool *from_other_graph,
  72. std::unordered_map<AnfNodePtr, AnfNodePtr> *other_graph_cnode);
  73. CNodePtr CreateNewCNode(const CNodePtr &cnode, KernelGraph *graph);
  74. // set parameters of final graph
  75. virtual GraphId SetFinalGraphInput(const std::vector<AnfNodePtr> &) { return kInvalidGraphId; }
  76. // set output of final graph
  77. virtual void SetFinalGraphOutput(const BaseRef &) {}
  78. // insert switch and set the relative active ops
  79. virtual void SwitchCompile(GraphId, GraphId, GraphId, const AnfNodePtr &) {}
  80. // set args of child graph.the arg maybe come from a output of other child graphs,or from final graph's parameter
  81. virtual void SetChildGraphInput(GraphId, const VectorRef &) {}
  82. // get graph id in child graphs by ME front anf node pointer
  83. virtual GraphId GetGraphIdByNode(const AnfNodePtr &) const { return kInvalidGraphId; }
  84. virtual GraphId GetFinalRunGraph() const { return kInvalidGraphId; }
  85. virtual void SetActive(GraphId, GraphId) {}
  86. virtual void GetSummaryNodes(KernelGraph *graph);
  87. #ifdef ENABLE_DEBUGGER
  88. // set debugger
  89. void SetDebugger() {
  90. debugger_ = Debugger::GetInstance();
  91. debugger_->Init(device_id_);
  92. }
  93. #endif
  94. protected:
  95. // Get graph by graph id ,if not exist return null ptr
  96. KernelGraphPtr GetGraph(GraphId graph_id);
  97. virtual void LoadInputData(const std::shared_ptr<KernelGraph> &kernel_graph,
  98. const std::vector<tensor::TensorPtr> &inputs_const) const;
  99. void UpdateOutputs(const std::shared_ptr<KernelGraph> &kernel_graph, VectorRef *const outputs,
  100. const std::vector<tensor::TensorPtr> &input_tensors) const;
  101. void Reorder(std::vector<CNodePtr> *node_list);
  102. void Summary(KernelGraph *graph);
  103. // create graph output for RunOp
  104. void CreateOutputNode(const CNodePtr &cnode, const std::shared_ptr<KernelGraph> &graph);
  105. CNodePtr ConstructOutput(const AnfNodePtrList &outputs, const std::shared_ptr<KernelGraph> &graph);
  106. // create a single run op graph
  107. std::shared_ptr<KernelGraph> ConstructSingleOpGraph(const OpRunInfo &op_run_info,
  108. const std::vector<tensor::TensorPtr> &input_tensors,
  109. const std::vector<int> &tensors_mask);
  110. // trans BaseRef list to py::tuple
  111. BaseRef TransformBaseRefListToTuple(const BaseRef &base_ref);
  112. // create a new kernel graph and update the graph sum
  113. KernelGraphPtr NewKernelGraph();
  114. std::vector<AnfNodePtr> CreateParameterFromTuple(const AnfNodePtr &node, bool valid_input, KernelGraph *graph);
  115. virtual ParameterPtr CreateNewParameterFromParameter(const AnfNodePtr &anf, bool valid_input, KernelGraph *graph);
  116. ValueNodePtr CreateValueNodeKernelGraph(const AnfNodePtr &anf, KernelGraph *graph);
  117. ParameterPtr CreateNewParameter(const AnfNodePtr &anf, KernelGraph *graph);
  118. AnfNodePtr CreateNewParameterFromCNode(const AnfNodePtr &anf, bool valid_input, KernelGraph *graph);
  119. void AddParameterToGraphInputs(const std::vector<AnfNodePtr> &parameters, KernelGraph *graph);
  120. void InitInternalOutputParameter(const AnfNodePtr &out_node, const AnfNodePtr &parameter);
  121. std::unordered_map<GraphId, std::shared_ptr<KernelGraph>> graphs_;
  122. std::unordered_map<GraphInfo, std::shared_ptr<KernelGraph>> run_op_graphs_;
  123. std::unordered_map<FuncGraphPtr, KernelGraphPtr> front_backend_graph_map_;
  124. std::shared_ptr<Context> context_;
  125. CallBackFunc summary_callback_;
  126. static GraphId graph_sum_;
  127. uint32_t device_id_;
  128. #ifdef ENABLE_DEBUGGER
  129. std::shared_ptr<Debugger> debugger_;
  130. #endif
  131. };
  132. using SessionPtr = std::shared_ptr<session::SessionBasic>;
  133. using NamedSummaryOutputs = std::map<std::string, std::pair<AnfNodePtr, int>>;
  134. } // namespace session
  135. } // namespace mindspore
  136. #endif // MINDSPORE_CCSRC_SESSION_SESSION_BASIC_H