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.

pynative_execute.h 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_PYNATIVE_PYNATIVE_EXECUTE_H_
  17. #define MINDSPORE_CCSRC_PYNATIVE_PYNATIVE_EXECUTE_H_
  18. #include <vector>
  19. #include <utility>
  20. #include <string>
  21. #include <memory>
  22. #include <unordered_map>
  23. #include <mutex>
  24. #include <stack>
  25. #include "pybind11/pybind11.h"
  26. #include "pybind11/numpy.h"
  27. #include "pynative/base.h"
  28. #include "utils/context/ms_context.h"
  29. #include "ir/anf.h"
  30. #include "pipeline/resource.h"
  31. #include "operator/composite/composite.h"
  32. namespace mindspore {
  33. namespace pynative {
  34. namespace py = pybind11;
  35. using ResourcePtr = std::shared_ptr<pipeline::Resource>;
  36. using GradOperationPtr = std::shared_ptr<prim::GradOperation>;
  37. py::object RunOpInVM(const OpExecInfoPtr &op_exec_info, PynativeStatusCode *status);
  38. py::tuple RunOp(const py::args &args);
  39. py::tuple ConvertInputs(const PrimitivePyPtr &prim, const py::list &py_args, py::tuple *const out_args,
  40. py::list *const out_args_list);
  41. void ClearPyNativeSession();
  42. struct GraphInfo {
  43. std::unordered_map<std::string, AnfNodePtr> param_map;
  44. std::unordered_map<std::string, std::pair<AnfNodePtr, std::vector<int>>> obj_node_map;
  45. AnfNodePtr output;
  46. std::vector<std::string> objects;
  47. };
  48. class PynativeExecutor : public std::enable_shared_from_this<PynativeExecutor> {
  49. public:
  50. static std::shared_ptr<PynativeExecutor> GetInstance() {
  51. std::lock_guard<std::mutex> i_lock(instance_lock_);
  52. if (executor_ == nullptr) {
  53. executor_ = std::shared_ptr<PynativeExecutor>(new (std::nothrow) PynativeExecutor());
  54. resource_ = std::make_shared<pipeline::Resource>();
  55. }
  56. return executor_;
  57. }
  58. void NewGraph(const py::object &cell, const py::args &args);
  59. void NewGraphInner(const py::object &cell, const py::args &args);
  60. void EndGraph(const py::object &cell, const py::object &out, const py::args &args);
  61. void EndGraphInner(const py::object &cell, const py::object &out, const py::args &args);
  62. void EndGraphByOutId(const std::string &out_id, const py::object &cell, const py::object &out, const py::args &args);
  63. std::vector<AnfNodePtr> GetWeightsArgs(const py::object &weights);
  64. abstract::AbstractBasePtrList GetArgsSpec(const py::args &args);
  65. void GradNet(const GradOperationPtr &grad, const py::object &cell, const py::object &weights, const py::args &args);
  66. void GradNetInner(const GradOperationPtr &grad, const py::object &cell, const py::object &weights,
  67. const py::args &args);
  68. void Clear(const std::string &flag = "");
  69. void Clean();
  70. void ClearRes();
  71. bool grad_flag() { return grad_flag_; }
  72. void set_grad_flag(bool flag) { grad_flag_ = flag; }
  73. AnfNodePtr GetInput(const py::object &obj, const py::object &op_mask);
  74. AnfNodePtr GetObjNode(const py::object &obj);
  75. FuncGraphPtr curr_g() { return curr_g_; }
  76. void set_pyobj(FuncGraphPtr g, const std::string obj) { graph_info_map_[g].objects.push_back(obj); }
  77. void set_obj_node_map(FuncGraphPtr g, const std::string obj, AnfNodePtr node) {
  78. graph_info_map_[g].obj_node_map[obj] = std::make_pair(node, std::vector<int>{-1});
  79. }
  80. void set_obj_node_map(FuncGraphPtr g, const std::string obj, AnfNodePtr node, int index) {
  81. graph_info_map_[g].obj_node_map[obj] = std::make_pair(node, std::vector<int>{index});
  82. }
  83. void set_obj_node_map(FuncGraphPtr g, const std::string obj, AnfNodePtr node, std::vector<int> index) {
  84. graph_info_map_[g].obj_node_map[obj] = std::make_pair(node, index);
  85. }
  86. AnfNodePtr MakeCNode(const OpExecInfoPtr &op_exec_info, const py::args &args, const py::tuple &out);
  87. py::object Run(const py::tuple &args, const py::object &phase);
  88. void Pushp();
  89. void Popp();
  90. FuncGraphPtr GradGraph(FuncGraphPtr g, const GradOperationPtr &grad_op, const std::vector<AnfNodePtr> &weights,
  91. size_t arg_size);
  92. void SetTupleOutput(const py::object &obj, const AnfNodePtr &cnode, std::vector<int> idx);
  93. AnfNodePtr MakeValueNode(const py::object &obj, const std::string &obj_id);
  94. ~PynativeExecutor();
  95. private:
  96. PynativeExecutor();
  97. static std::shared_ptr<PynativeExecutor> executor_;
  98. static std::mutex instance_lock_;
  99. static ResourcePtr resource_;
  100. bool grad_flag_;
  101. std::unordered_map<std::string, FuncGraphPtr> graph_map_;
  102. std::unordered_map<std::string, FuncGraphPtr> cell_graph_map_;
  103. std::unordered_map<FuncGraphPtr, GraphInfo> graph_info_map_;
  104. std::stack<FuncGraphPtr> graph_p_;
  105. FuncGraphPtr top_g_;
  106. FuncGraphPtr df_builder_;
  107. FuncGraphPtr curr_g_;
  108. };
  109. using PynativeExecutorPtr = std::shared_ptr<PynativeExecutor>;
  110. } // namespace pynative
  111. } // namespace mindspore
  112. #endif // MINDSPORE_CCSRC_PYNATIVE_PYNATIVE_EXECUTE_H_