blob: 04987eebe0aa8eda158e45594fee05b8b5824c76 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
2 * Copyright (c) 2018 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/graph2/printers/DotGraphPrinter.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/graph2/Graph.h"
28#include "arm_compute/graph2/Tensor.h"
29#include "arm_compute/graph2/TypePrinter.h"
30#include "arm_compute/graph2/nodes/Nodes.h"
31
32namespace arm_compute
33{
34namespace graph2
35{
36void DotGraphVisitor::visit(ActivationLayerNode &n)
37{
38 std::stringstream ss;
39 ss << n.activation_info().activation();
40 _info = ss.str();
41}
42
43void 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
50void DotGraphVisitor::visit(ConvolutionLayerNode &n)
51{
52 std::stringstream ss;
53 ss << n.convolution_method();
54 _info = ss.str();
55}
56
57void DotGraphVisitor::visit(DepthConcatenateLayerNode &n)
58{
59 std::stringstream ss;
60 ss << "Enabled: " << n.is_enabled();
61 _info = ss.str();
62}
63
64void DotGraphVisitor::visit(DepthwiseConvolutionLayerNode &n)
65{
66 std::stringstream ss;
67 ss << n.depthwise_convolution_method();
68 _info = ss.str();
69}
70
71void DotGraphVisitor::visit(EltwiseLayerNode &n)
72{
73 std::stringstream ss;
74 ss << n.eltwise_operation();
75 _info = ss.str();
76}
77
78void DotGraphVisitor::visit(NormalizationLayerNode &n)
79{
80 std::stringstream ss;
81 ss << n.normalization_info().type();
82 _info = ss.str();
83}
84
85void DotGraphVisitor::visit(PoolingLayerNode &n)
86{
87 std::stringstream ss;
88 ss << n.pooling_info().pool_type();
89 ss << R"( \n )";
90 ss << n.pooling_info().pool_size();
91 ss << R"( \n )";
92 ss << n.pooling_info().pad_stride_info();
93 _info = ss.str();
94}
95
96void DotGraphVisitor::default_visit()
97{
98 _info.clear();
99}
100
101const std::string &DotGraphVisitor::info() const
102{
103 return _info;
104}
105
106void DotGraphPrinter::print(const Graph &g, std::ostream &os)
107{
108 // Print header
109 print_header(g, os);
110
111 // Print nodes
112 print_nodes(g, os);
113
114 // Print edges
115 print_edges(g, os);
116
117 // Print footer
118 print_footer(g, os);
119}
120
121void DotGraphPrinter::print_header(const Graph &g, std::ostream &os)
122{
123 // Print graph name
124 std::string graph_name = (g.name().empty()) ? "Graph" : g.name();
125 os << "digraph " << graph_name << "{\n";
126}
127
128void DotGraphPrinter::print_footer(const Graph &g, std::ostream &os)
129{
130 ARM_COMPUTE_UNUSED(g);
131 os << "}\n";
132}
133
134void DotGraphPrinter::print_nodes(const Graph &g, std::ostream &os)
135{
136 for(const auto &n : g.nodes())
137 {
138 if(n)
139 {
140 // Output node id
141 std::string node_id = std::string("n") + support::cpp11::to_string(n->id());
142 os << node_id << " ";
143
144 // Output label
145 n->accept(_dot_node_visitor);
146
147 std::string name = n->name().empty() ? node_id : n->name();
148 auto node_description = _dot_node_visitor.info();
149
150 os << R"([label = ")" << name << R"( \n )" << n->assigned_target() << R"( \n )" << node_description << R"("])";
151 os << ";\n";
152 }
153 }
154}
155
156void DotGraphPrinter::print_edges(const Graph &g, std::ostream &os)
157{
158 for(const auto &e : g.edges())
159 {
160 if(e)
161 {
162 std::string source_node_id = std::string("n") + support::cpp11::to_string(e->producer_id());
163 std::string sink_node_id = std::string("n") + support::cpp11::to_string(e->consumer_id());
164 os << source_node_id << " -> " << sink_node_id << " ";
165 const Tensor *t = e->tensor();
166 ARM_COMPUTE_ERROR_ON(t == nullptr);
167 os << R"([label = ")" << t->desc().shape << R"( \n )" << t->desc().data_type << R"("])";
168 os << ";\n";
169 }
170 }
171}
172} // namespace graph2
173} // namespace arm_compute