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.

action.cc 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #include "pipeline/action.h"
  17. #include <memory>
  18. #include <utility>
  19. #include <vector>
  20. #include <string>
  21. #include <algorithm>
  22. #include <functional>
  23. #include "ir/func_graph_cloner.h"
  24. #include "parallel/costmodel_context.h"
  25. #include "pipeline/pass.h"
  26. #include "pipeline/parse/parse_base.h"
  27. #include "pipeline/parse/data_converter.h"
  28. #include "pipeline/static_analysis/abstract_value.h"
  29. #include "pipeline/static_analysis/static_analysis.h"
  30. #include "pipeline/static_analysis/program_specialize.h"
  31. #include "pipeline/resource.h"
  32. #include "pipeline/remove_value_node_dup.h"
  33. #include "optimizer/optimizer.h"
  34. #include "vm/transform.h"
  35. namespace mindspore {
  36. namespace pipeline {
  37. using CompileGraphs = compile::CompileGraphs;
  38. using abstract::AnalysisResult;
  39. using mindspore::abstract::AnalysisContextPtr;
  40. abstract::AnalysisResult AbstractAnalyze(const ResourcePtr &res, const FuncGraphPtr &func_graph,
  41. const abstract::AbstractBasePtrList &args_spec, bool clear) {
  42. MS_LOG(DEBUG) << "AbstractAnalyze start";
  43. auto engine = res->engine();
  44. MS_EXCEPTION_IF_NULL(engine);
  45. if (clear) {
  46. auto manager = res->manager();
  47. MS_EXCEPTION_IF_NULL(manager);
  48. engine->Clear();
  49. for (auto &node : manager->all_nodes()) {
  50. MS_EXCEPTION_IF_NULL(node);
  51. const AbstractBasePtr &prev_inferred = node->abstract();
  52. // Keep previous inferred value for ValueNode if the inferred value is not AbstractFunction.
  53. if (!node->isa<ValueNode>() || (prev_inferred != nullptr && prev_inferred->isa<abstract::AbstractFunction>())) {
  54. node->set_abstract(nullptr);
  55. MS_LOG(DEBUG) << "Abstract of node " << node->ToString() << " is set to nullptr";
  56. }
  57. }
  58. }
  59. auto ret = engine->Run(func_graph, args_spec);
  60. MS_LOG(DEBUG) << "AbstractAnalyze end";
  61. return ret;
  62. }
  63. FuncGraphPtr ProgramSpecialize(const ResourcePtr &res, const FuncGraphPtr &func_graph,
  64. const abstract::AnalysisContextPtr &context) {
  65. MS_LOG(DEBUG) << "ProgramSpecialize start";
  66. abstract::ProgramSpecializer spc(res->engine());
  67. FuncGraphPtr result = spc.Run(func_graph, context);
  68. auto manager = res->manager();
  69. MS_EXCEPTION_IF_NULL(manager);
  70. manager->KeepRoots({result});
  71. MS_LOG(DEBUG) << "ProgramSpecialize end";
  72. return result;
  73. }
  74. FuncGraphPtr Renormalize(const ResourcePtr &res, const FuncGraphPtr &func_graph,
  75. const abstract::AbstractBasePtrList &args_spec) {
  76. MS_LOG(DEBUG) << "Renormalize start";
  77. #ifdef ENABLE_PROFILE
  78. double t1 = GetTime();
  79. #endif
  80. abstract::AnalysisResult result = AbstractAnalyze(res, func_graph, args_spec, true);
  81. #ifdef ENABLE_PROFILE
  82. double t2 = GetTime();
  83. #endif
  84. auto ret = ProgramSpecialize(res, func_graph, result.context);
  85. res->set_func_graph(ret);
  86. #ifdef ENABLE_PROFILE
  87. double t3 = GetTime();
  88. MsProfile::StatTime("renormalize.infer", t2 - t1);
  89. MsProfile::StatTime("renormalize.specialize", t3 - t2);
  90. #endif
  91. MS_LOG(DEBUG) << "Renormalize end";
  92. return ret;
  93. }
  94. bool ParseAction(const ResourcePtr &res) {
  95. if (!res->input()) {
  96. MS_LOG(EXCEPTION) << "Parse error";
  97. }
  98. py::object input = res->input();
  99. parse::Parser::InitParserEnvironment(input);
  100. py::module path = py::module::import("os.path");
  101. std::string dir = path.attr("dirname")(py::globals()["__file__"]).cast<std::string>();
  102. parse::python_adapter::set_python_env_flag(true);
  103. parse::python_adapter::SetPythonPath(dir);
  104. FuncGraphPtr fg = parse::ConvertToFuncGraph(input);
  105. if (fg == nullptr) {
  106. MS_LOG(EXCEPTION) << "Parse error.";
  107. }
  108. res->set_func_graph(fg);
  109. FuncGraphManagerPtr manager = res->manager();
  110. if (manager == nullptr) {
  111. MS_LOG(EXCEPTION) << "Manager is nullptr.";
  112. }
  113. manager->AddFuncGraph(fg);
  114. return true;
  115. }
  116. // obj_map's graphs have the same construct, these graphs can be optimized to one graph.
  117. // This step do this optimize: graph1(x){xx(fv1),xxx(fv2)}, graph2(x){xxx(fv3),xxx(fv4)}->
  118. // graph1(x){base_graph(x, fv1, fv2)}, graph1(x){base_graph(x, fv3, fv4)}, base_graph(x, fv...){xxx,xxx}
  119. // all obj_map's graph shared base_graph
  120. bool CombineLikeGraphs(const ResourcePtr &) {
  121. auto &obj_map = parse::data_converter::GetObjGraphs();
  122. for (auto it : obj_map) {
  123. auto &graphs = it.second;
  124. MS_LOG(DEBUG) << "Start combine like graph:" << it.first << ", size:" << graphs.size();
  125. auto fg = graphs[0];
  126. FuncGraphPtrList func_graphs = {fg};
  127. ClonerPtr cloner = std::make_shared<Cloner>(func_graphs, false, false, true, std::make_shared<TraceCopy>(),
  128. std::make_shared<TraceCombileLikeGraphs>());
  129. cloner->Run();
  130. auto base_graph = cloner->cloned_func_graph()[fg];
  131. MS_LOG(DEBUG) << "Basegraph:" << base_graph->ToString();
  132. if (fg->paramter_obj_nodes().size() == 0 || graphs.size() <= 1) {
  133. continue;
  134. }
  135. auto mng = Manage(base_graph, false);
  136. for (auto &fv : fg->paramter_obj_nodes()) {
  137. TraceManager::DebugTrace(std::make_shared<TraceCombileLikeGraphs>(fv->debug_info()));
  138. auto param = base_graph->add_parameter();
  139. TraceManager::EndTrace();
  140. auto repl_node = (*cloner->cloned_node())[fv];
  141. (void)mng->Replace(repl_node, param);
  142. }
  143. MS_LOG(DEBUG) << "Fg0 paramter_obj_nodes size :" << fg->paramter_obj_nodes().size();
  144. for (auto &g : graphs) {
  145. auto fvs = g->paramter_obj_nodes();
  146. std::vector<AnfNodePtr> new_node_inputs;
  147. new_node_inputs.push_back(NewValueNode(base_graph));
  148. for (auto &p : g->parameters()) {
  149. AnfNodePtr para_after_cast = parse::GetMixedPrecisionCastHelp(g, p);
  150. new_node_inputs.push_back(para_after_cast);
  151. }
  152. (void)new_node_inputs.insert(new_node_inputs.end(), fvs.begin(), fvs.end());
  153. AnfNodePtr out = g->NewCNode(new_node_inputs);
  154. g->set_output(out);
  155. MS_LOG(DEBUG) << "Combine graph newout:" << out->DebugString(4);
  156. }
  157. MS_LOG(DEBUG) << "End combine graph:" << it.first;
  158. }
  159. return true;
  160. }
  161. bool SymbolResolveAction(const ResourcePtr &res) {
  162. if (res->manager() == nullptr) {
  163. MS_LOG(EXCEPTION) << "SymbolResolve error, manager is null";
  164. }
  165. if (res->func_graph() == nullptr) {
  166. MS_LOG(EXCEPTION) << "SymbolResolve error, graph is null";
  167. }
  168. FuncGraphPtr func_graph = res->func_graph();
  169. auto succ = parse::ResolveFuncGraph(func_graph, res);
  170. // Remove unused nodes in cnode order list.
  171. func_graph->EraseUnusedNodeInOrder();
  172. func_graph->ReleaseFullOrderToEffectOrder();
  173. for (auto fg : func_graph->func_graphs_used_total()) {
  174. MS_EXCEPTION_IF_NULL(fg);
  175. fg->EraseUnusedNodeInOrder();
  176. fg->ReleaseFullOrderToEffectOrder();
  177. }
  178. return succ;
  179. }
  180. bool InferenceOptPrepareAction(const ResourcePtr &res) {
  181. if (res->manager() == nullptr) {
  182. MS_LOG(EXCEPTION) << "InferenceOptPrepare error, manager is null.";
  183. }
  184. if (res->func_graph() == nullptr) {
  185. MS_LOG(EXCEPTION) << "InferenceOptPrepare error, graph is null.";
  186. }
  187. return InferenceOptPreparePass(res);
  188. }
  189. bool AbstractSpecializeAction(const ResourcePtr &res) {
  190. if (res->func_graph() == nullptr) {
  191. MS_LOG(EXCEPTION) << "AbstractSpecialize error";
  192. }
  193. FuncGraphPtr func_graph = res->func_graph();
  194. abstract::AbstractBasePtrList args_spec = res->args_spec();
  195. // suppose that there is not KeywordArgument for the top graph
  196. // get the hyper parameter
  197. for (const auto &param : func_graph->parameters()) {
  198. auto param_node = std::static_pointer_cast<Parameter>(param);
  199. if (param_node->has_default()) {
  200. AbstractBasePtr ptr =
  201. abstract::FromValue(parse::data_converter::PyDataToValue(param_node->default_param()), true);
  202. args_spec.push_back(ptr);
  203. }
  204. }
  205. // Analyze
  206. AnalysisResult result = AbstractAnalyze(res, func_graph, args_spec);
  207. // The top graph may be replaced by infer, update the top graph when the infer is done
  208. parse::Parser::UpdateTopFuncGraph(result.context->func_graph());
  209. // Specialize
  210. FuncGraphPtr new_fg = ProgramSpecialize(res, result.context->func_graph(), result.context);
  211. res->set_func_graph(new_fg);
  212. MS_LOG(DEBUG) << "End graph: " << new_fg->ToString() << ", return: " << new_fg->get_return()->DebugString(true);
  213. return true;
  214. }
  215. bool OptimizeAction(const ResourcePtr &res, const std::vector<PassItem> &passes) {
  216. for (auto &pass : passes) {
  217. WITH(MsProfile::GetProfile()->Step(pass.first))[&pass, &res]() {
  218. MS_LOG(DEBUG) << "Pass " << pass.first << " start ...";
  219. auto result = pass.second(res);
  220. if (!result) {
  221. MS_LOG(EXCEPTION) << "Pass running to end, failed in pass:" << pass.first;
  222. }
  223. MS_LOG(DEBUG) << "Pass " << pass.first << " end.";
  224. };
  225. }
  226. return true;
  227. }
  228. bool GeOptimizeAction(const ResourcePtr &res) { return OptimizeAction(res, kGePasses); }
  229. bool VmOptimizeAction(const ResourcePtr &res) { return OptimizeAction(res, kVmPasses); }
  230. bool TaskEmitAction(const ResourcePtr &res) {
  231. if (res->func_graph() == nullptr) {
  232. MS_LOG(EXCEPTION) << "TaskEmit args error";
  233. }
  234. FuncGraphPtr func_graph = res->func_graph();
  235. auto bc_ptr = res->results()[kBackend].cast<compile::BackendPtr>();
  236. std::vector<PrimitivePtr> cut_list = compile::nonlinear_ops;
  237. if (bc_ptr->name() == kMsConvert) {
  238. cut_list = compile::GetMsNonlinearOps();
  239. }
  240. std::shared_ptr<CompileGraphs> compile = std::make_shared<CompileGraphs>(bc_ptr, cut_list);
  241. res->results()[kOutput] = compile->CompileAndLink(func_graph);
  242. return true;
  243. }
  244. bool ExecuteAction(const ResourcePtr &res) {
  245. if (res->results().count(kOutput) == 0 || !res->results()[kOutput].is<compile::FinalVMPtr>()) {
  246. MS_LOG(EXCEPTION) << "Execute args error";
  247. }
  248. compile::FinalVMPtr vm = res->results()[kOutput].cast<compile::FinalVMPtr>();
  249. if (vm == nullptr) {
  250. MS_LOG(INFO) << "Call GE to Run the func_graph instead of VM";
  251. return true;
  252. }
  253. compile::VmEvalFuncPtr run =
  254. std::make_shared<compile::VmEvalFunc>(std::bind(&compile::FinalVM::Eval, vm, std::placeholders::_1));
  255. res->results()[kOutput] = run;
  256. return true;
  257. }
  258. // The parallel primitive related valuenode might be partitioned so that its value changes by device,
  259. // that will result in a syncronization error due to different executing order.
  260. // Here we temporarily avoid the problem by skipping valuenode merging used by parallel related primitive,
  261. // the final solution will be proposed later as a parallel feature.
  262. bool KeepValueNodeDuplication(const AnfNodePtr &value_node, const ResourcePtr &res) {
  263. auto &node_users = res->manager()->node_users();
  264. auto &users = node_users[value_node];
  265. auto used_by_keep_value_prim =
  266. std::any_of(users.begin(), users.end(), [](const std::pair<AnfNodePtr, int> &user) -> bool {
  267. MS_EXCEPTION_IF_NULL(user.first);
  268. auto cnode = user.first->cast<CNodePtr>();
  269. if (cnode == nullptr) {
  270. return false;
  271. }
  272. auto prim_node = cnode->input(0);
  273. if (IsValueNode<Primitive>(prim_node)) {
  274. auto prim = GetValue<PrimitivePtr>(prim_node->cast<ValueNodePtr>()->value());
  275. // value_node is referenced by some parallel primitive
  276. return prim->HasAttr("keep_value_node_input");
  277. }
  278. return false;
  279. });
  280. return used_by_keep_value_prim;
  281. }
  282. bool RemoveValueNodeDuplicationsAction(const ResourcePtr &res) {
  283. if (res->func_graph() == nullptr) {
  284. MS_LOG(EXCEPTION) << "Remove value node duplications error.";
  285. }
  286. FuncGraphPtr func_graph = res->func_graph();
  287. auto manager = res->manager();
  288. // Remove duplicated value nodes, due to replace operation, can't use reference.
  289. auto value_nodes = manager->valuenodes()[func_graph];
  290. HashCache hash_cache;
  291. HashValue hashes;
  292. for (const auto &value_pair : value_nodes) {
  293. if (KeepValueNodeDuplication(value_pair.first, res)) {
  294. continue;
  295. }
  296. TryToDoReplace(manager.get(), value_pair.first, &hash_cache, &hashes);
  297. }
  298. return true;
  299. }
  300. bool ValidateAction(const ResourcePtr &res) { return ValidatePass(res); }
  301. static std::vector<ActionItem> CommonPipeline() {
  302. std::vector<ActionItem> actions;
  303. // Parse the python ast to ANF graph
  304. actions.emplace_back(std::make_pair("parse", ParseAction));
  305. // Resolve the python func
  306. actions.emplace_back(std::make_pair("symbol_resolve", SymbolResolveAction));
  307. auto multi_graphs = parallel::CostModelContext::GetInstance()->is_multi_subgraphs();
  308. if (!multi_graphs) {
  309. actions.emplace_back(std::make_pair("combine_like_graphs", CombineLikeGraphs));
  310. }
  311. actions.emplace_back(std::make_pair("inference_opt_prepare", InferenceOptPrepareAction));
  312. // Evaluate type and shape, and specialize
  313. actions.emplace_back(std::make_pair("abstract_specialize", AbstractSpecializeAction));
  314. return actions;
  315. }
  316. std::vector<ActionItem> GePipeline() {
  317. auto actions = CommonPipeline();
  318. // optimize
  319. actions.emplace_back(std::make_pair("optimize", GeOptimizeAction));
  320. actions.emplace_back(std::make_pair("remove_value_node_duplications", RemoveValueNodeDuplicationsAction));
  321. actions.emplace_back(std::make_pair("validate", ValidateAction));
  322. return actions;
  323. }
  324. std::vector<ActionItem> VmPipeline() {
  325. auto actions = CommonPipeline();
  326. // optimize
  327. actions.emplace_back(std::make_pair("optimize", VmOptimizeAction));
  328. actions.emplace_back(std::make_pair("validate", ValidateAction));
  329. // compile the ANF graph
  330. actions.emplace_back(std::make_pair("task_emit", TaskEmitAction));
  331. // to execute the graph
  332. actions.emplace_back(std::make_pair("execute", ExecuteAction));
  333. return actions;
  334. }
  335. } // namespace pipeline
  336. } // namespace mindspore