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" |
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 | #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 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 84 | } // namespace |
| 85 | |
| 86 | NodeID GraphBuilder::add_const_node(Graph &g, NodeParams params, TensorDescriptor desc, ITensorAccessorUPtr accessor) |
| 87 | { |
| 88 | auto nid = g.add_node<ConstNode>(desc); |
| 89 | set_node_params(g, nid, params); |
| 90 | set_accessor_on_node(g, nid, true, 0, std::move(accessor)); |
| 91 | return nid; |
| 92 | } |
| 93 | |
| 94 | NodeID GraphBuilder::add_input_node(Graph &g, NodeParams params, TensorDescriptor desc, ITensorAccessorUPtr accessor) |
| 95 | { |
| 96 | auto nid = g.add_node<InputNode>(desc); |
| 97 | set_node_params(g, nid, params); |
| 98 | set_accessor_on_node(g, nid, true, 0, std::move(accessor)); |
| 99 | return nid; |
| 100 | } |
| 101 | |
| 102 | NodeID GraphBuilder::add_output_node(Graph &g, NodeParams params, NodeIdxPair input, ITensorAccessorUPtr accessor) |
| 103 | { |
| 104 | CHECK_NODEIDX_PAIR(input, g); |
| 105 | |
| 106 | NodeID nid = g.add_node<OutputNode>(); |
| 107 | g.add_connection(input.node_id, input.index, nid, 0); |
| 108 | set_node_params(g, nid, params); |
| 109 | set_accessor_on_node(g, nid, false, 0, std::move(accessor)); |
| 110 | |
| 111 | return nid; |
| 112 | } |
| 113 | |
| 114 | NodeID GraphBuilder::add_activation_node(Graph &g, NodeParams params, NodeIdxPair input, ActivationLayerInfo act_info) |
| 115 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 116 | 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] | 117 | } |
| 118 | |
| 119 | NodeID GraphBuilder::add_batch_normalization_node(Graph &g, NodeParams params, NodeIdxPair input, float epsilon, |
| 120 | ITensorAccessorUPtr mean_accessor, ITensorAccessorUPtr var_accessor, |
| 121 | ITensorAccessorUPtr beta_accessor, ITensorAccessorUPtr gamma_accessor) |
| 122 | { |
| 123 | CHECK_NODEIDX_PAIR(input, g); |
| 124 | |
| 125 | bool has_beta = (beta_accessor != nullptr); |
| 126 | bool has_gamma = (gamma_accessor != nullptr); |
| 127 | |
| 128 | // Get input tensor descriptor |
| 129 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 130 | |
| 131 | // Calculate Common Descriptor |
| 132 | TensorDescriptor common_desc = input_tensor_desc; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 133 | common_desc.shape = TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 134 | |
| 135 | // Create mean and nodes |
| 136 | auto mean_nid = add_const_node_with_name(g, params, "Mean", common_desc, std::move(mean_accessor)); |
| 137 | auto var_nid = add_const_node_with_name(g, params, "Variance", common_desc, std::move(var_accessor)); |
| 138 | |
| 139 | // Create beta node |
| 140 | NodeID beta_nid = EmptyNodeID; |
| 141 | if(has_beta) |
| 142 | { |
| 143 | beta_nid = add_const_node_with_name(g, params, "Beta", common_desc, std::move(beta_accessor)); |
| 144 | } |
| 145 | |
| 146 | // Create gamma node |
| 147 | NodeID gamma_nid = EmptyNodeID; |
| 148 | if(has_gamma) |
| 149 | { |
| 150 | gamma_nid = add_const_node_with_name(g, params, "Gamma", common_desc, std::move(gamma_accessor)); |
| 151 | } |
| 152 | |
| 153 | // Create batch normalization node and add connections |
| 154 | NodeID batch_norm_nid = g.add_node<BatchNormalizationLayerNode>(epsilon); |
| 155 | g.add_connection(input.node_id, input.index, batch_norm_nid, 0); |
| 156 | g.add_connection(mean_nid, 0, batch_norm_nid, 1); |
| 157 | g.add_connection(var_nid, 0, batch_norm_nid, 2); |
| 158 | if(has_beta) |
| 159 | { |
| 160 | g.add_connection(beta_nid, 0, batch_norm_nid, 3); |
| 161 | } |
| 162 | if(has_gamma) |
| 163 | { |
| 164 | g.add_connection(gamma_nid, 0, batch_norm_nid, 4); |
| 165 | } |
| 166 | set_node_params(g, batch_norm_nid, params); |
| 167 | |
| 168 | return batch_norm_nid; |
| 169 | } |
| 170 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 171 | NodeID GraphBuilder::add_channel_shuffle_node(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_groups) |
| 172 | { |
| 173 | return create_simple_single_input_output_node<ChannelShuffleLayerNode>(g, params, input, num_groups); |
| 174 | } |
| 175 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 176 | NodeID GraphBuilder::add_convolution_node(Graph &g, NodeParams params, NodeIdxPair input, |
| 177 | Size2D kernel_spatial_extend, unsigned int depth, PadStrideInfo conv_info, |
Giorgio Arena | 59631a1 | 2018-05-02 13:59:04 +0100 | [diff] [blame] | 178 | unsigned int num_groups, ConvolutionMethod method, FastMathHint fast_math_hint, |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 179 | ITensorAccessorUPtr weights_accessor, ITensorAccessorUPtr bias_accessor, |
| 180 | const QuantizationInfo weights_quant_info, |
| 181 | const QuantizationInfo out_quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 182 | { |
| 183 | CHECK_NODEIDX_PAIR(input, g); |
| 184 | ARM_COMPUTE_ERROR_ON(depth == 0); |
| 185 | ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0)); |
| 186 | |
| 187 | bool has_bias = (bias_accessor != nullptr); |
| 188 | |
| 189 | // Get input tensor descriptor |
| 190 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 191 | |
| 192 | // Create weights node |
| 193 | TensorDescriptor w_desc = input_tensor_desc; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 194 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::WIDTH), kernel_spatial_extend.width); |
| 195 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height); |
| 196 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::CHANNEL), |
| 197 | get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL) / num_groups); |
| 198 | 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] | 199 | if(!weights_quant_info.empty()) |
| 200 | { |
| 201 | w_desc.quant_info = weights_quant_info; |
| 202 | } |
| 203 | |
| 204 | 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] | 205 | |
| 206 | // Create bias nodes |
| 207 | NodeID b_nid = EmptyNodeID; |
| 208 | if(has_bias) |
| 209 | { |
| 210 | TensorDescriptor b_desc = input_tensor_desc; |
| 211 | b_desc.shape = TensorShape(depth); |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 212 | if(is_data_type_quantized_asymmetric(input_tensor_desc.data_type)) |
| 213 | { |
| 214 | b_desc.data_type = DataType::S32; |
| 215 | } |
| 216 | 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] | 217 | } |
| 218 | |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame^] | 219 | // Create convolution node and connect |
| 220 | NodeID conv_nid = g.add_node<ConvolutionLayerNode>(conv_info, num_groups, method, fast_math_hint, out_quant_info); |
| 221 | g.add_connection(input.node_id, input.index, conv_nid, 0); |
| 222 | g.add_connection(w_nid, 0, conv_nid, 1); |
| 223 | if(has_bias) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 224 | { |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame^] | 225 | g.add_connection(b_nid, 0, conv_nid, 2); |
| 226 | } |
| 227 | set_node_params(g, conv_nid, params); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 228 | |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame^] | 229 | return conv_nid; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 232 | NodeID GraphBuilder::add_deconvolution_node(Graph &g, NodeParams params, NodeIdxPair input, |
| 233 | Size2D kernel_spatial_extend, unsigned int depth, PadStrideInfo deconv_info, |
| 234 | Size2D inner_border, ITensorAccessorUPtr weights_accessor, |
| 235 | ITensorAccessorUPtr bias_accessor) |
| 236 | { |
| 237 | CHECK_NODEIDX_PAIR(input, g); |
| 238 | ARM_COMPUTE_ERROR_ON(depth == 0); |
| 239 | ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0)); |
| 240 | |
| 241 | bool has_bias = (bias_accessor != nullptr); |
| 242 | |
| 243 | // Get input tensor descriptor |
| 244 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 245 | |
| 246 | // Create weights node |
| 247 | TensorDescriptor w_desc = input_tensor_desc; |
| 248 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::WIDTH), kernel_spatial_extend.width); |
| 249 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height); |
| 250 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::CHANNEL), |
| 251 | get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
| 252 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::BATCHES), depth); |
| 253 | |
| 254 | NodeID w_nid = add_const_node_with_name(g, params, "Weights", w_desc, std::move(weights_accessor)); |
| 255 | |
| 256 | // Create bias nodes |
| 257 | NodeID b_nid = EmptyNodeID; |
| 258 | if(has_bias) |
| 259 | { |
| 260 | TensorDescriptor b_desc = input_tensor_desc; |
| 261 | b_desc.shape = TensorShape(depth); |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 262 | if(is_data_type_quantized_asymmetric(input_tensor_desc.data_type)) |
| 263 | { |
| 264 | b_desc.data_type = DataType::S32; |
| 265 | } |
| 266 | 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] | 267 | } |
| 268 | |
| 269 | // Create convolution node and connect |
| 270 | NodeID deconv_nid = g.add_node<DeconvolutionLayerNode>(deconv_info, inner_border); |
| 271 | g.add_connection(input.node_id, input.index, deconv_nid, 0); |
| 272 | g.add_connection(w_nid, 0, deconv_nid, 1); |
| 273 | if(has_bias) |
| 274 | { |
| 275 | g.add_connection(b_nid, 0, deconv_nid, 2); |
| 276 | } |
| 277 | set_node_params(g, deconv_nid, params); |
| 278 | |
| 279 | return deconv_nid; |
| 280 | } |
| 281 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 282 | 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] | 283 | { |
| 284 | ARM_COMPUTE_ERROR_ON(inputs.size() == 0); |
| 285 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 286 | NodeID nid = g.add_node<ConcatenateLayerNode>(inputs.size(), axis); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 287 | |
| 288 | unsigned int i = 0; |
| 289 | for(const auto &input : inputs) |
| 290 | { |
| 291 | CHECK_NODEIDX_PAIR(input, g); |
| 292 | g.add_connection(input.node_id, input.index, nid, i++); |
| 293 | } |
| 294 | set_node_params(g, nid, params); |
| 295 | |
| 296 | return nid; |
| 297 | } |
| 298 | |
| 299 | NodeID GraphBuilder::add_depthwise_convolution_node(Graph &g, NodeParams params, NodeIdxPair input, Size2D kernel_spatial_extend, PadStrideInfo conv_info, |
| 300 | DepthwiseConvolutionMethod method, |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 301 | ITensorAccessorUPtr weights_accessor, ITensorAccessorUPtr bias_accessor, const QuantizationInfo quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 302 | { |
| 303 | CHECK_NODEIDX_PAIR(input, g); |
| 304 | ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0)); |
| 305 | |
| 306 | bool has_bias = (bias_accessor != nullptr); |
| 307 | |
| 308 | // Get input tensor descriptor |
| 309 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 310 | |
| 311 | // Create weights node |
| 312 | TensorDescriptor w_desc = input_tensor_desc; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 313 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::WIDTH), kernel_spatial_extend.width); |
| 314 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height); |
| 315 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::CHANNEL), |
| 316 | get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 317 | if(!quant_info.empty()) |
| 318 | { |
| 319 | w_desc.quant_info = quant_info; |
| 320 | } |
| 321 | |
| 322 | 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] | 323 | |
| 324 | // Create bias nodes |
| 325 | NodeID b_nid = EmptyNodeID; |
| 326 | if(has_bias) |
| 327 | { |
| 328 | TensorDescriptor b_desc = input_tensor_desc; |
Gian Marco Iodice | dff601d | 2018-08-09 13:28:41 +0100 | [diff] [blame] | 329 | b_desc.shape = TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 330 | b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor)); |
| 331 | } |
| 332 | |
| 333 | // Create convolution node and connect |
| 334 | NodeID conv_nid = g.add_node<DepthwiseConvolutionLayerNode>(conv_info, method); |
| 335 | g.add_connection(input.node_id, input.index, conv_nid, 0); |
| 336 | g.add_connection(w_nid, 0, conv_nid, 1); |
| 337 | if(has_bias) |
| 338 | { |
| 339 | g.add_connection(b_nid, 0, conv_nid, 2); |
| 340 | } |
| 341 | set_node_params(g, conv_nid, params); |
| 342 | |
| 343 | return conv_nid; |
| 344 | } |
| 345 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 346 | NodeID GraphBuilder::add_dummy_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape) |
| 347 | { |
Georgios Pinitas | b7a2023 | 2018-07-02 16:12:54 +0100 | [diff] [blame] | 348 | return create_simple_single_input_output_node<DummyNode>(g, params, input, shape); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 351 | NodeID GraphBuilder::add_elementwise_node(Graph &g, NodeParams params, NodeIdxPair input0, NodeIdxPair input1, EltwiseOperation operation) |
| 352 | { |
| 353 | CHECK_NODEIDX_PAIR(input0, g); |
| 354 | CHECK_NODEIDX_PAIR(input1, g); |
| 355 | |
| 356 | NodeID nid = g.add_node<EltwiseLayerNode>(operation); |
| 357 | |
| 358 | g.add_connection(input0.node_id, input0.index, nid, 0); |
| 359 | g.add_connection(input1.node_id, input1.index, nid, 1); |
| 360 | |
| 361 | set_node_params(g, nid, params); |
| 362 | |
| 363 | return nid; |
| 364 | } |
| 365 | |
| 366 | NodeID GraphBuilder::add_flatten_node(Graph &g, NodeParams params, NodeIdxPair input) |
| 367 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 368 | return create_simple_single_input_output_node<FlattenLayerNode>(g, params, input); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | 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] | 372 | ITensorAccessorUPtr weights_accessor, ITensorAccessorUPtr bias_accessor, |
Georgios Pinitas | c55cef1 | 2018-08-01 15:24:18 +0100 | [diff] [blame] | 373 | const FullyConnectedLayerInfo fc_info, |
Georgios Pinitas | 2f1366a | 2018-07-31 16:33:06 +0100 | [diff] [blame] | 374 | const QuantizationInfo weights_quant_info, const QuantizationInfo out_quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 375 | { |
| 376 | CHECK_NODEIDX_PAIR(input, g); |
| 377 | ARM_COMPUTE_ERROR_ON(num_outputs == 0); |
| 378 | |
| 379 | bool has_bias = (bias_accessor != nullptr); |
| 380 | |
| 381 | // Get input tensor descriptor |
| 382 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 383 | |
| 384 | // Create weights node |
Georgios Pinitas | 195b0ba | 2018-08-02 17:18:51 +0100 | [diff] [blame] | 385 | 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] | 386 | 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] | 387 | |
| 388 | // Create bias nodes |
| 389 | NodeID b_nid = EmptyNodeID; |
| 390 | if(has_bias) |
| 391 | { |
| 392 | TensorDescriptor b_desc = input_tensor_desc; |
| 393 | b_desc.shape = TensorShape(num_outputs); |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 394 | if(is_data_type_quantized_asymmetric(input_tensor_desc.data_type)) |
| 395 | { |
| 396 | b_desc.data_type = DataType::S32; |
| 397 | } |
| 398 | 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] | 399 | } |
| 400 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 401 | // Create fully connected node and connect |
Georgios Pinitas | 2f1366a | 2018-07-31 16:33:06 +0100 | [diff] [blame] | 402 | 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] | 403 | g.add_connection(input.node_id, input.index, fc_nid, 0); |
| 404 | g.add_connection(w_nid, 0, fc_nid, 1); |
| 405 | if(has_bias) |
| 406 | { |
| 407 | g.add_connection(b_nid, 0, fc_nid, 2); |
| 408 | } |
| 409 | |
| 410 | set_node_params(g, fc_nid, params); |
| 411 | |
| 412 | return fc_nid; |
| 413 | } |
| 414 | |
| 415 | NodeID GraphBuilder::add_normalization_node(Graph &g, NodeParams params, NodeIdxPair input, NormalizationLayerInfo norm_info) |
| 416 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 417 | 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] | 418 | } |
| 419 | |
Georgios Pinitas | 57c4824 | 2018-08-02 13:41:49 +0100 | [diff] [blame] | 420 | NodeID GraphBuilder::add_permute_node(Graph &g, NodeParams params, NodeIdxPair input, PermutationVector perm, DataLayout layout) |
| 421 | { |
| 422 | return create_simple_single_input_output_node<PermuteLayerNode>(g, params, input, perm, layout); |
| 423 | } |
| 424 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 425 | NodeID GraphBuilder::add_pooling_node(Graph &g, NodeParams params, NodeIdxPair input, PoolingLayerInfo pool_info) |
| 426 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 427 | 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] | 428 | } |
| 429 | |
| 430 | NodeID GraphBuilder::add_reshape_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape) |
| 431 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 432 | return create_simple_single_input_output_node<ReshapeLayerNode>(g, params, input, shape); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 435 | NodeID GraphBuilder::add_resize_node(Graph &g, NodeParams params, NodeIdxPair input, InterpolationPolicy policy, |
| 436 | float width_scale, float height_scale) |
| 437 | { |
| 438 | return create_simple_single_input_output_node<ResizeLayerNode>(g, params, input, policy, width_scale, height_scale); |
| 439 | } |
| 440 | |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 441 | NodeID GraphBuilder::add_scale_layer(Graph &g, const NodeParams ¶ms, NodeIdxPair input, ITensorAccessorUPtr mul_accessor, ITensorAccessorUPtr add_accessor) |
| 442 | { |
| 443 | CHECK_NODEIDX_PAIR(input, g); |
| 444 | |
| 445 | // Get input tensor descriptor |
| 446 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 447 | |
| 448 | // Create mul node |
| 449 | TensorDescriptor mul_desc = input_tensor_desc; |
| 450 | const size_t C = input_tensor_desc.shape[get_dimension_idx(mul_desc, DataLayoutDimension::CHANNEL)]; |
| 451 | mul_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::WIDTH), 1); |
| 452 | mul_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::HEIGHT), 1); |
| 453 | mul_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::CHANNEL), C); |
| 454 | NodeID mul_const_nid = add_const_node_with_name(g, params, "Mul", mul_desc, std::move(mul_accessor)); |
| 455 | NodeIdxPair mul_const_nidxp = { mul_const_nid, 0 }; |
| 456 | |
| 457 | // Create add node |
| 458 | TensorDescriptor add_desc = mul_desc; |
| 459 | NodeID add_const_nid = add_const_node_with_name(g, params, "Add", add_desc, std::move(add_accessor)); |
| 460 | NodeIdxPair add_const_nidxp = { add_const_nid, 0 }; |
| 461 | |
| 462 | // Create node and connect |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 463 | 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] | 464 | NodeIdxPair mulnode_nidxp = { mul_node, 0 }; |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 465 | 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] | 466 | |
| 467 | return add_node; |
| 468 | } |
| 469 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 470 | NodeID GraphBuilder::add_softmax_node(Graph &g, NodeParams params, NodeIdxPair input, float beta) |
| 471 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 472 | return create_simple_single_input_output_node<SoftmaxLayerNode>(g, params, input, beta); |
| 473 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 474 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 475 | NodeID GraphBuilder::add_split_node(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_splits, unsigned int axis) |
| 476 | { |
| 477 | 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] | 478 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 479 | } // namespace graph |
Michele Di Giorgio | 3a3b431 | 2018-07-06 12:34:19 +0100 | [diff] [blame] | 480 | } // namespace arm_compute |