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.

anf_ir_dump.cc 21 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /**
  2. * Copyright 2019-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 "debug/anf_ir_dump.h"
  17. #if defined(_WIN32) || defined(_WIN64)
  18. #include <stdlib.h>
  19. #endif
  20. #include <fstream>
  21. #include <iomanip>
  22. #include <memory>
  23. #include <unordered_map>
  24. #include "ir/primitive.h"
  25. #include "ir/func_graph.h"
  26. #include "runtime/device/kernel_info.h"
  27. #include "ir/graph_utils.h"
  28. #include "backend/session/anf_runtime_algorithm.h"
  29. #include "frontend/parallel/ops_info/operator_info.h"
  30. #include "pipeline/jit/base.h"
  31. #include "debug/common.h"
  32. #include "debug/trace.h"
  33. #include "utils/trace_base.h"
  34. namespace mindspore {
  35. const std::string ToShortString(const TypeId &typeId) {
  36. std::string label = TypeIdLabel(typeId);
  37. std::string prefix = "kNumberType";
  38. if (prefix.length() > label.length()) {
  39. return label;
  40. }
  41. auto position = label.find(prefix);
  42. // position is 0 when label begins with prefix
  43. if (position != 0) {
  44. return label;
  45. }
  46. auto sub_position = position + prefix.length();
  47. if (sub_position >= label.length()) {
  48. return label;
  49. }
  50. return label.substr(sub_position);
  51. }
  52. void PrintKernelFormatAndType(std::ostringstream &buffer, const std::string &fmt, const TypeId &type,
  53. const std::vector<size_t> &shape) {
  54. buffer << "<" << ToShortString(type);
  55. if (!fmt.empty()) {
  56. buffer << "x" << fmt << shape;
  57. }
  58. buffer << ">";
  59. }
  60. void PrintNodeOutputType(std::ostringstream &buffer, const AnfNodePtr &nd) {
  61. if (nd == nullptr) {
  62. return;
  63. }
  64. abstract::ShapePtr shape = dyn_cast<abstract::Shape>(nd->Shape());
  65. TypePtr type = dyn_cast<Type>(nd->Type());
  66. if ((nullptr != shape) && (nullptr != type)) {
  67. buffer << "<" << type << "x" << shape->shape() << ">";
  68. } else if (nullptr != type) {
  69. buffer << "<" << type << ">";
  70. } else {
  71. buffer << "<null>";
  72. }
  73. }
  74. void PrintNodeInputType(std::ostringstream &buffer, const AnfNodePtr &nd) {
  75. if (nd == nullptr) {
  76. return;
  77. }
  78. const auto &inputs = GetInputs(nd);
  79. size_t len = inputs.size();
  80. if (len > 1) {
  81. // skip inputs[0] which is Primitive value node
  82. for (size_t i = 1; i < len; ++i) {
  83. AnfNodePtr in = inputs[i];
  84. if (i != 1) {
  85. buffer << ", ";
  86. }
  87. PrintNodeOutputType(buffer, in);
  88. }
  89. }
  90. }
  91. void PrintInputAndOutputInferType(std::ostringstream &buffer, const AnfNodePtr &nd) {
  92. buffer << " : (";
  93. PrintNodeInputType(buffer, nd);
  94. buffer << ") -> (";
  95. PrintNodeOutputType(buffer, nd);
  96. buffer << ")";
  97. }
  98. struct SubGraphIRInfo {
  99. int32_t local_var;
  100. std::ostringstream buffer;
  101. OrderedMap<AnfNodePtr, int32_t> local_var_map;
  102. };
  103. void DumpGlobalInfoEntry(const FuncGraphPtr &graph, std::ostringstream &buffer) {
  104. if (graph == nullptr) {
  105. return;
  106. }
  107. buffer << "#IR entry : @" << graph->ToString() << "." << graph->debug_info()->get_id() << std::endl;
  108. buffer << "#attrs :" << std::endl;
  109. for (const auto &attr : graph->attrs()) {
  110. buffer << attr.first << " : ";
  111. if (attr.second->isa<BoolImm>()) {
  112. buffer << GetValue<bool>(attr.second);
  113. } else if (attr.second->isa<StringImm>()) {
  114. buffer << GetValue<std::string>(attr.second);
  115. }
  116. buffer << std::endl;
  117. }
  118. }
  119. void DumpKernelInfo(const CNodePtr &node, const std::shared_ptr<SubGraphIRInfo> &gsub) {
  120. if (node == nullptr || gsub == nullptr) {
  121. return;
  122. }
  123. auto kernel_info = node->kernel_info();
  124. if (kernel_info == nullptr || !kernel_info->has_build_info()) {
  125. return;
  126. }
  127. gsub->buffer << " : (";
  128. size_t input_num = AnfAlgo::GetInputTensorNum(node);
  129. for (size_t i = 0; i < input_num; ++i) {
  130. if (i != 0) {
  131. gsub->buffer << ", ";
  132. }
  133. auto format = AnfAlgo::GetInputFormat(node, i);
  134. auto type = AnfAlgo::GetInputDeviceDataType(node, i);
  135. auto shape = AnfAlgo::GetInputDeviceShape(node, i);
  136. PrintKernelFormatAndType(gsub->buffer, format, type, shape);
  137. }
  138. gsub->buffer << ") -> (";
  139. size_t output_num = AnfAlgo::GetOutputTensorNum(node);
  140. for (size_t i = 0; i < output_num; ++i) {
  141. if (i != 0) {
  142. gsub->buffer << ", ";
  143. }
  144. auto format = AnfAlgo::GetOutputFormat(node, i);
  145. auto type = AnfAlgo::GetOutputDeviceDataType(node, i);
  146. auto shape = AnfAlgo::GetOutputDeviceShape(node, i);
  147. PrintKernelFormatAndType(gsub->buffer, format, type, shape);
  148. }
  149. gsub->buffer << ")";
  150. gsub->buffer << std::endl;
  151. }
  152. int32_t DumpParams(const FuncGraphPtr &graph, std::ostringstream &buffer, OrderedMap<AnfNodePtr, int32_t> *para_map) {
  153. if (graph == nullptr) {
  154. MS_LOG(INFO) << "Param graph is nullptr.";
  155. return 0;
  156. }
  157. std::vector<AnfNodePtr> parameters = graph->parameters();
  158. buffer << "#Total params : " << parameters.size() << std::endl;
  159. buffer << std::endl;
  160. // dump parameters
  161. int32_t para = 1;
  162. for (const auto &p : parameters) {
  163. if (p == nullptr) {
  164. continue;
  165. }
  166. auto parameter_ptr = p->cast<ParameterPtr>();
  167. if (parameter_ptr == nullptr) {
  168. MS_LOG(EXCEPTION) << "p cannot cast to ParameterPtr";
  169. }
  170. buffer << "%para" << para << "_" << parameter_ptr->name() << " : ";
  171. // print parameters' type and shape
  172. PrintNodeOutputType(buffer, p);
  173. auto kernel_info = p->kernel_info();
  174. if (kernel_info != nullptr && kernel_info->has_build_info()) {
  175. buffer << " : ";
  176. auto type = AnfAlgo::GetOutputDeviceDataType(p, 0);
  177. auto format = AnfAlgo::GetOutputFormat(p, 0);
  178. auto shape = AnfAlgo::GetOutputDeviceShape(p, 0);
  179. PrintKernelFormatAndType(buffer, format, type, shape);
  180. buffer << " : IsWeight:" << std::boolalpha << AnfAlgo::IsParameterWeight(parameter_ptr);
  181. }
  182. buffer << std::endl;
  183. if (para_map != nullptr) {
  184. (*para_map)[p] = para++;
  185. }
  186. MS_LOG(DEBUG) << "Record param: " << p->ToString() << " graph belong : " << p->func_graph()->ToString();
  187. }
  188. return para;
  189. }
  190. void DumpOperator(const AnfNodePtr &op, const std::shared_ptr<SubGraphIRInfo> &gsub) {
  191. if (op == nullptr) {
  192. MS_LOG(INFO) << "Param op is nullptr";
  193. return;
  194. }
  195. if (gsub == nullptr) {
  196. MS_LOG(INFO) << "Param gsub is nullptr";
  197. return;
  198. }
  199. if (IsValueNode<FuncGraph>(op)) {
  200. FuncGraphPtr fg = GetValueNode<FuncGraphPtr>(op);
  201. if (fg != nullptr) {
  202. gsub->buffer << "call @" << fg->ToString() << "." << fg->debug_info()->get_id();
  203. }
  204. } else if (op->isa<CNode>()) {
  205. if (gsub->local_var_map.find(op) != gsub->local_var_map.end()) {
  206. gsub->buffer << "%" << gsub->local_var_map[op];
  207. } else {
  208. auto node = op->cast<CNodePtr>();
  209. auto fg = node->func_graph();
  210. gsub->buffer << "$(" << fg->ToString() << "." << fg->debug_info()->get_id() << ":" << node->ToString() << ")";
  211. }
  212. } else if (op->isa<ValueNode>()) {
  213. gsub->buffer << GetValueNode(op)->ToString();
  214. } else {
  215. gsub->buffer << op->ToString();
  216. }
  217. }
  218. void DumpOperands(const AnfNodePtr &nd, OrderedMap<AnfNodePtr, int32_t> *para_map,
  219. const std::shared_ptr<SubGraphIRInfo> &gsub) {
  220. if (nd == nullptr || para_map == nullptr || gsub == nullptr) {
  221. return;
  222. }
  223. gsub->buffer << "(";
  224. const auto &inputs = GetInputs(nd);
  225. size_t len = inputs.size();
  226. if (len > 1) {
  227. // skip inputs[0] which is Primitive valuenode
  228. for (size_t i = 1; i < len; ++i) {
  229. AnfNodePtr in = inputs[i];
  230. MS_EXCEPTION_IF_NULL(in);
  231. if (i != 1) {
  232. gsub->buffer << ", ";
  233. }
  234. if (in->isa<Parameter>()) {
  235. if (!(*para_map)[in]) {
  236. gsub->buffer << "%para_" << in->ToString();
  237. } else {
  238. gsub->buffer << "%para" << (*para_map)[in] << "_" << in->ToString();
  239. }
  240. } else if (in->isa<CNode>()) {
  241. if (gsub->local_var_map.find(in) != gsub->local_var_map.end()) {
  242. gsub->buffer << "%" << gsub->local_var_map[in];
  243. } else {
  244. auto node = in->cast<CNodePtr>();
  245. auto fg = node->func_graph();
  246. gsub->buffer << "$(" << fg->ToString() << "." << fg->debug_info()->get_id() << ":" << node->ToString() << ")";
  247. }
  248. } else if (in->isa<ValueNode>() && !IsValueNode<FuncGraph>(in)) {
  249. // non Primitive valuenode
  250. gsub->buffer << GetValueNode(in)->ToString();
  251. } else if (IsValueNode<FuncGraph>(in)) {
  252. FuncGraphPtr fg = GetValueNode<FuncGraphPtr>(in);
  253. gsub->buffer << "@" << fg->ToString() << "." << fg->debug_info()->get_id();
  254. } else {
  255. gsub->buffer << in->ToString();
  256. }
  257. }
  258. }
  259. gsub->buffer << ")";
  260. }
  261. void DumpParallelInfo(const CNodePtr &node, const std::shared_ptr<SubGraphIRInfo> &gsub) {
  262. if ((node == nullptr) || (gsub == nullptr)) {
  263. return;
  264. }
  265. auto operator_info = node->user_data<parallel::OperatorInfo>();
  266. if (operator_info == nullptr) {
  267. return;
  268. }
  269. auto strategy = operator_info->strategy();
  270. if (strategy == nullptr) {
  271. return;
  272. }
  273. ValuePtr temp = MakeValue(strategy->GetInputDim());
  274. gsub->buffer << " { strategy: ";
  275. gsub->buffer << temp->ToString();
  276. gsub->buffer << " }";
  277. }
  278. void DumpOperateAttrs(const AnfNodePtr &op, const std::shared_ptr<SubGraphIRInfo> &gsub) {
  279. if (op == nullptr || gsub == nullptr) {
  280. return;
  281. }
  282. if (IsValueNode<Primitive>(op)) {
  283. PrimitivePtr primitive = GetValueNode<PrimitivePtr>(op);
  284. if (!primitive->instance_name().empty()) {
  285. gsub->buffer << " {";
  286. gsub->buffer << "instance name"
  287. << ": ";
  288. gsub->buffer << primitive->instance_name();
  289. gsub->buffer << "}";
  290. }
  291. auto attrs = primitive->attrs();
  292. if (!attrs.empty()) {
  293. gsub->buffer << " primitive_attrs: {";
  294. int i = 0;
  295. for (const auto &attr : attrs) {
  296. if (attr.first == PARALLEL_STRATEGY) {
  297. continue; // skip the strategy
  298. }
  299. if (i++ != 0) {
  300. gsub->buffer << ", ";
  301. }
  302. gsub->buffer << attr.first << ": ";
  303. if (attr.second == nullptr) {
  304. gsub->buffer << "null";
  305. } else {
  306. gsub->buffer << attr.second->ToString();
  307. }
  308. }
  309. gsub->buffer << "}";
  310. }
  311. }
  312. }
  313. void DumpCNodeAttrs(const CNodePtr &op, const std::shared_ptr<SubGraphIRInfo> &gsub) {
  314. if (op == nullptr || gsub == nullptr) {
  315. return;
  316. }
  317. if (op->attrs().empty()) {
  318. gsub->buffer << std::endl;
  319. return;
  320. }
  321. auto attrs = op->attrs();
  322. gsub->buffer << " cnode_attrs: {";
  323. int i = 0;
  324. for (const auto &attr : attrs) {
  325. if (i++ != 0) {
  326. gsub->buffer << ", ";
  327. }
  328. gsub->buffer << attr.first << ": ";
  329. if (attr.second == nullptr) {
  330. gsub->buffer << "null";
  331. } else {
  332. gsub->buffer << attr.second->ToString();
  333. }
  334. }
  335. gsub->buffer << "}";
  336. gsub->buffer << std::endl;
  337. }
  338. void DumpShape(const AnfNodePtr &nd, const FuncGraphPtr &sub_graph, const std::shared_ptr<SubGraphIRInfo> &gsub) {
  339. if (nd == nullptr || sub_graph == nullptr || gsub == nullptr) {
  340. return;
  341. }
  342. if (nd != sub_graph->get_return()) {
  343. gsub->buffer << " : (";
  344. PrintNodeInputType(gsub->buffer, nd);
  345. gsub->buffer << ") -> (";
  346. PrintNodeOutputType(gsub->buffer, nd);
  347. gsub->buffer << ")";
  348. } else {
  349. gsub->buffer << " : (";
  350. PrintNodeInputType(gsub->buffer, nd);
  351. gsub->buffer << ")";
  352. }
  353. gsub->buffer << std::endl;
  354. }
  355. void DumpCNode(const CNodePtr &nd, const FuncGraphPtr &sub_graph, OrderedMap<AnfNodePtr, int32_t> *const para_map,
  356. const std::shared_ptr<SubGraphIRInfo> &gsub, bool dump_full_name = false,
  357. LocDumpMode dump_location = kOff) {
  358. if (nd == nullptr || sub_graph == nullptr || para_map == nullptr || gsub == nullptr) {
  359. return;
  360. }
  361. if (nd != sub_graph->get_return()) {
  362. gsub->buffer << " %" << gsub->local_var << "(" << nd->ToString() << ")"
  363. << " = ";
  364. gsub->local_var_map[nd] = gsub->local_var++;
  365. } else {
  366. gsub->buffer << " ";
  367. }
  368. if (nd->inputs().empty()) {
  369. MS_LOG(EXCEPTION) << "Input of apply node is empty";
  370. }
  371. // print operator
  372. AnfNodePtr op = nd->input(0);
  373. DumpOperator(op, gsub);
  374. // print operands
  375. DumpOperands(nd, para_map, gsub);
  376. // print operator attrs
  377. DumpOperateAttrs(op, gsub);
  378. // print cnode attrs
  379. DumpCNodeAttrs(nd, gsub);
  380. // print parallel info
  381. DumpParallelInfo(nd, gsub);
  382. // print shape info
  383. DumpShape(nd, sub_graph, gsub);
  384. // print kernel info
  385. DumpKernelInfo(nd, gsub);
  386. if (dump_full_name) {
  387. gsub->buffer << " : (" << nd->fullname_with_scope() << ")" << std::endl;
  388. }
  389. if (dump_location == kTopStack) {
  390. if (label_manage::GetGlobalTraceLabelType() == label_manage::TraceLabelType::kWithUniqueId) {
  391. gsub->buffer << trace::GetDebugInfo(nd->debug_info(), " # ", kSourceLineTipDiscard) << "#"
  392. << label_manage::Label(nd->debug_info()) << "\n";
  393. } else {
  394. gsub->buffer << trace::GetDebugInfo(nd->debug_info(), " # ", kSourceLineTipDiscard) << "\n";
  395. }
  396. } else if (dump_location == kWholeStack) {
  397. auto traces = mindspore::trace::GetSourceLineList(nd);
  398. for (auto &trace : traces) {
  399. gsub->buffer << " # " << trace;
  400. }
  401. }
  402. }
  403. void DumpIRInSubgraph(const std::vector<AnfNodePtr> &nodes, OrderedMap<AnfNodePtr, int32_t> *para_map,
  404. OrderedMap<FuncGraphPtr, std::shared_ptr<SubGraphIRInfo>> *const sub_graphs, int32_t total_para,
  405. bool dump_full_name = false, LocDumpMode dump_location = kOff) {
  406. if (para_map == nullptr || sub_graphs == nullptr) {
  407. return;
  408. }
  409. for (const auto &nd : nodes) {
  410. MS_EXCEPTION_IF_NULL(nd);
  411. FuncGraphPtr sub_graph = nd->func_graph();
  412. if (sub_graph == nullptr) {
  413. MS_LOG(DEBUG) << "Node[" << nd->ToString() << "] belongs to no graph!";
  414. continue;
  415. }
  416. std::shared_ptr<SubGraphIRInfo> gsub = (*sub_graphs)[sub_graph];
  417. if (gsub == nullptr) {
  418. gsub = std::make_shared<SubGraphIRInfo>();
  419. gsub->local_var = 0;
  420. (*sub_graphs)[sub_graph] = gsub;
  421. }
  422. std::vector<AnfNodePtr> parameters = sub_graph->parameters();
  423. for (size_t idx = 0; idx < parameters.size(); idx++) {
  424. MS_EXCEPTION_IF_NULL(parameters[idx]);
  425. if ((*para_map).count(parameters[idx]) == 0) {
  426. (*para_map)[parameters[idx]] = total_para++;
  427. }
  428. }
  429. if (!nd->isa<Parameter>()) {
  430. if (nd->isa<CNode>()) {
  431. // print and record output of operator if it is not 'Return'
  432. DumpCNode(nd->cast<CNodePtr>(), sub_graph, para_map, gsub, dump_full_name, dump_location);
  433. } else {
  434. gsub->buffer << " " << nd->ToString() << std::endl;
  435. }
  436. }
  437. }
  438. }
  439. void DumpSubgraph(const OrderedMap<FuncGraphPtr, std::shared_ptr<SubGraphIRInfo>> *sub_graphs,
  440. const FuncGraphPtr &graph, OrderedMap<AnfNodePtr, int32_t> *para_map, std::ofstream &fout) {
  441. if (sub_graphs == nullptr || graph == nullptr) {
  442. return;
  443. }
  444. fout << "#Total subgraph : " << sub_graphs->size() << std::endl;
  445. fout << std::endl;
  446. for (const auto &sg : *sub_graphs) {
  447. fout << "subgraph attr:" << std::endl;
  448. MS_EXCEPTION_IF_NULL(sg.first);
  449. for (const auto &attr : sg.first->attrs()) {
  450. fout << attr.first << " : ";
  451. if (attr.second->isa<BoolImm>()) {
  452. fout << GetValue<bool>(attr.second);
  453. } else if (attr.second->isa<StringImm>()) {
  454. fout << GetValue<std::string>(attr.second);
  455. }
  456. fout << std::endl;
  457. }
  458. fout << "subgraph @" << sg.first->ToString() << ".";
  459. fout << sg.first->debug_info()->get_id() << "(";
  460. if (sg.first != graph) {
  461. std::vector<AnfNodePtr> parameters = sg.first->parameters();
  462. if (parameters.size() == 1) {
  463. MS_EXCEPTION_IF_NULL(parameters[0]);
  464. fout << "%para" << (*para_map)[parameters[0]] << "_" << parameters[0]->ToString();
  465. } else if (parameters.size() > 1) {
  466. for (size_t idx = 0; idx < parameters.size() - 1; idx++) {
  467. MS_EXCEPTION_IF_NULL(parameters[idx]);
  468. fout << "%para" << (*para_map)[parameters[idx]] << "_" << parameters[idx]->ToString();
  469. fout << ", ";
  470. }
  471. MS_EXCEPTION_IF_NULL(parameters[parameters.size() - 1]);
  472. fout << "%para" << (*para_map)[parameters[parameters.size() - 1]] << "_"
  473. << parameters[parameters.size() - 1]->ToString();
  474. }
  475. }
  476. fout << ") {" << std::endl;
  477. MS_EXCEPTION_IF_NULL(sg.second);
  478. fout << sg.second->buffer.str();
  479. fout << "}" << std::endl;
  480. fout << std::endl;
  481. }
  482. }
  483. std::string AddGlobalId(const std::string &filename) {
  484. static size_t g_id = 0;
  485. std::ostringstream s;
  486. auto i = filename.rfind(".ir");
  487. if (i >= filename.size()) {
  488. s << filename;
  489. s << "_" << std::setfill('0') << std::setw(4) << g_id;
  490. } else {
  491. s << filename.substr(0, i);
  492. s << "_" << std::setfill('0') << std::setw(4) << g_id;
  493. if (i + 1 < filename.size()) {
  494. s << filename.substr(i);
  495. }
  496. }
  497. ++g_id;
  498. return s.str();
  499. }
  500. void GetEnvDumpIrLineLevel(LocDumpMode *dump_location) {
  501. static std::unordered_map<std::string, enum LocDumpMode> dump_level_map = {
  502. {std::to_string(kOff), kOff}, {std::to_string(kTopStack), kTopStack}, {std::to_string(kWholeStack), kWholeStack}};
  503. static auto dump_level_in_env = common::GetEnv("ENV_DUMP_IR_LINE_LEVEL");
  504. auto it = dump_level_map.find(dump_level_in_env);
  505. if (it == dump_level_map.end()) {
  506. return;
  507. }
  508. // Use the env setting instead parameter setting.
  509. *dump_location = it->second;
  510. }
  511. #ifdef ENABLE_DUMP_IR
  512. void DumpIR(const std::string &filename, const FuncGraphPtr &graph, bool dump_full_name, LocDumpMode dump_location) {
  513. GetEnvDumpIrLineLevel(&dump_location);
  514. if (graph == nullptr) {
  515. return;
  516. }
  517. auto path = pipeline::GetSaveGraphsPathName(AddGlobalId(filename));
  518. auto realpath = Common::GetRealPath(path);
  519. if (!realpath.has_value()) {
  520. MS_LOG(ERROR) << "Get real path failed. path=" << path;
  521. return;
  522. }
  523. ChangeFileMode(realpath.value(), S_IRWXU);
  524. std::ofstream fout(realpath.value());
  525. std::ostringstream buffer;
  526. if (!fout.is_open()) {
  527. MS_LOG(ERROR) << "Open dump file '" << realpath.value() << "' failed!";
  528. return;
  529. }
  530. auto nodes = TopoSort(graph->get_return(), SuccDeeperSimple, AlwaysInclude);
  531. OrderedMap<AnfNodePtr, int32_t> para_map;
  532. // dump global info
  533. DumpGlobalInfoEntry(graph, buffer);
  534. int32_t total_para = DumpParams(graph, buffer, &para_map);
  535. OrderedMap<FuncGraphPtr, std::shared_ptr<SubGraphIRInfo>> sub_graphs;
  536. // dump ir in each sub graph
  537. DumpIRInSubgraph(nodes, &para_map, &sub_graphs, total_para, dump_full_name, dump_location);
  538. // output global info
  539. fout << buffer.str() << std::endl;
  540. // output each sub graph
  541. DumpSubgraph(&sub_graphs, graph, &para_map, fout);
  542. fout.close();
  543. // set file mode to read only by user
  544. ChangeFileMode(realpath.value(), S_IRUSR);
  545. }
  546. void DumpIRForRDR(const std::string &filename, const FuncGraphPtr &graph, bool dump_full_name,
  547. LocDumpMode dump_location) {
  548. GetEnvDumpIrLineLevel(&dump_location);
  549. if (graph == nullptr) {
  550. return;
  551. }
  552. auto path = AddGlobalId(filename);
  553. auto realpath = Common::GetRealPath(path);
  554. if (!realpath.has_value()) {
  555. MS_LOG(ERROR) << "Get real path failed. path=" << path;
  556. return;
  557. }
  558. ChangeFileMode(realpath.value(), S_IRWXU);
  559. std::ofstream fout(realpath.value());
  560. std::ostringstream buffer;
  561. if (!fout.is_open()) {
  562. MS_LOG(ERROR) << "Open dump file '" << realpath.value() << "' failed!";
  563. return;
  564. }
  565. auto nodes = TopoSort(graph->get_return(), SuccDeeperSimple, AlwaysInclude);
  566. OrderedMap<AnfNodePtr, int32_t> para_map;
  567. // dump global info
  568. DumpGlobalInfoEntry(graph, buffer);
  569. int32_t total_para = DumpParams(graph, buffer, &para_map);
  570. OrderedMap<FuncGraphPtr, std::shared_ptr<SubGraphIRInfo>> sub_graphs;
  571. // dump ir in each sub graph
  572. DumpIRInSubgraph(nodes, &para_map, &sub_graphs, total_para, dump_full_name, dump_location);
  573. // output global info
  574. fout << buffer.str() << std::endl;
  575. // output each sub graph
  576. DumpSubgraph(&sub_graphs, graph, &para_map, fout);
  577. fout.close();
  578. // set file mode to read only by user
  579. ChangeFileMode(realpath.value(), S_IRUSR);
  580. }
  581. #else
  582. void DumpIR(const std::string &, const FuncGraphPtr &, bool, LocDumpMode) {
  583. static bool already_printed = false;
  584. if (already_printed) {
  585. return;
  586. }
  587. already_printed = true;
  588. MS_LOG(WARNING) << "The functionality of dumping function graph IR is disabled, "
  589. << "please recompile source to enable it. See help of building script.";
  590. }
  591. void DumpIRForRDR(const std::string &, const FuncGraphPtr &, bool, LocDumpMode) {
  592. static bool already_printed = false;
  593. if (already_printed) {
  594. return;
  595. }
  596. already_printed = true;
  597. MS_LOG(WARNING) << "The functionality of dumping function graph IR is disabled, "
  598. << "please recompile source to enable it. See help of building script.";
  599. }
  600. #endif
  601. } // namespace mindspore