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

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