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