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.

pipeline.h 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_PIPELINE_JIT_PIPELINE_H_
  17. #define MINDSPORE_CCSRC_PIPELINE_JIT_PIPELINE_H_
  18. #include <vector>
  19. #include <utility>
  20. #include <string>
  21. #include <memory>
  22. #include <unordered_map>
  23. #include <map>
  24. #include <mutex>
  25. #include "pybind11/pybind11.h"
  26. #include "ir/anf.h"
  27. #include "ir/tensor.h"
  28. #include "pipeline/jit/action.h"
  29. #include "vm/segment_runner.h"
  30. #include "vm/transform.h"
  31. #include "pipeline/jit/base.h"
  32. namespace mindspore {
  33. extern const char kMsConvert[];
  34. extern const char kMsVm[];
  35. // namespace to support pipeline structures definition
  36. namespace pipeline {
  37. namespace py = pybind11;
  38. class Pipeline {
  39. public:
  40. Pipeline(const ResourcePtr &res, const std::vector<ActionItem> &actions) : resource_(res), actions_(actions) {}
  41. ~Pipeline() = default;
  42. void Run();
  43. ResourcePtr resource() { return resource_; }
  44. private:
  45. ResourcePtr resource_;
  46. std::vector<ActionItem> actions_;
  47. };
  48. // A function pipeline.
  49. class ExecutorPy : public std::enable_shared_from_this<ExecutorPy> {
  50. public:
  51. static std::shared_ptr<ExecutorPy> GetInstance() {
  52. std::lock_guard<std::mutex> i_lock(instance_lock_);
  53. if (executor_ == nullptr) {
  54. executor_ = std::shared_ptr<ExecutorPy>(new (std::nothrow) ExecutorPy());
  55. }
  56. return executor_;
  57. }
  58. ~ExecutorPy();
  59. void SaveCompiledGraph(const std::string &phase_s);
  60. bool CompileInner(const py::object &obj, const py::tuple &args, const py::object &phase, bool use_vm);
  61. bool Compile(const py::object &obj, const py::tuple &args, const py::object &phase, bool use_vm);
  62. void ProcessVmArg(const py::tuple &args, const std::string &phase, VectorRef *arg_list);
  63. // for pynative mode when use_vm is on
  64. py::object Run(const py::tuple &args, const py::object &phase);
  65. ResourcePtr GetResource(const std::string &phase);
  66. FuncGraphPtr GetFuncGraph(const std::string &phase);
  67. py::bytes GetFuncGraphProto(const std::string &phase, const std::string &type);
  68. compile::VmEvalFuncPtr GetVmEvalFunc(const std::string &phase);
  69. bool HasCompiled(const std::string &phase) const;
  70. FuncGraphPtr BuildGraph(const py::dict &init_params, const std::string &phase,
  71. const py::object &broadcast_params = {});
  72. void UpdataParamNodeDefaultInput(const std::string &phase,
  73. const std::unordered_map<std::string, tensor::TensorPtr> &params);
  74. void RunInitGraph(const py::dict &init_params, const std::string &phase);
  75. py::dict GetParameterLayout(const std::string &phase);
  76. py::dict GetCNodeStrategy(const std::string &phase);
  77. void SetCNodeStrategy(const std::string &name, const parallel::Strategys &strategy);
  78. py::dict GetAllreduceFusion(const std::string &phase);
  79. void DelNetRes(const std::string &id);
  80. void ReleaseResource(const py::object &phase);
  81. static void ClearRes();
  82. static bool GetDebugTerminate() { return debugger_terminate_; }
  83. static void DebugTerminate(bool val) { debugger_terminate_ = val; }
  84. void TerminateDebugger();
  85. std::map<std::string, std::pair<PrimitivePyPtr, std::string>> FetchInfoForQuantExport(const std::string &phase_s);
  86. private:
  87. ExecutorPy();
  88. void ConvertObjectToTensors(const py::dict &dict, std::map<std::string, tensor::TensorPtr> *tensors);
  89. void GetGeBackendPolicy() const;
  90. // filter some pipeline actions according to phase, e.g. when exporting onnx, it is no need to execute actions after
  91. // 'validate' stage
  92. static std::vector<ActionItem> FilterActions(const std::vector<ActionItem> &actions, const std::string &phase);
  93. std::map<std::string, ExecutorInfoPtr> info_;
  94. static std::shared_ptr<ExecutorPy> executor_;
  95. static std::mutex instance_lock_;
  96. static bool debugger_terminate_;
  97. std::map<std::string, py::dict> stra_dict_;
  98. std::string phase_ = "";
  99. };
  100. using ExecutorPyPtr = std::shared_ptr<ExecutorPy>;
  101. // Generate a key for mapping function graph
  102. py::tuple GenerateKey(const std::string &name, const std::unordered_map<std::string, py::object> &defaults);
  103. py::bool_ VerifyInputSignature(const py::list &input_signature, const py::tuple &inputs);
  104. bool InitDistribute(const std::map<std::string, std::string> &options);
  105. void ResetOpId();
  106. void InitHccl();
  107. void FinalizeHccl();
  108. void InitBackend();
  109. void FinalizeBackend();
  110. void ClearResAtexit();
  111. void ReleaseGeTsd();
  112. void ExportGraph(const std::string &file_name, const std::string &, const std::string &phase);
  113. // init and exec dataset sub graph
  114. bool InitExecDataset(const std::string &queue_name, int64_t iter_num, int64_t batch_size,
  115. const std::vector<TypePtr> &types, const std::vector<std::vector<int64_t>> &shapes,
  116. const std::vector<int64_t> &input_indexes, const std::string &phase, bool need_run);
  117. // Build and run dataset subgraph for ms backend
  118. bool InitExecDatasetVm(const std::string &queue_name, int64_t size, int64_t batch_size,
  119. const std::vector<TypePtr> &types, const std::vector<std::vector<int64_t>> &shapes,
  120. const std::vector<int64_t> &input_indexes, bool need_run);
  121. void ProcessVmArgInner(const py::tuple &args, const ResourcePtr &res, VectorRef *const arg_list);
  122. } // namespace pipeline
  123. } // namespace mindspore
  124. #endif // MINDSPORE_CCSRC_PIPELINE_JIT_PIPELINE_H_