Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 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 Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph/printers/DotGraphPrinter.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Error.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 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" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 31 | |
| 32 | namespace arm_compute |
| 33 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 34 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 35 | { |
| 36 | void DotGraphVisitor::visit(ActivationLayerNode &n) |
| 37 | { |
| 38 | std::stringstream ss; |
| 39 | ss << n.activation_info().activation(); |
| 40 | _info = ss.str(); |
| 41 | } |
| 42 | |
| 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 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 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 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 59 | void DotGraphVisitor::visit(ConvolutionLayerNode &n) |
| 60 | { |
| 61 | std::stringstream ss; |
| 62 | ss << n.convolution_method(); |
| 63 | _info = ss.str(); |
| 64 | } |
| 65 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 66 | void DotGraphVisitor::visit(DepthwiseConvolutionLayerNode &n) |
| 67 | { |
| 68 | std::stringstream ss; |
| 69 | ss << n.depthwise_convolution_method(); |
| 70 | _info = ss.str(); |
| 71 | } |
| 72 | |
| 73 | void DotGraphVisitor::visit(EltwiseLayerNode &n) |
| 74 | { |
| 75 | std::stringstream ss; |
| 76 | ss << n.eltwise_operation(); |
| 77 | _info = ss.str(); |
| 78 | } |
| 79 | |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 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 | |
Manuel Bottini | bffb41e | 2019-06-20 16:00:27 +0100 | [diff] [blame] | 88 | void DotGraphVisitor::visit(FusedDepthwiseConvolutionBatchNormalizationNode &n) |
| 89 | { |
| 90 | ARM_COMPUTE_UNUSED(n); |
| 91 | std::stringstream ss; |
| 92 | ss << "FusedDepthwiseConvolutionBatchNormalizationNode"; |
| 93 | _info = ss.str(); |
| 94 | } |
| 95 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 96 | void DotGraphVisitor::visit(NormalizationLayerNode &n) |
| 97 | { |
| 98 | std::stringstream ss; |
| 99 | ss << n.normalization_info().type(); |
| 100 | _info = ss.str(); |
| 101 | } |
| 102 | |
| 103 | void DotGraphVisitor::visit(PoolingLayerNode &n) |
| 104 | { |
| 105 | std::stringstream ss; |
Sang-Hoon Park | 0cb3da6 | 2020-01-15 12:39:56 +0000 | [diff] [blame] | 106 | ss << n.pooling_info().pool_type; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 107 | ss << R"( \n )"; |
Sang-Hoon Park | 0cb3da6 | 2020-01-15 12:39:56 +0000 | [diff] [blame] | 108 | ss << n.pooling_info().pool_size; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 109 | ss << R"( \n )"; |
Sang-Hoon Park | 0cb3da6 | 2020-01-15 12:39:56 +0000 | [diff] [blame] | 110 | ss << n.pooling_info().pad_stride_info; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 111 | _info = ss.str(); |
| 112 | } |
| 113 | |
| 114 | void DotGraphVisitor::default_visit() |
| 115 | { |
| 116 | _info.clear(); |
| 117 | } |
| 118 | |
| 119 | const std::string &DotGraphVisitor::info() const |
| 120 | { |
| 121 | return _info; |
| 122 | } |
| 123 | |
| 124 | void DotGraphPrinter::print(const Graph &g, std::ostream &os) |
| 125 | { |
| 126 | // Print header |
| 127 | print_header(g, os); |
| 128 | |
| 129 | // Print nodes |
| 130 | print_nodes(g, os); |
| 131 | |
| 132 | // Print edges |
| 133 | print_edges(g, os); |
| 134 | |
| 135 | // Print footer |
| 136 | print_footer(g, os); |
| 137 | } |
| 138 | |
| 139 | void DotGraphPrinter::print_header(const Graph &g, std::ostream &os) |
| 140 | { |
| 141 | // Print graph name |
| 142 | std::string graph_name = (g.name().empty()) ? "Graph" : g.name(); |
| 143 | os << "digraph " << graph_name << "{\n"; |
| 144 | } |
| 145 | |
| 146 | void DotGraphPrinter::print_footer(const Graph &g, std::ostream &os) |
| 147 | { |
| 148 | ARM_COMPUTE_UNUSED(g); |
| 149 | os << "}\n"; |
| 150 | } |
| 151 | |
| 152 | void DotGraphPrinter::print_nodes(const Graph &g, std::ostream &os) |
| 153 | { |
| 154 | for(const auto &n : g.nodes()) |
| 155 | { |
| 156 | if(n) |
| 157 | { |
| 158 | // Output node id |
| 159 | std::string node_id = std::string("n") + support::cpp11::to_string(n->id()); |
| 160 | os << node_id << " "; |
| 161 | |
| 162 | // Output label |
| 163 | n->accept(_dot_node_visitor); |
| 164 | |
| 165 | std::string name = n->name().empty() ? node_id : n->name(); |
| 166 | auto node_description = _dot_node_visitor.info(); |
| 167 | |
| 168 | os << R"([label = ")" << name << R"( \n )" << n->assigned_target() << R"( \n )" << node_description << R"("])"; |
| 169 | os << ";\n"; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void DotGraphPrinter::print_edges(const Graph &g, std::ostream &os) |
| 175 | { |
| 176 | for(const auto &e : g.edges()) |
| 177 | { |
| 178 | if(e) |
| 179 | { |
| 180 | std::string source_node_id = std::string("n") + support::cpp11::to_string(e->producer_id()); |
| 181 | std::string sink_node_id = std::string("n") + support::cpp11::to_string(e->consumer_id()); |
| 182 | os << source_node_id << " -> " << sink_node_id << " "; |
| 183 | const Tensor *t = e->tensor(); |
| 184 | ARM_COMPUTE_ERROR_ON(t == nullptr); |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 185 | os << R"([label = ")" << t->desc().shape << R"( \n )" << t->desc().data_type << R"( \n )" << t->desc().layout << R"("])"; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 186 | os << ";\n"; |
| 187 | } |
| 188 | } |
| 189 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 190 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 191 | } // namespace arm_compute |