Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [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 | */ |
| 24 | #ifndef __ARM_COMPUTE_GRAPH_BACKENDS_DETAIL_FUNCTION_HELPERS_H__ |
| 25 | #define __ARM_COMPUTE_GRAPH_BACKENDS_DETAIL_FUNCTION_HELPERS_H__ |
| 26 | |
| 27 | #include "arm_compute/graph/Logger.h" |
| 28 | #include "arm_compute/graph/Tensor.h" |
| 29 | #include "arm_compute/graph/TypePrinter.h" |
| 30 | #include "arm_compute/graph/Types.h" |
| 31 | #include "arm_compute/graph/backends/Utils.h" |
| 32 | #include "arm_compute/graph/nodes/Nodes.h" |
| 33 | |
| 34 | #include "arm_compute/core/Error.h" |
| 35 | #include "arm_compute/core/Helpers.h" |
| 36 | #include "arm_compute/core/ITensorInfo.h" |
| 37 | #include "arm_compute/core/utils/misc/Cast.h" |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace graph |
| 42 | { |
| 43 | namespace backends |
| 44 | { |
| 45 | namespace detail |
| 46 | { |
| 47 | /** Returns backing tensor of a given tensor |
| 48 | * |
| 49 | * @tparam TargetInfo Target information |
| 50 | * |
| 51 | * @param[in] tensor Tensor to extract the backing tensor from |
| 52 | * |
| 53 | * @return Backing tensor if present else nullptr |
| 54 | */ |
| 55 | template <typename TargetInfo> |
| 56 | typename TargetInfo::TensorType *get_backing_tensor(arm_compute::graph::Tensor *tensor) |
| 57 | { |
| 58 | typename TargetInfo::TensorType *backing_tensor = nullptr; |
| 59 | if(tensor != nullptr) |
| 60 | { |
| 61 | ARM_COMPUTE_ERROR_ON(tensor->desc().target != TargetInfo::TargetType); |
| 62 | // Get backing tensor handle |
| 63 | ITensorHandle *tensor_handle = tensor->handle(); |
| 64 | // Get backing tensor |
| 65 | backing_tensor = (tensor_handle != nullptr) ? arm_compute::utils::cast::polymorphic_cast<typename TargetInfo::TensorType *>(&tensor_handle->tensor()) : nullptr; |
| 66 | } |
| 67 | |
| 68 | return backing_tensor; |
| 69 | } |
| 70 | |
| 71 | template <typename TargetInfo> |
| 72 | void validate_node(const INode &node, size_t num_expected_inputs, size_t num_expected_outputs) |
| 73 | { |
| 74 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Creating " << node.type() |
| 75 | << " Target : " << TargetInfo::TargetType |
| 76 | << " ID : " << node.id() |
| 77 | << " Name: " << node.name() |
| 78 | << std::endl); |
| 79 | |
| 80 | ARM_COMPUTE_ERROR_ON(TargetInfo::TargetType != node.assigned_target()); |
| 81 | ARM_COMPUTE_ERROR_ON(node.num_inputs() != num_expected_inputs); |
| 82 | ARM_COMPUTE_ERROR_ON(node.num_outputs() != num_expected_outputs); |
| 83 | } |
| 84 | |
| 85 | /** Creates a backend activation layer function |
| 86 | * |
| 87 | * @tparam ActivationLayerFunction Backend activation function |
| 88 | * @tparam TargetInfo Target-specific information |
| 89 | * |
| 90 | * @param[in] node Node to create the backend function for |
| 91 | * |
| 92 | * @return Backend activation layer function |
| 93 | */ |
| 94 | template <typename ActivationLayerFunction, typename TargetInfo> |
| 95 | std::unique_ptr<IFunction> create_activation_layer(ActivationLayerNode &node) |
| 96 | { |
| 97 | validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */); |
| 98 | |
| 99 | // Extract IO and info |
| 100 | typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0)); |
| 101 | typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0)); |
| 102 | const ActivationLayerInfo act_info = node.activation_info(); |
| 103 | |
| 104 | // Create function |
| 105 | auto func = support::cpp14::make_unique<ActivationLayerFunction>(); |
| 106 | func->configure(input, output, act_info); |
| 107 | |
| 108 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() |
| 109 | << " Target " << TargetInfo::TargetType |
| 110 | << " Data Type: " << input->info()->data_type() |
| 111 | << " Shape: " << input->info()->tensor_shape() |
| 112 | << " Activation function: " << act_info.activation() |
| 113 | << " a: " << act_info.a() |
| 114 | << " b: " << act_info.b() |
| 115 | << " InPlace : " << is_in_place_operation(input, output) |
| 116 | << std::endl); |
| 117 | |
| 118 | return std::move(func); |
| 119 | } |
| 120 | |
| 121 | /** Create a backend batch normalization layer function |
| 122 | * |
| 123 | * @tparam BatchNormalizationLayerFunction Backend batch normalization function |
| 124 | * @tparam TargetInfo Target-specific information |
| 125 | * |
| 126 | * @param[in] node Node to create the backend function for |
| 127 | * |
| 128 | * @return Backend batch normalization layer function |
| 129 | */ |
| 130 | template <typename BatchNormalizationLayerFunction, typename TargetInfo> |
| 131 | std::unique_ptr<IFunction> create_batch_normalization_layer(BatchNormalizationLayerNode &node) |
| 132 | { |
| 133 | validate_node<TargetInfo>(node, 5 /* expected inputs */, 1 /* expected outputs */); |
| 134 | |
| 135 | // Extract IO and info |
| 136 | typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0)); |
| 137 | typename TargetInfo::TensorType *mean = get_backing_tensor<TargetInfo>(node.input(1)); |
| 138 | typename TargetInfo::TensorType *var = get_backing_tensor<TargetInfo>(node.input(2)); |
| 139 | typename TargetInfo::TensorType *beta = get_backing_tensor<TargetInfo>(node.input(3)); |
| 140 | typename TargetInfo::TensorType *gamma = get_backing_tensor<TargetInfo>(node.input(4)); |
| 141 | typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0)); |
| 142 | const float epsilon = node.epsilon(); |
| 143 | const ActivationLayerInfo fused_act = node.fused_activation(); |
| 144 | |
| 145 | // Create and configure function |
| 146 | auto func = support::cpp14::make_unique<BatchNormalizationLayerFunction>(); |
| 147 | func->configure(input, output, mean, var, beta, gamma, epsilon, fused_act); |
| 148 | |
| 149 | // Log info |
| 150 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() |
| 151 | << " Target " << TargetInfo::TargetType |
| 152 | << " Data Type: " << input->info()->data_type() |
| 153 | << " Shape: " << input->info()->tensor_shape() |
| 154 | << " Epsilon: " << epsilon << " " |
| 155 | << (fused_act.enabled() ? to_string(fused_act.activation()) : "") |
| 156 | << " InPlace : " << is_in_place_operation(input, output) |
| 157 | << std::endl); |
| 158 | |
| 159 | return std::move(func); |
| 160 | } |
| 161 | |
| 162 | /** Create a backend channel shuffle layer function |
| 163 | * |
| 164 | * @tparam ChannelShuffleLayerFunction Backend channel shuffle function |
| 165 | * @tparam TargetInfo Target-specific information |
| 166 | * |
| 167 | * @param[in] node Node to create the backend function for |
| 168 | * |
| 169 | * @return Backend channel shuffle layer function |
| 170 | */ |
| 171 | template <typename ChannelShuffleLayerFunction, typename TargetInfo> |
| 172 | std::unique_ptr<IFunction> create_channel_shuffle_layer(ChannelShuffleLayerNode &node) |
| 173 | { |
| 174 | validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */); |
| 175 | |
| 176 | // Extract IO and info |
| 177 | typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0)); |
| 178 | typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0)); |
| 179 | const unsigned int num_groups = node.num_groups(); |
| 180 | |
| 181 | // Create function |
| 182 | auto func = support::cpp14::make_unique<ChannelShuffleLayerFunction>(); |
| 183 | func->configure(input, output, num_groups); |
| 184 | |
| 185 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() |
| 186 | << " Target " << TargetInfo::TargetType |
| 187 | << " Data Type: " << input->info()->data_type() |
| 188 | << " Shape: " << input->info()->tensor_shape() |
| 189 | << " Num groups: " << num_groups |
| 190 | << std::endl); |
| 191 | |
| 192 | return std::move(func); |
| 193 | } |
| 194 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 195 | /** Create a backend layer concatenate function |
| 196 | * |
| 197 | * @tparam ConcatenateLayerFunction Backend concatenate function |
| 198 | * @tparam TargetInfo Target-specific information |
| 199 | * |
| 200 | * @param[in] node Node to create the backend function for |
| 201 | * |
| 202 | * @return Backend concatenate layer function |
| 203 | */ |
| 204 | template <typename ConcatenateLayerFunction, typename TargetInfo> |
| 205 | std::unique_ptr<arm_compute::IFunction> create_concatenate_layer(ConcatenateLayerNode &node) |
| 206 | { |
| 207 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Creating Concatenate node with ID : " << node.id() << " and Name: " << node.name() << std::endl); |
| 208 | ARM_COMPUTE_ERROR_ON(node.num_outputs() != 1); |
| 209 | |
| 210 | // Return nullptr if depth concatenate is switched off |
| 211 | if(!node.is_enabled()) |
| 212 | { |
| 213 | return nullptr; |
| 214 | } |
| 215 | |
| 216 | // Extract IO and info |
| 217 | std::vector<typename TargetInfo::TensorType *> inputs; |
| 218 | for(unsigned int i = 0; i < node.num_inputs(); ++i) |
| 219 | { |
| 220 | inputs.push_back(get_backing_tensor<TargetInfo>(node.input(i))); |
| 221 | } |
| 222 | typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0)); |
| 223 | const DataLayoutDimension concat_axis = node.concatenation_axis(); |
| 224 | |
| 225 | // Create and configure function |
| 226 | auto func = support::cpp14::make_unique<ConcatenateLayerFunction>(); |
| 227 | func->configure(inputs, output, concat_axis); |
| 228 | |
| 229 | // Log info |
| 230 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() |
| 231 | << " Target " << TargetInfo::TargetType |
| 232 | << " Data Type: " << output->info()->data_type() |
| 233 | << " Shape: " << output->info()->tensor_shape() |
| 234 | << " Num Inputs: " << inputs.size() |
| 235 | << " Axis: " << concat_axis |
| 236 | << std::endl); |
| 237 | |
| 238 | return std::move(func); |
| 239 | } |
| 240 | |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 241 | /** Create a backend convolution layer function |
| 242 | * |
| 243 | * @tparam ConvolutionLayerFunctions Backend convolution functions |
| 244 | * @tparam TargetInfo Target-specific information |
| 245 | * |
| 246 | * @param[in] node Node to create the backend function for |
| 247 | * @param[in] ctx Graph context |
| 248 | * |
| 249 | * @return Backend convolution layer function |
| 250 | */ |
| 251 | template <typename ConvolutionLayerFunctions, typename TargetInfo> |
| 252 | std::unique_ptr<IFunction> create_convolution_layer(ConvolutionLayerNode &node, GraphContext &ctx) |
| 253 | { |
| 254 | validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */); |
| 255 | |
| 256 | // Extract IO and info |
| 257 | typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0)); |
| 258 | typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1)); |
| 259 | typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2)); |
| 260 | typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0)); |
| 261 | |
| 262 | if(is_data_type_quantized_asymmetric(input->info()->data_type())) |
| 263 | { |
| 264 | biases->info()->set_data_type(DataType::S32); |
| 265 | } |
| 266 | |
| 267 | const PadStrideInfo conv_info = node.convolution_info(); |
| 268 | const ConvolutionMethod conv_algorithm = node.convolution_method(); |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 269 | const bool fast_math = node.fast_math_hint() == FastMathHint::Enabled; |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 270 | |
| 271 | // Create and configure function (we assume that functions have been validated before creation) |
| 272 | std::shared_ptr<IMemoryManager> mm = get_memory_manager(ctx, TargetInfo::TargetType); |
| 273 | std::unique_ptr<IFunction> func; |
| 274 | std::string func_name; |
| 275 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 276 | if(conv_algorithm == ConvolutionMethod::Winograd) |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 277 | { |
| 278 | std::tie(func, func_name) = create_named_memory_managed_function<typename ConvolutionLayerFunctions::WinogradConvolutionLayer>( |
| 279 | std::string("WinogradConvolutionLayer"), mm, |
| 280 | input, weights, biases, output, conv_info, ActivationLayerInfo(), fast_math); |
| 281 | } |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 282 | else if(conv_algorithm == ConvolutionMethod::Direct) |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 283 | { |
| 284 | std::tie(func, func_name) = create_named_function<typename ConvolutionLayerFunctions::DirectConvolutionLayer>( |
| 285 | std::string("DirectConvolutionLayer"), |
| 286 | input, weights, biases, output, conv_info); |
| 287 | } |
| 288 | else if(conv_algorithm == ConvolutionMethod::GEMM) |
| 289 | { |
| 290 | std::tie(func, func_name) = create_named_memory_managed_function<typename ConvolutionLayerFunctions::GEMMConvolutionLayer>( |
| 291 | std::string("GEMMConvolutionLayer"), mm, |
| 292 | input, weights, biases, output, conv_info); |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | std::tie(func, func_name) = create_named_memory_managed_function<typename ConvolutionLayerFunctions::GenericConvolutionLayer>( |
| 297 | std::string("GenericConvolutionLayer"), mm, |
| 298 | input, weights, biases, output, conv_info, WeightsInfo(), Size2D(1U, 1U), ActivationLayerInfo(), fast_math); |
| 299 | } |
| 300 | |
| 301 | // Log info |
| 302 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << func_name |
| 303 | << " Target " << TargetInfo::TargetType |
| 304 | << " Data Type: " << input->info()->data_type() |
| 305 | << " Input QuantInfo: " << input->info()->quantization_info() |
| 306 | << " Weights QuantInfo: " << weights->info()->quantization_info() |
| 307 | << " Input shape: " << input->info()->tensor_shape() |
| 308 | << " Weights shape: " << weights->info()->tensor_shape() |
| 309 | << " Output shape: " << output->info()->tensor_shape() |
| 310 | << std::endl); |
| 311 | return func; |
| 312 | } |
| 313 | |
| 314 | /** Create a backend deconvolution layer function |
| 315 | * |
| 316 | * @tparam DeconvolutionLayerFunction Backend deconvolution function |
| 317 | * @tparam TargetInfo Target-specific information |
| 318 | * |
| 319 | * @param[in] node Node to create the backend function for |
| 320 | * @param[in] ctx Graph context |
| 321 | * |
| 322 | * @return Backend deconvolution layer function |
| 323 | */ |
| 324 | template <typename DeconvolutionLayerFunction, typename TargetInfo> |
| 325 | std::unique_ptr<IFunction> create_deconvolution_layer(DeconvolutionLayerNode &node, GraphContext &ctx) |
| 326 | { |
| 327 | validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */); |
| 328 | |
| 329 | // Extract IO and info |
| 330 | typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0)); |
| 331 | typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1)); |
| 332 | typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2)); |
| 333 | typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0)); |
| 334 | |
| 335 | const PadStrideInfo deconv_info = node.deconvolution_info(); |
| 336 | const Size2D inner_border = node.inner_border(); |
| 337 | |
| 338 | // Create and configure function (we assume that functions have been validated before creation) |
| 339 | std::shared_ptr<IMemoryManager> mm = get_memory_manager(ctx, TargetInfo::TargetType); |
| 340 | std::unique_ptr<IFunction> func; |
| 341 | |
| 342 | std::tie(func, std::ignore) = create_named_memory_managed_function<DeconvolutionLayerFunction>( |
| 343 | std::string(), mm, |
| 344 | input, weights, biases, output, deconv_info, inner_border.x(), inner_border.y()); |
| 345 | |
| 346 | // Log info |
| 347 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() |
| 348 | << " Target " << TargetInfo::TargetType |
| 349 | << " Data Type: " << input->info()->data_type() |
| 350 | << " Input shape: " << input->info()->tensor_shape() |
| 351 | << " Weights shape: " << weights->info()->tensor_shape() |
| 352 | << " Output shape: " << output->info()->tensor_shape() |
| 353 | << std::endl); |
| 354 | return func; |
| 355 | } |
| 356 | |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 357 | /** Create a backend layer depth-wise convolution function |
| 358 | * |
| 359 | * @tparam DepthwiseConvolutionLayerFunctions Backend depthwise convolution function |
| 360 | * @tparam TargetInfo Target-specific information |
| 361 | * |
| 362 | * @param[in] node Node to create the backend function for |
| 363 | * |
| 364 | * @return Backend depth-wise convolution layer function |
| 365 | */ |
| 366 | template <typename DepthwiseConvolutionLayerFunctions, typename TargetInfo> |
| 367 | std::unique_ptr<IFunction> create_depthwise_convolution_layer(DepthwiseConvolutionLayerNode &node) |
| 368 | { |
| 369 | validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */); |
| 370 | |
| 371 | // Extract IO and info |
| 372 | typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0)); |
| 373 | typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1)); |
| 374 | typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2)); |
| 375 | typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0)); |
| 376 | |
| 377 | if(is_data_type_quantized_asymmetric(input->info()->data_type())) |
| 378 | { |
| 379 | biases->info()->set_data_type(DataType::S32); |
| 380 | } |
| 381 | |
| 382 | const PadStrideInfo conv_info = node.convolution_info(); |
| 383 | const DepthwiseConvolutionMethod dwc_algorithm = node.depthwise_convolution_method(); |
| 384 | |
| 385 | // Create and configure function (we assume that functions have been validated before creation) |
| 386 | std::unique_ptr<IFunction> func; |
| 387 | std::string func_name; |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 388 | if(dwc_algorithm == DepthwiseConvolutionMethod::Optimized3x3) |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 389 | { |
| 390 | std::tie(func, func_name) = create_named_function<typename DepthwiseConvolutionLayerFunctions::DepthwiseConvolutionLayer3x3>( |
| 391 | std::string("DepthwiseConvolutionLayer3x3"), |
| 392 | input, weights, biases, output, conv_info); |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | std::tie(func, func_name) = create_named_function<typename DepthwiseConvolutionLayerFunctions::GenericDepthwiseConvolutionLayer>( |
| 397 | std::string("DepthwiseConvolutionLayer"), |
| 398 | input, weights, biases, output, conv_info); |
| 399 | } |
| 400 | |
| 401 | // Log info |
| 402 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << func_name |
| 403 | << " Target " << TargetInfo::TargetType |
| 404 | << " Data Type: " << input->info()->data_type() |
| 405 | << " Input QuantInfo: " << input->info()->quantization_info() |
| 406 | << " Weights QuantInfo: " << weights->info()->quantization_info() |
| 407 | << " Input shape: " << input->info()->tensor_shape() |
| 408 | << " Weights shape: " << weights->info()->tensor_shape() |
| 409 | << " Output shape: " << output->info()->tensor_shape() |
| 410 | << std::endl); |
| 411 | return func; |
| 412 | } |
| 413 | |
| 414 | /** Create a backend element-wise operation layer function |
| 415 | * |
| 416 | * @tparam EltwiseFunctions Backend element-wise function |
| 417 | * @tparam TargetInfo Target-specific information |
| 418 | * |
| 419 | * @param[in] node Node to create the backend function for |
| 420 | * |
| 421 | * @return Backend element-wise operation layer function |
| 422 | */ |
| 423 | template <typename EltwiseFunctions, typename TargetInfo> |
| 424 | std::unique_ptr<IFunction> create_eltwise_layer(EltwiseLayerNode &node) |
| 425 | { |
| 426 | validate_node<TargetInfo>(node, 2 /* expected inputs */, 1 /* expected outputs */); |
| 427 | |
| 428 | // Extract IO and info |
| 429 | typename TargetInfo::TensorType *input1 = get_backing_tensor<TargetInfo>(node.input(0)); |
| 430 | typename TargetInfo::TensorType *input2 = get_backing_tensor<TargetInfo>(node.input(1)); |
| 431 | typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0)); |
| 432 | const EltwiseOperation eltwise_op = node.eltwise_operation(); |
| 433 | const ConvertPolicy convert_policy = node.convert_policy(); |
| 434 | ARM_COMPUTE_ERROR_ON(input1 == nullptr); |
| 435 | ARM_COMPUTE_ERROR_ON(input2 == nullptr); |
| 436 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
| 437 | |
| 438 | std::unique_ptr<IFunction> func = nullptr; |
| 439 | std::string func_name; |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 440 | if(eltwise_op == EltwiseOperation::Add) |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 441 | { |
| 442 | std::tie(func, func_name) = create_named_function<typename EltwiseFunctions::Addition>( |
| 443 | std::string("ArithmeticAddition"), |
| 444 | input1, input2, output, convert_policy); |
| 445 | } |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 446 | else if(eltwise_op == EltwiseOperation::Sub) |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 447 | { |
| 448 | std::tie(func, func_name) = create_named_function<typename EltwiseFunctions::Subtraction>( |
| 449 | std::string("ArithmeticSubtraction"), |
| 450 | input1, input2, output, convert_policy); |
| 451 | } |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 452 | else if(eltwise_op == EltwiseOperation::Mul) |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 453 | { |
| 454 | std::tie(func, func_name) = create_named_function<typename EltwiseFunctions::Multiplication>( |
| 455 | std::string("PixelWiseMultiplication"), |
| 456 | input1, input2, output, 1.f, convert_policy, node.rounding_policy()); |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | ARM_COMPUTE_ERROR("Unsupported element-wise operation!"); |
| 461 | } |
| 462 | |
| 463 | // Log info |
| 464 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() |
| 465 | << " Target " << TargetInfo::TargetType |
| 466 | << " Operation " << func_name |
| 467 | << " Data Type: " << input1->info()->data_type() |
| 468 | << " Shape : " << input1->info()->tensor_shape() |
| 469 | << std::endl); |
| 470 | |
| 471 | return func; |
| 472 | } |
| 473 | |
| 474 | /** Create a backend flatten layer function |
| 475 | * |
| 476 | * @tparam FlattenLayerFunction Backend flatten function |
| 477 | * @tparam TargetInfo Target-specific information |
| 478 | * |
| 479 | * @param[in] node Node to create the backend function for |
| 480 | * |
| 481 | * @return Backend flatten layer function |
| 482 | */ |
| 483 | template <typename FlattenLayerFunction, typename TargetInfo> |
| 484 | std::unique_ptr<IFunction> create_flatten_layer(FlattenLayerNode &node) |
| 485 | { |
| 486 | validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */); |
| 487 | |
| 488 | // Extract IO and info |
| 489 | typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0)); |
| 490 | typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0)); |
| 491 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 492 | ARM_COMPUTE_ERROR_ON(input == nullptr); |
| 493 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
| 494 | |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 495 | // Create and configure function |
| 496 | auto func = support::cpp14::make_unique<FlattenLayerFunction>(); |
| 497 | func->configure(input, output); |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 498 | |
| 499 | // Log info |
| 500 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() |
| 501 | << " Target " << TargetInfo::TargetType |
| 502 | << " Data Type: " << input->info()->data_type() |
| 503 | << " Input shape: " << input->info()->tensor_shape() |
| 504 | << " Output shape: " << output->info()->tensor_shape() |
| 505 | << std::endl); |
| 506 | |
| 507 | return std::move(func); |
| 508 | } |
| 509 | |
| 510 | /** Create a backend fully connected layer function |
| 511 | * |
| 512 | * @tparam FullyConnectedLayerFunction Backend fully-connected function |
| 513 | * @tparam TargetInfo Target-specific information |
| 514 | * |
| 515 | * @param[in] node Node to create the backend function for |
| 516 | * @param[in] ctx Graph context |
| 517 | * |
| 518 | * @return Backend fully connected layer function |
| 519 | */ |
| 520 | template <typename FullyConnectedLayerFunction, typename TargetInfo> |
| 521 | std::unique_ptr<IFunction> create_fully_connected_layer(FullyConnectedLayerNode &node, GraphContext &ctx) |
| 522 | { |
| 523 | validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */); |
| 524 | |
| 525 | // Extract IO and info |
| 526 | typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0)); |
| 527 | typename TargetInfo::TensorType *weights = get_backing_tensor<TargetInfo>(node.input(1)); |
| 528 | typename TargetInfo::TensorType *biases = get_backing_tensor<TargetInfo>(node.input(2)); |
| 529 | typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0)); |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 530 | const FullyConnectedLayerInfo fc_info = node.info(); |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 531 | |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 532 | ARM_COMPUTE_ERROR_ON(input == nullptr); |
| 533 | ARM_COMPUTE_ERROR_ON(weights == nullptr); |
| 534 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
| 535 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 536 | // Create and configure function |
| 537 | auto func = support::cpp14::make_unique<FullyConnectedLayerFunction>(get_memory_manager(ctx, TargetInfo::TargetType)); |
| 538 | func->configure(input, weights, biases, output, fc_info); |
| 539 | |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 540 | // Log info |
| 541 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() |
| 542 | << " Target " << TargetInfo::TargetType |
| 543 | << " Data Type: " << input->info()->data_type() |
Georgios Pinitas | 2f1366a | 2018-07-31 16:33:06 +0100 | [diff] [blame] | 544 | << " Input QuantInfo: " << input->info()->quantization_info() |
| 545 | << " Weights QuantInfo: " << weights->info()->quantization_info() |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 546 | << " Input shape: " << input->info()->tensor_shape() |
| 547 | << " Weights shape: " << weights->info()->tensor_shape() |
| 548 | << " Output shape: " << output->info()->tensor_shape() |
| 549 | << std::endl); |
| 550 | |
| 551 | return std::move(func); |
| 552 | } |
| 553 | |
| 554 | /** Create a backend normalization layer function |
| 555 | * |
| 556 | * @tparam NormalizationLayerFunction Backend normalization function |
| 557 | * @tparam TargetInfo Target-specific information |
| 558 | * |
| 559 | * @param[in] node Node to create the backend function for |
| 560 | * @param[in] ctx Graph context |
| 561 | * |
| 562 | * @return Backend normalization layer function |
| 563 | */ |
| 564 | template <typename NormalizationLayerFunction, typename TargetInfo> |
| 565 | std::unique_ptr<IFunction> create_normalization_layer(NormalizationLayerNode &node, GraphContext &ctx) |
| 566 | { |
| 567 | ARM_COMPUTE_UNUSED(ctx); |
| 568 | |
| 569 | validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */); |
| 570 | |
| 571 | // Extract IO and info |
| 572 | typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0)); |
| 573 | typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0)); |
| 574 | const NormalizationLayerInfo norm_info = node.normalization_info(); |
| 575 | ARM_COMPUTE_ERROR_ON(input == nullptr); |
| 576 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
| 577 | |
| 578 | // Create and configure function |
| 579 | auto func = support::cpp14::make_unique<NormalizationLayerFunction>(); |
| 580 | func->configure(input, output, norm_info); |
| 581 | |
| 582 | // Log info |
| 583 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() |
| 584 | << " Target " << TargetInfo::TargetType |
| 585 | << " Data Type: " << input->info()->data_type() |
| 586 | << " Input shape: " << input->info()->tensor_shape() |
| 587 | << " Output shape: " << output->info()->tensor_shape() |
| 588 | << " Normalization info: " << norm_info.type() |
| 589 | << std::endl); |
| 590 | |
| 591 | return std::move(func); |
| 592 | } |
| 593 | |
| 594 | /** Create a backend pooling layer function |
| 595 | * |
| 596 | * @tparam PoolingLayerFunction Backend pooling function |
| 597 | * @tparam TargetInfo Target-specific information |
| 598 | * |
| 599 | * @param[in] node Node to create the backend function for |
| 600 | * |
| 601 | * @return Backend pooling layer function |
| 602 | */ |
| 603 | template <typename PoolingLayerFunction, typename TargetInfo> |
| 604 | std::unique_ptr<IFunction> create_pooling_layer(PoolingLayerNode &node) |
| 605 | { |
| 606 | validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */); |
| 607 | |
| 608 | // Extract IO and info |
| 609 | typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0)); |
| 610 | typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0)); |
| 611 | const PoolingLayerInfo pool_info = node.pooling_info(); |
| 612 | ARM_COMPUTE_ERROR_ON(input == nullptr); |
| 613 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
| 614 | |
| 615 | // Create and configure function |
| 616 | auto func = support::cpp14::make_unique<PoolingLayerFunction>(); |
| 617 | func->configure(input, output, pool_info); |
| 618 | |
| 619 | // Log info |
| 620 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() |
| 621 | << " Target " << TargetInfo::TargetType |
| 622 | << " Data Type: " << input->info()->data_type() |
| 623 | << " Input shape: " << input->info()->tensor_shape() |
| 624 | << " Output shape: " << output->info()->tensor_shape() |
| 625 | << " Pooling info: " << pool_info.pool_type() |
| 626 | << std::endl); |
| 627 | |
| 628 | return std::move(func); |
| 629 | } |
| 630 | |
| 631 | /** Create a backend reshape layer function |
| 632 | * |
| 633 | * @tparam ReshapeLayerFunction Backend reshape function |
| 634 | * @tparam TargetInfo Target-specific information |
| 635 | * |
| 636 | * @param[in] node Node to create the backend function for |
| 637 | * |
| 638 | * @return Backend reshape layer function |
| 639 | */ |
| 640 | template <typename ReshapeLayerFunction, typename TargetInfo> |
| 641 | std::unique_ptr<IFunction> create_reshape_layer(ReshapeLayerNode &node) |
| 642 | { |
| 643 | validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */); |
| 644 | |
| 645 | // Extract IO and info |
| 646 | typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0)); |
| 647 | typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0)); |
| 648 | ARM_COMPUTE_ERROR_ON(input == nullptr); |
| 649 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
| 650 | |
| 651 | // Create and configure function |
| 652 | auto func = support::cpp14::make_unique<ReshapeLayerFunction>(); |
| 653 | func->configure(input, output); |
| 654 | |
| 655 | // Log info |
| 656 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() |
| 657 | << " Target " << TargetInfo::TargetType |
| 658 | << " Data Type: " << input->info()->data_type() |
| 659 | << " Input shape: " << input->info()->tensor_shape() |
| 660 | << " Output shape: " << output->info()->tensor_shape() |
| 661 | << std::endl); |
| 662 | |
| 663 | return std::move(func); |
| 664 | } |
| 665 | |
| 666 | /** Create a backend resize layer function |
| 667 | * |
| 668 | * @tparam ResizeLayerFunction Backend resize function |
| 669 | * @tparam TargetInfo Target-specific information |
| 670 | * |
| 671 | * @param[in] node Node to create the backend function for |
| 672 | * |
| 673 | * @return Backend resize layer function |
| 674 | */ |
| 675 | template <typename ResizeLayerFunction, typename TargetInfo> |
| 676 | std::unique_ptr<IFunction> create_resize_layer(ResizeLayerNode &node) |
| 677 | { |
| 678 | validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */); |
| 679 | |
| 680 | // Extract IO and info |
| 681 | typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0)); |
| 682 | typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0)); |
| 683 | ARM_COMPUTE_ERROR_ON(input == nullptr); |
| 684 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
| 685 | const InterpolationPolicy policy = node.policy(); |
| 686 | |
| 687 | // Create and configure function |
| 688 | auto func = support::cpp14::make_unique<ResizeLayerFunction>(); |
| 689 | func->configure(input, output, policy, BorderMode::CONSTANT); |
| 690 | |
| 691 | // Log info |
| 692 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() |
| 693 | << " Target " << TargetInfo::TargetType |
| 694 | << " Data Type: " << input->info()->data_type() |
| 695 | << " Input shape: " << input->info()->tensor_shape() |
| 696 | << " Output shape: " << output->info()->tensor_shape() |
| 697 | << " Interpolation: " << policy |
| 698 | << std::endl); |
| 699 | |
| 700 | return std::move(func); |
| 701 | } |
| 702 | |
| 703 | /** Create a backend softmax layer function |
| 704 | * |
| 705 | * @tparam SoftmaxLayerFunction Backend softmax function |
| 706 | * @tparam TargetInfo Target-specific information |
| 707 | * |
| 708 | * @param[in] node Node to create the backend function for |
| 709 | * @param[in] ctx Graph context |
| 710 | * |
| 711 | * @return Backend softmax layer function |
| 712 | */ |
| 713 | template <typename SoftmaxLayerFunction, typename TargetInfo> |
| 714 | std::unique_ptr<IFunction> create_softmax_layer(SoftmaxLayerNode &node, GraphContext &ctx) |
| 715 | { |
| 716 | validate_node<TargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */); |
| 717 | |
| 718 | // Extract IO and info |
| 719 | typename TargetInfo::TensorType *input = get_backing_tensor<TargetInfo>(node.input(0)); |
| 720 | typename TargetInfo::TensorType *output = get_backing_tensor<TargetInfo>(node.output(0)); |
| 721 | const float beta = node.beta(); |
| 722 | ARM_COMPUTE_ERROR_ON(input == nullptr); |
| 723 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
| 724 | |
| 725 | // Create and configure function |
| 726 | auto func = support::cpp14::make_unique<SoftmaxLayerFunction>(get_memory_manager(ctx, TargetInfo::TargetType)); |
| 727 | func->configure(input, output, beta); |
| 728 | |
| 729 | // Log info |
| 730 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() |
| 731 | << " Target " << TargetInfo::TargetType |
| 732 | << " Data Type: " << input->info()->data_type() |
| 733 | << " Input shape: " << input->info()->tensor_shape() |
| 734 | << " Output shape: " << output->info()->tensor_shape() |
| 735 | << std::endl); |
| 736 | |
| 737 | return std::move(func); |
| 738 | } |
| 739 | } // namespace detail |
| 740 | } // namespace backends |
| 741 | } // namespace graph |
| 742 | } // namespace arm_compute |
| 743 | |
| 744 | #endif /* __ARM_COMPUTE_GRAPH_BACKENDS_DETAIL_FUNCTION_HELPERS_H__ */ |