Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Georgios 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" |
| 28 | #include "arm_compute/graph/algorithms/BFS.h" |
| 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 | #define CHECK_NODEIDX_PAIR(pair, g) \ |
| 34 | 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())); |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 38 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 39 | { |
| 40 | namespace |
| 41 | { |
| 42 | Status set_node_params(Graph &g, NodeID nid, NodeParams ¶ms) |
| 43 | { |
| 44 | INode *node = g.node(nid); |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON(!node); |
| 46 | |
| 47 | node->set_common_node_parameters(params); |
| 48 | |
| 49 | return Status{}; |
| 50 | } |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 51 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 52 | Status set_accessor_on_node(Graph &g, NodeID nid, bool is_output, size_t idx, ITensorAccessorUPtr accessor) |
| 53 | { |
| 54 | INode *node = g.node(nid); |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON(!node); |
| 56 | |
| 57 | Tensor *tensor = is_output ? node->output(idx) : node->input(idx); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON(!tensor); |
| 59 | |
| 60 | tensor->set_accessor(std::move(accessor)); |
| 61 | |
| 62 | return Status{}; |
| 63 | } |
| 64 | |
| 65 | NodeID add_const_node_with_name(Graph &g, NodeParams params, const std::string &name, TensorDescriptor desc, ITensorAccessorUPtr accessor) |
| 66 | { |
| 67 | params.name = params.name.empty() ? "" : params.name + name; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 68 | auto nid = GraphBuilder::add_const_node(g, params, std::move(desc), std::move(accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 69 | set_node_params(g, nid, params); |
| 70 | return nid; |
| 71 | } |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 72 | |
| 73 | template <typename NT, typename... Args> |
| 74 | NodeID create_simple_single_input_output_node(Graph &g, NodeParams ¶ms, NodeIdxPair input, Args &&... args) |
| 75 | { |
| 76 | CHECK_NODEIDX_PAIR(input, g); |
| 77 | |
| 78 | NodeID nid = g.add_node<NT>(std::forward<Args>(args)...); |
| 79 | g.add_connection(input.node_id, input.index, nid, 0); |
| 80 | set_node_params(g, nid, params); |
| 81 | |
| 82 | return nid; |
| 83 | } |
| 84 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 85 | NodeID create_grouped_convolution(Graph &g, const NodeParams ¶ms, NodeIdxPair input, NodeID weights, NodeID bias, |
Giorgio Arena | 59631a1 | 2018-05-02 13:59:04 +0100 | [diff] [blame] | 86 | PadStrideInfo conv_info, ConvolutionMethod method, FastMathHint fast_math_hint, unsigned int num_groups) |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 87 | { |
| 88 | bool has_bias = (bias != EmptyNodeID); |
| 89 | |
| 90 | // Split input |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 91 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 92 | const unsigned int input_idx = get_dimension_idx(input_tensor_desc, DataLayoutDimension::CHANNEL); |
| 93 | NodeID input_split = GraphBuilder::add_split_node(g, params, input, num_groups, input_idx); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 94 | |
| 95 | // Split weights |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 96 | const TensorDescriptor weights_tensor_desc = get_tensor_descriptor(g, g.node(weights)->outputs()[0]); |
| 97 | const unsigned int batch_idx = get_dimension_idx(weights_tensor_desc, DataLayoutDimension::BATCHES); |
| 98 | NodeID weights_split = GraphBuilder::add_split_node(g, params, { weights, 0 }, num_groups, batch_idx); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 99 | |
| 100 | // Split bias |
| 101 | NodeID bias_split = EmptyNodeID; |
| 102 | if(has_bias) |
| 103 | { |
| 104 | // Split bias |
| 105 | bias_split = GraphBuilder::add_split_node(g, params, { bias, 0 }, num_groups, 0); |
| 106 | } |
| 107 | |
| 108 | std::vector<NodeIdxPair> convolution_outputs; |
| 109 | for(unsigned int i = 0; i < num_groups; ++i) |
| 110 | { |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 111 | NodeParams group_params = params; |
| 112 | NodeID conv_nid = g.add_node<ConvolutionLayerNode>(conv_info, method, fast_math_hint); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 113 | g.add_connection(input_split, i, conv_nid, 0); |
| 114 | g.add_connection(weights_split, i, conv_nid, 1); |
| 115 | if(has_bias) |
| 116 | { |
| 117 | g.add_connection(bias_split, i, conv_nid, 2); |
| 118 | } |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 119 | // Add group name |
| 120 | if(!group_params.name.empty()) |
| 121 | { |
| 122 | group_params.name.append("_g" + arm_compute::support::cpp11::to_string(i)); |
| 123 | } |
| 124 | set_node_params(g, conv_nid, group_params); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 125 | convolution_outputs.push_back({ conv_nid, 0 }); |
| 126 | } |
| 127 | |
| 128 | // Depth concatenate output |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 129 | return GraphBuilder::add_concatenate_node(g, params, convolution_outputs, DataLayoutDimension::CHANNEL); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 130 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 131 | } // namespace |
| 132 | |
| 133 | NodeID GraphBuilder::add_const_node(Graph &g, NodeParams params, TensorDescriptor desc, ITensorAccessorUPtr accessor) |
| 134 | { |
| 135 | auto nid = g.add_node<ConstNode>(desc); |
| 136 | set_node_params(g, nid, params); |
| 137 | set_accessor_on_node(g, nid, true, 0, std::move(accessor)); |
| 138 | return nid; |
| 139 | } |
| 140 | |
| 141 | NodeID GraphBuilder::add_input_node(Graph &g, NodeParams params, TensorDescriptor desc, ITensorAccessorUPtr accessor) |
| 142 | { |
| 143 | auto nid = g.add_node<InputNode>(desc); |
| 144 | set_node_params(g, nid, params); |
| 145 | set_accessor_on_node(g, nid, true, 0, std::move(accessor)); |
| 146 | return nid; |
| 147 | } |
| 148 | |
| 149 | NodeID GraphBuilder::add_output_node(Graph &g, NodeParams params, NodeIdxPair input, ITensorAccessorUPtr accessor) |
| 150 | { |
| 151 | CHECK_NODEIDX_PAIR(input, g); |
| 152 | |
| 153 | NodeID nid = g.add_node<OutputNode>(); |
| 154 | g.add_connection(input.node_id, input.index, nid, 0); |
| 155 | set_node_params(g, nid, params); |
| 156 | set_accessor_on_node(g, nid, false, 0, std::move(accessor)); |
| 157 | |
| 158 | return nid; |
| 159 | } |
| 160 | |
| 161 | NodeID GraphBuilder::add_activation_node(Graph &g, NodeParams params, NodeIdxPair input, ActivationLayerInfo act_info) |
| 162 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 163 | return create_simple_single_input_output_node<ActivationLayerNode>(g, params, input, act_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | NodeID GraphBuilder::add_batch_normalization_node(Graph &g, NodeParams params, NodeIdxPair input, float epsilon, |
| 167 | ITensorAccessorUPtr mean_accessor, ITensorAccessorUPtr var_accessor, |
| 168 | ITensorAccessorUPtr beta_accessor, ITensorAccessorUPtr gamma_accessor) |
| 169 | { |
| 170 | CHECK_NODEIDX_PAIR(input, g); |
| 171 | |
| 172 | bool has_beta = (beta_accessor != nullptr); |
| 173 | bool has_gamma = (gamma_accessor != nullptr); |
| 174 | |
| 175 | // Get input tensor descriptor |
| 176 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 177 | |
| 178 | // Calculate Common Descriptor |
| 179 | TensorDescriptor common_desc = input_tensor_desc; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 180 | common_desc.shape = TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 181 | |
| 182 | // Create mean and nodes |
| 183 | auto mean_nid = add_const_node_with_name(g, params, "Mean", common_desc, std::move(mean_accessor)); |
| 184 | auto var_nid = add_const_node_with_name(g, params, "Variance", common_desc, std::move(var_accessor)); |
| 185 | |
| 186 | // Create beta node |
| 187 | NodeID beta_nid = EmptyNodeID; |
| 188 | if(has_beta) |
| 189 | { |
| 190 | beta_nid = add_const_node_with_name(g, params, "Beta", common_desc, std::move(beta_accessor)); |
| 191 | } |
| 192 | |
| 193 | // Create gamma node |
| 194 | NodeID gamma_nid = EmptyNodeID; |
| 195 | if(has_gamma) |
| 196 | { |
| 197 | gamma_nid = add_const_node_with_name(g, params, "Gamma", common_desc, std::move(gamma_accessor)); |
| 198 | } |
| 199 | |
| 200 | // Create batch normalization node and add connections |
| 201 | NodeID batch_norm_nid = g.add_node<BatchNormalizationLayerNode>(epsilon); |
| 202 | g.add_connection(input.node_id, input.index, batch_norm_nid, 0); |
| 203 | g.add_connection(mean_nid, 0, batch_norm_nid, 1); |
| 204 | g.add_connection(var_nid, 0, batch_norm_nid, 2); |
| 205 | if(has_beta) |
| 206 | { |
| 207 | g.add_connection(beta_nid, 0, batch_norm_nid, 3); |
| 208 | } |
| 209 | if(has_gamma) |
| 210 | { |
| 211 | g.add_connection(gamma_nid, 0, batch_norm_nid, 4); |
| 212 | } |
| 213 | set_node_params(g, batch_norm_nid, params); |
| 214 | |
| 215 | return batch_norm_nid; |
| 216 | } |
| 217 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 218 | NodeID GraphBuilder::add_channel_shuffle_node(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_groups) |
| 219 | { |
| 220 | return create_simple_single_input_output_node<ChannelShuffleLayerNode>(g, params, input, num_groups); |
| 221 | } |
| 222 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 223 | NodeID GraphBuilder::add_convolution_node(Graph &g, NodeParams params, NodeIdxPair input, |
| 224 | Size2D kernel_spatial_extend, unsigned int depth, PadStrideInfo conv_info, |
Giorgio Arena | 59631a1 | 2018-05-02 13:59:04 +0100 | [diff] [blame] | 225 | unsigned int num_groups, ConvolutionMethod method, FastMathHint fast_math_hint, |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 226 | ITensorAccessorUPtr weights_accessor, ITensorAccessorUPtr bias_accessor, |
| 227 | const QuantizationInfo weights_quant_info, |
| 228 | const QuantizationInfo out_quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 229 | { |
| 230 | CHECK_NODEIDX_PAIR(input, g); |
| 231 | ARM_COMPUTE_ERROR_ON(depth == 0); |
| 232 | ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0)); |
| 233 | |
| 234 | bool has_bias = (bias_accessor != nullptr); |
| 235 | |
| 236 | // Get input tensor descriptor |
| 237 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 238 | |
| 239 | // Create weights node |
| 240 | TensorDescriptor w_desc = input_tensor_desc; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 241 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::WIDTH), kernel_spatial_extend.width); |
| 242 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height); |
| 243 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::CHANNEL), |
| 244 | get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL) / num_groups); |
| 245 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::BATCHES), depth); |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 246 | if(!weights_quant_info.empty()) |
| 247 | { |
| 248 | w_desc.quant_info = weights_quant_info; |
| 249 | } |
| 250 | |
| 251 | 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] | 252 | |
| 253 | // Create bias nodes |
| 254 | NodeID b_nid = EmptyNodeID; |
| 255 | if(has_bias) |
| 256 | { |
| 257 | TensorDescriptor b_desc = input_tensor_desc; |
| 258 | b_desc.shape = TensorShape(depth); |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 259 | if(is_data_type_quantized_asymmetric(input_tensor_desc.data_type)) |
| 260 | { |
| 261 | b_desc.data_type = DataType::S32; |
| 262 | } |
| 263 | 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] | 264 | } |
| 265 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 266 | if(num_groups == 1) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 267 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 268 | // Create convolution node and connect |
Giorgio Arena | 59631a1 | 2018-05-02 13:59:04 +0100 | [diff] [blame] | 269 | NodeID conv_nid = g.add_node<ConvolutionLayerNode>(conv_info, method, fast_math_hint, out_quant_info); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 270 | g.add_connection(input.node_id, input.index, conv_nid, 0); |
| 271 | g.add_connection(w_nid, 0, conv_nid, 1); |
| 272 | if(has_bias) |
| 273 | { |
| 274 | g.add_connection(b_nid, 0, conv_nid, 2); |
| 275 | } |
| 276 | set_node_params(g, conv_nid, params); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 277 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 278 | return conv_nid; |
| 279 | } |
| 280 | else |
| 281 | { |
Giorgio Arena | 59631a1 | 2018-05-02 13:59:04 +0100 | [diff] [blame] | 282 | return create_grouped_convolution(g, params, input, w_nid, b_nid, conv_info, method, fast_math_hint, num_groups); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 283 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 286 | NodeID GraphBuilder::add_deconvolution_node(Graph &g, NodeParams params, NodeIdxPair input, |
| 287 | Size2D kernel_spatial_extend, unsigned int depth, PadStrideInfo deconv_info, |
| 288 | Size2D inner_border, ITensorAccessorUPtr weights_accessor, |
| 289 | ITensorAccessorUPtr bias_accessor) |
| 290 | { |
| 291 | CHECK_NODEIDX_PAIR(input, g); |
| 292 | ARM_COMPUTE_ERROR_ON(depth == 0); |
| 293 | ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0)); |
| 294 | |
| 295 | bool has_bias = (bias_accessor != nullptr); |
| 296 | |
| 297 | // Get input tensor descriptor |
| 298 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 299 | |
| 300 | // Create weights node |
| 301 | TensorDescriptor w_desc = input_tensor_desc; |
| 302 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::WIDTH), kernel_spatial_extend.width); |
| 303 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height); |
| 304 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::CHANNEL), |
| 305 | get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
| 306 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::BATCHES), depth); |
| 307 | |
| 308 | NodeID w_nid = add_const_node_with_name(g, params, "Weights", w_desc, std::move(weights_accessor)); |
| 309 | |
| 310 | // Create bias nodes |
| 311 | NodeID b_nid = EmptyNodeID; |
| 312 | if(has_bias) |
| 313 | { |
| 314 | TensorDescriptor b_desc = input_tensor_desc; |
| 315 | b_desc.shape = TensorShape(depth); |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 316 | if(is_data_type_quantized_asymmetric(input_tensor_desc.data_type)) |
| 317 | { |
| 318 | b_desc.data_type = DataType::S32; |
| 319 | } |
| 320 | 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] | 321 | } |
| 322 | |
| 323 | // Create convolution node and connect |
| 324 | NodeID deconv_nid = g.add_node<DeconvolutionLayerNode>(deconv_info, inner_border); |
| 325 | g.add_connection(input.node_id, input.index, deconv_nid, 0); |
| 326 | g.add_connection(w_nid, 0, deconv_nid, 1); |
| 327 | if(has_bias) |
| 328 | { |
| 329 | g.add_connection(b_nid, 0, deconv_nid, 2); |
| 330 | } |
| 331 | set_node_params(g, deconv_nid, params); |
| 332 | |
| 333 | return deconv_nid; |
| 334 | } |
| 335 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 336 | NodeID GraphBuilder::add_concatenate_node(Graph &g, NodeParams params, std::vector<NodeIdxPair> inputs, DataLayoutDimension axis) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 337 | { |
| 338 | ARM_COMPUTE_ERROR_ON(inputs.size() == 0); |
| 339 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 340 | NodeID nid = g.add_node<ConcatenateLayerNode>(inputs.size(), axis); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 341 | |
| 342 | unsigned int i = 0; |
| 343 | for(const auto &input : inputs) |
| 344 | { |
| 345 | CHECK_NODEIDX_PAIR(input, g); |
| 346 | g.add_connection(input.node_id, input.index, nid, i++); |
| 347 | } |
| 348 | set_node_params(g, nid, params); |
| 349 | |
| 350 | return nid; |
| 351 | } |
| 352 | |
| 353 | NodeID GraphBuilder::add_depthwise_convolution_node(Graph &g, NodeParams params, NodeIdxPair input, Size2D kernel_spatial_extend, PadStrideInfo conv_info, |
| 354 | DepthwiseConvolutionMethod method, |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 355 | ITensorAccessorUPtr weights_accessor, ITensorAccessorUPtr bias_accessor, const QuantizationInfo quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 356 | { |
| 357 | CHECK_NODEIDX_PAIR(input, g); |
| 358 | ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0)); |
| 359 | |
| 360 | bool has_bias = (bias_accessor != nullptr); |
| 361 | |
| 362 | // Get input tensor descriptor |
| 363 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 364 | |
| 365 | // Create weights node |
| 366 | TensorDescriptor w_desc = input_tensor_desc; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 367 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::WIDTH), kernel_spatial_extend.width); |
| 368 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height); |
| 369 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::CHANNEL), |
| 370 | get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 371 | if(!quant_info.empty()) |
| 372 | { |
| 373 | w_desc.quant_info = quant_info; |
| 374 | } |
| 375 | |
| 376 | 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] | 377 | |
| 378 | // Create bias nodes |
| 379 | NodeID b_nid = EmptyNodeID; |
| 380 | if(has_bias) |
| 381 | { |
| 382 | TensorDescriptor b_desc = input_tensor_desc; |
Gian Marco Iodice | dff601d | 2018-08-09 13:28:41 +0100 | [diff] [blame] | 383 | b_desc.shape = TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 384 | b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor)); |
| 385 | } |
| 386 | |
| 387 | // Create convolution node and connect |
| 388 | NodeID conv_nid = g.add_node<DepthwiseConvolutionLayerNode>(conv_info, method); |
| 389 | g.add_connection(input.node_id, input.index, conv_nid, 0); |
| 390 | g.add_connection(w_nid, 0, conv_nid, 1); |
| 391 | if(has_bias) |
| 392 | { |
| 393 | g.add_connection(b_nid, 0, conv_nid, 2); |
| 394 | } |
| 395 | set_node_params(g, conv_nid, params); |
| 396 | |
| 397 | return conv_nid; |
| 398 | } |
| 399 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 400 | NodeID GraphBuilder::add_dummy_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape) |
| 401 | { |
Georgios Pinitas | b7a2023 | 2018-07-02 16:12:54 +0100 | [diff] [blame] | 402 | return create_simple_single_input_output_node<DummyNode>(g, params, input, shape); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 403 | } |
| 404 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 405 | NodeID GraphBuilder::add_elementwise_node(Graph &g, NodeParams params, NodeIdxPair input0, NodeIdxPair input1, EltwiseOperation operation) |
| 406 | { |
| 407 | CHECK_NODEIDX_PAIR(input0, g); |
| 408 | CHECK_NODEIDX_PAIR(input1, g); |
| 409 | |
| 410 | NodeID nid = g.add_node<EltwiseLayerNode>(operation); |
| 411 | |
| 412 | g.add_connection(input0.node_id, input0.index, nid, 0); |
| 413 | g.add_connection(input1.node_id, input1.index, nid, 1); |
| 414 | |
| 415 | set_node_params(g, nid, params); |
| 416 | |
| 417 | return nid; |
| 418 | } |
| 419 | |
| 420 | NodeID GraphBuilder::add_flatten_node(Graph &g, NodeParams params, NodeIdxPair input) |
| 421 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 422 | return create_simple_single_input_output_node<FlattenLayerNode>(g, params, input); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | 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] | 426 | ITensorAccessorUPtr weights_accessor, ITensorAccessorUPtr bias_accessor, |
Georgios Pinitas | c55cef1 | 2018-08-01 15:24:18 +0100 | [diff] [blame] | 427 | const FullyConnectedLayerInfo fc_info, |
Georgios Pinitas | 2f1366a | 2018-07-31 16:33:06 +0100 | [diff] [blame] | 428 | const QuantizationInfo weights_quant_info, const QuantizationInfo out_quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 429 | { |
| 430 | CHECK_NODEIDX_PAIR(input, g); |
| 431 | ARM_COMPUTE_ERROR_ON(num_outputs == 0); |
| 432 | |
| 433 | bool has_bias = (bias_accessor != nullptr); |
| 434 | |
| 435 | // Get input tensor descriptor |
| 436 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 437 | |
| 438 | // Create weights node |
Georgios Pinitas | 195b0ba | 2018-08-02 17:18:51 +0100 | [diff] [blame] | 439 | 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] | 440 | 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] | 441 | |
| 442 | // Create bias nodes |
| 443 | NodeID b_nid = EmptyNodeID; |
| 444 | if(has_bias) |
| 445 | { |
| 446 | TensorDescriptor b_desc = input_tensor_desc; |
| 447 | b_desc.shape = TensorShape(num_outputs); |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 448 | if(is_data_type_quantized_asymmetric(input_tensor_desc.data_type)) |
| 449 | { |
| 450 | b_desc.data_type = DataType::S32; |
| 451 | } |
| 452 | 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] | 453 | } |
| 454 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 455 | // Create fully connected node and connect |
Georgios Pinitas | 2f1366a | 2018-07-31 16:33:06 +0100 | [diff] [blame] | 456 | 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] | 457 | g.add_connection(input.node_id, input.index, fc_nid, 0); |
| 458 | g.add_connection(w_nid, 0, fc_nid, 1); |
| 459 | if(has_bias) |
| 460 | { |
| 461 | g.add_connection(b_nid, 0, fc_nid, 2); |
| 462 | } |
| 463 | |
| 464 | set_node_params(g, fc_nid, params); |
| 465 | |
| 466 | return fc_nid; |
| 467 | } |
| 468 | |
| 469 | NodeID GraphBuilder::add_normalization_node(Graph &g, NodeParams params, NodeIdxPair input, NormalizationLayerInfo norm_info) |
| 470 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 471 | 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] | 472 | } |
| 473 | |
Georgios Pinitas | 57c4824 | 2018-08-02 13:41:49 +0100 | [diff] [blame] | 474 | NodeID GraphBuilder::add_permute_node(Graph &g, NodeParams params, NodeIdxPair input, PermutationVector perm, DataLayout layout) |
| 475 | { |
| 476 | return create_simple_single_input_output_node<PermuteLayerNode>(g, params, input, perm, layout); |
| 477 | } |
| 478 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 479 | NodeID GraphBuilder::add_pooling_node(Graph &g, NodeParams params, NodeIdxPair input, PoolingLayerInfo pool_info) |
| 480 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 481 | 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] | 482 | } |
| 483 | |
| 484 | NodeID GraphBuilder::add_reshape_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape) |
| 485 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 486 | return create_simple_single_input_output_node<ReshapeLayerNode>(g, params, input, shape); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 489 | NodeID GraphBuilder::add_resize_node(Graph &g, NodeParams params, NodeIdxPair input, InterpolationPolicy policy, |
| 490 | float width_scale, float height_scale) |
| 491 | { |
| 492 | return create_simple_single_input_output_node<ResizeLayerNode>(g, params, input, policy, width_scale, height_scale); |
| 493 | } |
| 494 | |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 495 | NodeID GraphBuilder::add_scale_layer(Graph &g, const NodeParams ¶ms, NodeIdxPair input, ITensorAccessorUPtr mul_accessor, ITensorAccessorUPtr add_accessor) |
| 496 | { |
| 497 | CHECK_NODEIDX_PAIR(input, g); |
| 498 | |
| 499 | // Get input tensor descriptor |
| 500 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 501 | |
| 502 | // Create mul node |
| 503 | TensorDescriptor mul_desc = input_tensor_desc; |
| 504 | const size_t C = input_tensor_desc.shape[get_dimension_idx(mul_desc, DataLayoutDimension::CHANNEL)]; |
| 505 | mul_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::WIDTH), 1); |
| 506 | mul_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::HEIGHT), 1); |
| 507 | mul_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::CHANNEL), C); |
| 508 | NodeID mul_const_nid = add_const_node_with_name(g, params, "Mul", mul_desc, std::move(mul_accessor)); |
| 509 | NodeIdxPair mul_const_nidxp = { mul_const_nid, 0 }; |
| 510 | |
| 511 | // Create add node |
| 512 | TensorDescriptor add_desc = mul_desc; |
| 513 | NodeID add_const_nid = add_const_node_with_name(g, params, "Add", add_desc, std::move(add_accessor)); |
| 514 | NodeIdxPair add_const_nidxp = { add_const_nid, 0 }; |
| 515 | |
| 516 | // Create node and connect |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 517 | 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] | 518 | NodeIdxPair mulnode_nidxp = { mul_node, 0 }; |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 519 | 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] | 520 | |
| 521 | return add_node; |
| 522 | } |
| 523 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 524 | NodeID GraphBuilder::add_softmax_node(Graph &g, NodeParams params, NodeIdxPair input, float beta) |
| 525 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 526 | return create_simple_single_input_output_node<SoftmaxLayerNode>(g, params, input, beta); |
| 527 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 528 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 529 | NodeID GraphBuilder::add_split_node(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_splits, unsigned int axis) |
| 530 | { |
| 531 | 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] | 532 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 533 | } // namespace graph |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 534 | } // namespace arm_compute |