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

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 "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 <map>
  23. #include <memory>
  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. namespace mindspore {
  32. const std::string ToShortString(const TypeId &typeId) {
  33. std::string label = TypeIdLabel(typeId);
  34. std::string prefix = "kNumberType";
  35. if (prefix.length() > label.length()) {
  36. return label;
  37. }
  38. auto position = label.find(prefix);
  39. // position is 0 when label begins with prefix
  40. if (position != 0) {
  41. return label;
  42. }
  43. auto sub_position = position + prefix.length();
  44. if (sub_position >= label.length()) {
  45. return label;
  46. }
  47. return label.substr(sub_position);
  48. }
  49. void PrintKernelFormatAndType(std::ostringstream &buffer, const std::string &fmt, const TypeId &type,
  50. const std::vector<size_t> &shape) {
  51. buffer << "<" << ToShortString(type);
  52. if (!fmt.empty()) {
  53. buffer << "x" << fmt << shape;
  54. }
  55. buffer << ">";
  56. }
  57. void PrintNodeOutputType(std::ostringstream &buffer, const AnfNodePtr &nd) {
  58. if (nd == nullptr) {
  59. return;
  60. }
  61. abstract::ShapePtr shape = dyn_cast<abstract::Shape>(nd->Shape());
  62. TypePtr type = dyn_cast<Type>(nd->Type());
  63. if ((nullptr != shape) && (nullptr != type)) {
  64. buffer << "<" << type << "x" << shape->shape() << ">";
  65. } else if (nullptr != type) {
  66. buffer << "<" << type << ">";
  67. } else {
  68. buffer << "<null>";
  69. }
  70. }
  71. void PrintNodeInputType(std::ostringstream &buffer, const AnfNodePtr &nd) {
  72. if (nd == nullptr) {
  73. return;
  74. }
  75. std::vector<AnfNodePtr> inputs = SuccIncoming(nd);
  76. size_t len = inputs.size();
  77. if (len > 1) {
  78. // skip inputs[0] which is Primitive value node
  79. for (size_t i = 1; i < len; ++i) {
  80. AnfNodePtr in = inputs[i];
  81. if (i != 1) {
  82. buffer << ", ";
  83. }
  84. PrintNodeOutputType(buffer, in);
  85. }
  86. }
  87. }
  88. void PrintInputAndOutputInferType(std::ostringstream &buffer, const AnfNodePtr &nd) {
  89. buffer << " : (";
  90. PrintNodeInputType(buffer, nd);
  91. buffer << ") -> (";
  92. PrintNodeOutputType(buffer, nd);
  93. buffer << ")";
  94. }
  95. struct SubGraphIRInfo {
  96. int32_t local_var;
  97. std::ostringstream buffer;
  98. OrderedMap<AnfNodePtr, int32_t> local_var_map;
  99. };
  100. void DumpGlobalInfoEntry(const FuncGraphPtr &graph, std::ostringstream &buffer) {
  101. if (graph == nullptr) {
  102. return;
  103. }
  104. buffer << "#IR entry : @" << graph->ToString() << "." << graph->debug_info()->get_id() << std::endl;
  105. buffer << "#attrs :" << std::endl;
  106. for (const auto &attr : graph->attrs()) {
  107. buffer << attr.first << " : ";
  108. if (attr.second->isa<BoolImm>()) {
  109. buffer << GetValue<bool>(attr.second);
  110. } else if (attr.second->isa<StringImm>()) {
  111. buffer << GetValue<std::string>(attr.second);
  112. }
  113. buffer << std::endl;
  114. }
  115. }
  116. void DumpKernelInfo(const CNodePtr &node, const std::shared_ptr<SubGraphIRInfo> &gsub) {
  117. if (node == nullptr || gsub == nullptr) {
  118. return;
  119. }
  120. auto kernel_info = node->kernel_info();
  121. if (kernel_info == nullptr || !kernel_info->has_build_info()) {
  122. return;
  123. }
  124. gsub->buffer << " : (";
  125. for (size_t i = 0; i < AnfAlgo::GetInputTensorNum(node); ++i) {
  126. if (i != 0) {
  127. gsub->buffer << ", ";
  128. }
  129. auto format = AnfAlgo::GetInputFormat(node, i);
  130. auto type = AnfAlgo::GetInputDeviceDataType(node, i);
  131. auto shape = AnfAlgo::GetInputDeviceShape(node, i);
  132. PrintKernelFormatAndType(gsub->buffer, format, type, shape);
  133. }
  134. gsub->buffer << ") -> (";
  135. for (size_t i = 0; i < AnfAlgo::GetOutputTensorNum(node); ++i) {
  136. if (i != 0) {
  137. gsub->buffer << ", ";
  138. }
  139. auto format = AnfAlgo::GetOutputFormat(node, i);
  140. auto type = AnfAlgo::GetOutputDeviceDataType(node, i);
  141. auto shape = AnfAlgo::GetOutputDeviceShape(node, i);
  142. PrintKernelFormatAndType(gsub->buffer, format, type, shape);
  143. }
  144. gsub->buffer << ")";
  145. gsub->buffer << std::endl;
  146. }
  147. void DumpParams(const FuncGraphPtr &graph, std::ostringstream &buffer, OrderedMap<AnfNodePtr, int32_t> *para_map) {
  148. if (graph == nullptr) {
  149. MS_LOG(INFO) << "Param graph is nullptr.";
  150. return;
  151. }
  152. std::vector<AnfNodePtr> parameters = graph->parameters();
  153. buffer << "#Total params : " << parameters.size() << std::endl;
  154. buffer << std::endl;
  155. // dump parameters
  156. int32_t para = 1;
  157. for (const auto &p : parameters) {
  158. if (p == nullptr) {
  159. continue;
  160. }
  161. auto parameter_ptr = p->cast<ParameterPtr>();
  162. if (parameter_ptr == nullptr) {
  163. MS_LOG(EXCEPTION) << "p cannot cast to ParameterPtr";
  164. }
  165. buffer << "%para" << para << " = " << parameter_ptr->name() << " : ";
  166. // print parameters' type and shape
  167. PrintNodeOutputType(buffer, p);
  168. auto kernel_info = p->kernel_info();
  169. if (kernel_info != nullptr && kernel_info->has_build_info()) {
  170. buffer << " : ";
  171. auto type = AnfAlgo::GetOutputDeviceDataType(p, 0);
  172. auto format = AnfAlgo::GetOutputFormat(p, 0);
  173. auto shape = AnfAlgo::GetOutputDeviceShape(p, 0);
  174. PrintKernelFormatAndType(buffer, format, type, shape);
  175. buffer << " : IsWeight:" << std::boolalpha << AnfAlgo::IsParameterWeight(parameter_ptr);
  176. }
  177. buffer << std::endl;
  178. if (para_map != nullptr) {
  179. (*para_map)[p] = para++;
  180. }
  181. MS_LOG(DEBUG) << "Record param: " << p->ToString() << " graph belong : " << p->func_graph()->ToString();
  182. }
  183. }
  184. void DumpOperator(const AnfNodePtr &op, const std::shared_ptr<SubGraphIRInfo> &gsub) {
  185. if (op == nullptr) {
  186. MS_LOG(INFO) << "Param op is nullptr";
  187. return;
  188. }
  189. if (gsub == nullptr) {
  190. MS_LOG(INFO) << "Param gsub is nullptr";
  191. return;
  192. }
  193. if (IsValueNode<FuncGraph>(op)) {
  194. FuncGraphPtr fg = GetValueNode<FuncGraphPtr>(op);
  195. if (fg != nullptr) {
  196. gsub->buffer << "call @" << fg->ToString() << "." << fg->debug_info()->get_id();
  197. }
  198. } else if (op->isa<CNode>()) {
  199. if (gsub->local_var_map.find(op) != gsub->local_var_map.end()) {
  200. gsub->buffer << "%" << gsub->local_var_map[op];
  201. }
  202. } else if (op->isa<ValueNode>()) {
  203. gsub->buffer << GetValueNode(op)->ToString();
  204. } else {
  205. gsub->buffer << op->ToString();
  206. }
  207. }
  208. void DumpOperands(const AnfNodePtr &nd, OrderedMap<AnfNodePtr, int32_t> *para_map,
  209. const std::shared_ptr<SubGraphIRInfo> &gsub) {
  210. if (nd == nullptr || para_map == nullptr || gsub == nullptr) {
  211. return;
  212. }
  213. gsub->buffer << "(";
  214. std::vector<AnfNodePtr> inputs = SuccIncoming(nd);
  215. size_t len = inputs.size();
  216. if (len > 1) {
  217. // skip inputs[0] which is Primitive valuenode
  218. for (size_t i = 1; i < len; ++i) {
  219. AnfNodePtr in = inputs[i];
  220. MS_EXCEPTION_IF_NULL(in);
  221. if (i != 1) {
  222. gsub->buffer << ", ";
  223. }
  224. if (in->isa<Parameter>()) {
  225. if (!(*para_map)[in]) {
  226. gsub->buffer << "%para_" << in->ToString();
  227. } else {
  228. gsub->buffer << "%para" << (*para_map)[in];
  229. }
  230. } else if (in->isa<CNode>()) {
  231. gsub->buffer << "%" << gsub->local_var_map[in];
  232. } else if (in->isa<ValueNode>() && !IsValueNode<FuncGraph>(in)) {
  233. // non Primitive valuenode
  234. gsub->buffer << GetValueNode(in)->ToString();
  235. } else if (IsValueNode<FuncGraph>(in)) {
  236. FuncGraphPtr fg = GetValueNode<FuncGraphPtr>(in);
  237. gsub->buffer << "@" << fg->ToString() << "." << fg->debug_info()->get_id();
  238. } else {
  239. gsub->buffer << in->ToString();
  240. }
  241. }
  242. }
  243. gsub->buffer << ")";
  244. }
  245. void DumpParallelInfo(const CNodePtr &node, const std::shared_ptr<SubGraphIRInfo> &gsub) {
  246. if ((node == nullptr) || (gsub == nullptr)) {
  247. return;
  248. }
  249. auto operator_info = node->user_data<parallel::OperatorInfo>();
  250. if (operator_info == nullptr) {
  251. return;
  252. }
  253. auto strategy = operator_info->strategy();
  254. if (strategy == nullptr) {
  255. return;
  256. }
  257. ValuePtr temp = MakeValue(strategy->GetInputDim());
  258. gsub->buffer << " { strategy: ";
  259. gsub->buffer << temp->ToString();
  260. gsub->buffer << " }";
  261. }
  262. void DumpOperateAttrs(const AnfNodePtr &op, const std::shared_ptr<SubGraphIRInfo> &gsub) {
  263. if (op == nullptr || gsub == nullptr) {
  264. return;
  265. }
  266. if (IsValueNode<Primitive>(op)) {
  267. PrimitivePtr primitive = GetValueNode<PrimitivePtr>(op);
  268. if (!primitive->instance_name().empty()) {
  269. gsub->buffer << " {";
  270. gsub->buffer << "instance name"
  271. << ": ";
  272. gsub->buffer << primitive->instance_name();
  273. gsub->buffer << "}";
  274. }
  275. auto attrs = primitive->attrs();
  276. if (!attrs.empty()) {
  277. gsub->buffer << " {";
  278. int i = 0;
  279. for (const auto &attr : attrs) {
  280. if (attr.first == PARALLEL_STRATEGY) {
  281. continue; // skip the strategy
  282. }
  283. if (i++ != 0) {
  284. gsub->buffer << ", ";
  285. }
  286. gsub->buffer << attr.first << ": ";
  287. if (attr.second == nullptr) {
  288. gsub->buffer << "null";
  289. } else {
  290. gsub->buffer << attr.second->ToString();
  291. }
  292. }
  293. gsub->buffer << "}";
  294. }
  295. }
  296. gsub->buffer << std::endl;
  297. }
  298. void DumpShape(const AnfNodePtr &nd, const FuncGraphPtr &sub_graph, const std::shared_ptr<SubGraphIRInfo> &gsub) {
  299. if (nd == nullptr || sub_graph == nullptr || gsub == nullptr) {
  300. return;
  301. }
  302. if (nd != sub_graph->get_return()) {
  303. gsub->buffer << " : (";
  304. PrintNodeInputType(gsub->buffer, nd);
  305. gsub->buffer << ") -> (";
  306. PrintNodeOutputType(gsub->buffer, nd);
  307. gsub->buffer << ")";
  308. } else {
  309. gsub->buffer << " : (";
  310. PrintNodeInputType(gsub->buffer, nd);
  311. gsub->buffer << ")";
  312. }
  313. gsub->buffer << std::endl;
  314. }
  315. void DumpCNode(const CNodePtr &nd, const FuncGraphPtr &sub_graph, OrderedMap<AnfNodePtr, int32_t> *const para_map,
  316. const std::shared_ptr<SubGraphIRInfo> &gsub, bool dump_full_name = false) {
  317. if (nd == nullptr || sub_graph == nullptr || para_map == nullptr || gsub == nullptr) {
  318. return;
  319. }
  320. if (nd != sub_graph->get_return()) {
  321. gsub->buffer << " %" << gsub->local_var << "(" << nd->ToString() << ")"
  322. << " = ";
  323. gsub->local_var_map[nd] = gsub->local_var++;
  324. } else {
  325. gsub->buffer << " ";
  326. }
  327. if (nd->inputs().empty()) {
  328. MS_LOG(EXCEPTION) << "Input of apply node is empty";
  329. }
  330. // print operator
  331. AnfNodePtr op = nd->input(0);
  332. DumpOperator(op, gsub);
  333. // print operands
  334. DumpOperands(nd, para_map, gsub);
  335. // print operator attrs
  336. DumpOperateAttrs(op, gsub);
  337. // print parallel info
  338. DumpParallelInfo(nd, gsub);
  339. // print shape info
  340. DumpShape(nd, sub_graph, gsub);
  341. // print kernel info
  342. DumpKernelInfo(nd, gsub);
  343. if (dump_full_name) {
  344. gsub->buffer << " : (" << nd->fullname_with_scope() << ")" << std::endl;
  345. }
  346. }
  347. void DumpIRInSubgraph(const std::vector<AnfNodePtr> &nodes, OrderedMap<AnfNodePtr, int32_t> *para_map,
  348. OrderedMap<FuncGraphPtr, std::shared_ptr<SubGraphIRInfo>> *const sub_graphs,
  349. bool dump_full_name = false) {
  350. if (para_map == nullptr || sub_graphs == nullptr) {
  351. return;
  352. }
  353. for (const auto &nd : nodes) {
  354. MS_EXCEPTION_IF_NULL(nd);
  355. FuncGraphPtr sub_graph = nd->func_graph();
  356. if (sub_graph == nullptr) {
  357. MS_LOG(DEBUG) << "Node[" << nd->ToString() << "] belongs to no graph!";
  358. continue;
  359. }
  360. std::shared_ptr<SubGraphIRInfo> gsub = (*sub_graphs)[sub_graph];
  361. if (gsub == nullptr) {
  362. gsub = std::make_shared<SubGraphIRInfo>();
  363. gsub->local_var = 0;
  364. (*sub_graphs)[sub_graph] = gsub;
  365. }
  366. if (!nd->isa<Parameter>()) {
  367. if (nd->isa<CNode>()) {
  368. // print and record output of operator if it is not 'Return'
  369. DumpCNode(nd->cast<CNodePtr>(), sub_graph, para_map, gsub, dump_full_name);
  370. } else {
  371. gsub->buffer << " " << nd->ToString() << std::endl;
  372. }
  373. }
  374. }
  375. }
  376. void DumpSubgraph(const OrderedMap<FuncGraphPtr, std::shared_ptr<SubGraphIRInfo>> *sub_graphs,
  377. const FuncGraphPtr &graph, std::ofstream &fout) {
  378. if (sub_graphs == nullptr || graph == nullptr) {
  379. return;
  380. }
  381. fout << "#Total subgraph : " << sub_graphs->size() << std::endl;
  382. fout << std::endl;
  383. for (const auto &sg : *sub_graphs) {
  384. fout << "subgraph attr:" << std::endl;
  385. MS_EXCEPTION_IF_NULL(sg.first);
  386. for (const auto &attr : sg.first->attrs()) {
  387. fout << attr.first << " : ";
  388. if (attr.second->isa<BoolImm>()) {
  389. fout << GetValue<bool>(attr.second);
  390. } else if (attr.second->isa<StringImm>()) {
  391. fout << GetValue<std::string>(attr.second);
  392. }
  393. fout << std::endl;
  394. }
  395. fout << "subgraph @" << sg.first->ToString() << ".";
  396. fout << sg.first->debug_info()->get_id() << "(";
  397. if (sg.first != graph) {
  398. std::vector<AnfNodePtr> parameters = sg.first->parameters();
  399. if (parameters.size() == 1) {
  400. MS_EXCEPTION_IF_NULL(parameters[0]);
  401. fout << "%para_" << parameters[0]->ToString();
  402. } else if (parameters.size() > 1) {
  403. for (size_t idx = 0; idx < parameters.size() - 1; idx++) {
  404. MS_EXCEPTION_IF_NULL(parameters[idx]);
  405. fout << "%para_" << parameters[idx]->ToString();
  406. fout << ", ";
  407. }
  408. MS_EXCEPTION_IF_NULL(parameters[parameters.size() - 1]);
  409. fout << "%para_" << parameters[parameters.size() - 1]->ToString();
  410. }
  411. }
  412. fout << ") {" << std::endl;
  413. MS_EXCEPTION_IF_NULL(sg.second);
  414. fout << sg.second->buffer.str();
  415. fout << "}" << std::endl;
  416. fout << std::endl;
  417. }
  418. }
  419. std::string AddGlobalId(const std::string &filename) {
  420. static size_t g_id = 0;
  421. std::ostringstream s;
  422. auto i = filename.rfind(".ir");
  423. if (i >= filename.size()) {
  424. s << filename;
  425. s << "_" << std::setfill('0') << std::setw(4) << g_id;
  426. } else {
  427. s << filename.substr(0, i);
  428. s << "_" << std::setfill('0') << std::setw(4) << g_id;
  429. if (i + 1 < filename.size()) {
  430. s << filename.substr(i);
  431. }
  432. }
  433. ++g_id;
  434. return s.str();
  435. }
  436. #ifdef ENABLE_DUMP_IR
  437. void DumpIR(const std::string &filename, const FuncGraphPtr &graph, bool dump_full_name) {
  438. if (graph == nullptr) {
  439. return;
  440. }
  441. auto real_filename = pipeline::GetSaveGraphsPathName(AddGlobalId(filename));
  442. if (real_filename.size() > PATH_MAX) {
  443. MS_LOG(ERROR) << "File path " << real_filename << " is too long.";
  444. return;
  445. }
  446. char real_path[PATH_MAX] = {0};
  447. #if defined(_WIN32) || defined(_WIN64)
  448. if (_fullpath(real_path, filename.c_str(), PATH_MAX) == nullptr) {
  449. MS_LOG(DEBUG) << "dir " << filename << " does not exit.";
  450. }
  451. #else
  452. if (nullptr == realpath(real_filename.c_str(), real_path)) {
  453. MS_LOG(DEBUG) << "Dir " << real_filename << " does not exit.";
  454. }
  455. #endif
  456. OrderedMap<AnfNodePtr, int32_t> para_map;
  457. std::string path_string = real_path;
  458. ChangeFileMode(path_string, S_IRWXU);
  459. std::ofstream fout(real_path);
  460. std::ostringstream buffer;
  461. if (!fout.is_open()) {
  462. MS_LOG(ERROR) << "Open dump file '" << real_path << "' failed!";
  463. return;
  464. }
  465. auto nodes = TopoSort(graph->get_return(), SuccDeeperSimple, AlwaysInclude);
  466. // dump global info
  467. DumpGlobalInfoEntry(graph, buffer);
  468. DumpParams(graph, buffer, &para_map);
  469. OrderedMap<FuncGraphPtr, std::shared_ptr<SubGraphIRInfo>> sub_graphs;
  470. // dump ir in each sub graph
  471. DumpIRInSubgraph(nodes, &para_map, &sub_graphs, dump_full_name);
  472. // output global info
  473. fout << buffer.str() << std::endl;
  474. // output each sub graph
  475. DumpSubgraph(&sub_graphs, graph, fout);
  476. fout.close();
  477. // set file mode to read only by user
  478. ChangeFileMode(path_string, S_IRUSR);
  479. }
  480. #else
  481. void DumpIR(const std::string &, const FuncGraphPtr &, bool) {
  482. static bool already_printed = false;
  483. if (already_printed) {
  484. return;
  485. }
  486. already_printed = true;
  487. MS_LOG(WARNING) << "The functionality of dumping function graph IR is disabled, "
  488. << "please recompile source to enable it. See help of building script.";
  489. }
  490. #endif
  491. } // namespace mindspore