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.

dump_proto.cc 22 kB

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