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.

optimizer.h 8.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_FRONTEND_OPTIMIZER_OPTIMIZER_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_OPTIMIZER_H_
  18. #include <algorithm>
  19. #include <functional>
  20. #include <iterator>
  21. #include <memory>
  22. #include <string>
  23. #include <vector>
  24. #include <map>
  25. #include <utility>
  26. #include <initializer_list>
  27. #include "debug/draw.h"
  28. #include "debug/anf_ir_dump.h"
  29. #include "debug/anf_ir_utils.h"
  30. #include "debug/trace.h"
  31. #include "frontend/optimizer/opt.h"
  32. #include "pipeline/jit/resource.h"
  33. #include "pipeline/jit/action.h"
  34. #include "utils/ms_context.h"
  35. namespace mindspore {
  36. namespace opt {
  37. using OptimizeGraphFunc = std::function<bool(const FuncGraphPtr &func_graph, const OptimizerPtr &optimizer)>;
  38. class OptPassConfig {
  39. public:
  40. explicit OptPassConfig(const OptimizeGraphFunc &func) : func_(func) {}
  41. explicit OptPassConfig(const std::vector<SubstitutionPtr> &list, bool is_once = false)
  42. : list_(list), is_once_(is_once) {}
  43. OptPassConfig(const std::initializer_list<SubstitutionPtr> &list, bool is_once = false)
  44. : list_(list), is_once_(is_once) {}
  45. ~OptPassConfig() = default;
  46. const std::vector<SubstitutionPtr> &list() const { return list_; }
  47. const OptimizeGraphFunc &func() const { return func_; }
  48. static OptPassConfig Renormalize() { return OptPassConfig(); }
  49. const bool is_renormalize() const { return is_renormalize_; }
  50. const bool is_once() const { return is_once_; }
  51. private:
  52. OptPassConfig() : is_renormalize_(true) {}
  53. OptimizeGraphFunc func_;
  54. std::vector<SubstitutionPtr> list_;
  55. bool is_renormalize_{false};
  56. bool is_once_{false};
  57. };
  58. class OptPass {
  59. public:
  60. explicit OptPass(const OptimizeGraphFunc &func) : pass_func_(func) {}
  61. ~OptPass() = default;
  62. bool operator()(const FuncGraphPtr &func_graph, const OptimizerPtr &optimizer) const {
  63. return pass_func_(func_graph, optimizer);
  64. }
  65. static OptPass Renormalize() { return OptPass(); }
  66. const bool is_renormalize() const { return is_renormalize_; }
  67. private:
  68. OptPass() : is_renormalize_(true) {}
  69. OptimizeGraphFunc pass_func_;
  70. bool is_renormalize_{false};
  71. };
  72. using OptPassGroupMap = std::vector<std::pair<std::string, OptPassConfig>>;
  73. class Optimizer : public std::enable_shared_from_this<Optimizer> {
  74. public:
  75. Optimizer(const std::string &name, const pipeline::ResourceBasePtr &resource_ptr)
  76. : name_(name),
  77. resource_(resource_ptr),
  78. run_only_once_(false),
  79. is_watch_renormalize_(false),
  80. is_enable_(true),
  81. is_untyped_generated_(false) {}
  82. virtual ~Optimizer() = default;
  83. void Init(const OptPassGroupMap &passes, bool run_only_once) {
  84. run_only_once_ = run_only_once;
  85. is_watch_renormalize_ = false;
  86. is_untyped_generated_ = false;
  87. is_on_debug_ = IS_OUTPUT_ON(mindspore::DEBUG);
  88. for (auto &iter : passes) {
  89. const std::string &name = iter.first;
  90. pass_names_.push_back(name);
  91. const OptPassConfig &config = iter.second;
  92. if (config.is_renormalize()) {
  93. passes_.push_back(OptPass::Renormalize());
  94. continue;
  95. }
  96. if (config.list().size() > 0) {
  97. OptimizeGraphFunc func = SubstitutionList(config.list(), config.is_once());
  98. passes_.push_back(OptPass(func));
  99. continue;
  100. }
  101. passes_.push_back(OptPass(config.func()));
  102. }
  103. if (passes_.size() == 1) {
  104. run_only_once_ = true;
  105. }
  106. }
  107. static std::shared_ptr<Optimizer> MakeOptimizer(const std::string &name, const pipeline::ResourceBasePtr resource_ptr,
  108. const OptPassGroupMap &passes, bool run_only_once = false,
  109. bool watch_renormalize = false) {
  110. OptimizerPtr optimizer = std::make_shared<Optimizer>(name, resource_ptr);
  111. optimizer->Init(passes, run_only_once);
  112. if (watch_renormalize) {
  113. optimizer->enable_watch_renormalize();
  114. }
  115. return optimizer;
  116. }
  117. FuncGraphPtr step(FuncGraphPtr func_graph, bool use_profile = true) {
  118. if (!is_enable_) {
  119. return func_graph;
  120. }
  121. // Optimizer step counter;
  122. int counter = 1;
  123. bool changes = true;
  124. while (changes) {
  125. changes = false;
  126. auto run_runc = [&counter, &func_graph, &changes, use_profile, this]() {
  127. for (size_t i = 0; i < passes_.size(); ++i) {
  128. const OptPass &opt = passes_[i];
  129. CurPass_ = {counter, pass_names_[i]};
  130. auto opt_func = [&func_graph, &changes, &opt, this]() {
  131. if (opt.is_renormalize()) {
  132. auto resource_ptr = std::dynamic_pointer_cast<pipeline::Resource>(resource_);
  133. if (resource_ptr != nullptr) {
  134. // StepParallel may replace the AbstractValue of the parameters of func_graph,
  135. // So generate the args_spec from parameters.
  136. abstract::AbstractBasePtrList maybe_new_args_spec;
  137. if (is_watch_renormalize_) {
  138. if (is_untyped_generated_) {
  139. std::transform(func_graph->parameters().begin(), func_graph->parameters().end(),
  140. std::back_inserter(maybe_new_args_spec),
  141. [](AnfNodePtr param) -> AbstractBasePtr { return param->abstract(); });
  142. func_graph = pipeline::Renormalize(resource_ptr, func_graph, maybe_new_args_spec);
  143. clear_is_untyped_generated();
  144. } else {
  145. MS_LOG(INFO) << "Optimizer::step: Skipping Renormalize because is_untyped_generated_ is False.";
  146. }
  147. } else {
  148. std::transform(func_graph->parameters().begin(), func_graph->parameters().end(),
  149. std::back_inserter(maybe_new_args_spec),
  150. [](AnfNodePtr param) -> AbstractBasePtr { return param->abstract(); });
  151. func_graph = pipeline::Renormalize(resource_ptr, func_graph, maybe_new_args_spec);
  152. }
  153. }
  154. } else if (opt(func_graph, shared_from_this())) {
  155. changes = true;
  156. }
  157. };
  158. use_profile ? (WITH(MsProfile::GetProfile()->Step(pass_names_[i])) opt_func) : opt_func();
  159. if (is_on_debug_ && MsContext::GetInstance()->get_param<bool>(MS_CTX_SAVE_GRAPHS_FLAG)) {
  160. MS_LOG(DEBUG) << "The opt " << name_ << " round " << counter << " OptPass " << pass_names_[i] << " end.";
  161. auto fg_name =
  162. "opt_substep_" + name_ + "_r" + std::to_string(counter) + "_" + std::to_string(i) + "_" + pass_names_[i];
  163. DumpIR(fg_name + ".ir", func_graph);
  164. if (MsContext::GetInstance()->get_param<int>(MS_CTX_EXECUTION_MODE) != kPynativeMode) {
  165. func_graph->DumpFuncGraph(fg_name);
  166. ExportIR(fg_name + ".dat", "", func_graph);
  167. }
  168. MS_LOG(DEBUG) << "Dump " << pass_names_[i] << " func graph.";
  169. }
  170. }
  171. };
  172. use_profile ? (WITH(MsProfile::GetProfile()->Lap(counter)) run_runc) : run_runc();
  173. counter++;
  174. if (run_only_once_) {
  175. break;
  176. }
  177. }
  178. return func_graph;
  179. }
  180. pipeline::ResourceBasePtr resource() const { return resource_; }
  181. FuncGraphManagerPtr manager() const {
  182. if (resource_ != nullptr) {
  183. return resource_->manager();
  184. }
  185. MS_LOG(EXCEPTION) << "No ResourceBase exists.";
  186. }
  187. const std::string name() const { return name_; }
  188. void set_is_untyped_generated() { is_untyped_generated_ = true; }
  189. void clear_is_untyped_generated() { is_untyped_generated_ = false; }
  190. void enable_watch_renormalize() { is_watch_renormalize_ = true; }
  191. void disable_watch_renormalize() { is_watch_renormalize_ = false; }
  192. bool is_watch_renormalize() { return is_watch_renormalize_; }
  193. void set_enable(bool enable) { is_enable_ = enable; }
  194. struct {
  195. int counter;
  196. std::string name;
  197. } CurPass_;
  198. bool is_on_debug_{false};
  199. private:
  200. const std::string name_;
  201. pipeline::ResourceBasePtr resource_;
  202. std::vector<OptPass> passes_;
  203. std::vector<std::string> pass_names_;
  204. bool run_only_once_;
  205. bool is_watch_renormalize_;
  206. bool is_enable_;
  207. bool is_untyped_generated_;
  208. };
  209. } // namespace opt
  210. } // namespace mindspore
  211. #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_OPTIMIZER_H_