Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / dumper / text / GraphDumper.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
17 #include "GraphDumper.h"
18
19 #include "ir/Graph.h"
20 #include "compiler/LoweredGraph.h"
21 #ifdef ONERT_TRAIN
22 #include "compiler/train/LoweredTrainableGraph.h"
23 #endif // ONERT_TRAIN
24 #include "util/logging.h"
25 #include "misc/string_helpers.h"
26
27 namespace onert
28 {
29 namespace dumper
30 {
31 namespace text
32 {
33
34 namespace
35 {
36
37 std::string formatOperandIndexSequence(const ir::OperandIndexSequence &seq)
38 {
39   std::vector<std::string> strs;
40   for (auto &&ind : seq)
41     strs.push_back(dumper::text::formatOperandBrief(ind));
42   return nnfw::misc::join(strs.begin(), strs.end(), ", ");
43 }
44
45 } // namespace
46
47 std::string formatOperandBrief(ir::OperandIndex ind)
48 {
49   std::stringstream ss;
50   ss << ind;
51   return ss.str();
52 }
53
54 std::string formatOperand(const ir::Graph &, ir::OperandIndex ind)
55 {
56   std::stringstream ss;
57   ss << ind;
58   // TODO Print shape, type and maybe more
59   return ss.str();
60 }
61
62 std::string formatOperation(const ir::IOperation &op, ir::OperationIndex ind)
63 {
64   std::stringstream ss;
65
66   ss << formatOperandIndexSequence(op.getOutputs());
67   ss << " = ";
68   ss << ind << "_" << op.name() << "(";
69   ss << formatOperandIndexSequence(op.getInputs());
70   ss << ")";
71   return ss.str();
72 }
73
74 std::string formatOperation(const ir::Graph &graph, ir::OperationIndex ind)
75 {
76   std::stringstream ss;
77   const auto &op = graph.operations().at(ind);
78   return formatOperation(op, ind);
79 }
80
81 void dumpGraph(const ir::Graph &graph)
82 {
83   VERBOSE(GraphDumper) << "{\n";
84   auto ops_topol = graph.topolSortOperations();
85   for (auto &&op_ind : ops_topol)
86   {
87     const auto &op = graph.operations().at(op_ind);
88     VERBOSE(GraphDumper) << "  " << formatOperation(op, op_ind) << "\n";
89   }
90   VERBOSE(GraphDumper) << "}\n";
91   VERBOSE(GraphDumper) << std::endl;
92 }
93
94 void dumpLoweredGraph(const compiler::LoweredGraph &lgraph)
95 {
96   // TODO Graph dump with backend info
97   dumpGraph(lgraph.graph());
98 }
99
100 #ifdef ONERT_TRAIN
101 void dumpLoweredGraph(const compiler::train::LoweredTrainableGraph &lgraph)
102 {
103   // TODO Graph dump with backend info
104   dumpGraph(lgraph.graph());
105 }
106 #endif // ONERT_TRAIN
107
108 } // namespace text
109 } // namespace dumper
110 } // namespace onert