Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
Matthew Bentham | 043613f | 2023-05-30 16:43:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 2023 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/GraphBuilder.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 25 | |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/utils/DataTypeUtils.h" |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 27 | #include "arm_compute/graph/algorithms/TopologicalSort.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | #include "arm_compute/graph/Graph.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 29 | #include "arm_compute/graph/nodes/Nodes.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 30 | #include "arm_compute/graph/Utils.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 31 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 32 | #include "support/ToolchainSupport.h" |
| 33 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 36 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 37 | { |
| 38 | namespace |
| 39 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 40 | inline void check_nodeidx_pair(const NodeIdxPair &pair, const Graph &g) |
| 41 | { |
| 42 | ARM_COMPUTE_UNUSED(pair); |
| 43 | ARM_COMPUTE_UNUSED(g); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 44 | ARM_COMPUTE_ERROR_ON((pair.node_id >= g.nodes().size()) || (g.node((pair).node_id) == nullptr) || |
| 45 | (pair.index >= g.node(pair.node_id)->num_outputs())); |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 48 | Status set_node_params(Graph &g, NodeID nid, NodeParams ¶ms) |
| 49 | { |
| 50 | INode *node = g.node(nid); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON(!node); |
| 52 | |
| 53 | node->set_common_node_parameters(params); |
| 54 | |
| 55 | return Status{}; |
| 56 | } |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 57 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 58 | Status set_accessor_on_node(Graph &g, NodeID nid, bool is_output, size_t idx, ITensorAccessorUPtr accessor) |
| 59 | { |
| 60 | INode *node = g.node(nid); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON(!node); |
| 62 | |
| 63 | Tensor *tensor = is_output ? node->output(idx) : node->input(idx); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON(!tensor); |
| 65 | |
| 66 | tensor->set_accessor(std::move(accessor)); |
| 67 | |
| 68 | return Status{}; |
| 69 | } |
| 70 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 71 | NodeID add_const_node_with_name( |
| 72 | Graph &g, NodeParams params, const std::string &name, const TensorDescriptor &desc, ITensorAccessorUPtr accessor) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 73 | { |
| 74 | params.name = params.name.empty() ? "" : params.name + name; |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 75 | auto nid = GraphBuilder::add_const_node(g, params, desc, std::move(accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 76 | set_node_params(g, nid, params); |
| 77 | return nid; |
| 78 | } |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 79 | |
| 80 | template <typename NT, typename... Args> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 81 | NodeID create_simple_single_input_output_node(Graph &g, NodeParams ¶ms, NodeIdxPair input, Args &&...args) |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 82 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 83 | check_nodeidx_pair(input, g); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 84 | |
| 85 | NodeID nid = g.add_node<NT>(std::forward<Args>(args)...); |
| 86 | g.add_connection(input.node_id, input.index, nid, 0); |
| 87 | set_node_params(g, nid, params); |
| 88 | |
| 89 | return nid; |
| 90 | } |
Michele Di Giorgio | ec69975 | 2019-03-22 15:25:32 +0000 | [diff] [blame] | 91 | |
| 92 | template <typename NT, typename... Args> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 93 | NodeID create_simple_multiple_input_single_output_node(Graph &g, |
| 94 | NodeParams ¶ms, |
| 95 | const std::vector<NodeIdxPair> &inputs, |
| 96 | Args &&...args) |
Michele Di Giorgio | ec69975 | 2019-03-22 15:25:32 +0000 | [diff] [blame] | 97 | { |
| 98 | ARM_COMPUTE_ERROR_ON(inputs.size() == 0); |
| 99 | |
| 100 | NodeID nid = g.add_node<NT>(std::forward<Args>(args)...); |
| 101 | |
| 102 | unsigned int i = 0; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 103 | for (const auto &input : inputs) |
Michele Di Giorgio | ec69975 | 2019-03-22 15:25:32 +0000 | [diff] [blame] | 104 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 105 | check_nodeidx_pair(input, g); |
Michele Di Giorgio | ec69975 | 2019-03-22 15:25:32 +0000 | [diff] [blame] | 106 | g.add_connection(input.node_id, input.index, nid, i++); |
| 107 | } |
| 108 | set_node_params(g, nid, params); |
| 109 | |
| 110 | return nid; |
| 111 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 112 | } // namespace |
| 113 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 114 | NodeID |
| 115 | GraphBuilder::add_const_node(Graph &g, NodeParams params, const TensorDescriptor &desc, ITensorAccessorUPtr accessor) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 116 | { |
| 117 | auto nid = g.add_node<ConstNode>(desc); |
| 118 | set_node_params(g, nid, params); |
| 119 | set_accessor_on_node(g, nid, true, 0, std::move(accessor)); |
| 120 | return nid; |
| 121 | } |
| 122 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 123 | NodeID |
| 124 | GraphBuilder::add_input_node(Graph &g, NodeParams params, const TensorDescriptor &desc, ITensorAccessorUPtr accessor) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 125 | { |
| 126 | auto nid = g.add_node<InputNode>(desc); |
| 127 | set_node_params(g, nid, params); |
| 128 | set_accessor_on_node(g, nid, true, 0, std::move(accessor)); |
| 129 | return nid; |
| 130 | } |
| 131 | |
| 132 | NodeID GraphBuilder::add_output_node(Graph &g, NodeParams params, NodeIdxPair input, ITensorAccessorUPtr accessor) |
| 133 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 134 | check_nodeidx_pair(input, g); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 135 | |
| 136 | NodeID nid = g.add_node<OutputNode>(); |
| 137 | g.add_connection(input.node_id, input.index, nid, 0); |
| 138 | set_node_params(g, nid, params); |
| 139 | set_accessor_on_node(g, nid, false, 0, std::move(accessor)); |
| 140 | |
| 141 | return nid; |
| 142 | } |
| 143 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 144 | NodeID GraphBuilder::add_activation_node(Graph &g, |
| 145 | NodeParams params, |
| 146 | NodeIdxPair input, |
| 147 | ActivationLayerInfo act_info, |
Michalis Spyrou | 18574c1 | 2019-06-05 10:51:07 +0100 | [diff] [blame] | 148 | const QuantizationInfo &out_quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 149 | { |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 150 | return create_simple_single_input_output_node<ActivationLayerNode>(g, params, input, act_info, out_quant_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 153 | NodeID GraphBuilder::add_arg_min_max_node(Graph &g, |
| 154 | NodeParams params, |
| 155 | NodeIdxPair input, |
| 156 | ReductionOperation op, |
| 157 | unsigned int axis, |
| 158 | DataType out_data_type, |
| 159 | const QuantizationInfo &out_quant_info) |
thecha01 | e8f05da | 2020-08-24 17:21:41 +0100 | [diff] [blame] | 160 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 161 | return create_simple_single_input_output_node<ArgMinMaxLayerNode>(g, params, input, op, axis, out_data_type, |
| 162 | out_quant_info); |
thecha01 | e8f05da | 2020-08-24 17:21:41 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 165 | NodeID GraphBuilder::add_batch_normalization_node(Graph &g, |
| 166 | NodeParams params, |
| 167 | NodeIdxPair input, |
| 168 | float epsilon, |
| 169 | ITensorAccessorUPtr mean_accessor, |
| 170 | ITensorAccessorUPtr var_accessor, |
| 171 | ITensorAccessorUPtr beta_accessor, |
| 172 | ITensorAccessorUPtr gamma_accessor) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 173 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 174 | check_nodeidx_pair(input, g); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 175 | |
| 176 | bool has_beta = (beta_accessor != nullptr); |
| 177 | bool has_gamma = (gamma_accessor != nullptr); |
| 178 | |
| 179 | // Get input tensor descriptor |
| 180 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 181 | |
| 182 | // Calculate Common Descriptor |
| 183 | TensorDescriptor common_desc = input_tensor_desc; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 184 | common_desc.shape = TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 185 | |
Michele Di Giorgio | 555d110 | 2018-09-12 13:51:59 +0100 | [diff] [blame] | 186 | // Create mean and var nodes |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 187 | auto mean_nid = add_const_node_with_name(g, params, "Mean", common_desc, std::move(mean_accessor)); |
| 188 | auto var_nid = add_const_node_with_name(g, params, "Variance", common_desc, std::move(var_accessor)); |
| 189 | |
| 190 | // Create beta node |
| 191 | NodeID beta_nid = EmptyNodeID; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 192 | if (has_beta) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 193 | { |
| 194 | beta_nid = add_const_node_with_name(g, params, "Beta", common_desc, std::move(beta_accessor)); |
| 195 | } |
| 196 | |
| 197 | // Create gamma node |
| 198 | NodeID gamma_nid = EmptyNodeID; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 199 | if (has_gamma) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 200 | { |
| 201 | gamma_nid = add_const_node_with_name(g, params, "Gamma", common_desc, std::move(gamma_accessor)); |
| 202 | } |
| 203 | |
| 204 | // Create batch normalization node and add connections |
| 205 | NodeID batch_norm_nid = g.add_node<BatchNormalizationLayerNode>(epsilon); |
| 206 | g.add_connection(input.node_id, input.index, batch_norm_nid, 0); |
| 207 | g.add_connection(mean_nid, 0, batch_norm_nid, 1); |
| 208 | g.add_connection(var_nid, 0, batch_norm_nid, 2); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 209 | if (has_beta) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 210 | { |
| 211 | g.add_connection(beta_nid, 0, batch_norm_nid, 3); |
| 212 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 213 | if (has_gamma) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 214 | { |
| 215 | g.add_connection(gamma_nid, 0, batch_norm_nid, 4); |
| 216 | } |
| 217 | set_node_params(g, batch_norm_nid, params); |
| 218 | |
| 219 | return batch_norm_nid; |
| 220 | } |
| 221 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 222 | NodeID GraphBuilder::add_bounding_box_transform_node( |
| 223 | Graph &g, NodeParams params, NodeIdxPair input, NodeIdxPair deltas, BoundingBoxTransformInfo info) |
Manuel Bottini | d2048ce | 2018-10-23 17:00:42 +0100 | [diff] [blame] | 224 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 225 | check_nodeidx_pair(input, g); |
| 226 | check_nodeidx_pair(deltas, g); |
Manuel Bottini | d2048ce | 2018-10-23 17:00:42 +0100 | [diff] [blame] | 227 | |
| 228 | NodeID nid = g.add_node<BoundingBoxTransformLayerNode>(info); |
| 229 | |
| 230 | g.add_connection(input.node_id, input.index, nid, 0); |
| 231 | g.add_connection(deltas.node_id, deltas.index, nid, 1); |
| 232 | |
| 233 | set_node_params(g, nid, params); |
| 234 | return nid; |
| 235 | } |
| 236 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 237 | NodeID GraphBuilder::add_channel_shuffle_node(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_groups) |
| 238 | { |
| 239 | return create_simple_single_input_output_node<ChannelShuffleLayerNode>(g, params, input, num_groups); |
| 240 | } |
| 241 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 242 | NodeID GraphBuilder::add_convolution_node(Graph &g, |
| 243 | NodeParams params, |
| 244 | NodeIdxPair input, |
| 245 | Size2D kernel_spatial_extend, |
| 246 | unsigned int depth, |
| 247 | PadStrideInfo conv_info, |
| 248 | unsigned int num_groups, |
| 249 | ConvolutionMethod method, |
| 250 | FastMathHint fast_math_hint, |
| 251 | ITensorAccessorUPtr weights_accessor, |
| 252 | ITensorAccessorUPtr bias_accessor, |
Michalis Spyrou | 18574c1 | 2019-06-05 10:51:07 +0100 | [diff] [blame] | 253 | const QuantizationInfo &weights_quant_info, |
| 254 | const QuantizationInfo &out_quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 255 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 256 | check_nodeidx_pair(input, g); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 257 | ARM_COMPUTE_ERROR_ON(depth == 0); |
| 258 | ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0)); |
| 259 | |
| 260 | bool has_bias = (bias_accessor != nullptr); |
| 261 | |
| 262 | // Get input tensor descriptor |
| 263 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 264 | const DataLayout input_data_layout = input_tensor_desc.layout; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 265 | |
| 266 | // Create weights node |
| 267 | TensorDescriptor w_desc = input_tensor_desc; |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 268 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::WIDTH), kernel_spatial_extend.width); |
| 269 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height); |
| 270 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL), |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 271 | get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL) / num_groups); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 272 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::BATCHES), depth); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 273 | if (!weights_quant_info.empty()) |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 274 | { |
| 275 | w_desc.quant_info = weights_quant_info; |
| 276 | } |
| 277 | |
| 278 | NodeID w_nid = add_const_node_with_name(g, params, "Weights", w_desc, std::move(weights_accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 279 | |
| 280 | // Create bias nodes |
| 281 | NodeID b_nid = EmptyNodeID; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 282 | if (has_bias) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 283 | { |
| 284 | TensorDescriptor b_desc = input_tensor_desc; |
| 285 | b_desc.shape = TensorShape(depth); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 286 | if (is_data_type_quantized_asymmetric(input_tensor_desc.data_type)) |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 287 | { |
| 288 | b_desc.data_type = DataType::S32; |
| 289 | } |
| 290 | b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 293 | // Create convolution node and connect |
| 294 | NodeID conv_nid = g.add_node<ConvolutionLayerNode>(conv_info, num_groups, method, fast_math_hint, out_quant_info); |
| 295 | g.add_connection(input.node_id, input.index, conv_nid, 0); |
| 296 | g.add_connection(w_nid, 0, conv_nid, 1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 297 | if (has_bias) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 298 | { |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 299 | g.add_connection(b_nid, 0, conv_nid, 2); |
| 300 | } |
| 301 | set_node_params(g, conv_nid, params); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 302 | |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 303 | return conv_nid; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 306 | NodeID GraphBuilder::add_deconvolution_node(Graph &g, |
| 307 | NodeParams params, |
| 308 | NodeIdxPair input, |
| 309 | Size2D kernel_spatial_extend, |
| 310 | unsigned int depth, |
| 311 | PadStrideInfo deconv_info, |
Manuel Bottini | c1b76fa | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 312 | ITensorAccessorUPtr weights_accessor, |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 313 | ITensorAccessorUPtr bias_accessor) |
| 314 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 315 | check_nodeidx_pair(input, g); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 316 | ARM_COMPUTE_ERROR_ON(depth == 0); |
| 317 | ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0)); |
| 318 | |
| 319 | bool has_bias = (bias_accessor != nullptr); |
| 320 | |
| 321 | // Get input tensor descriptor |
| 322 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 323 | const DataLayout input_data_layout = input_tensor_desc.layout; |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 324 | |
| 325 | // Create weights node |
| 326 | TensorDescriptor w_desc = input_tensor_desc; |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 327 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::WIDTH), kernel_spatial_extend.width); |
| 328 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height); |
| 329 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL), |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 330 | get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 331 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::BATCHES), depth); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 332 | |
| 333 | NodeID w_nid = add_const_node_with_name(g, params, "Weights", w_desc, std::move(weights_accessor)); |
| 334 | |
| 335 | // Create bias nodes |
| 336 | NodeID b_nid = EmptyNodeID; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 337 | if (has_bias) |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 338 | { |
| 339 | TensorDescriptor b_desc = input_tensor_desc; |
| 340 | b_desc.shape = TensorShape(depth); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 341 | if (is_data_type_quantized_asymmetric(input_tensor_desc.data_type)) |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 342 | { |
| 343 | b_desc.data_type = DataType::S32; |
| 344 | } |
| 345 | b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor)); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | // Create convolution node and connect |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 349 | NodeID deconv_nid = g.add_node<DeconvolutionLayerNode>(descriptors::DeconvolutionLayerDescriptor{deconv_info}); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 350 | g.add_connection(input.node_id, input.index, deconv_nid, 0); |
| 351 | g.add_connection(w_nid, 0, deconv_nid, 1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 352 | if (has_bias) |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 353 | { |
| 354 | g.add_connection(b_nid, 0, deconv_nid, 2); |
| 355 | } |
| 356 | set_node_params(g, deconv_nid, params); |
| 357 | |
| 358 | return deconv_nid; |
| 359 | } |
| 360 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 361 | NodeID GraphBuilder::add_concatenate_node(Graph &g, |
| 362 | NodeParams params, |
| 363 | const std::vector<NodeIdxPair> &inputs, |
| 364 | const descriptors::ConcatLayerDescriptor &concat_descriptor) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 365 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 366 | return create_simple_multiple_input_single_output_node<ConcatenateLayerNode>(g, params, inputs, inputs.size(), |
| 367 | concat_descriptor); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 370 | NodeID GraphBuilder::add_depthwise_convolution_node(Graph &g, |
| 371 | NodeParams params, |
| 372 | NodeIdxPair input, |
| 373 | Size2D kernel_spatial_extend, |
| 374 | PadStrideInfo conv_info, |
| 375 | int depth_multiplier, |
| 376 | DepthwiseConvolutionMethod method, |
| 377 | ITensorAccessorUPtr weights_accessor, |
| 378 | ITensorAccessorUPtr bias_accessor, |
| 379 | const QuantizationInfo &quant_info, |
| 380 | const QuantizationInfo &out_quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 381 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 382 | check_nodeidx_pair(input, g); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 383 | ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0)); |
| 384 | |
| 385 | bool has_bias = (bias_accessor != nullptr); |
| 386 | |
| 387 | // Get input tensor descriptor |
| 388 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 389 | const DataLayout input_data_layout = input_tensor_desc.layout; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 390 | |
| 391 | // Create weights node |
| 392 | TensorDescriptor w_desc = input_tensor_desc; |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 393 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::WIDTH), kernel_spatial_extend.width); |
| 394 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height); |
| 395 | w_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL), |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 396 | get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL) * depth_multiplier); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 397 | if (!quant_info.empty()) |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 398 | { |
| 399 | w_desc.quant_info = quant_info; |
| 400 | } |
| 401 | |
| 402 | NodeID w_nid = add_const_node_with_name(g, params, "Weights", w_desc, std::move(weights_accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 403 | |
| 404 | // Create bias nodes |
| 405 | NodeID b_nid = EmptyNodeID; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 406 | if (has_bias) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 407 | { |
| 408 | TensorDescriptor b_desc = input_tensor_desc; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 409 | b_desc.shape = |
| 410 | TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL) * depth_multiplier); |
Giorgio Arena | 2aa0ec4 | 2018-08-29 14:28:38 +0100 | [diff] [blame] | 411 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 412 | if (is_data_type_quantized_asymmetric(b_desc.data_type)) |
Giorgio Arena | 2aa0ec4 | 2018-08-29 14:28:38 +0100 | [diff] [blame] | 413 | { |
| 414 | b_desc.data_type = DataType::S32; |
| 415 | } |
| 416 | |
| 417 | b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | // Create convolution node and connect |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 421 | NodeID conv_nid = g.add_node<DepthwiseConvolutionLayerNode>(conv_info, depth_multiplier, method, out_quant_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 422 | g.add_connection(input.node_id, input.index, conv_nid, 0); |
| 423 | g.add_connection(w_nid, 0, conv_nid, 1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 424 | if (has_bias) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 425 | { |
| 426 | g.add_connection(b_nid, 0, conv_nid, 2); |
| 427 | } |
| 428 | set_node_params(g, conv_nid, params); |
| 429 | |
| 430 | return conv_nid; |
| 431 | } |
thecha01 | 0a05e6a | 2020-08-28 18:40:38 +0100 | [diff] [blame] | 432 | |
| 433 | NodeID GraphBuilder::add_depth_to_space_node(Graph &g, NodeParams params, NodeIdxPair input, int32_t block_shape) |
| 434 | { |
| 435 | return create_simple_single_input_output_node<DepthToSpaceLayerNode>(g, params, input, block_shape); |
| 436 | } |
| 437 | |
Isabella Gottardi | cd4e9ab | 2019-11-05 17:50:27 +0000 | [diff] [blame] | 438 | NodeID GraphBuilder::add_dequantization_node(Graph &g, NodeParams params, NodeIdxPair input) |
| 439 | { |
| 440 | return create_simple_single_input_output_node<DequantizationLayerNode>(g, params, input); |
| 441 | } |
| 442 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 443 | NodeID GraphBuilder::add_detection_output_node(Graph &g, |
| 444 | NodeParams params, |
| 445 | NodeIdxPair input_loc, |
| 446 | NodeIdxPair input_conf, |
| 447 | NodeIdxPair input_priorbox, |
| 448 | const DetectionOutputLayerInfo &detect_info) |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 449 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 450 | check_nodeidx_pair(input_loc, g); |
| 451 | check_nodeidx_pair(input_conf, g); |
| 452 | check_nodeidx_pair(input_priorbox, g); |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 453 | |
| 454 | // Create detection_output node and connect |
| 455 | NodeID detect_nid = g.add_node<DetectionOutputLayerNode>(detect_info); |
| 456 | g.add_connection(input_loc.node_id, input_loc.index, detect_nid, 0); |
| 457 | g.add_connection(input_conf.node_id, input_conf.index, detect_nid, 1); |
| 458 | g.add_connection(input_priorbox.node_id, input_priorbox.index, detect_nid, 2); |
| 459 | |
| 460 | set_node_params(g, detect_nid, params); |
| 461 | |
| 462 | return detect_nid; |
| 463 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 464 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 465 | NodeID GraphBuilder::add_detection_post_process_node(Graph &g, |
| 466 | NodeParams params, |
| 467 | NodeIdxPair input_box_encoding, |
| 468 | NodeIdxPair input_class_prediction, |
| 469 | const DetectionPostProcessLayerInfo &detect_info, |
| 470 | ITensorAccessorUPtr anchors_accessor, |
| 471 | const QuantizationInfo &anchor_quant_info) |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 472 | { |
| 473 | check_nodeidx_pair(input_box_encoding, g); |
| 474 | check_nodeidx_pair(input_class_prediction, g); |
| 475 | |
| 476 | // Get input tensor descriptor |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 477 | const TensorDescriptor input_box_encoding_tensor_desc = |
| 478 | get_tensor_descriptor(g, g.node(input_box_encoding.node_id)->outputs()[0]); |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 479 | |
| 480 | // Calculate anchor descriptor |
| 481 | TensorDescriptor anchor_desc = input_box_encoding_tensor_desc; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 482 | if (!anchor_quant_info.empty()) |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 483 | { |
| 484 | anchor_desc.quant_info = anchor_quant_info; |
| 485 | } |
| 486 | |
| 487 | // Create anchors nodes |
| 488 | auto anchors_nid = add_const_node_with_name(g, params, "Anchors", anchor_desc, std::move(anchors_accessor)); |
| 489 | |
| 490 | // Create detection_output node and connect |
| 491 | NodeID detect_nid = g.add_node<DetectionPostProcessLayerNode>(detect_info); |
| 492 | g.add_connection(input_box_encoding.node_id, input_box_encoding.index, detect_nid, 0); |
| 493 | g.add_connection(input_class_prediction.node_id, input_class_prediction.index, detect_nid, 1); |
| 494 | g.add_connection(anchors_nid, 0, detect_nid, 2); |
| 495 | |
| 496 | set_node_params(g, detect_nid, params); |
| 497 | |
| 498 | return detect_nid; |
| 499 | } |
| 500 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 501 | NodeID GraphBuilder::add_dummy_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape) |
| 502 | { |
Georgios Pinitas | b7a2023 | 2018-07-02 16:12:54 +0100 | [diff] [blame] | 503 | return create_simple_single_input_output_node<DummyNode>(g, params, input, shape); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 504 | } |
| 505 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 506 | NodeID GraphBuilder::add_elementwise_node( |
| 507 | Graph &g, NodeParams params, NodeIdxPair input0, NodeIdxPair input1, EltwiseOperation operation) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 508 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 509 | check_nodeidx_pair(input0, g); |
| 510 | check_nodeidx_pair(input1, g); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 511 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 512 | NodeID nid = g.add_node<EltwiseLayerNode>(descriptors::EltwiseLayerDescriptor{operation}); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 513 | |
| 514 | g.add_connection(input0.node_id, input0.index, nid, 0); |
| 515 | g.add_connection(input1.node_id, input1.index, nid, 1); |
| 516 | |
| 517 | set_node_params(g, nid, params); |
| 518 | |
| 519 | return nid; |
| 520 | } |
| 521 | |
| 522 | NodeID GraphBuilder::add_flatten_node(Graph &g, NodeParams params, NodeIdxPair input) |
| 523 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 524 | return create_simple_single_input_output_node<FlattenLayerNode>(g, params, input); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 527 | NodeID GraphBuilder::add_fully_connected_layer(Graph &g, |
| 528 | NodeParams params, |
| 529 | NodeIdxPair input, |
| 530 | unsigned int num_outputs, |
| 531 | NodeID weights_nid, |
| 532 | NodeID bias_nid, |
| 533 | const FullyConnectedLayerInfo fc_info, |
| 534 | const QuantizationInfo &out_quant_info, |
| 535 | FastMathHint fast_math_hint) |
Michele Di Giorgio | a42f55f | 2019-03-08 14:52:17 +0000 | [diff] [blame] | 536 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 537 | check_nodeidx_pair(input, g); |
Michele Di Giorgio | a42f55f | 2019-03-08 14:52:17 +0000 | [diff] [blame] | 538 | ARM_COMPUTE_ERROR_ON(num_outputs == 0); |
| 539 | ARM_COMPUTE_ERROR_ON(weights_nid == EmptyNodeID); |
| 540 | |
| 541 | const bool has_bias = (bias_nid != EmptyNodeID); |
| 542 | |
| 543 | // Get input tensor descriptor |
| 544 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 545 | |
| 546 | // Create fully connected node and connect |
cfRod | f2c022e | 2021-11-05 11:29:53 +0000 | [diff] [blame] | 547 | NodeID fc_nid = g.add_node<FullyConnectedLayerNode>(num_outputs, out_quant_info, fc_info, fast_math_hint); |
Michele Di Giorgio | a42f55f | 2019-03-08 14:52:17 +0000 | [diff] [blame] | 548 | g.add_connection(input.node_id, input.index, fc_nid, 0); |
| 549 | g.add_connection(weights_nid, 0, fc_nid, 1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 550 | if (has_bias) |
Michele Di Giorgio | a42f55f | 2019-03-08 14:52:17 +0000 | [diff] [blame] | 551 | { |
| 552 | g.add_connection(bias_nid, 0, fc_nid, 2); |
| 553 | } |
| 554 | |
| 555 | set_node_params(g, fc_nid, params); |
| 556 | |
| 557 | return fc_nid; |
| 558 | } |
| 559 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 560 | NodeID GraphBuilder::add_fully_connected_layer(Graph &g, |
| 561 | NodeParams params, |
| 562 | NodeIdxPair input, |
| 563 | unsigned int num_outputs, |
| 564 | ITensorAccessorUPtr weights_accessor, |
| 565 | ITensorAccessorUPtr bias_accessor, |
Georgios Pinitas | c55cef1 | 2018-08-01 15:24:18 +0100 | [diff] [blame] | 566 | const FullyConnectedLayerInfo fc_info, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 567 | const QuantizationInfo &weights_quant_info, |
| 568 | const QuantizationInfo &out_quant_info, |
| 569 | FastMathHint fast_math_hint) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 570 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 571 | check_nodeidx_pair(input, g); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 572 | ARM_COMPUTE_ERROR_ON(num_outputs == 0); |
| 573 | |
| 574 | bool has_bias = (bias_accessor != nullptr); |
| 575 | |
| 576 | // Get input tensor descriptor |
| 577 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 578 | |
| 579 | // Create weights node |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 580 | TensorDescriptor w_desc = FullyConnectedLayerNode::compute_weights_descriptor(input_tensor_desc, num_outputs, |
| 581 | fc_info, weights_quant_info); |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 582 | NodeID w_nid = add_const_node_with_name(g, params, "Weights", w_desc, std::move(weights_accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 583 | |
| 584 | // Create bias nodes |
| 585 | NodeID b_nid = EmptyNodeID; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 586 | if (has_bias) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 587 | { |
| 588 | TensorDescriptor b_desc = input_tensor_desc; |
| 589 | b_desc.shape = TensorShape(num_outputs); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 590 | if (is_data_type_quantized_asymmetric(input_tensor_desc.data_type)) |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 591 | { |
| 592 | b_desc.data_type = DataType::S32; |
| 593 | } |
| 594 | b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 597 | // Create fully connected node and connect |
cfRod | f2c022e | 2021-11-05 11:29:53 +0000 | [diff] [blame] | 598 | NodeID fc_nid = g.add_node<FullyConnectedLayerNode>(num_outputs, out_quant_info, fc_info, fast_math_hint); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 599 | g.add_connection(input.node_id, input.index, fc_nid, 0); |
| 600 | g.add_connection(w_nid, 0, fc_nid, 1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 601 | if (has_bias) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 602 | { |
| 603 | g.add_connection(b_nid, 0, fc_nid, 2); |
| 604 | } |
| 605 | |
| 606 | set_node_params(g, fc_nid, params); |
| 607 | |
| 608 | return fc_nid; |
| 609 | } |
| 610 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 611 | NodeID GraphBuilder::add_generate_proposals_node(Graph &g, |
| 612 | NodeParams params, |
| 613 | NodeIdxPair scores, |
| 614 | NodeIdxPair deltas, |
| 615 | NodeIdxPair anchors, |
| 616 | GenerateProposalsInfo info) |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 617 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 618 | check_nodeidx_pair(scores, g); |
| 619 | check_nodeidx_pair(deltas, g); |
| 620 | check_nodeidx_pair(anchors, g); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 621 | |
| 622 | NodeID nid = g.add_node<GenerateProposalsLayerNode>(info); |
| 623 | |
| 624 | g.add_connection(scores.node_id, scores.index, nid, 0); |
| 625 | g.add_connection(deltas.node_id, deltas.index, nid, 1); |
| 626 | g.add_connection(anchors.node_id, anchors.index, nid, 2); |
| 627 | |
| 628 | set_node_params(g, nid, params); |
| 629 | return nid; |
| 630 | } |
| 631 | |
thecha01 | 3603aff | 2020-09-01 14:52:38 +0100 | [diff] [blame] | 632 | NodeID GraphBuilder::add_l2_normalize_node(Graph &g, NodeParams params, NodeIdxPair input, int axis, float epsilon) |
| 633 | { |
| 634 | return create_simple_single_input_output_node<L2NormalizeLayerNode>(g, params, input, axis, epsilon); |
| 635 | } |
| 636 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 637 | NodeID |
| 638 | GraphBuilder::add_normalization_node(Graph &g, NodeParams params, NodeIdxPair input, NormalizationLayerInfo norm_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 639 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 640 | return create_simple_single_input_output_node<NormalizationLayerNode>(g, params, input, norm_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 643 | NodeID GraphBuilder::add_normalize_planar_yuv_node( |
| 644 | Graph &g, NodeParams params, NodeIdxPair input, ITensorAccessorUPtr mean_accessor, ITensorAccessorUPtr std_accessor) |
Michele Di Giorgio | 555d110 | 2018-09-12 13:51:59 +0100 | [diff] [blame] | 645 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 646 | check_nodeidx_pair(input, g); |
Michele Di Giorgio | 555d110 | 2018-09-12 13:51:59 +0100 | [diff] [blame] | 647 | |
| 648 | // Get input tensor descriptor |
| 649 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 650 | |
| 651 | // Calculate Common Descriptor |
| 652 | TensorDescriptor common_desc = input_tensor_desc; |
| 653 | common_desc.shape = TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
| 654 | |
| 655 | // Create mean and std nodes |
| 656 | auto mean_nid = add_const_node_with_name(g, params, "Mean", common_desc, std::move(mean_accessor)); |
| 657 | auto std_nid = add_const_node_with_name(g, params, "Std", common_desc, std::move(std_accessor)); |
| 658 | |
| 659 | // Create normalize planar YUV node and add connections |
| 660 | NodeID norm_planar_yuv_nid = g.add_node<NormalizePlanarYUVLayerNode>(); |
| 661 | g.add_connection(input.node_id, input.index, norm_planar_yuv_nid, 0); |
| 662 | g.add_connection(mean_nid, 0, norm_planar_yuv_nid, 1); |
| 663 | g.add_connection(std_nid, 0, norm_planar_yuv_nid, 2); |
| 664 | set_node_params(g, norm_planar_yuv_nid, params); |
| 665 | |
| 666 | return norm_planar_yuv_nid; |
| 667 | } |
| 668 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 669 | NodeID GraphBuilder::add_pad_node( |
| 670 | Graph &g, NodeParams params, NodeIdxPair input, const PaddingList &paddings, PixelValue pad_value) |
Michele Di Giorgio | 4bb1733 | 2018-09-26 13:56:51 +0100 | [diff] [blame] | 671 | { |
Georgios Pinitas | 102b0ce | 2020-02-13 17:59:09 +0000 | [diff] [blame] | 672 | return create_simple_single_input_output_node<PadLayerNode>(g, params, input, paddings, pad_value); |
Michele Di Giorgio | 4bb1733 | 2018-09-26 13:56:51 +0100 | [diff] [blame] | 673 | } |
| 674 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 675 | NodeID GraphBuilder::add_permute_node( |
| 676 | Graph &g, NodeParams params, NodeIdxPair input, PermutationVector perm, DataLayout layout) |
Georgios Pinitas | 57c4824 | 2018-08-02 13:41:49 +0100 | [diff] [blame] | 677 | { |
| 678 | return create_simple_single_input_output_node<PermuteLayerNode>(g, params, input, perm, layout); |
| 679 | } |
| 680 | |
Georgios Pinitas | f8c4749 | 2020-02-04 17:39:59 +0000 | [diff] [blame] | 681 | NodeID GraphBuilder::add_prelu_node(Graph &g, NodeParams params, NodeIdxPair input, NodeIdxPair alpha) |
| 682 | { |
| 683 | check_nodeidx_pair(input, g); |
| 684 | check_nodeidx_pair(alpha, g); |
| 685 | |
| 686 | NodeID prelu_nid = g.add_node<PReluLayerNode>(); |
| 687 | g.add_connection(input.node_id, input.index, prelu_nid, 0); |
| 688 | g.add_connection(alpha.node_id, alpha.index, prelu_nid, 1); |
| 689 | |
| 690 | set_node_params(g, prelu_nid, params); |
| 691 | |
| 692 | return prelu_nid; |
| 693 | } |
| 694 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 695 | NodeID GraphBuilder::add_pooling_node(Graph &g, NodeParams params, NodeIdxPair input, PoolingLayerInfo pool_info) |
| 696 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 697 | return create_simple_single_input_output_node<PoolingLayerNode>(g, params, input, pool_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 700 | NodeID GraphBuilder::add_print_node(Graph &g, |
| 701 | NodeParams params, |
| 702 | NodeIdxPair input, |
| 703 | std::ostream &stream, |
| 704 | const IOFormatInfo &format_info, |
| 705 | const std::function<ITensor *(ITensor *)> transform) |
Giorgio Arena | 6e9d0e0 | 2020-01-03 15:02:04 +0000 | [diff] [blame] | 706 | { |
| 707 | return create_simple_single_input_output_node<PrintLayerNode>(g, params, input, stream, format_info, transform); |
| 708 | } |
| 709 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 710 | NodeID GraphBuilder::add_priorbox_node( |
| 711 | Graph &g, NodeParams params, NodeIdxPair input0, NodeIdxPair input1, const PriorBoxLayerInfo &prior_info) |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 712 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 713 | check_nodeidx_pair(input0, g); |
| 714 | check_nodeidx_pair(input1, g); |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 715 | |
| 716 | // Create priorbox node and connect |
| 717 | NodeID prior_nid = g.add_node<PriorBoxLayerNode>(prior_info); |
| 718 | g.add_connection(input0.node_id, input0.index, prior_nid, 0); |
| 719 | g.add_connection(input1.node_id, input1.index, prior_nid, 1); |
| 720 | |
| 721 | set_node_params(g, prior_nid, params); |
| 722 | |
| 723 | return prior_nid; |
| 724 | } |
| 725 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 726 | NodeID GraphBuilder::add_quantization_node(Graph &g, |
| 727 | NodeParams params, |
| 728 | NodeIdxPair input, |
| 729 | const QuantizationInfo &out_quant_info) |
Isabella Gottardi | 3db1ba9 | 2019-05-17 12:35:20 +0100 | [diff] [blame] | 730 | { |
| 731 | return create_simple_single_input_output_node<QuantizationLayerNode>(g, params, input, out_quant_info); |
| 732 | } |
| 733 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 734 | NodeID GraphBuilder::add_reduction_operation_node( |
| 735 | Graph &g, NodeParams params, NodeIdxPair input, ReductionOperation op, int axis, bool keep_dims) |
thecha01 | d64444b | 2020-09-07 14:50:21 +0100 | [diff] [blame] | 736 | { |
| 737 | return create_simple_single_input_output_node<ReductionLayerNode>(g, params, input, op, axis, keep_dims); |
| 738 | } |
| 739 | |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 740 | NodeID GraphBuilder::add_reorg_node(Graph &g, NodeParams params, NodeIdxPair input, int stride) |
| 741 | { |
| 742 | return create_simple_single_input_output_node<ReorgLayerNode>(g, params, input, stride); |
| 743 | } |
| 744 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 745 | NodeID GraphBuilder::add_reshape_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape) |
| 746 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 747 | return create_simple_single_input_output_node<ReshapeLayerNode>(g, params, input, shape); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 750 | NodeID GraphBuilder::add_resize_node( |
| 751 | Graph &g, NodeParams params, NodeIdxPair input, InterpolationPolicy policy, float width_scale, float height_scale) |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 752 | { |
| 753 | return create_simple_single_input_output_node<ResizeLayerNode>(g, params, input, policy, width_scale, height_scale); |
| 754 | } |
| 755 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 756 | NodeID GraphBuilder::add_roi_align_node( |
| 757 | Graph &g, NodeParams params, NodeIdxPair input, NodeIdxPair rois, ROIPoolingLayerInfo pool_info) |
Manuel Bottini | 3f9d4d7 | 2018-10-19 14:04:42 +0100 | [diff] [blame] | 758 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 759 | check_nodeidx_pair(input, g); |
| 760 | check_nodeidx_pair(rois, g); |
Manuel Bottini | 3f9d4d7 | 2018-10-19 14:04:42 +0100 | [diff] [blame] | 761 | |
| 762 | NodeID nid = g.add_node<ROIAlignLayerNode>(pool_info); |
| 763 | |
| 764 | g.add_connection(input.node_id, input.index, nid, 0); |
| 765 | g.add_connection(rois.node_id, rois.index, nid, 1); |
| 766 | |
| 767 | set_node_params(g, nid, params); |
| 768 | return nid; |
| 769 | } |
| 770 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 771 | NodeID GraphBuilder::add_scale_layer(Graph &g, |
| 772 | const NodeParams ¶ms, |
| 773 | NodeIdxPair input, |
| 774 | ITensorAccessorUPtr mul_accessor, |
| 775 | ITensorAccessorUPtr add_accessor) |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 776 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 777 | check_nodeidx_pair(input, g); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 778 | |
| 779 | // Get input tensor descriptor |
| 780 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 781 | const DataLayout input_data_layout = input_tensor_desc.layout; |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 782 | |
| 783 | // Create mul node |
| 784 | TensorDescriptor mul_desc = input_tensor_desc; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 785 | const size_t C = input_tensor_desc.shape[get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL)]; |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 786 | mul_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::WIDTH), 1); |
| 787 | mul_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::HEIGHT), 1); |
| 788 | mul_desc.shape.set(get_dimension_idx(input_data_layout, DataLayoutDimension::CHANNEL), C); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 789 | NodeID mul_const_nid = add_const_node_with_name(g, params, "Mul", mul_desc, std::move(mul_accessor)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 790 | NodeIdxPair mul_const_nidxp = {mul_const_nid, 0}; |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 791 | |
| 792 | // Create add node |
| 793 | TensorDescriptor add_desc = mul_desc; |
| 794 | NodeID add_const_nid = add_const_node_with_name(g, params, "Add", add_desc, std::move(add_accessor)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 795 | NodeIdxPair add_const_nidxp = {add_const_nid, 0}; |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 796 | |
| 797 | // Create node and connect |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 798 | NodeID mul_node = GraphBuilder::add_elementwise_node(g, params, input, mul_const_nidxp, EltwiseOperation::Mul); |
| 799 | NodeIdxPair mulnode_nidxp = {mul_node, 0}; |
| 800 | NodeID add_node = |
| 801 | GraphBuilder::add_elementwise_node(g, params, mulnode_nidxp, add_const_nidxp, EltwiseOperation::Add); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 802 | |
| 803 | return add_node; |
| 804 | } |
| 805 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 806 | NodeID GraphBuilder::add_softmax_node(Graph &g, NodeParams params, NodeIdxPair input, float beta) |
| 807 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 808 | return create_simple_single_input_output_node<SoftmaxLayerNode>(g, params, input, beta); |
| 809 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 810 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 811 | NodeID |
| 812 | GraphBuilder::add_slice_node(Graph &g, NodeParams params, NodeIdxPair input, Coordinates &starts, Coordinates &ends) |
Michele Di Giorgio | c30b668 | 2018-09-12 17:44:08 +0100 | [diff] [blame] | 813 | { |
| 814 | return create_simple_single_input_output_node<SliceLayerNode>(g, params, input, starts, ends); |
| 815 | } |
| 816 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 817 | NodeID |
| 818 | GraphBuilder::add_split_node(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_splits, unsigned int axis) |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 819 | { |
| 820 | return create_simple_single_input_output_node<SplitLayerNode>(g, params, input, num_splits, axis); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 821 | } |
Michalis Spyrou | 96f6769 | 2018-09-13 11:39:28 +0100 | [diff] [blame] | 822 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 823 | NodeID GraphBuilder::add_strided_slice_node(Graph &g, |
| 824 | NodeParams params, |
| 825 | NodeIdxPair input, |
| 826 | Coordinates &starts, |
| 827 | Coordinates &ends, |
| 828 | BiStrides &strides, |
| 829 | StridedSliceLayerInfo info) |
thecha01 | 2bfadd9 | 2020-08-12 17:25:51 +0100 | [diff] [blame] | 830 | { |
| 831 | return create_simple_single_input_output_node<StridedSliceLayerNode>(g, params, input, starts, ends, strides, info); |
| 832 | } |
| 833 | |
Michele Di Giorgio | ec69975 | 2019-03-22 15:25:32 +0000 | [diff] [blame] | 834 | NodeID GraphBuilder::add_stack_node(Graph &g, NodeParams params, const std::vector<NodeIdxPair> &inputs, int axis) |
| 835 | { |
| 836 | return create_simple_multiple_input_single_output_node<StackLayerNode>(g, params, inputs, inputs.size(), axis); |
| 837 | } |
| 838 | |
Georgios Pinitas | 0b1c2db | 2020-12-04 15:51:34 +0000 | [diff] [blame] | 839 | NodeID GraphBuilder::add_yolo_node(Graph &g, NodeParams params, NodeIdxPair input, ActivationLayerInfo act_info) |
Michalis Spyrou | 96f6769 | 2018-09-13 11:39:28 +0100 | [diff] [blame] | 840 | { |
Georgios Pinitas | 0b1c2db | 2020-12-04 15:51:34 +0000 | [diff] [blame] | 841 | check_nodeidx_pair(input, g); |
| 842 | |
| 843 | // Get input tensor descriptor |
| 844 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 845 | const bool is_nhwc = input_tensor_desc.layout == DataLayout::NHWC; |
| 846 | |
| 847 | // Box format: [Objectness:1][Box:4][Classes:N] |
| 848 | |
| 849 | // Activate objectness and front part of the box |
| 850 | const Coordinates box_start(0, 0, 0); |
| 851 | const Coordinates box_end = is_nhwc ? Coordinates(3, -1, -1) : Coordinates(-1, -1, 3); |
| 852 | NodeID box = g.add_node<SliceLayerNode>(box_start, box_end); |
| 853 | NodeID act_box = g.add_node<ActivationLayerNode>(act_info); |
| 854 | set_node_params(g, box, params); |
| 855 | set_node_params(g, act_box, params); |
| 856 | g.add_connection(input.node_id, input.index, box, 0); |
| 857 | g.add_connection(box, 0, act_box, 0); |
| 858 | |
| 859 | // Immutable part |
| 860 | const Coordinates imm_start = is_nhwc ? Coordinates(3, 0, 0) : Coordinates(0, 0, 3); |
| 861 | const Coordinates imm_end = is_nhwc ? Coordinates(5, -1, -1) : Coordinates(-1, -1, 5); |
| 862 | NodeID imm = g.add_node<SliceLayerNode>(imm_start, imm_end); |
| 863 | set_node_params(g, imm, params); |
| 864 | g.add_connection(input.node_id, input.index, imm, 0); |
| 865 | |
| 866 | // Activation classes and end part of box |
| 867 | const Coordinates cls_start = is_nhwc ? Coordinates(5, 0, 0) : Coordinates(0, 0, 5); |
Georgios Pinitas | 2ae0860 | 2021-02-23 00:04:26 +0000 | [diff] [blame] | 868 | const Coordinates cls_end = Coordinates(-1, -1, -1); |
Georgios Pinitas | 0b1c2db | 2020-12-04 15:51:34 +0000 | [diff] [blame] | 869 | NodeID cls = g.add_node<SliceLayerNode>(cls_start, cls_end); |
| 870 | NodeID cls_act = g.add_node<ActivationLayerNode>(act_info); |
| 871 | set_node_params(g, cls, params); |
| 872 | set_node_params(g, cls_act, params); |
| 873 | g.add_connection(input.node_id, input.index, cls, 0); |
| 874 | g.add_connection(cls, 0, cls_act, 0); |
| 875 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 876 | NodeID concat = |
| 877 | g.add_node<ConcatenateLayerNode>(3, descriptors::ConcatLayerDescriptor(DataLayoutDimension::CHANNEL)); |
Georgios Pinitas | 0b1c2db | 2020-12-04 15:51:34 +0000 | [diff] [blame] | 878 | set_node_params(g, concat, params); |
| 879 | g.add_connection(act_box, 0, concat, 0); |
| 880 | g.add_connection(imm, 0, concat, 1); |
| 881 | g.add_connection(cls_act, 0, concat, 2); |
| 882 | |
| 883 | return concat; |
Michalis Spyrou | 96f6769 | 2018-09-13 11:39:28 +0100 | [diff] [blame] | 884 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 885 | } // namespace graph |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 886 | } // namespace arm_compute |