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/backends/NEON/NEFunctionFactory.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/utils/misc/Cast.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 27 | #include "arm_compute/graph/Graph.h" |
| 28 | #include "arm_compute/graph/GraphContext.h" |
| 29 | #include "arm_compute/graph/Logger.h" |
| 30 | #include "arm_compute/graph/TypePrinter.h" |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 31 | #include "arm_compute/graph/backends/FunctionHelpers.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 32 | #include "arm_compute/graph/backends/Utils.h" |
| 33 | #include "arm_compute/graph/nodes/Nodes.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 34 | #include "arm_compute/runtime/NEON/NEFunctions.h" |
| 35 | #include "support/ToolchainSupport.h" |
| 36 | |
| 37 | using namespace arm_compute::utils::cast; |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 41 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 42 | { |
| 43 | namespace backends |
| 44 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 45 | /** Target specific information structure used to pass information to the layer templates */ |
| 46 | struct NETargetInfo |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 47 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 48 | using TensorType = arm_compute::ITensor; |
| 49 | static Target TargetType; |
| 50 | }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 51 | |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 52 | Target NETargetInfo::TargetType = Target::NEON; |
| 53 | |
| 54 | /** Collection of CL convolution functions */ |
| 55 | struct NEConvolutionLayerFunctions |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 56 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 57 | using GenericConvolutionLayer = NEConvolutionLayer; |
| 58 | using GEMMConvolutionLayer = NEGEMMConvolutionLayer; |
| 59 | using DirectConvolutionLayer = NEDirectConvolutionLayer; |
| 60 | using WinogradConvolutionLayer = NEWinogradConvolutionLayer; |
| 61 | }; |
| 62 | |
| 63 | /** Collection of CL depthwise convolution functions */ |
| 64 | struct NEDepthwiseConvolutionLayerFunctions |
| 65 | { |
| 66 | using GenericDepthwiseConvolutionLayer = NEDepthwiseConvolutionLayer; |
| 67 | using DepthwiseConvolutionLayer3x3 = NEDepthwiseConvolutionLayer3x3; |
| 68 | }; |
| 69 | |
| 70 | /** Collection of CL element-wise functions */ |
| 71 | struct NEEltwiseFunctions |
| 72 | { |
| 73 | using Addition = NEArithmeticAddition; |
| 74 | using Subtraction = NEArithmeticSubtraction; |
| 75 | using Multiplication = NEPixelWiseMultiplication; |
| 76 | }; |
| 77 | |
| 78 | namespace detail |
| 79 | { |
| 80 | // Specialize functions |
| 81 | template <> |
| 82 | std::unique_ptr<IFunction> create_convolution_layer<NEConvolutionLayerFunctions, NETargetInfo>(ConvolutionLayerNode &node, |
| 83 | GraphContext &ctx) |
| 84 | { |
| 85 | validate_node<NETargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 86 | |
| 87 | // Extract IO and info |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 88 | NETargetInfo::TensorType *input = get_backing_tensor<NETargetInfo>(node.input(0)); |
| 89 | NETargetInfo::TensorType *weights = get_backing_tensor<NETargetInfo>(node.input(1)); |
| 90 | NETargetInfo::TensorType *biases = get_backing_tensor<NETargetInfo>(node.input(2)); |
| 91 | NETargetInfo::TensorType *output = get_backing_tensor<NETargetInfo>(node.output(0)); |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 92 | |
| 93 | if(is_data_type_quantized_asymmetric(input->info()->data_type())) |
| 94 | { |
| 95 | biases->info()->set_data_type(DataType::S32); |
| 96 | } |
| 97 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 98 | const PadStrideInfo conv_info = node.convolution_info(); |
| 99 | const ConvolutionMethod conv_algorithm = node.convolution_method(); |
| 100 | |
| 101 | // Create and configure function (we assume that functions have been validated before creation) |
| 102 | std::shared_ptr<IMemoryManager> mm = get_memory_manager(ctx, Target::NEON); |
| 103 | std::unique_ptr<IFunction> func; |
| 104 | std::string func_name; |
| 105 | if(conv_algorithm == ConvolutionMethod::DIRECT) |
| 106 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 107 | std::tie(func, func_name) = create_named_memory_managed_function<NEDirectConvolutionLayer>( |
| 108 | std::string("DirectConvolutionLayer"), mm, input, weights, biases, output, conv_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 109 | } |
| 110 | else if(conv_algorithm == ConvolutionMethod::GEMM) |
| 111 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 112 | std::tie(func, func_name) = create_named_memory_managed_function<NEGEMMConvolutionLayer>( |
| 113 | std::string("GEMMConvolutionLayer"), mm, input, weights, biases, output, conv_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 114 | } |
| 115 | else if(conv_algorithm == ConvolutionMethod::WINOGRAD) |
| 116 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 117 | std::tie(func, func_name) = create_named_memory_managed_function<NEWinogradConvolutionLayer>( |
| 118 | std::string("WinogradConvolutionLayer"), mm, input, weights, biases, output, conv_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 119 | } |
| 120 | else |
| 121 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 122 | std::tie(func, func_name) = create_named_memory_managed_function<NEConvolutionLayer>( |
| 123 | std::string("ConvolutionLayer"), mm, input, weights, biases, output, conv_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Log info |
| 127 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << func_name |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 128 | << " Target " << NETargetInfo::TargetType |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 129 | << " Data Type: " << input->info()->data_type() |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 130 | << " Input QuantInfo: " << input->info()->quantization_info() |
| 131 | << " Weights QuantInfo: " << weights->info()->quantization_info() |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 132 | << " Input shape: " << input->info()->tensor_shape() |
| 133 | << " Weights shape: " << weights->info()->tensor_shape() |
| 134 | << " Output shape: " << output->info()->tensor_shape() |
| 135 | << std::endl); |
| 136 | return func; |
| 137 | } |
| 138 | |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 139 | template <> |
| 140 | std::unique_ptr<IFunction> create_normalization_layer<NENormalizationLayer, NETargetInfo>(NormalizationLayerNode &node, GraphContext &ctx) |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 141 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 142 | validate_node<NETargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 143 | |
| 144 | // Extract IO and info |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 145 | NETargetInfo::TensorType *input = get_backing_tensor<NETargetInfo>(node.input(0)); |
| 146 | NETargetInfo::TensorType *output = get_backing_tensor<NETargetInfo>(node.output(0)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 147 | const NormalizationLayerInfo norm_info = node.normalization_info(); |
| 148 | ARM_COMPUTE_ERROR_ON(input == nullptr); |
| 149 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
| 150 | |
| 151 | // Create and configure function |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 152 | auto func = support::cpp14::make_unique<NENormalizationLayer>(get_memory_manager(ctx, NETargetInfo::TargetType)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 153 | func->configure(input, output, norm_info); |
| 154 | |
| 155 | // Log info |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 156 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() |
| 157 | << " Target " << NETargetInfo::TargetType |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 158 | << " Data Type: " << input->info()->data_type() |
| 159 | << " Input shape: " << input->info()->tensor_shape() |
| 160 | << " Output shape: " << output->info()->tensor_shape() |
| 161 | << " Normalization info: " << norm_info.type() |
| 162 | << std::endl); |
| 163 | |
| 164 | return std::move(func); |
| 165 | } |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 166 | } // namespace detail |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 167 | |
| 168 | std::unique_ptr<IFunction> NEFunctionFactory::create(INode *node, GraphContext &ctx) |
| 169 | { |
| 170 | if(node == nullptr) |
| 171 | { |
| 172 | return nullptr; |
| 173 | } |
| 174 | |
| 175 | NodeType type = node->type(); |
| 176 | switch(type) |
| 177 | { |
| 178 | case NodeType::ActivationLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 179 | return detail::create_activation_layer<NEActivationLayer, NETargetInfo>(*polymorphic_downcast<ActivationLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 180 | case NodeType::BatchNormalizationLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 181 | return detail::create_batch_normalization_layer<NEBatchNormalizationLayer, NETargetInfo>(*polymorphic_downcast<BatchNormalizationLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 182 | case NodeType::ConvolutionLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 183 | return detail::create_convolution_layer<NEConvolutionLayerFunctions, NETargetInfo>(*polymorphic_downcast<ConvolutionLayerNode *>(node), ctx); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 184 | case NodeType::DeconvolutionLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 185 | return detail::create_deconvolution_layer<NEDeconvolutionLayer, NETargetInfo>(*polymorphic_downcast<DeconvolutionLayerNode *>(node), ctx); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 186 | case NodeType::DepthConcatenateLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 187 | return detail::create_depth_concatenate_layer<NEDepthConcatenateLayer, NETargetInfo>(*polymorphic_downcast<DepthConcatenateLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 188 | case NodeType::DepthwiseConvolutionLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 189 | return detail::create_depthwise_convolution_layer<NEDepthwiseConvolutionLayerFunctions, NETargetInfo>(*polymorphic_downcast<DepthwiseConvolutionLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 190 | case NodeType::EltwiseLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 191 | return detail::create_eltwise_layer<NEEltwiseFunctions, NETargetInfo>(*polymorphic_downcast<EltwiseLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 192 | case NodeType::FlattenLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 193 | return detail::create_flatten_layer<NEFlattenLayer, NETargetInfo>(*polymorphic_downcast<FlattenLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 194 | case NodeType::FullyConnectedLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 195 | return detail::create_fully_connected_layer<NEFullyConnectedLayer, NETargetInfo>(*polymorphic_downcast<FullyConnectedLayerNode *>(node), ctx); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 196 | case NodeType::NormalizationLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 197 | return detail::create_normalization_layer<NENormalizationLayer, NETargetInfo>(*polymorphic_downcast<NormalizationLayerNode *>(node), ctx); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 198 | case NodeType::PoolingLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 199 | return detail::create_pooling_layer<NEPoolingLayer, NETargetInfo>(*polymorphic_downcast<PoolingLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 200 | case NodeType::ReshapeLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 201 | return detail::create_reshape_layer<NEReshapeLayer, NETargetInfo>(*polymorphic_downcast<ReshapeLayerNode *>(node)); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 202 | case NodeType::ResizeLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 203 | return detail::create_resize_layer<NEScale, NETargetInfo>(*polymorphic_downcast<ResizeLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 204 | case NodeType::SoftmaxLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame^] | 205 | return detail::create_softmax_layer<NESoftmaxLayer, NETargetInfo>(*polymorphic_downcast<SoftmaxLayerNode *>(node), ctx); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 206 | default: |
| 207 | return nullptr; |
| 208 | } |
| 209 | } |
| 210 | } // namespace backends |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 211 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 212 | } // namespace arm_compute |