• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 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 #ifndef MINDSPORE_PROTO_EXPORTER_H
17 #define MINDSPORE_PROTO_EXPORTER_H
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 #include "debug/common.h"
24 #include "debug/data_dump/dump_json_parser.h"
25 #include "ir/graph_utils.h"
26 #include "proto/debug_graph.pb.h"
27 #include "utils/symbolic.h"
28 #include "utils/trace_base.h"
29 
30 namespace mindspore {
31 enum LocDebugDumpMode { kDebugOff = 0, kDebugTopStack = 1, kDebugWholeStack = 2 };
32 class DebuggerProtoExporter {
33  public:
DebuggerProtoExporter()34   DebuggerProtoExporter() {}
~DebuggerProtoExporter()35   ~DebuggerProtoExporter() {}
36 
37   std::string GetFuncGraphProtoString(const FuncGraphPtr &func_graph,
38                                       LocDebugDumpMode dump_location = kDebugWholeStack);
39   debugger::ModelProto GetFuncGraphProto(const FuncGraphPtr &func_graph);
40 
41  private:
42   void InitModelInfo();
43   void GetOpNodeTypeAndAttrs(const FuncGraphPtr &func_graph, const AnfNodePtr &node, debugger::NodeProto *node_proto);
44   std::string GetOpNodeInputId(const FuncGraphPtr &func_graph, const AnfNodePtr &node,
45                                const std::map<AnfNodePtr, size_t> &apply_map,
46                                std::map<AnfNodePtr, size_t> *const_map_ptr);
47   void SetValueToProto(const ValuePtr &attr_value, debugger::ValueProto *value_proto);
48   void SetScalarToProto(const ScalarPtr &val, debugger::ValueProto *value_proto);
49   void SetSequenceToProto(const ValueSequeuePtr &val, debugger::ValueProto *value_proto);
50   void SetDictionaryToProto(const ValueDictionaryPtr &val, debugger::ValueProto *value_proto);
51   void SetNodeOutputType(const AnfNodePtr &node, debugger::TypeProto *type_proto);
52   void ExportFuncGraph(const FuncGraphPtr &func_graph, debugger::GraphProto *const graph_proto,
53                        LocDebugDumpMode dump_location = kDebugWholeStack);
54   void ExportParameters(const FuncGraphPtr &func_graph, debugger::GraphProto *graph_proto);
55   void ExportCNodes(const FuncGraphPtr &func_graph, debugger::GraphProto *const graph_proto,
56                     std::map<AnfNodePtr, size_t> *const_map_ptr, LocDebugDumpMode dump_location = kDebugWholeStack);
57   void ExportCNode(const FuncGraphPtr &func_graph, const CNodePtr &node, std::map<AnfNodePtr, size_t> *apply_map_ptr,
58                    std::map<AnfNodePtr, size_t> *const_map_ptr, debugger::GraphProto *const graph_proto,
59                    LocDebugDumpMode dump_location);
60   void ExportFuncGraphOutput(const FuncGraphPtr &func_graph, const CNodePtr &ret_node,
61                              const std::map<AnfNodePtr, size_t> &apply_map, std::map<AnfNodePtr, size_t> *const_map_ptr,
62                              debugger::GraphProto *graph_proto);
63   void ExportValueNodes(const std::map<AnfNodePtr, size_t> &const_map, debugger::GraphProto *graph_proto);
64 
GetConstNodeId(size_t idx)65   static std::string GetConstNodeId(size_t idx) { return std::string("cst") + std::to_string(idx); }
66 
67   debugger::ModelProto model_;
68 };
69 void DumpIRProtoWithSrcInfo(const FuncGraphPtr &func_graph, const std::string &suffix, const std::string &target_dir,
70                             LocDebugDumpMode dump_location = kDebugWholeStack);
71 }  // namespace mindspore
72 #endif  // MINDSPORE_CCSRC_DEBUG_DEBUGGER_MINDSPORE_PROTO_EXPORTER_H_
73