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 | |
| 31 | #define CHECK_NODEIDX_PAIR(pair, g) \ |
| 32 | 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())); |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 36 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 37 | { |
| 38 | namespace |
| 39 | { |
| 40 | Status set_node_params(Graph &g, NodeID nid, NodeParams ¶ms) |
| 41 | { |
| 42 | INode *node = g.node(nid); |
| 43 | ARM_COMPUTE_RETURN_ERROR_ON(!node); |
| 44 | |
| 45 | node->set_common_node_parameters(params); |
| 46 | |
| 47 | return Status{}; |
| 48 | } |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 49 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 50 | Status set_accessor_on_node(Graph &g, NodeID nid, bool is_output, size_t idx, ITensorAccessorUPtr accessor) |
| 51 | { |
| 52 | INode *node = g.node(nid); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(!node); |
| 54 | |
| 55 | Tensor *tensor = is_output ? node->output(idx) : node->input(idx); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(!tensor); |
| 57 | |
| 58 | tensor->set_accessor(std::move(accessor)); |
| 59 | |
| 60 | return Status{}; |
| 61 | } |
| 62 | |
| 63 | NodeID add_const_node_with_name(Graph &g, NodeParams params, const std::string &name, TensorDescriptor desc, ITensorAccessorUPtr accessor) |
| 64 | { |
| 65 | params.name = params.name.empty() ? "" : params.name + name; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 66 | 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] | 67 | set_node_params(g, nid, params); |
| 68 | return nid; |
| 69 | } |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 70 | |
| 71 | template <typename NT, typename... Args> |
| 72 | NodeID create_simple_single_input_output_node(Graph &g, NodeParams ¶ms, NodeIdxPair input, Args &&... args) |
| 73 | { |
| 74 | CHECK_NODEIDX_PAIR(input, g); |
| 75 | |
| 76 | NodeID nid = g.add_node<NT>(std::forward<Args>(args)...); |
| 77 | g.add_connection(input.node_id, input.index, nid, 0); |
| 78 | set_node_params(g, nid, params); |
| 79 | |
| 80 | return nid; |
| 81 | } |
| 82 | |
| 83 | NodeID create_grouped_convolution(Graph &g, NodeParams ¶ms, NodeIdxPair input, NodeID weights, NodeID bias, |
| 84 | PadStrideInfo conv_info, ConvolutionMethod method, unsigned int num_groups) |
| 85 | { |
| 86 | bool has_bias = (bias != EmptyNodeID); |
| 87 | |
| 88 | // Split input |
| 89 | NodeID input_split = GraphBuilder::add_split_node(g, params, input, num_groups, 2); |
| 90 | |
| 91 | // Split weights |
| 92 | NodeID weights_split = GraphBuilder::add_split_node(g, params, { weights, 0 }, num_groups, 3); |
| 93 | |
| 94 | // Split bias |
| 95 | NodeID bias_split = EmptyNodeID; |
| 96 | if(has_bias) |
| 97 | { |
| 98 | // Split bias |
| 99 | bias_split = GraphBuilder::add_split_node(g, params, { bias, 0 }, num_groups, 0); |
| 100 | } |
| 101 | |
| 102 | std::vector<NodeIdxPair> convolution_outputs; |
| 103 | for(unsigned int i = 0; i < num_groups; ++i) |
| 104 | { |
| 105 | NodeID conv_nid = g.add_node<ConvolutionLayerNode>(conv_info, method); |
| 106 | g.add_connection(input_split, i, conv_nid, 0); |
| 107 | g.add_connection(weights_split, i, conv_nid, 1); |
| 108 | if(has_bias) |
| 109 | { |
| 110 | g.add_connection(bias_split, i, conv_nid, 2); |
| 111 | } |
| 112 | set_node_params(g, conv_nid, params); |
| 113 | convolution_outputs.push_back({ conv_nid, 0 }); |
| 114 | } |
| 115 | |
| 116 | // Depth concatenate output |
| 117 | return GraphBuilder::add_depth_concatenate_node(g, params, convolution_outputs); |
| 118 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 119 | } // namespace |
| 120 | |
| 121 | NodeID GraphBuilder::add_const_node(Graph &g, NodeParams params, TensorDescriptor desc, ITensorAccessorUPtr accessor) |
| 122 | { |
| 123 | auto nid = g.add_node<ConstNode>(desc); |
| 124 | set_node_params(g, nid, params); |
| 125 | set_accessor_on_node(g, nid, true, 0, std::move(accessor)); |
| 126 | return nid; |
| 127 | } |
| 128 | |
| 129 | NodeID GraphBuilder::add_input_node(Graph &g, NodeParams params, TensorDescriptor desc, ITensorAccessorUPtr accessor) |
| 130 | { |
| 131 | auto nid = g.add_node<InputNode>(desc); |
| 132 | set_node_params(g, nid, params); |
| 133 | set_accessor_on_node(g, nid, true, 0, std::move(accessor)); |
| 134 | return nid; |
| 135 | } |
| 136 | |
| 137 | NodeID GraphBuilder::add_output_node(Graph &g, NodeParams params, NodeIdxPair input, ITensorAccessorUPtr accessor) |
| 138 | { |
| 139 | CHECK_NODEIDX_PAIR(input, g); |
| 140 | |
| 141 | NodeID nid = g.add_node<OutputNode>(); |
| 142 | g.add_connection(input.node_id, input.index, nid, 0); |
| 143 | set_node_params(g, nid, params); |
| 144 | set_accessor_on_node(g, nid, false, 0, std::move(accessor)); |
| 145 | |
| 146 | return nid; |
| 147 | } |
| 148 | |
| 149 | NodeID GraphBuilder::add_activation_node(Graph &g, NodeParams params, NodeIdxPair input, ActivationLayerInfo act_info) |
| 150 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 151 | 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] | 152 | } |
| 153 | |
| 154 | NodeID GraphBuilder::add_batch_normalization_node(Graph &g, NodeParams params, NodeIdxPair input, float epsilon, |
| 155 | ITensorAccessorUPtr mean_accessor, ITensorAccessorUPtr var_accessor, |
| 156 | ITensorAccessorUPtr beta_accessor, ITensorAccessorUPtr gamma_accessor) |
| 157 | { |
| 158 | CHECK_NODEIDX_PAIR(input, g); |
| 159 | |
| 160 | bool has_beta = (beta_accessor != nullptr); |
| 161 | bool has_gamma = (gamma_accessor != nullptr); |
| 162 | |
| 163 | // Get input tensor descriptor |
| 164 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 165 | |
| 166 | // Calculate Common Descriptor |
| 167 | TensorDescriptor common_desc = input_tensor_desc; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 168 | common_desc.shape = TensorShape(get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 169 | |
| 170 | // Create mean and nodes |
| 171 | auto mean_nid = add_const_node_with_name(g, params, "Mean", common_desc, std::move(mean_accessor)); |
| 172 | auto var_nid = add_const_node_with_name(g, params, "Variance", common_desc, std::move(var_accessor)); |
| 173 | |
| 174 | // Create beta node |
| 175 | NodeID beta_nid = EmptyNodeID; |
| 176 | if(has_beta) |
| 177 | { |
| 178 | beta_nid = add_const_node_with_name(g, params, "Beta", common_desc, std::move(beta_accessor)); |
| 179 | } |
| 180 | |
| 181 | // Create gamma node |
| 182 | NodeID gamma_nid = EmptyNodeID; |
| 183 | if(has_gamma) |
| 184 | { |
| 185 | gamma_nid = add_const_node_with_name(g, params, "Gamma", common_desc, std::move(gamma_accessor)); |
| 186 | } |
| 187 | |
| 188 | // Create batch normalization node and add connections |
| 189 | NodeID batch_norm_nid = g.add_node<BatchNormalizationLayerNode>(epsilon); |
| 190 | g.add_connection(input.node_id, input.index, batch_norm_nid, 0); |
| 191 | g.add_connection(mean_nid, 0, batch_norm_nid, 1); |
| 192 | g.add_connection(var_nid, 0, batch_norm_nid, 2); |
| 193 | if(has_beta) |
| 194 | { |
| 195 | g.add_connection(beta_nid, 0, batch_norm_nid, 3); |
| 196 | } |
| 197 | if(has_gamma) |
| 198 | { |
| 199 | g.add_connection(gamma_nid, 0, batch_norm_nid, 4); |
| 200 | } |
| 201 | set_node_params(g, batch_norm_nid, params); |
| 202 | |
| 203 | return batch_norm_nid; |
| 204 | } |
| 205 | |
| 206 | NodeID GraphBuilder::add_convolution_node(Graph &g, NodeParams params, NodeIdxPair input, |
| 207 | Size2D kernel_spatial_extend, unsigned int depth, PadStrideInfo conv_info, |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 208 | unsigned int num_groups, ConvolutionMethod method, |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 209 | ITensorAccessorUPtr weights_accessor, ITensorAccessorUPtr bias_accessor, |
| 210 | const QuantizationInfo weights_quant_info, |
| 211 | const QuantizationInfo out_quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 212 | { |
| 213 | CHECK_NODEIDX_PAIR(input, g); |
| 214 | ARM_COMPUTE_ERROR_ON(depth == 0); |
| 215 | ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0)); |
| 216 | |
| 217 | bool has_bias = (bias_accessor != nullptr); |
| 218 | |
| 219 | // Get input tensor descriptor |
| 220 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 221 | |
| 222 | // Create weights node |
| 223 | TensorDescriptor w_desc = input_tensor_desc; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 224 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::WIDTH), kernel_spatial_extend.width); |
| 225 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height); |
| 226 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::CHANNEL), |
| 227 | get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL) / num_groups); |
| 228 | 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] | 229 | if(!weights_quant_info.empty()) |
| 230 | { |
| 231 | w_desc.quant_info = weights_quant_info; |
| 232 | } |
| 233 | |
| 234 | 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] | 235 | |
| 236 | // Create bias nodes |
| 237 | NodeID b_nid = EmptyNodeID; |
| 238 | if(has_bias) |
| 239 | { |
| 240 | TensorDescriptor b_desc = input_tensor_desc; |
| 241 | b_desc.shape = TensorShape(depth); |
| 242 | b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor)); |
| 243 | } |
| 244 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 245 | if(num_groups == 1) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 246 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 247 | // Create convolution node and connect |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 248 | NodeID conv_nid = g.add_node<ConvolutionLayerNode>(conv_info, method, out_quant_info); |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 249 | g.add_connection(input.node_id, input.index, conv_nid, 0); |
| 250 | g.add_connection(w_nid, 0, conv_nid, 1); |
| 251 | if(has_bias) |
| 252 | { |
| 253 | g.add_connection(b_nid, 0, conv_nid, 2); |
| 254 | } |
| 255 | set_node_params(g, conv_nid, params); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 256 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 257 | return conv_nid; |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | return create_grouped_convolution(g, params, input, w_nid, b_nid, conv_info, method, num_groups); |
| 262 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | NodeID GraphBuilder::add_depth_concatenate_node(Graph &g, NodeParams params, std::vector<NodeIdxPair> inputs) |
| 266 | { |
| 267 | ARM_COMPUTE_ERROR_ON(inputs.size() == 0); |
| 268 | |
| 269 | NodeID nid = g.add_node<DepthConcatenateLayerNode>(inputs.size()); |
| 270 | |
| 271 | unsigned int i = 0; |
| 272 | for(const auto &input : inputs) |
| 273 | { |
| 274 | CHECK_NODEIDX_PAIR(input, g); |
| 275 | g.add_connection(input.node_id, input.index, nid, i++); |
| 276 | } |
| 277 | set_node_params(g, nid, params); |
| 278 | |
| 279 | return nid; |
| 280 | } |
| 281 | |
| 282 | NodeID GraphBuilder::add_depthwise_convolution_node(Graph &g, NodeParams params, NodeIdxPair input, Size2D kernel_spatial_extend, PadStrideInfo conv_info, |
| 283 | DepthwiseConvolutionMethod method, |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 284 | ITensorAccessorUPtr weights_accessor, ITensorAccessorUPtr bias_accessor, const QuantizationInfo quant_info) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 285 | { |
| 286 | CHECK_NODEIDX_PAIR(input, g); |
| 287 | ARM_COMPUTE_ERROR_ON((kernel_spatial_extend.width == 0) || (kernel_spatial_extend.height == 0)); |
| 288 | |
| 289 | bool has_bias = (bias_accessor != nullptr); |
| 290 | |
| 291 | // Get input tensor descriptor |
| 292 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 293 | |
| 294 | // Create weights node |
| 295 | TensorDescriptor w_desc = input_tensor_desc; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 296 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::WIDTH), kernel_spatial_extend.width); |
| 297 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::HEIGHT), kernel_spatial_extend.height); |
| 298 | w_desc.shape.set(get_dimension_idx(input_tensor_desc, DataLayoutDimension::CHANNEL), |
| 299 | get_dimension_size(input_tensor_desc, DataLayoutDimension::CHANNEL)); |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 300 | if(!quant_info.empty()) |
| 301 | { |
| 302 | w_desc.quant_info = quant_info; |
| 303 | } |
| 304 | |
| 305 | 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] | 306 | |
| 307 | // Create bias nodes |
| 308 | NodeID b_nid = EmptyNodeID; |
| 309 | if(has_bias) |
| 310 | { |
| 311 | TensorDescriptor b_desc = input_tensor_desc; |
| 312 | b_desc.shape = TensorShape(b_desc.shape.z()); |
| 313 | b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor)); |
| 314 | } |
| 315 | |
| 316 | // Create convolution node and connect |
| 317 | NodeID conv_nid = g.add_node<DepthwiseConvolutionLayerNode>(conv_info, method); |
| 318 | g.add_connection(input.node_id, input.index, conv_nid, 0); |
| 319 | g.add_connection(w_nid, 0, conv_nid, 1); |
| 320 | if(has_bias) |
| 321 | { |
| 322 | g.add_connection(b_nid, 0, conv_nid, 2); |
| 323 | } |
| 324 | set_node_params(g, conv_nid, params); |
| 325 | |
| 326 | return conv_nid; |
| 327 | } |
| 328 | |
| 329 | NodeID GraphBuilder::add_elementwise_node(Graph &g, NodeParams params, NodeIdxPair input0, NodeIdxPair input1, EltwiseOperation operation) |
| 330 | { |
| 331 | CHECK_NODEIDX_PAIR(input0, g); |
| 332 | CHECK_NODEIDX_PAIR(input1, g); |
| 333 | |
| 334 | NodeID nid = g.add_node<EltwiseLayerNode>(operation); |
| 335 | |
| 336 | g.add_connection(input0.node_id, input0.index, nid, 0); |
| 337 | g.add_connection(input1.node_id, input1.index, nid, 1); |
| 338 | |
| 339 | set_node_params(g, nid, params); |
| 340 | |
| 341 | return nid; |
| 342 | } |
| 343 | |
| 344 | NodeID GraphBuilder::add_flatten_node(Graph &g, NodeParams params, NodeIdxPair input) |
| 345 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 346 | return create_simple_single_input_output_node<FlattenLayerNode>(g, params, input); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | NodeID GraphBuilder::add_fully_connected_layer(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_outputs, |
| 350 | ITensorAccessorUPtr weights_accessor, ITensorAccessorUPtr bias_accessor) |
| 351 | { |
| 352 | CHECK_NODEIDX_PAIR(input, g); |
| 353 | ARM_COMPUTE_ERROR_ON(num_outputs == 0); |
| 354 | |
| 355 | bool has_bias = (bias_accessor != nullptr); |
| 356 | |
| 357 | // Get input tensor descriptor |
| 358 | const TensorDescriptor input_tensor_desc = get_tensor_descriptor(g, g.node(input.node_id)->outputs()[0]); |
| 359 | |
| 360 | // Create weights node |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 361 | TensorDescriptor w_desc = FullyConnectedLayerNode::compute_weights_descriptor(input_tensor_desc, num_outputs); |
| 362 | 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] | 363 | |
| 364 | // Create bias nodes |
| 365 | NodeID b_nid = EmptyNodeID; |
| 366 | if(has_bias) |
| 367 | { |
| 368 | TensorDescriptor b_desc = input_tensor_desc; |
| 369 | b_desc.shape = TensorShape(num_outputs); |
| 370 | b_nid = add_const_node_with_name(g, params, "Bias", b_desc, std::move(bias_accessor)); |
| 371 | } |
| 372 | |
| 373 | // Create convolution node and connect |
| 374 | NodeID fc_nid = g.add_node<FullyConnectedLayerNode>(num_outputs); |
| 375 | g.add_connection(input.node_id, input.index, fc_nid, 0); |
| 376 | g.add_connection(w_nid, 0, fc_nid, 1); |
| 377 | if(has_bias) |
| 378 | { |
| 379 | g.add_connection(b_nid, 0, fc_nid, 2); |
| 380 | } |
| 381 | |
| 382 | set_node_params(g, fc_nid, params); |
| 383 | |
| 384 | return fc_nid; |
| 385 | } |
| 386 | |
| 387 | NodeID GraphBuilder::add_normalization_node(Graph &g, NodeParams params, NodeIdxPair input, NormalizationLayerInfo norm_info) |
| 388 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 389 | 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] | 390 | } |
| 391 | |
| 392 | NodeID GraphBuilder::add_pooling_node(Graph &g, NodeParams params, NodeIdxPair input, PoolingLayerInfo pool_info) |
| 393 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 394 | 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] | 395 | } |
| 396 | |
| 397 | NodeID GraphBuilder::add_reshape_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape) |
| 398 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 399 | return create_simple_single_input_output_node<ReshapeLayerNode>(g, params, input, shape); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | NodeID GraphBuilder::add_softmax_node(Graph &g, NodeParams params, NodeIdxPair input, float beta) |
| 403 | { |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 404 | return create_simple_single_input_output_node<SoftmaxLayerNode>(g, params, input, beta); |
| 405 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 406 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 407 | NodeID GraphBuilder::add_split_node(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_splits, unsigned int axis) |
| 408 | { |
| 409 | 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] | 410 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 411 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 412 | } // namespace arm_compute |