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

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