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

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