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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Copyright 2019 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 "session/session_context.h"
  25. #include "session/kernel_graph.h"
  26. #include "ir/anf.h"
  27. #include "ir/meta_tensor.h"
  28. #include "utils/any.h"
  29. #include "utils/base_ref.h"
  30. #include "pynative/pynative_execute.h"
  31. #include "device/kernel_info.h"
  32. namespace mindspore {
  33. using GraphId = uint32_t;
  34. using GraphInfo = std::string;
  35. namespace session {
  36. using CallBackFunc = uint32_t (*)(uint32_t graph_id,
  37. const std::map<std::string, mindspore::tensor::TensorPtr> &params_list);
  38. using AnyList = std::vector<Any>;
  39. using AnyListPtr = std::shared_ptr<AnyList>;
  40. using OpRunInfo = pynative::OpExecInfo;
  41. using OpRunInfoPtr = std::shared_ptr<OpRunInfo>;
  42. class SessionBasic {
  43. public:
  44. SessionBasic() : device_id_(0) {
  45. graphs_ = {};
  46. run_op_graphs_ = {};
  47. summary_callback_ = nullptr;
  48. }
  49. virtual void Init(uint32_t device_id) { device_id_ = device_id; }
  50. virtual ~SessionBasic() { summary_callback_ = nullptr; }
  51. virtual GraphId CompileGraph(const AnfNodePtrList &lst, const AnfNodePtrList &outputs) = 0;
  52. // build graph, used to handle multiple child graphs
  53. virtual void BuildGraph(GraphId) {}
  54. virtual void RunGraph(const GraphId &graph_id, const std::vector<tensor::TensorPtr> &inputs, VectorRef *outputs) = 0;
  55. virtual void BuildOp(const OpRunInfo &, const GraphInfo &, const std::vector<tensor::TensorPtr> &input_tensors,
  56. const std::vector<bool> &tensors_mask) {}
  57. virtual py::tuple RunOp(const OpRunInfo &, const GraphInfo &, const std::vector<tensor::TensorPtr> &input_tensors) {
  58. return py::tuple();
  59. }
  60. virtual void RegisterSummaryCallBackFunc(const CallBackFunc &callback);
  61. std::shared_ptr<KernelGraph> ConstructKernelGraph(const AnfNodePtrList &lst, const AnfNodePtrList &outputs);
  62. CNodePtr CreateNewCNode(const CNodePtr &cnode, bool valid_input, KernelGraph *graph, bool *from_other_graph,
  63. std::unordered_map<AnfNodePtr, AnfNodePtr> *other_graph_cnode);
  64. // set parameters of final graph
  65. virtual GraphId SetFinalGraphInput(const std::vector<AnfNodePtr> &) { return kInvalidGraphId; }
  66. // set output of final graph
  67. virtual void SetFinalGraphOutput(const BaseRef &) {}
  68. // insert switch and set the relative active ops
  69. virtual void SwitchCompile(GraphId, GraphId, GraphId, const AnfNodePtr &) {}
  70. // set args of child graph.the arg maybe come from a output of other child graphs,or from final graph's parameter
  71. virtual void SetChildGraphInput(GraphId, const VectorRef &) {}
  72. // get graph id in child graphs by ME front anf node pointer
  73. virtual GraphId GetGraphIdByNode(const AnfNodePtr &) const { return kInvalidGraphId; }
  74. virtual GraphId GetFinalRunGraph() const { return kInvalidGraphId; }
  75. virtual void SetActive(GraphId, GraphId) {}
  76. protected:
  77. virtual void LoadInputData(const std::shared_ptr<KernelGraph> &kernel_graph,
  78. const std::vector<tensor::TensorPtr> &inputs_const) const;
  79. void UpdateOutputs(const std::shared_ptr<KernelGraph> &kernel_graph, VectorRef *const outputs,
  80. const std::vector<tensor::TensorPtr> &input_tensors) const;
  81. void Reorder(std::vector<CNodePtr> *node_list);
  82. void Summary(KernelGraph *graph);
  83. // create graph output for RunOp
  84. void CreateOutputNode(const CNodePtr &cnode, const std::shared_ptr<KernelGraph> &graph);
  85. CNodePtr ConstructOutput(const AnfNodePtrList &outputs, const std::shared_ptr<KernelGraph> &graph);
  86. // create a single run op graph
  87. std::shared_ptr<KernelGraph> ConstructSingleOpGraph(const OpRunInfo &op_run_info,
  88. const std::vector<tensor::TensorPtr> &input_tensors,
  89. const std::vector<bool> &tensors_mask);
  90. // trans BaseRef list to py::tuple
  91. BaseRef TransformBaseRefListToTuple(const BaseRef &base_ref);
  92. std::unordered_map<GraphId, std::shared_ptr<KernelGraph>> graphs_;
  93. std::unordered_map<GraphInfo, std::shared_ptr<KernelGraph>> run_op_graphs_;
  94. std::shared_ptr<Context> context_;
  95. CallBackFunc summary_callback_;
  96. static GraphId graph_sum_;
  97. uint32_t device_id_;
  98. };
  99. using SessionPtr = std::shared_ptr<session::SessionBasic>;
  100. } // namespace session
  101. } // namespace mindspore
  102. #endif // MINDSPORE_CCSRC_SESSION_SESSION_BASIC_H