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

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