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