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