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.

trace.cc 17 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. #include "debug/trace.h"
  17. #include <iostream>
  18. #include <fstream>
  19. #include <map>
  20. #include <unordered_map>
  21. #include <vector>
  22. #include <string>
  23. #include <sstream>
  24. #include <utility>
  25. #include <stack>
  26. #include <algorithm>
  27. #include "ir/meta_func_graph.h"
  28. #include "ir/graph_utils.h"
  29. #include "frontend/operator/composite/composite.h"
  30. #include "ir/tensor.h"
  31. #include "debug/anf_ir_utils.h"
  32. #include "pipeline/jit/static_analysis/evaluator.h"
  33. #include "utils/log_adapter.h"
  34. namespace mindspore {
  35. // namespace to support debug trace infomation
  36. namespace trace {
  37. using abstract::AbstractBasePtr;
  38. using abstract::AnalysisContextPtr;
  39. using abstract::AnalysisEnginePtr;
  40. using abstract::AnfNodeConfigPtr;
  41. std::string GetAbstractStr(const abstract::AbstractBasePtr &abs) {
  42. if (abs == nullptr) {
  43. return "Null Abstract";
  44. }
  45. auto shape = abs->BuildShape()->cast<abstract::ShapePtr>();
  46. TypePtr type = abs->BuildType();
  47. std::ostringstream oss;
  48. if ((shape != nullptr) && (type != nullptr)) {
  49. oss << type->DumpText() << shape->DumpText();
  50. } else if (type != nullptr) {
  51. oss << type->DumpText();
  52. } else {
  53. oss << "Undefined";
  54. }
  55. return oss.str();
  56. }
  57. std::string GetGraphParamString(const FuncGraphPtr &graph, abstract::AbstractBasePtrList args_spec_list) {
  58. std::ostringstream oss;
  59. oss << "graph:" << graph->ToString() << " with args[";
  60. auto params = graph->parameters();
  61. for (size_t i = 0; i < args_spec_list.size(); i++) {
  62. oss << params[i]->ToString() << ":<" << GetAbstractStr(args_spec_list[i]) << ">,";
  63. }
  64. oss << "]";
  65. oss << GetDebugInfo(graph->debug_info(), kSourceLineTipDiscard);
  66. return oss.str();
  67. }
  68. void DumpInferStack(std::ostringstream &oss) {
  69. auto &infer_stack = GetCurrenGraphInferStack();
  70. if (infer_stack.empty()) {
  71. return;
  72. }
  73. std::vector<std::pair<abstract::EvaluatorPtr, abstract::AnfNodeConfigPtr>> infer_vec;
  74. while (!infer_stack.empty()) {
  75. auto top = infer_stack.top();
  76. infer_vec.push_back(top);
  77. infer_stack.pop();
  78. }
  79. std::reverse(infer_vec.begin(), infer_vec.end());
  80. int index = 0;
  81. for (auto &item : infer_vec) {
  82. auto graph_infer = std::dynamic_pointer_cast<abstract::BaseFuncGraphEvaluator>(item.first);
  83. if (graph_infer == nullptr) {
  84. MS_LOG(WARNING) << "DumpInferStack failed, got null graph evaluator";
  85. infer_vec.clear();
  86. break;
  87. }
  88. auto graph_context = graph_infer->graph_context();
  89. if (graph_context == nullptr) {
  90. MS_LOG(INFO) << "Null context continue";
  91. continue;
  92. }
  93. auto graph = graph_context->func_graph();
  94. auto args_spec_list = graph_context->args_spec_list();
  95. oss << " #" << index++ << " " << GetGraphParamString(graph, args_spec_list);
  96. }
  97. }
  98. void TraceGraphEval() {
  99. auto &infer_stack = GetCurrenGraphInferStack();
  100. std::ostringstream oss;
  101. if (infer_stack.empty()) {
  102. return;
  103. }
  104. MS_LOG(INFO) << "\n*******************************graph evaluate stack**********************************";
  105. oss << std::endl;
  106. DumpInferStack(oss);
  107. MS_LOG(INFO) << oss.str();
  108. MS_LOG(INFO) << "\n*************************************************************************************";
  109. }
  110. class AnalyzedFuncGraphExporter : public AnfExporter {
  111. public:
  112. AnalyzedFuncGraphExporter() : AnfExporter("", true, false) {}
  113. ~AnalyzedFuncGraphExporter() override = default;
  114. void ExportFuncGraph(const std::string &filename, const std::vector<abstract::AnfNodeConfigPtr> &node_cfgs);
  115. void ExportOneFuncGraph(std::ofstream &ofs, const FuncGraphPtr &func_graph);
  116. void OutputCNodes(std::ofstream &ofs, const std::vector<AnfNodePtr> &nodes, const FuncGraphPtr &func_graph);
  117. void OutputCNode(std::ofstream &ofs, const CNodePtr &cnode, const FuncGraphPtr &func_graph, int *idx,
  118. std::map<AnfNodePtr, int> *const apply_map);
  119. private:
  120. std::string GetNodeType(const AnfNodePtr &nd) override;
  121. AbstractBasePtr GetNodeAbstract(const AnfNodePtr &nd);
  122. AnfNodeConfigPtr GetFordwardConfigPtr(const AnfNodeConfigPtr &cfg);
  123. AnalysisContextPtr ProcessFuncGraphCall(const CNodePtr &node);
  124. // key: context, val: whether the context has already been printed
  125. std::unordered_map<AnalysisContextPtr, bool> context_map_;
  126. std::vector<AnalysisContextPtr> context_vec_;
  127. AnalysisContextPtr cur_ctx_ = nullptr;
  128. AnalysisEnginePtr engine_ = nullptr;
  129. };
  130. std::unordered_map<FuncGraphPtr, TaggedNodeMap> CalcTaggedFuncGraphs() {
  131. std::unordered_map<FuncGraphPtr, TaggedNodeMap> tagged_func_graphs;
  132. auto &list = GetCNodeDebugStack();
  133. for (size_t i = 0; i < list.size(); ++i) {
  134. auto node_cfg = list[i];
  135. auto fg = node_cfg->context()->func_graph();
  136. auto node = node_cfg->node();
  137. tagged_func_graphs[fg][node] = i;
  138. }
  139. return tagged_func_graphs;
  140. }
  141. void OutputAnalyzedGraphWithType() {
  142. AnalyzedFuncGraphExporter exporter;
  143. exporter.ExportFuncGraph("analyze_fail.dat", GetCNodeDebugStack());
  144. }
  145. std::string AnalyzedFuncGraphExporter::GetNodeType(const AnfNodePtr &node) {
  146. if (cur_ctx_ == nullptr) {
  147. return AnfExporter::GetNodeType(node);
  148. }
  149. MS_EXCEPTION_IF_NULL(engine_);
  150. auto cfg = engine_->MakeConfig(node, cur_ctx_);
  151. auto ret = engine_->cache().GetValue(cfg);
  152. if (ret == nullptr) {
  153. return "Undefined";
  154. }
  155. auto abs = ret->abstract();
  156. if (abs == nullptr) {
  157. return "Undefined";
  158. }
  159. auto dtype = abs->BuildType();
  160. auto shape = abs->BuildShape();
  161. std::ostringstream oss;
  162. if (dtype != nullptr && abs->isa<abstract::AbstractTensor>() && shape != nullptr) {
  163. oss << dtype->DumpText() << shape->DumpText();
  164. } else if (dtype != nullptr) {
  165. oss << dtype->DumpText();
  166. } else {
  167. oss << "Undefined";
  168. }
  169. return oss.str();
  170. }
  171. AbstractBasePtr AnalyzedFuncGraphExporter::GetNodeAbstract(const AnfNodePtr &node) {
  172. if (cur_ctx_ == nullptr) {
  173. return nullptr;
  174. }
  175. MS_EXCEPTION_IF_NULL(engine_);
  176. auto cfg = engine_->MakeConfig(node, cur_ctx_);
  177. auto ret = engine_->cache().GetValue(cfg);
  178. return ret == nullptr ? nullptr : ret->abstract();
  179. }
  180. AnfNodeConfigPtr AnalyzedFuncGraphExporter::GetFordwardConfigPtr(const AnfNodeConfigPtr &cfg) {
  181. AnfNodeConfigPtr cur_cfg = cfg;
  182. auto iter = engine_->anfnode_config_map().find(cur_cfg);
  183. while (iter != engine_->anfnode_config_map().end()) {
  184. auto node = cur_cfg->node();
  185. cur_cfg = iter->second;
  186. MS_LOG(DEBUG) << "Get forword node: " << node.get() << "[" << node->ToString() << "] --> " << cur_cfg->node().get()
  187. << "[" << cur_cfg->node()->ToString() << "]";
  188. iter = engine_->anfnode_config_map().find(cur_cfg);
  189. }
  190. return cur_cfg;
  191. }
  192. AnalysisContextPtr AnalyzedFuncGraphExporter::ProcessFuncGraphCall(const CNodePtr &node) {
  193. if (node == nullptr) {
  194. return nullptr;
  195. }
  196. auto cfg = engine_->MakeConfig(node, cur_ctx_);
  197. cfg = GetFordwardConfigPtr(cfg);
  198. auto cnode = dyn_cast<CNode>(cfg->node());
  199. if (cnode == nullptr) {
  200. MS_LOG(DEBUG) << "CNode is nullptr";
  201. return nullptr;
  202. }
  203. const auto &inputs = cnode->inputs();
  204. auto op_abs = GetNodeAbstract(inputs[0]);
  205. if (op_abs == nullptr) {
  206. MS_LOG(DEBUG) << "Abstract of inputs[0] of cnode " << cnode->ToString() << " is nullptr";
  207. return nullptr;
  208. }
  209. if (!op_abs->isa<abstract::FuncGraphAbstractClosure>() && !op_abs->isa<abstract::MetaFuncGraphAbstractClosure>()) {
  210. MS_LOG(DEBUG) << "Inputs[0] of cnode " << cnode->ToString() << " is of type " << op_abs->type_name()
  211. << ", not function, ignore it";
  212. return nullptr;
  213. }
  214. auto evaluator = engine_->GetEvaluatorFor(dyn_cast<abstract::AbstractFunction>(op_abs));
  215. if (!evaluator->isa<abstract::BaseFuncGraphEvaluator>()) {
  216. MS_LOG(DEBUG) << "Evaluator for inputs[0] of cnode " << cnode->ToString() << " is of type "
  217. << evaluator->type_name() << ", not BaseFuncGraphEvaluator, ignore it.";
  218. return nullptr;
  219. }
  220. auto base_fg_evaluator = dyn_cast<abstract::BaseFuncGraphEvaluator>(evaluator);
  221. auto ctx = base_fg_evaluator->graph_context();
  222. if (ctx != nullptr && context_map_.insert({ctx, false}).second) {
  223. MS_LOG(DEBUG) << "Add new context, ctx.addr = " << ctx.get() << "ctx = " << ctx->ToString();
  224. context_vec_.push_back(ctx);
  225. }
  226. return ctx;
  227. }
  228. void AnalyzedFuncGraphExporter::OutputCNode(std::ofstream &ofs, const CNodePtr &cnode, const FuncGraphPtr &func_graph,
  229. int *idx, std::map<AnfNodePtr, int> *const apply_map) {
  230. auto &inputs = cnode->inputs();
  231. std::string op_text = GetAnfNodeText(func_graph, inputs[0], *apply_map);
  232. // non-return node
  233. if (cnode != func_graph->get_return()) {
  234. int apply_idx = (*idx)++;
  235. (*apply_map)[cnode] = apply_idx;
  236. std::string type_info = GetNodeType(cnode);
  237. if (type_info == "Undefined") {
  238. ofs << " %" << apply_idx << " = " << op_text << "(";
  239. } else {
  240. ofs << " %" << apply_idx << " : " << type_info << " = " << op_text << "(";
  241. }
  242. } else {
  243. ofs << " " << op_text << "(";
  244. }
  245. for (size_t i = 1; i < inputs.size(); ++i) {
  246. if (i != 1) {
  247. ofs << ", ";
  248. }
  249. AnfNodePtr arg = inputs[i];
  250. ofs << GetAnfNodeText(func_graph, arg, *apply_map);
  251. }
  252. ofs << ")";
  253. // process function graph call
  254. auto ctx = ProcessFuncGraphCall(cnode);
  255. // output comment
  256. OutputStatementComment(ofs, cnode);
  257. if (ctx != nullptr) {
  258. ofs << " @ctx.addr=" << ctx.get();
  259. }
  260. ofs << "\n";
  261. if (label_manage::GetGlobalTraceLabelType() == label_manage::TraceLabelType::kWithUniqueId) {
  262. ofs << trace::GetDebugInfo(cnode->debug_info(), " # ", kSourceLineTipDiscard) << "#"
  263. << label_manage::Label(cnode->debug_info()) << "\n";
  264. } else {
  265. ofs << trace::GetDebugInfo(cnode->debug_info(), " # ", kSourceLineTipDiscard) << "\n";
  266. }
  267. }
  268. void AnalyzedFuncGraphExporter::OutputCNodes(std::ofstream &ofs, const std::vector<AnfNodePtr> &nodes,
  269. const FuncGraphPtr &func_graph) {
  270. if (func_graph == nullptr) {
  271. return;
  272. }
  273. int idx = 1;
  274. std::map<AnfNodePtr, int> apply_map;
  275. for (const AnfNodePtr &node : nodes) {
  276. MS_EXCEPTION_IF_NULL(node);
  277. if (!node->isa<CNode>()) {
  278. continue;
  279. }
  280. auto iter = tagged_cnodes_.find(node);
  281. if (iter != tagged_cnodes_.end()) {
  282. ofs << "\n#------------------------> " << iter->second << "\n";
  283. }
  284. auto cnode = node->cast<CNodePtr>();
  285. OutputCNode(ofs, cnode, func_graph, &idx, &apply_map);
  286. }
  287. }
  288. void AnalyzedFuncGraphExporter::ExportOneFuncGraph(std::ofstream &ofs, const FuncGraphPtr &func_graph) {
  289. if (func_graph == nullptr) {
  290. return;
  291. }
  292. std::vector<AnfNodePtr> nodes = TopoSort(func_graph->get_return(), SuccIncoming, AlwaysInclude);
  293. std::vector<AnfNodePtr> parameters = func_graph->parameters();
  294. OrderedMap<AnfNodePtr, int, ParamPtrHasher, ParamPtrEqual> param_map;
  295. ofs << "# [No." << (exported.size() + 1) << "] " << func_graph->DumpText() << "."
  296. << func_graph->debug_info()->get_id();
  297. if (cur_ctx_ != nullptr) {
  298. ofs << " @ctx.addr=" << cur_ctx_.get();
  299. }
  300. ofs << "\n";
  301. if (label_manage::GetGlobalTraceLabelType() == label_manage::TraceLabelType::kWithUniqueId) {
  302. ofs << trace::GetDebugInfo(func_graph->debug_info(), "# ", kSourceLineTipDiscard) << "#"
  303. << label_manage::Label(func_graph->debug_info()) << "\n";
  304. } else {
  305. ofs << trace::GetDebugInfo(func_graph->debug_info(), "# ", kSourceLineTipDiscard) << "\n";
  306. }
  307. ofs << "funcgraph fg_" << func_graph->debug_info()->get_id();
  308. // output name of parent of graph if exists
  309. if (func_graph->parent() != nullptr) {
  310. ofs << "[fg_" << func_graph->parent()->debug_info()->get_id() << "]";
  311. }
  312. ofs << "(\n";
  313. OutputParameters(ofs, parameters, &param_map);
  314. exported[func_graph] = param_map;
  315. ofs << (!parameters.empty() ? " " : "") << ") {\n";
  316. OutputCNodes(ofs, nodes, func_graph);
  317. ofs << "}\n";
  318. }
  319. void AnalyzedFuncGraphExporter::ExportFuncGraph(const std::string &filename,
  320. const std::vector<abstract::AnfNodeConfigPtr> &node_cfgs) {
  321. if (node_cfgs.empty()) {
  322. MS_LOG(DEBUG) << "Node configs is empty";
  323. return;
  324. }
  325. context_map_.clear();
  326. context_vec_.clear();
  327. std::ofstream ofs(filename);
  328. if (!ofs.is_open()) {
  329. MS_LOG(ERROR) << "Open file '" << filename << "' failed!";
  330. return;
  331. }
  332. param_index = 1;
  333. auto tagged_func_graphs = CalcTaggedFuncGraphs();
  334. // first output graph on the analysis stack
  335. for (const auto &node_cfg : node_cfgs) {
  336. auto ctx = node_cfg->context();
  337. if (engine_ == nullptr) {
  338. engine_ = node_cfg->engine();
  339. }
  340. if (context_map_.insert({ctx, false}).second) {
  341. context_vec_.push_back(ctx);
  342. }
  343. // the graph has already been printed
  344. if (context_map_[ctx]) {
  345. continue;
  346. }
  347. context_map_[ctx] = true;
  348. auto fg = ctx->func_graph();
  349. // set current context
  350. cur_ctx_ = ctx;
  351. tagged_cnodes_ = tagged_func_graphs[fg];
  352. ExportOneFuncGraph(ofs, fg);
  353. ofs << "\n\n";
  354. }
  355. tagged_cnodes_.clear();
  356. // print seperator between function graphs on analyzed graph call stack and others
  357. ofs << "#===============================================================================\n\n\n";
  358. // second output other graphs
  359. size_t ctx_idx = 0;
  360. while (ctx_idx < context_vec_.size()) {
  361. auto ctx = context_vec_[ctx_idx++];
  362. if (context_map_[ctx]) {
  363. continue;
  364. }
  365. context_map_[ctx] = true;
  366. cur_ctx_ = ctx;
  367. ExportOneFuncGraph(ofs, ctx->func_graph());
  368. ofs << "\n\n";
  369. }
  370. ofs << "# num of total function graphs: " << context_map_.size() << "\n";
  371. ofs.close();
  372. }
  373. void GetEvalStackInfo(std::ostringstream &oss) {
  374. MS_LOG(INFO) << "Get graph analysis information begin";
  375. auto stack = GetCNodeDebugStack();
  376. if (stack.empty()) {
  377. MS_LOG(INFO) << "Length of analysis information stack is empty.";
  378. return;
  379. }
  380. OutputAnalyzedGraphWithType();
  381. oss << "\nThe function call stack:\n";
  382. int index = 0;
  383. std::string last_py_func = "";
  384. for (size_t i = 0; i < stack.size(); ++i) {
  385. auto node_cfg = stack[i];
  386. auto cnode = dyn_cast<CNode>(node_cfg->node());
  387. if (cnode == nullptr) {
  388. MS_LOG(DEBUG) << "CNode of elements[" << i << "] is nullptr.";
  389. continue;
  390. }
  391. auto debug_info = cnode->debug_info();
  392. auto this_py_func = debug_info->get_python_func_belonged();
  393. if (i > 0 && (this_py_func == last_py_func)) {
  394. MS_LOG(DEBUG) << "Python function of elements[" << i << "] is same as previous.";
  395. continue;
  396. }
  397. last_py_func = this_py_func;
  398. oss << "# " << index++ << " " << trace::GetDebugInfo(debug_info, std::string(""));
  399. }
  400. stack.clear();
  401. MS_LOG(INFO) << "Get graph analysis information *end*";
  402. }
  403. // trace the graph evaluator stack
  404. static std::stack<std::pair<abstract::EvaluatorPtr, abstract::AnfNodeConfigPtr>> graph_infer_stack;
  405. // trace the cnode infer debug info
  406. static std::vector<abstract::AnfNodeConfigPtr> cnode_debug_stack{};
  407. void TraceGraphEvalEnter(const abstract::EvaluatorPtr &eval, const abstract::AnfNodeConfigPtr &node) {
  408. if (eval == nullptr) {
  409. MS_LOG(EXCEPTION) << "GraphInferEnter got null eval";
  410. }
  411. if (eval->isa<abstract::FuncGraphEvaluator>() || eval->isa<abstract::MetaFuncGraphEvaluator>()) {
  412. graph_infer_stack.emplace(std::pair<abstract::EvaluatorPtr, abstract::AnfNodeConfigPtr>(eval, node));
  413. }
  414. }
  415. void TraceGraphEvalLeave(const abstract::EvaluatorPtr &eval) {
  416. if (eval == nullptr) {
  417. MS_LOG(EXCEPTION) << "GraphInferEnter got null eval";
  418. }
  419. if (eval->isa<abstract::FuncGraphEvaluator>() || eval->isa<abstract::MetaFuncGraphEvaluator>()) {
  420. graph_infer_stack.pop();
  421. }
  422. }
  423. void TraceEvalCNodeEnter(const abstract::AnfNodeConfigPtr &node_cfg) { cnode_debug_stack.push_back(node_cfg); }
  424. void TraceEvalCNodeLeave() { cnode_debug_stack.pop_back(); }
  425. std::vector<abstract::AnfNodeConfigPtr> &GetCNodeDebugStack() { return cnode_debug_stack; }
  426. std::stack<std::pair<abstract::EvaluatorPtr, abstract::AnfNodeConfigPtr>> &GetCurrenGraphInferStack() {
  427. return graph_infer_stack;
  428. }
  429. void ClearTraceStack() {
  430. while (!graph_infer_stack.empty()) {
  431. graph_infer_stack.pop();
  432. }
  433. cnode_debug_stack.clear();
  434. }
  435. // Register trace provider to LogWriter.
  436. struct TraceProviderRegister {
  437. TraceProviderRegister() {
  438. LogWriter::set_trace_provider([](std::ostringstream &oss) {
  439. TraceGraphEval();
  440. GetEvalStackInfo(oss);
  441. });
  442. }
  443. ~TraceProviderRegister() = default;
  444. } trace_provider_regsiter;
  445. } // namespace trace
  446. } // namespace mindspore