blob: ef156ea252e8f8adb1157a9312e0c10a63edae9a [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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph/printers/DotGraphPrinter.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000025
26#include "arm_compute/core/Error.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010027#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"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000031
32namespace arm_compute
33{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010034namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000035{
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
Georgios Pinitase2220552018-07-20 13:23:44 +010050void 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
Georgios Pinitasd8734b52017-12-22 15:27:52 +000059void DotGraphVisitor::visit(ConvolutionLayerNode &n)
60{
61 std::stringstream ss;
62 ss << n.convolution_method();
63 _info = ss.str();
64}
65
Georgios Pinitasd8734b52017-12-22 15:27:52 +000066void DotGraphVisitor::visit(DepthwiseConvolutionLayerNode &n)
67{
68 std::stringstream ss;
69 ss << n.depthwise_convolution_method();
70 _info = ss.str();
71}
72
73void DotGraphVisitor::visit(EltwiseLayerNode &n)
74{
75 std::stringstream ss;
76 ss << n.eltwise_operation();
77 _info = ss.str();
78}
79
80void DotGraphVisitor::visit(NormalizationLayerNode &n)
81{
82 std::stringstream ss;
83 ss << n.normalization_info().type();
84 _info = ss.str();
85}
86
87void DotGraphVisitor::visit(PoolingLayerNode &n)
88{
89 std::stringstream ss;
90 ss << n.pooling_info().pool_type();
91 ss << R"( \n )";
92 ss << n.pooling_info().pool_size();
93 ss << R"( \n )";
94 ss << n.pooling_info().pad_stride_info();
95 _info = ss.str();
96}
97
98void DotGraphVisitor::default_visit()
99{
100 _info.clear();
101}
102
103const std::string &DotGraphVisitor::info() const
104{
105 return _info;
106}
107
108void DotGraphPrinter::print(const Graph &g, std::ostream &os)
109{
110 // Print header
111 print_header(g, os);
112
113 // Print nodes
114 print_nodes(g, os);
115
116 // Print edges
117 print_edges(g, os);
118
119 // Print footer
120 print_footer(g, os);
121}
122
123void DotGraphPrinter::print_header(const Graph &g, std::ostream &os)
124{
125 // Print graph name
126 std::string graph_name = (g.name().empty()) ? "Graph" : g.name();
127 os << "digraph " << graph_name << "{\n";
128}
129
130void DotGraphPrinter::print_footer(const Graph &g, std::ostream &os)
131{
132 ARM_COMPUTE_UNUSED(g);
133 os << "}\n";
134}
135
136void DotGraphPrinter::print_nodes(const Graph &g, std::ostream &os)
137{
138 for(const auto &n : g.nodes())
139 {
140 if(n)
141 {
142 // Output node id
143 std::string node_id = std::string("n") + support::cpp11::to_string(n->id());
144 os << node_id << " ";
145
146 // Output label
147 n->accept(_dot_node_visitor);
148
149 std::string name = n->name().empty() ? node_id : n->name();
150 auto node_description = _dot_node_visitor.info();
151
152 os << R"([label = ")" << name << R"( \n )" << n->assigned_target() << R"( \n )" << node_description << R"("])";
153 os << ";\n";
154 }
155 }
156}
157
158void DotGraphPrinter::print_edges(const Graph &g, std::ostream &os)
159{
160 for(const auto &e : g.edges())
161 {
162 if(e)
163 {
164 std::string source_node_id = std::string("n") + support::cpp11::to_string(e->producer_id());
165 std::string sink_node_id = std::string("n") + support::cpp11::to_string(e->consumer_id());
166 os << source_node_id << " -> " << sink_node_id << " ";
167 const Tensor *t = e->tensor();
168 ARM_COMPUTE_ERROR_ON(t == nullptr);
Georgios Pinitascac13b12018-04-27 19:07:19 +0100169 os << R"([label = ")" << t->desc().shape << R"( \n )" << t->desc().data_type << R"( \n )" << t->desc().layout << R"("])";
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000170 os << ";\n";
171 }
172 }
173}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100174} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000175} // namespace arm_compute