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 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /**
  2. * Copyright 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 "utils/graph_utils.h"
  29. #include "operator/composite/composite.h"
  30. #include "ir/meta_tensor.h"
  31. #include "debug/anf_ir_utils.h"
  32. #include "pipeline/static_analysis/evaluator.h"
  33. namespace mindspore {
  34. // namespace to support debug trace infomation
  35. namespace trace {
  36. std::string GetAbstractStr(const abstract::AbstractBasePtr &abs) {
  37. if (abs == nullptr) {
  38. return "Null Abstract";
  39. }
  40. auto shape = abs->BuildShape()->cast<abstract::ShapePtr>();
  41. TypePtr type = abs->BuildType();
  42. std::ostringstream oss;
  43. if ((shape != nullptr) && (type != nullptr)) {
  44. oss << type->DumpText() << shape->DumpText();
  45. } else if (type != nullptr) {
  46. oss << type->DumpText();
  47. } else {
  48. oss << "Undefined";
  49. }
  50. return oss.str();
  51. }
  52. std::vector<DebugInfoPtr> GetSourceCodeDebugInfoVec(DebugInfoPtr debug_info) {
  53. std::vector<DebugInfoPtr> debug_with_loc_vec;
  54. while (debug_info != nullptr) {
  55. if (debug_info->location() != nullptr) {
  56. debug_with_loc_vec.push_back(debug_info);
  57. }
  58. if (debug_info->trace_info() != nullptr) {
  59. debug_info = debug_info->trace_info()->debug_info();
  60. } else {
  61. break;
  62. }
  63. }
  64. return debug_with_loc_vec;
  65. }
  66. DebugInfoPtr GetSourceCodeDebugInfo(const DebugInfoPtr &info) {
  67. auto debug_with_loc_vec = GetSourceCodeDebugInfoVec(info);
  68. if (debug_with_loc_vec.size() > 0) {
  69. return debug_with_loc_vec[0];
  70. } else {
  71. return info;
  72. }
  73. }
  74. std::string GetDebugInfo(const DebugInfoPtr &info, SourceLineTip tip) {
  75. if (info == nullptr) {
  76. return "";
  77. }
  78. auto src_info = GetSourceCodeDebugInfo(info);
  79. if (src_info->location() != nullptr) {
  80. return src_info->location()->ToString(tip);
  81. }
  82. return "";
  83. }
  84. // a trace info identifies a node transform, so we can trace the node transform through
  85. // a link of trace info and debug info
  86. std::string GetInfoWithAction(const std::vector<DebugInfoPtr> &info_vec, SourceLineTip tip) {
  87. if (info_vec.size() < 1) {
  88. return "";
  89. }
  90. if (info_vec.size() == 1) {
  91. return info_vec[0]->location()->ToString(tip);
  92. }
  93. std::string traced_info = info_vec[0]->location()->ToString(tip);
  94. for (size_t i = 1; i < info_vec.size(); i++) {
  95. auto action_name = info_vec[i - 1]->trace_info()->GetActionBetweenNode(info_vec[i]);
  96. if (action_name == "") {
  97. break;
  98. }
  99. traced_info = traced_info + action_name + info_vec[i]->location()->ToString(tip);
  100. }
  101. return traced_info;
  102. }
  103. std::string GetTracedDebugInfo(const DebugInfoPtr &info, SourceLineTip tip) {
  104. if (info == nullptr) {
  105. return "";
  106. }
  107. auto info_vec = GetSourceCodeDebugInfoVec(info);
  108. if (info_vec.size() == 0) {
  109. return "";
  110. } else if (info_vec.size() == 1) {
  111. return info_vec[0]->location()->ToString(tip);
  112. } else if (info_vec.size() > 1) {
  113. return GetInfoWithAction(info_vec, tip);
  114. }
  115. return "";
  116. }
  117. std::string GetDebugInfo(const DebugInfoPtr &info, const std::string &prefix, SourceLineTip tip) {
  118. std::ostringstream oss;
  119. if (info == nullptr) {
  120. return "";
  121. }
  122. auto debug_info = GetTracedDebugInfo(info, tip);
  123. if (tip == kSourceLineTipDiscard) {
  124. std::replace(debug_info.begin(), debug_info.end(), '\r', '/');
  125. std::replace(debug_info.begin(), debug_info.end(), '\n', '/');
  126. }
  127. oss << prefix << debug_info;
  128. return oss.str();
  129. }
  130. std::string GetGraphParamString(const FuncGraphPtr &graph, abstract::AbstractBasePtrList args_spec_list) {
  131. std::ostringstream oss;
  132. oss << "graph:" << graph->ToString() << " with args[";
  133. auto params = graph->parameters();
  134. for (size_t i = 0; i < args_spec_list.size(); i++) {
  135. oss << params[i]->ToString() << ":<" << GetAbstractStr(args_spec_list[i]) << ">,";
  136. }
  137. oss << "]";
  138. oss << GetDebugInfo(graph->debug_info(), kSourceLineTipDiscard);
  139. return oss.str();
  140. }
  141. void DumpInferStack(std::ostringstream &oss) {
  142. auto &infer_stack = GetCurrenGraphInferStack();
  143. if (infer_stack.empty()) {
  144. return;
  145. }
  146. std::vector<std::pair<abstract::EvaluatorPtr, abstract::AnfNodeConfigPtr>> infer_vec;
  147. while (!infer_stack.empty()) {
  148. auto top = infer_stack.top();
  149. infer_vec.push_back(top);
  150. infer_stack.pop();
  151. }
  152. std::reverse(infer_vec.begin(), infer_vec.end());
  153. int index = 0;
  154. for (auto &item : infer_vec) {
  155. auto graph_infer = std::dynamic_pointer_cast<abstract::BaseFuncGraphEvaluator>(item.first);
  156. if (graph_infer == nullptr) {
  157. MS_LOG(WARNING) << "DumpInferStack failed, got null graph evaluator";
  158. infer_vec.clear();
  159. break;
  160. }
  161. auto graph_context = graph_infer->graph_context();
  162. if (graph_context == nullptr) {
  163. MS_LOG(INFO) << "Null context continue";
  164. continue;
  165. }
  166. auto graph = graph_context->func_graph();
  167. auto args_spec_list = graph_context->args_spec_list();
  168. oss << " #" << index++ << " " << GetGraphParamString(graph, args_spec_list);
  169. }
  170. }
  171. void TraceGraphInfer() {
  172. auto &infer_stack = GetCurrenGraphInferStack();
  173. std::ostringstream oss;
  174. if (infer_stack.empty()) {
  175. return;
  176. }
  177. MS_LOG(INFO) << "\n*******************************graph evaluate stack**********************************";
  178. oss << std::endl;
  179. DumpInferStack(oss);
  180. MS_LOG(INFO) << oss.str();
  181. MS_LOG(INFO) << "\n*************************************************************************************";
  182. }
  183. class AnalyzedFuncGraphExporter : public AnfExporter {
  184. public:
  185. AnalyzedFuncGraphExporter() : AnfExporter("", true, false) {}
  186. ~AnalyzedFuncGraphExporter() override = default;
  187. void ExportFuncGraph(const std::string &filename, const std::vector<abstract::AnfNodeConfigPtr> &node_cfgs);
  188. private:
  189. std::string GetNodeType(const AnfNodePtr &nd) override;
  190. };
  191. std::unordered_map<FuncGraphPtr, TaggedNodeMap> CalcTaggedFuncGraphs() {
  192. std::unordered_map<FuncGraphPtr, TaggedNodeMap> tagged_func_graphs;
  193. auto &list = GetCNodeDebugStack();
  194. for (size_t i = 0; i < list.size(); ++i) {
  195. auto node_cfg = list[i];
  196. auto fg = node_cfg->context()->func_graph();
  197. auto node = node_cfg->node();
  198. tagged_func_graphs[fg][node] = i;
  199. }
  200. return tagged_func_graphs;
  201. }
  202. void OutputAnalyzedGraphWithType() {
  203. AnalyzedFuncGraphExporter exporter;
  204. exporter.ExportFuncGraph("analyze_fail.dat", GetCNodeDebugStack());
  205. }
  206. std::string AnalyzedFuncGraphExporter::GetNodeType(const AnfNodePtr &node) {
  207. if (node_cfg_ == nullptr) {
  208. return AnfExporter::GetNodeType(node);
  209. }
  210. auto ctx = node_cfg_->context();
  211. auto engine = node_cfg_->engine();
  212. auto cfg = engine->MakeConfig(node, ctx);
  213. auto abs = engine->cache().GetValue(cfg);
  214. if (abs == nullptr) {
  215. return "Undefined";
  216. }
  217. auto dtype = abs->BuildType();
  218. auto shape = abs->BuildShape();
  219. std::ostringstream oss;
  220. if (dtype != nullptr && abs->isa<abstract::AbstractTensor>() && shape != nullptr) {
  221. oss << dtype->DumpText() << shape->DumpText();
  222. } else if (dtype != nullptr) {
  223. oss << dtype->DumpText();
  224. } else {
  225. oss << "Undefined";
  226. }
  227. return oss.str();
  228. }
  229. void AnalyzedFuncGraphExporter::ExportFuncGraph(const std::string &filename,
  230. const std::vector<abstract::AnfNodeConfigPtr> &node_cfgs) {
  231. if (node_cfgs.empty()) {
  232. MS_LOG(DEBUG) << "Node configs is empty";
  233. return;
  234. }
  235. std::ofstream ofs(filename);
  236. if (!ofs.is_open()) {
  237. MS_LOG(ERROR) << "Open file '" << filename << "' failed!";
  238. return;
  239. }
  240. param_index = 1;
  241. auto tagged_func_graphs = CalcTaggedFuncGraphs();
  242. // first output graph on the analysis stack
  243. for (const auto &node_cfg : node_cfgs) {
  244. auto fg = node_cfg->context()->func_graph();
  245. // the graph is already output, skip it
  246. if (exported.find(fg) != exported.end()) {
  247. continue;
  248. }
  249. // set node_cfg info for getting type
  250. node_cfg_ = node_cfg;
  251. tagged_cnodes_ = tagged_func_graphs[fg];
  252. ExportOneFuncGraph(ofs, fg);
  253. ofs << "\n\n";
  254. }
  255. node_cfg_ = nullptr;
  256. tagged_cnodes_.clear();
  257. // print seperator between function graphs on analyzed graph call stack and others
  258. ofs << "#===============================================================================\n\n\n";
  259. // second output other graphs
  260. while (!func_graph_set.empty()) {
  261. FuncGraphPtr fg = *func_graph_set.begin();
  262. ExportOneFuncGraph(ofs, fg);
  263. ofs << "\n\n";
  264. (void)func_graph_set.erase(fg);
  265. }
  266. ofs << "# num of total function graphs: " << exported.size();
  267. ofs.close();
  268. }
  269. void GetInferStackInfo(std::ostringstream &oss) {
  270. MS_LOG(INFO) << "Get graph analysis information begin";
  271. auto stack = GetCNodeDebugStack();
  272. if (stack.empty()) {
  273. MS_LOG(INFO) << "Length of analysis information stack is empty.";
  274. return;
  275. }
  276. OutputAnalyzedGraphWithType();
  277. oss << "\nThe function call stack:\n";
  278. int index = 0;
  279. std::string last_py_func = "";
  280. for (size_t i = 0; i < stack.size(); ++i) {
  281. auto node_cfg = stack[i];
  282. auto cnode = dyn_cast<CNode>(node_cfg->node());
  283. if (cnode == nullptr) {
  284. MS_LOG(DEBUG) << "CNode of elements[" << i << "] is nullptr.";
  285. continue;
  286. }
  287. auto debug_info = cnode->debug_info();
  288. auto this_py_func = debug_info->get_python_func_belonged();
  289. if (i > 0 && (this_py_func == last_py_func)) {
  290. MS_LOG(DEBUG) << "Python function of elements[" << i << "] is same as previous.";
  291. continue;
  292. }
  293. last_py_func = this_py_func;
  294. oss << "# " << index++ << " " << trace::GetDebugInfo(debug_info, std::string(""));
  295. }
  296. stack.clear();
  297. MS_LOG(INFO) << "Get graph analysis information *end*";
  298. }
  299. // trace the graph evaluator stack
  300. static std::stack<std::pair<abstract::EvaluatorPtr, abstract::AnfNodeConfigPtr>> graph_infer_stack;
  301. // trace the cnode infer debug info
  302. static std::vector<abstract::AnfNodeConfigPtr> cnode_debug_stack{};
  303. void TraceGraphInferEnter(const abstract::EvaluatorPtr &eval, const abstract::AnfNodeConfigPtr &node) {
  304. if (eval == nullptr) {
  305. MS_LOG(EXCEPTION) << "GraphInferEnter got null eval";
  306. }
  307. if (eval->isa<abstract::FuncGraphEvaluator>() || eval->isa<abstract::MetaFuncGraphEvaluator>()) {
  308. graph_infer_stack.emplace(std::pair<abstract::EvaluatorPtr, abstract::AnfNodeConfigPtr>(eval, node));
  309. }
  310. }
  311. void TraceGraphInferLeave(const abstract::EvaluatorPtr &eval) {
  312. if (eval == nullptr) {
  313. MS_LOG(EXCEPTION) << "GraphInferEnter got null eval";
  314. }
  315. if (eval->isa<abstract::FuncGraphEvaluator>() || eval->isa<abstract::MetaFuncGraphEvaluator>()) {
  316. graph_infer_stack.pop();
  317. }
  318. }
  319. void TraceInferCNodeEnter(const abstract::AnfNodeConfigPtr &node_cfg) { cnode_debug_stack.push_back(node_cfg); }
  320. void TraceInferCNodeLeave() { cnode_debug_stack.pop_back(); }
  321. std::vector<abstract::AnfNodeConfigPtr> &GetCNodeDebugStack() { return cnode_debug_stack; }
  322. std::stack<std::pair<abstract::EvaluatorPtr, abstract::AnfNodeConfigPtr>> &GetCurrenGraphInferStack() {
  323. return graph_infer_stack;
  324. }
  325. void ClearTraceStack() {
  326. while (!graph_infer_stack.empty()) {
  327. graph_infer_stack.pop();
  328. }
  329. cnode_debug_stack.clear();
  330. }
  331. } // namespace trace
  332. } // namespace mindspore