1 /*
2 * Copyright (c) 2018-2021 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24 #include "arm_compute/graph/printers/DotGraphPrinter.h"
25
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/graph/Graph.h"
28 #include "arm_compute/graph/Tensor.h"
29 #include "arm_compute/graph/TypePrinter.h"
30 #include "arm_compute/graph/nodes/Nodes.h"
31
32 namespace arm_compute
33 {
34 namespace graph
35 {
visit(ActivationLayerNode & n)36 void DotGraphVisitor::visit(ActivationLayerNode &n)
37 {
38 std::stringstream ss;
39 ss << n.activation_info().activation();
40 _info = ss.str();
41 }
42
visit(BatchNormalizationLayerNode & n)43 void DotGraphVisitor::visit(BatchNormalizationLayerNode &n)
44 {
45 std::stringstream ss;
46 ss << (n.fused_activation().enabled() ? to_string(n.fused_activation().activation()) : "");
47 _info = ss.str();
48 }
49
visit(ConcatenateLayerNode & n)50 void DotGraphVisitor::visit(ConcatenateLayerNode &n)
51 {
52 std::stringstream ss;
53 ss << "Enabled: " << n.is_enabled();
54 ss << R"( \n )";
55 ss << "Axis: " << n.concatenation_axis();
56 _info = ss.str();
57 }
58
visit(ConvolutionLayerNode & n)59 void DotGraphVisitor::visit(ConvolutionLayerNode &n)
60 {
61 std::stringstream ss;
62 ss << n.convolution_method();
63 _info = ss.str();
64 }
65
visit(DepthwiseConvolutionLayerNode & n)66 void DotGraphVisitor::visit(DepthwiseConvolutionLayerNode &n)
67 {
68 std::stringstream ss;
69 ss << n.depthwise_convolution_method();
70 _info = ss.str();
71 }
72
visit(EltwiseLayerNode & n)73 void DotGraphVisitor::visit(EltwiseLayerNode &n)
74 {
75 std::stringstream ss;
76 ss << n.eltwise_operation();
77 _info = ss.str();
78 }
79
visit(FusedConvolutionBatchNormalizationNode & n)80 void DotGraphVisitor::visit(FusedConvolutionBatchNormalizationNode &n)
81 {
82 ARM_COMPUTE_UNUSED(n);
83 std::stringstream ss;
84 ss << "FusedConvolutionBatchNormalizationNode";
85 _info = ss.str();
86 }
87
visit(FusedConvolutionBatchNormalizationWithPostOpsNode & n)88 void DotGraphVisitor::visit(FusedConvolutionBatchNormalizationWithPostOpsNode &n)
89 {
90 ARM_COMPUTE_UNUSED(n);
91 std::stringstream ss;
92 ss << "FusedConvolutionBatchNormalizationWithPostOpsNode";
93 _info = ss.str();
94 }
95
visit(FusedConvolutionWithPostOpNode & n)96 void DotGraphVisitor::visit(FusedConvolutionWithPostOpNode &n)
97 {
98 ARM_COMPUTE_UNUSED(n);
99 std::stringstream ss;
100 ss << "FusedConvolutionWithPostOpNode";
101 _info = ss.str();
102 }
103
visit(FusedDepthwiseConvolutionBatchNormalizationNode & n)104 void DotGraphVisitor::visit(FusedDepthwiseConvolutionBatchNormalizationNode &n)
105 {
106 ARM_COMPUTE_UNUSED(n);
107 std::stringstream ss;
108 ss << "FusedDepthwiseConvolutionBatchNormalizationNode";
109 _info = ss.str();
110 }
111
visit(NormalizationLayerNode & n)112 void DotGraphVisitor::visit(NormalizationLayerNode &n)
113 {
114 std::stringstream ss;
115 ss << n.normalization_info().type();
116 _info = ss.str();
117 }
118
visit(PoolingLayerNode & n)119 void DotGraphVisitor::visit(PoolingLayerNode &n)
120 {
121 std::stringstream ss;
122 ss << n.pooling_info().pool_type;
123 ss << R"( \n )";
124 ss << n.pooling_info().pool_size;
125 ss << R"( \n )";
126 ss << n.pooling_info().pad_stride_info;
127 _info = ss.str();
128 }
129
default_visit(INode & n)130 void DotGraphVisitor::default_visit(INode &n)
131 {
132 ARM_COMPUTE_UNUSED(n);
133 _info.clear();
134 }
135
info() const136 const std::string &DotGraphVisitor::info() const
137 {
138 return _info;
139 }
140
print(const Graph & g,std::ostream & os)141 void DotGraphPrinter::print(const Graph &g, std::ostream &os)
142 {
143 // Print header
144 print_header(g, os);
145
146 // Print nodes
147 print_nodes(g, os);
148
149 // Print edges
150 print_edges(g, os);
151
152 // Print footer
153 print_footer(g, os);
154 }
155
print_header(const Graph & g,std::ostream & os)156 void DotGraphPrinter::print_header(const Graph &g, std::ostream &os)
157 {
158 // Print graph name
159 std::string graph_name = (g.name().empty()) ? "Graph" : g.name();
160 os << "digraph " << graph_name << "{\n";
161 }
162
print_footer(const Graph & g,std::ostream & os)163 void DotGraphPrinter::print_footer(const Graph &g, std::ostream &os)
164 {
165 ARM_COMPUTE_UNUSED(g);
166 os << "}\n";
167 }
168
print_nodes(const Graph & g,std::ostream & os)169 void DotGraphPrinter::print_nodes(const Graph &g, std::ostream &os)
170 {
171 for(const auto &n : g.nodes())
172 {
173 if(n)
174 {
175 // Output node id
176 std::string node_id = std::string("n") + support::cpp11::to_string(n->id());
177 os << node_id << " ";
178
179 // Output label
180 n->accept(_dot_node_visitor);
181
182 std::string name = n->name().empty() ? node_id : n->name();
183 auto node_description = _dot_node_visitor.info();
184
185 os << R"([label = ")" << name << R"( \n )" << n->assigned_target() << R"( \n )" << node_description << R"("])";
186 os << ";\n";
187 }
188 }
189 }
190
print_edges(const Graph & g,std::ostream & os)191 void DotGraphPrinter::print_edges(const Graph &g, std::ostream &os)
192 {
193 for(const auto &e : g.edges())
194 {
195 if(e)
196 {
197 std::string source_node_id = std::string("n") + support::cpp11::to_string(e->producer_id());
198 std::string sink_node_id = std::string("n") + support::cpp11::to_string(e->consumer_id());
199 os << source_node_id << " -> " << sink_node_id << " ";
200 const Tensor *t = e->tensor();
201 ARM_COMPUTE_ERROR_ON(t == nullptr);
202 os << R"([label = ")" << t->desc().shape << R"( \n )" << t->desc().data_type << R"( \n )" << t->desc().layout << R"("])";
203 os << ";\n";
204 }
205 }
206 }
207 } // namespace graph
208 } // namespace arm_compute
209