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.

grad.cc 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * Copyright 2020-2021 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. #include "frontend/optimizer/ad/grad.h"
  17. #include "frontend/optimizer/ad/dfunctor.h"
  18. #include "frontend/optimizer/irpass.h"
  19. #include "ir/func_graph_cloner.h"
  20. #include "utils/ms_context.h"
  21. #include "utils/symbolic.h"
  22. namespace mindspore {
  23. namespace ad {
  24. namespace {
  25. FuncGraphPtr PartialEliminateOptPass(const ResourcePtr &resource, const FuncGraphPtr &func_graph) {
  26. MS_EXCEPTION_IF_NULL(resource);
  27. opt::irpass::OptimizeIRPassLib irpass;
  28. opt::OptPassConfig partial_eliminate_opt_ = opt::OptPassConfig(
  29. {irpass.partial_eliminate_, irpass.switch_partial_eliminater_, irpass.switch_layer_partial_eliminater_});
  30. opt::OptPassGroupMap map({{"partial_eliminate_", partial_eliminate_opt_}});
  31. auto after_lift_opt = opt::Optimizer::MakeOptimizer("partial_eliminate", resource, map);
  32. FuncGraphPtr opt_fg = nullptr;
  33. WITH(MsProfile::GetProfile()->Step("partial_eliminate_before_grad"))[&after_lift_opt, func_graph, &opt_fg]() {
  34. opt_fg = after_lift_opt->step(func_graph, true);
  35. };
  36. return opt_fg;
  37. }
  38. FuncGraphPtr LiftFv(const pipeline::ResourceBasePtr &resource, const FuncGraphPtr &func_graph) {
  39. #ifdef ENABLE_DUMP_IR
  40. bool save_graphs_flag = MsContext::GetInstance()->get_param<bool>(MS_CTX_SAVE_GRAPHS_FLAG);
  41. if (save_graphs_flag) {
  42. DumpIR("before_lift_" + func_graph->ToString() + ".ir", func_graph);
  43. }
  44. #endif
  45. FuncGraphPtr new_fg = LiftingClone(func_graph);
  46. #ifdef ENABLE_DUMP_IR
  47. if (save_graphs_flag) {
  48. DumpIR("after_lift_" + new_fg->ToString() + ".ir", new_fg);
  49. }
  50. #endif
  51. auto new_res = std::dynamic_pointer_cast<pipeline::Resource>(resource);
  52. if (new_res == nullptr) {
  53. MS_LOG(EXCEPTION) << "Parameter resources is not a pipeline::Resource";
  54. }
  55. auto opt_fg = PartialEliminateOptPass(new_res, new_fg);
  56. #ifdef ENABLE_DUMP_IR
  57. if (save_graphs_flag) {
  58. DumpIR("after_opt_" + opt_fg->ToString() + ".ir", opt_fg);
  59. }
  60. #endif
  61. return opt_fg;
  62. }
  63. } // namespace
  64. FuncGraphPtr Grad(const FuncGraphPtr &func_graph, const pipeline::ResourceBasePtr &resources, bool is_top) {
  65. MS_EXCEPTION_IF_NULL(func_graph);
  66. auto gradkv = func_graph->transforms().find("grad");
  67. if (gradkv != func_graph->transforms().end()) {
  68. return gradkv->second.func_graph();
  69. }
  70. auto manager_ptr = resources->manager();
  71. MS_EXCEPTION_IF_NULL(manager_ptr);
  72. manager_ptr->AddFuncGraph(func_graph);
  73. FuncGraphPtr grad_fg = func_graph;
  74. lift_fv_before_grad = (common::GetEnv("ENV_DONT_LIFT_FV_BEFORE_GRAD") != "1");
  75. if (lift_fv_before_grad && func_graph->func_graphs_used().size() != 0) {
  76. grad_fg = LiftFv(resources, func_graph);
  77. }
  78. auto multi_graph_sink = [&func_graph](const FuncGraphPtr &f) {
  79. if (MsContext::GetInstance()->get_param<bool>(MS_CTX_IS_MULTI_GRAPH_SINK)) {
  80. if (func_graph->has_flag(FUNC_GRAPH_FLAG_IGNORE_VALUES)) {
  81. f->set_flag(FUNC_GRAPH_FLAG_IGNORE_VALUES, true);
  82. }
  83. }
  84. };
  85. auto f = std::make_shared<DFunctor>(grad_fg, resources);
  86. auto user_defined = f->KUserDefined(grad_fg);
  87. if (user_defined != nullptr) {
  88. multi_graph_sink(user_defined);
  89. if (is_top) {
  90. DFunctor::Clear();
  91. }
  92. return user_defined;
  93. }
  94. f->Init(is_top);
  95. f->MapObject();
  96. f->MapMorphism();
  97. f->Finish();
  98. auto res = f->k_graph();
  99. auto tape = f->tape();
  100. tape->set_flag(mindspore::kFuncGraphFlagBackPropEntry, true);
  101. if (is_top) {
  102. DFunctor::Clear();
  103. }
  104. multi_graph_sink(res);
  105. if (func_graph != grad_fg) {
  106. (void)func_graph->transforms().emplace("grad", FuncGraphTransform(res));
  107. }
  108. return res;
  109. }
  110. FuncGraphPtr Kprim(const ValueNodePtr &value_node, const pipeline::ResourceBasePtr &resources) {
  111. auto fg = g_k_prims.KPrimitive(nullptr, value_node, resources);
  112. if (fg == nullptr) {
  113. return nullptr;
  114. }
  115. return BasicClone(fg);
  116. }
  117. MetaFuncGraphPtr Kmeta(const PrimitivePtr &prim, const pipeline::ResourceBasePtr &) {
  118. MetaFuncGraphPtr fg = g_k_prims.KMetaFuncGraph(prim);
  119. return fg;
  120. }
  121. void CleanRes() { DFunctor::Clear(); }
  122. } // namespace ad
  123. } // namespace mindspore