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.

proto_exporter.cc 23 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /**
  2. * Copyright 2020 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/debugger/proto_exporter.h"
  17. #include <fstream>
  18. #include <map>
  19. #include <memory>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22. #include <utility>
  23. #include <algorithm>
  24. #include "debug/common.h"
  25. #include "debug/debugger/debugger.h"
  26. #include "debug/data_dump/dump_json_parser.h"
  27. #include "proto/debug_graph.pb.h"
  28. #include "ir/graph_utils.h"
  29. #include "utils/symbolic.h"
  30. #include "utils/trace_base.h"
  31. namespace mindspore {
  32. void CheckIfValidType(const TypePtr &type, debugger::TypeProto *type_proto) {
  33. if (!(type->isa<Number>() || type->isa<TensorType>() || type->isa<Tuple>() || type->isa<TypeType>() ||
  34. type->isa<List>() || type->isa<TypeAnything>() || type->isa<RefKeyType>() || type->isa<RefType>() ||
  35. type->isa<Function>() || type->isa<TypeNone>() || type->isa<String>() || type->isa<SymbolicKeyType>() ||
  36. type->isa<UMonadType>() || type->isa<IOMonadType>())) {
  37. MS_LOG(EXCEPTION) << "Unknown type: " << type->type_name();
  38. }
  39. if (type->isa<Number>()) {
  40. type_proto->set_data_type(GetDebuggerNumberDataType(type));
  41. }
  42. }
  43. void DebuggerProtoExporter::SetNodeOutputType(const TypePtr &type, const BaseShapePtr &shape,
  44. debugger::TypeProto *type_proto) {
  45. if (type_proto == nullptr) {
  46. return;
  47. }
  48. if (type == nullptr) {
  49. type_proto->set_data_type(debugger::DT_UNDEFINED);
  50. return;
  51. }
  52. CheckIfValidType(type, type_proto);
  53. if (type->isa<TensorType>()) {
  54. TypePtr elem_type = dyn_cast<TensorType>(type)->element();
  55. type_proto->mutable_tensor_type()->set_elem_type(GetDebuggerNumberDataType(elem_type));
  56. type_proto->set_data_type(debugger::DT_TENSOR);
  57. if (shape != nullptr && shape->isa<abstract::Shape>()) {
  58. abstract::ShapePtr shape_info = dyn_cast<abstract::Shape>(shape);
  59. for (const auto &elem : shape_info->shape()) {
  60. type_proto->mutable_tensor_type()->mutable_shape()->add_dim()->set_size(elem);
  61. }
  62. }
  63. } else if (type->isa<Tuple>()) {
  64. TuplePtr tuple_type = dyn_cast<Tuple>(type);
  65. type_proto->set_data_type(debugger::DT_TUPLE);
  66. for (const auto &elem_type : tuple_type->elements()) {
  67. SetNodeOutputType(elem_type, nullptr, type_proto->mutable_sequence_type()->add_elem_types());
  68. }
  69. } else if (type->isa<TypeType>()) {
  70. type_proto->set_data_type(debugger::DT_TYPE);
  71. } else if (type->isa<List>()) {
  72. ListPtr list_type = dyn_cast<List>(type);
  73. type_proto->set_data_type(debugger::DT_LIST);
  74. for (const auto &elem_type : list_type->elements()) {
  75. SetNodeOutputType(elem_type, nullptr, type_proto->mutable_sequence_type()->add_elem_types());
  76. }
  77. } else if (type->isa<TypeAnything>()) {
  78. type_proto->set_data_type(debugger::DT_ANYTHING);
  79. } else if (type->isa<RefKeyType>()) {
  80. type_proto->set_data_type(debugger::DT_REFKEY);
  81. } else if (type->isa<RefType>()) {
  82. type_proto->set_data_type(debugger::DT_REF);
  83. } else if (type->isa<Function>()) {
  84. type_proto->set_data_type(debugger::DT_GRAPH);
  85. } else if (type->isa<TypeNone>()) {
  86. type_proto->set_data_type(debugger::DT_NONE);
  87. } else if (type->isa<String>()) {
  88. type_proto->set_data_type(debugger::DT_STRING);
  89. } else if (type->isa<UMonadType>()) {
  90. type_proto->set_data_type(debugger::DT_UMONAD);
  91. } else if (type->isa<IOMonadType>()) {
  92. type_proto->set_data_type(debugger::DT_IOMONAD);
  93. }
  94. }
  95. void DebuggerProtoExporter::SetNodeOutputType(const AnfNodePtr &node, debugger::TypeProto *type_proto) {
  96. if (node == nullptr || type_proto == nullptr) {
  97. return;
  98. }
  99. SetNodeOutputType(node->Type(), node->Shape(), type_proto);
  100. }
  101. void DebuggerProtoExporter::SetValueToProto(const ValuePtr &val, debugger::ValueProto *value_proto) {
  102. if (val == nullptr || value_proto == nullptr) {
  103. return;
  104. }
  105. if (val->isa<StringImm>()) {
  106. const StringImmPtr &value = dyn_cast<StringImm>(val);
  107. value_proto->set_dtype(debugger::DT_STRING);
  108. value_proto->set_str_val(value->value());
  109. } else if (val->isa<Scalar>()) {
  110. SetScalarToProto(dyn_cast<Scalar>(val), value_proto);
  111. } else if (val->isa<Bool>()) {
  112. value_proto->set_dtype(debugger::DT_TYPE);
  113. value_proto->mutable_type_val()->set_data_type(debugger::DT_BOOL);
  114. } else if (val->isa<Int>()) {
  115. value_proto->set_dtype(debugger::DT_TYPE);
  116. value_proto->mutable_type_val()->set_data_type(debugger::DT_BASE_INT);
  117. } else if (val->isa<Float>()) {
  118. value_proto->set_dtype(debugger::DT_TYPE);
  119. value_proto->mutable_type_val()->set_data_type(debugger::DT_BASE_FLOAT);
  120. } else if (val->isa<ValueSequeue>()) {
  121. SetSequenceToProto(dyn_cast<ValueSequeue>(val), value_proto);
  122. } else if (val->isa<None>()) {
  123. value_proto->set_dtype(debugger::DT_NONE);
  124. value_proto->set_str_val("None");
  125. } else if (val->isa<SymbolicKeyInstance>()) {
  126. SymbolicKeyInstancePtr sym_inst = dyn_cast<SymbolicKeyInstance>(val);
  127. ParameterPtr sym_node = dyn_cast<Parameter>(sym_inst->node());
  128. value_proto->set_dtype(debugger::DT_SYM_INST);
  129. value_proto->set_str_val(sym_node == nullptr ? std::string("nullptr") : sym_node->ToString());
  130. } else if (val->isa<ValueDictionary>()) {
  131. SetDictionaryToProto(dyn_cast<ValueDictionary>(val), value_proto);
  132. } else if (val->isa<tensor::Tensor>()) {
  133. tensor::TensorPtr tensor_ptr = dyn_cast<tensor::Tensor>(val);
  134. value_proto->set_dtype(debugger::DT_TENSOR);
  135. debugger::TensorProto *tensor_proto = value_proto->mutable_tensor_val();
  136. tensor_proto->set_data_type(GetDebuggerNumberDataType(tensor_ptr->Dtype()));
  137. for (auto &elem : tensor_ptr->shape()) {
  138. tensor_proto->add_dims(elem);
  139. }
  140. tensor_proto->set_tensor_content(tensor_ptr->data_c(), tensor_ptr->data().nbytes());
  141. } else if (val->isa<TensorType>()) {
  142. value_proto->set_dtype(debugger::DT_TYPE);
  143. debugger::TypeProto *type_proto = value_proto->mutable_type_val();
  144. type_proto->set_data_type(debugger::DT_TENSOR);
  145. TypePtr elem_type = dyn_cast<TensorType>(val)->element();
  146. type_proto->mutable_tensor_type()->set_elem_type(GetDebuggerNumberDataType(elem_type));
  147. } else {
  148. MS_LOG(WARNING) << "Unsupported type " << val->type_name();
  149. }
  150. }
  151. void DebuggerProtoExporter::SetScalarToProto(const ScalarPtr &val, debugger::ValueProto *value_proto) {
  152. if (val == nullptr || value_proto == nullptr) {
  153. return;
  154. }
  155. if (val->isa<BoolImm>()) {
  156. const BoolImmPtr &value = dyn_cast<BoolImm>(val);
  157. value_proto->set_dtype(debugger::DT_BOOL);
  158. value_proto->set_bool_val(value->value());
  159. } else if (val->isa<Int8Imm>()) {
  160. const Int8ImmPtr &value = dyn_cast<Int8Imm>(val);
  161. value_proto->set_dtype(debugger::DT_INT8);
  162. value_proto->set_int_val(value->value());
  163. } else if (val->isa<Int16Imm>()) {
  164. const Int16ImmPtr &value = dyn_cast<Int16Imm>(val);
  165. value_proto->set_dtype(debugger::DT_INT16);
  166. value_proto->set_int_val(value->value());
  167. } else if (val->isa<Int32Imm>()) {
  168. const Int32ImmPtr &value = dyn_cast<Int32Imm>(val);
  169. value_proto->set_dtype(debugger::DT_INT32);
  170. value_proto->set_int_val(value->value());
  171. } else if (val->isa<Int64Imm>()) {
  172. const Int64ImmPtr &value = dyn_cast<Int64Imm>(val);
  173. value_proto->set_dtype(debugger::DT_INT64);
  174. value_proto->set_int_val(value->value());
  175. } else if (val->isa<UInt8Imm>()) {
  176. const UInt8ImmPtr &value = dyn_cast<UInt8Imm>(val);
  177. value_proto->set_dtype(debugger::DT_UINT8);
  178. value_proto->set_uint_val(value->value());
  179. } else if (val->isa<UInt16Imm>()) {
  180. const UInt16ImmPtr &value = dyn_cast<UInt16Imm>(val);
  181. value_proto->set_dtype(debugger::DT_UINT16);
  182. value_proto->set_uint_val(value->value());
  183. } else if (val->isa<UInt32Imm>()) {
  184. const UInt32ImmPtr &value = dyn_cast<UInt32Imm>(val);
  185. value_proto->set_dtype(debugger::DT_UINT32);
  186. value_proto->set_uint_val(value->value());
  187. } else if (val->isa<UInt64Imm>()) {
  188. const UInt64ImmPtr &value = dyn_cast<UInt64Imm>(val);
  189. value_proto->set_dtype(debugger::DT_UINT64);
  190. value_proto->set_uint_val(value->value());
  191. } else if (val->isa<FP32Imm>()) {
  192. const FP32ImmPtr &value = dyn_cast<FP32Imm>(val);
  193. value_proto->set_dtype(debugger::DT_FLOAT32);
  194. value_proto->set_float_val(value->value());
  195. } else if (val->isa<FP64Imm>()) {
  196. const FP64ImmPtr &value = dyn_cast<FP64Imm>(val);
  197. value_proto->set_dtype(debugger::DT_FLOAT64);
  198. value_proto->set_double_val(value->value());
  199. } else {
  200. MS_LOG(EXCEPTION) << "Unknown scalar type " << val->ToString();
  201. }
  202. }
  203. void DebuggerProtoExporter::SetSequenceToProto(const ValueSequeuePtr &val, debugger::ValueProto *value_proto) {
  204. if (val == nullptr || value_proto == nullptr) {
  205. return;
  206. }
  207. if (val->isa<ValueTuple>()) {
  208. const ValueTuplePtr &value = dyn_cast<ValueTuple>(val);
  209. value_proto->set_dtype(debugger::DT_TUPLE);
  210. for (const auto &item : value->value()) {
  211. SetValueToProto(item, value_proto->add_values());
  212. }
  213. } else if (val->isa<ValueList>()) {
  214. const ValueListPtr &value = dyn_cast<ValueList>(val);
  215. value_proto->set_dtype(debugger::DT_LIST);
  216. for (const auto &item : value->value()) {
  217. SetValueToProto(item, value_proto->add_values());
  218. }
  219. }
  220. }
  221. void DebuggerProtoExporter::SetDictionaryToProto(const ValueDictionaryPtr &val, debugger::ValueProto *value_proto) {
  222. if (val == nullptr || value_proto == nullptr) {
  223. return;
  224. }
  225. value_proto->set_dtype(debugger::DT_DICT);
  226. for (const auto &item : val->value()) {
  227. debugger::NamedValueProto *named_val = value_proto->add_dict_val();
  228. named_val->set_key(item.first);
  229. SetValueToProto(item.second, named_val->mutable_value());
  230. }
  231. }
  232. void DebuggerProtoExporter::GetOpNodeTypeAndAttrs(const FuncGraphPtr &, const AnfNodePtr &node,
  233. debugger::NodeProto *node_proto) {
  234. if (node == nullptr || node_proto == nullptr) {
  235. return;
  236. }
  237. if (node->isa<CNode>() || node->isa<Parameter>() || IsValueNode<FuncGraph>(node)) {
  238. MS_LOG(EXCEPTION) << "Op node can not be CNode, Parameter or ValueNode Graph. But got " << node->ToString();
  239. }
  240. if (!IsValueNode<Primitive>(node)) {
  241. MS_LOG(EXCEPTION) << "Op node is not primitive: " << node->ToString();
  242. }
  243. const PrimitivePtr &prim = GetValueNode<PrimitivePtr>(node);
  244. node_proto->set_op_type(prim->name());
  245. for (const auto &attr : prim->attrs()) {
  246. debugger::AttributeProto *attr_proto = node_proto->add_attribute();
  247. attr_proto->set_name(attr.first);
  248. SetValueToProto(attr.second, attr_proto->mutable_value());
  249. }
  250. node_proto->set_scope(node->scope()->name());
  251. }
  252. std::string DebuggerProtoExporter::GetOpNodeInputId(const FuncGraphPtr &, const AnfNodePtr &node,
  253. const std::map<AnfNodePtr, size_t> &apply_map,
  254. std::map<AnfNodePtr, size_t> *const_map_ptr) {
  255. if (node == nullptr || const_map_ptr == nullptr) {
  256. return "";
  257. }
  258. if (node->isa<CNode>()) {
  259. auto iter = apply_map.find(node);
  260. if (iter == apply_map.end()) {
  261. MS_LOG(EXCEPTION) << "Can not find node '" << node->ToString() << "' in apply_map";
  262. }
  263. return std::to_string(iter->second);
  264. }
  265. if (node->isa<Parameter>()) {
  266. return node->ToString();
  267. }
  268. if (node->isa<ValueNode>()) {
  269. auto iter = const_map_ptr->find(node);
  270. if (iter == const_map_ptr->end()) {
  271. // Start index number from 1
  272. auto const_idx = const_map_ptr->size() + 1;
  273. (*const_map_ptr)[node] = const_idx;
  274. }
  275. return GetConstNodeId((*const_map_ptr)[node]);
  276. }
  277. MS_LOG(EXCEPTION) << "Unknown node type. node is '" << node->ToString() << "'";
  278. }
  279. std::string DebuggerProtoExporter::GetFuncGraphProtoString(const FuncGraphPtr &func_graph,
  280. LocDebugDumpMode dump_location) {
  281. if (func_graph == nullptr) {
  282. return "";
  283. }
  284. InitModelInfo();
  285. debugger::GraphProto *graph_proto = model_.mutable_graph();
  286. ExportFuncGraph(func_graph, graph_proto, dump_location);
  287. return model_.SerializeAsString();
  288. }
  289. debugger::ModelProto DebuggerProtoExporter::GetFuncGraphProto(const FuncGraphPtr &func_graph) {
  290. if (func_graph == nullptr) {
  291. return ModelProto();
  292. }
  293. InitModelInfo();
  294. debugger::GraphProto *graph_proto = model_.mutable_graph();
  295. ExportFuncGraph(func_graph, graph_proto);
  296. return model_;
  297. }
  298. void DebuggerProtoExporter::ExportFuncGraph(const FuncGraphPtr &func_graph, debugger::GraphProto *graph_proto,
  299. LocDebugDumpMode dump_location) {
  300. if (func_graph == nullptr || graph_proto == nullptr) {
  301. return;
  302. }
  303. // map for store ValueNodes of this graph
  304. std::map<AnfNodePtr, size_t> const_map;
  305. // set graph name
  306. graph_proto->set_name(func_graph->ToString());
  307. MS_LOG(INFO) << "graph names: " << func_graph->ToString();
  308. ExportParameters(func_graph, graph_proto);
  309. ExportCNodes(func_graph, graph_proto, &const_map, dump_location);
  310. ExportValueNodes(const_map, graph_proto);
  311. }
  312. void DebuggerProtoExporter::ExportParameters(const FuncGraphPtr &func_graph, debugger::GraphProto *graph_proto) {
  313. if (func_graph == nullptr || graph_proto == nullptr) {
  314. return;
  315. }
  316. // cast FuncGraph to KernelGraph to access inputs()
  317. std::vector<AnfNodePtr> parameters = static_cast<session::KernelGraph *>(func_graph.get())->inputs();
  318. for (auto &param : parameters) {
  319. debugger::ParameterProto *param_proto = graph_proto->add_parameters();
  320. param_proto->set_name(param->ToString());
  321. SetNodeOutputType(param, param_proto->mutable_type());
  322. const ParameterPtr param_ptr = dyn_cast<Parameter>(param);
  323. if (param_ptr == nullptr) {
  324. MS_LOG(EXCEPTION) << "Parameter '" << param->ToString() << "' could not cast to parameter.";
  325. }
  326. }
  327. }
  328. void DebuggerProtoExporter::ExportCNodes(const FuncGraphPtr &func_graph, debugger::GraphProto *graph_proto,
  329. std::map<AnfNodePtr, size_t> *const_map_ptr, LocDebugDumpMode dump_location) {
  330. if (func_graph == nullptr || graph_proto == nullptr || const_map_ptr == nullptr) {
  331. return;
  332. }
  333. // topo sort nodes
  334. std::vector<AnfNodePtr> nodes = TopoSort(func_graph->get_return(), SuccIncoming, AlwaysInclude);
  335. std::map<AnfNodePtr, size_t> apply_map;
  336. for (const AnfNodePtr &node : nodes) {
  337. MS_EXCEPTION_IF_NULL(node);
  338. if (!node->isa<CNode>()) {
  339. continue;
  340. }
  341. auto cnode = node->cast<CNodePtr>();
  342. if (cnode != func_graph->get_return()) {
  343. ExportCNode(func_graph, cnode, &apply_map, const_map_ptr, graph_proto, dump_location);
  344. } else {
  345. ExportFuncGraphOutput(func_graph, cnode, apply_map, const_map_ptr, graph_proto);
  346. }
  347. }
  348. }
  349. void DebuggerProtoExporter::ExportCNode(const FuncGraphPtr &func_graph, const CNodePtr &node,
  350. std::map<AnfNodePtr, size_t> *apply_map_ptr,
  351. std::map<AnfNodePtr, size_t> *const_map_ptr, debugger::GraphProto *graph_proto,
  352. LocDebugDumpMode dump_location) {
  353. if (func_graph == nullptr || node == nullptr || apply_map_ptr == nullptr || const_map_ptr == nullptr ||
  354. graph_proto == nullptr) {
  355. return;
  356. }
  357. auto apply_idx = apply_map_ptr->size() + 1;
  358. (*apply_map_ptr)[node] = apply_idx;
  359. auto &inputs = node->inputs();
  360. if (inputs.size() < 1) {
  361. MS_LOG(EXCEPTION) << "Inputs of apply node is empty";
  362. }
  363. AnfNodePtr op = inputs[0];
  364. debugger::NodeProto *node_proto = graph_proto->add_node();
  365. // CNode/ConstGraph/Const/Parameter
  366. if (op->isa<CNode>() || IsValueNode<FuncGraph>(op) || op->isa<Parameter>()) {
  367. MS_LOG(WARNING) << "Operator must be a primitive";
  368. } else {
  369. GetOpNodeTypeAndAttrs(func_graph, op, node_proto);
  370. node_proto->set_name(std::to_string(apply_idx));
  371. node_proto->set_scope(node->scope()->name());
  372. // add full_name for debugger
  373. node_proto->set_full_name(node->fullname_with_scope());
  374. MS_LOG(INFO) << "full_name: " << node->fullname_with_scope();
  375. if (dump_location == kDebugWholeStack) {
  376. std::ostringstream buffer;
  377. auto traces = mindspore::trace::GetSourceLineList(node);
  378. for (auto &trace : traces) {
  379. buffer << " # " << trace;
  380. }
  381. node_proto->set_source_address(buffer.str());
  382. }
  383. // process OP inputs
  384. for (size_t i = 1; i < inputs.size(); ++i) {
  385. debugger::InputProto *input_proto = node_proto->add_input();
  386. input_proto->set_type(debugger::InputProto_EdgeType_DATA_EDGE);
  387. std::string id = GetOpNodeInputId(func_graph, inputs[i], *apply_map_ptr, const_map_ptr);
  388. input_proto->set_name(id);
  389. }
  390. // set node output type
  391. SetNodeOutputType(node, node_proto->mutable_output_type());
  392. }
  393. }
  394. void DebuggerProtoExporter::ExportFuncGraphOutput(const FuncGraphPtr &func_graph, const CNodePtr &ret_node,
  395. const std::map<AnfNodePtr, size_t> &apply_map,
  396. std::map<AnfNodePtr, size_t> *const_map_ptr,
  397. debugger::GraphProto *graph_proto) {
  398. if (ret_node == nullptr || !ret_node->isa<CNode>()) {
  399. MS_LOG(EXCEPTION) << "Graph return node is illegal";
  400. }
  401. AnfNodePtr arg = ret_node->input(1);
  402. if (graph_proto == nullptr) {
  403. MS_LOG(EXCEPTION) << "graph_proto is nullptr";
  404. }
  405. debugger::OutputProto *output_proto = graph_proto->add_outputs();
  406. if (output_proto == nullptr) {
  407. MS_LOG(EXCEPTION) << "output_proto is nullptr";
  408. }
  409. std::string id = GetOpNodeInputId(func_graph, arg, apply_map, const_map_ptr);
  410. output_proto->set_name(id);
  411. SetNodeOutputType(arg, output_proto->mutable_type());
  412. }
  413. static bool CompareValue(const std::pair<AnfNodePtr, size_t> &x, const std::pair<AnfNodePtr, size_t> &y) {
  414. return x.second < y.second;
  415. }
  416. void DebuggerProtoExporter::ExportValueNodes(const std::map<AnfNodePtr, size_t> &const_map,
  417. debugger::GraphProto *graph_proto) {
  418. std::vector<std::pair<AnfNodePtr, size_t>> nodes;
  419. (void)std::transform(const_map.cbegin(), const_map.cend(), std::back_inserter(nodes),
  420. [](const std::pair<AnfNodePtr, size_t> &item) { return item; });
  421. sort(nodes.begin(), nodes.end(), CompareValue);
  422. for (auto &item : nodes) {
  423. if (graph_proto == nullptr) {
  424. MS_LOG(EXCEPTION) << "graph_proto is nullptr";
  425. }
  426. debugger::NamedValueProto *named_value = graph_proto->add_const_vals();
  427. MS_EXCEPTION_IF_NULL(named_value);
  428. named_value->set_key(GetConstNodeId(item.second));
  429. SetValueToProto(GetValueNode(item.first), named_value->mutable_value());
  430. }
  431. }
  432. void DebuggerProtoExporter::InitModelInfo() { model_.set_ir_version(debugger::IR_VERSION); }
  433. std::string GetDebuggerFuncGraphProtoString(const FuncGraphPtr &func_graph) {
  434. DebuggerProtoExporter exporter;
  435. return exporter.GetFuncGraphProtoString(func_graph);
  436. }
  437. debugger::ModelProto GetDebuggerFuncGraphProto(const FuncGraphPtr &func_graph) {
  438. DebuggerProtoExporter exporter;
  439. return exporter.GetFuncGraphProto(func_graph);
  440. }
  441. debugger::DataType GetDebuggerNumberDataType(const TypePtr &type) {
  442. switch (type->type_id()) {
  443. case kNumberTypeBool:
  444. return debugger::DT_BOOL;
  445. case kNumberTypeInt8:
  446. return debugger::DT_INT8;
  447. case kNumberTypeInt16:
  448. return debugger::DT_INT16;
  449. case kNumberTypeInt32:
  450. return debugger::DT_INT32;
  451. case kNumberTypeInt64:
  452. return debugger::DT_INT64;
  453. case kNumberTypeUInt8:
  454. return debugger::DT_UINT8;
  455. case kNumberTypeUInt16:
  456. return debugger::DT_UINT16;
  457. case kNumberTypeUInt32:
  458. return debugger::DT_UINT32;
  459. case kNumberTypeUInt64:
  460. return debugger::DT_UINT64;
  461. case kNumberTypeFloat16:
  462. return debugger::DT_FLOAT16;
  463. case kNumberTypeFloat32:
  464. return debugger::DT_FLOAT32;
  465. case kNumberTypeFloat64:
  466. return debugger::DT_FLOAT64;
  467. case kNumberTypeInt:
  468. return debugger::DT_BASE_INT;
  469. case kNumberTypeUInt:
  470. return debugger::DT_BASE_UINT;
  471. case kNumberTypeFloat:
  472. return debugger::DT_BASE_FLOAT;
  473. default:
  474. MS_LOG(EXCEPTION) << "Unexpected type " << type->type_name();
  475. }
  476. }
  477. #ifdef ENABLE_DUMP_IR
  478. void DumpIRProtoWithSrcInfo(const FuncGraphPtr &func_graph, const std::string &suffix, const std::string &target_dir,
  479. LocDebugDumpMode dump_location) {
  480. DebuggerProtoExporter exporter;
  481. std::string graph_proto = exporter.GetFuncGraphProtoString(func_graph, dump_location);
  482. if (func_graph == nullptr) {
  483. MS_LOG(ERROR) << "Func graph is nullptr";
  484. return;
  485. }
  486. std::string file_path = target_dir + "/" + "ms_output_" + suffix + ".pb";
  487. bool status = Common::CreateNotExistDirs(target_dir);
  488. if (!status) {
  489. MS_LOG(ERROR) << "Failed at CreateNotExistDirs in dump_proto";
  490. return;
  491. }
  492. if (file_path.size() > PATH_MAX) {
  493. MS_LOG(ERROR) << "File path " << file_path << " is too long.";
  494. return;
  495. }
  496. char real_path[PATH_MAX] = {0};
  497. char *real_path_ret = nullptr;
  498. #if defined(_WIN32) || defined(_WIN64)
  499. real_path_ret = _fullpath(real_path, file_path.c_str(), PATH_MAX);
  500. #else
  501. real_path_ret = realpath(file_path.c_str(), real_path);
  502. #endif
  503. if (nullptr == real_path_ret) {
  504. MS_LOG(DEBUG) << "dir " << file_path << " does not exit.";
  505. } else {
  506. std::string path_string = real_path;
  507. if (chmod(common::SafeCStr(path_string), S_IRUSR | S_IWUSR) == -1) {
  508. MS_LOG(ERROR) << "Modify file:" << real_path << " to rw fail.";
  509. return;
  510. }
  511. }
  512. // write to pb file
  513. std::ofstream ofs(real_path);
  514. if (!ofs.is_open()) {
  515. MS_LOG(ERROR) << "Open file '" << real_path << "' failed!";
  516. return;
  517. }
  518. ofs << graph_proto;
  519. ofs.close();
  520. // set file mode to read only by user
  521. ChangeFileMode(file_path, S_IRUSR);
  522. }
  523. #else
  524. void DumpIRProtoWithSrcInfo(const FuncGraphPtr &, const std::string &, const std::string &) {
  525. static bool already_printed = false;
  526. if (already_printed) {
  527. return;
  528. }
  529. already_printed = true;
  530. MS_LOG(WARNING) << "The functionality of dumping function graph IR in protobuf format is disabled,"
  531. << "because ENABLE_DEBUGGER option is off"
  532. << "please recompile source to enable it. See help of building script.";
  533. }
  534. #endif
  535. } // namespace mindspore