Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 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" |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 34 | #include "arm_compute/runtime/CPP/CPPFunctions.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 35 | #include "arm_compute/runtime/NEON/NEFunctions.h" |
| 36 | #include "support/ToolchainSupport.h" |
| 37 | |
| 38 | using namespace arm_compute::utils::cast; |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 42 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 43 | { |
| 44 | namespace backends |
| 45 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 46 | /** Target specific information structure used to pass information to the layer templates */ |
| 47 | struct NETargetInfo |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 48 | { |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 49 | using TensorType = arm_compute::ITensor; |
| 50 | using TensorConcreteType = arm_compute::Tensor; |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 51 | static Target TargetType; |
| 52 | }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 53 | |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 54 | Target NETargetInfo::TargetType = Target::NEON; |
| 55 | |
Sheri Zhang | 16dddd2 | 2020-05-27 15:03:48 +0100 | [diff] [blame] | 56 | /** Collection of NEON convolution functions */ |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 57 | struct NEConvolutionLayerFunctions |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 58 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 59 | using GenericConvolutionLayer = NEConvolutionLayer; |
| 60 | using GEMMConvolutionLayer = NEGEMMConvolutionLayer; |
| 61 | using DirectConvolutionLayer = NEDirectConvolutionLayer; |
| 62 | using WinogradConvolutionLayer = NEWinogradConvolutionLayer; |
| 63 | }; |
| 64 | |
Sheri Zhang | 16dddd2 | 2020-05-27 15:03:48 +0100 | [diff] [blame] | 65 | /** Collection of NEON element-wise functions */ |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 66 | struct NEEltwiseFunctions |
| 67 | { |
| 68 | using Addition = NEArithmeticAddition; |
| 69 | using Subtraction = NEArithmeticSubtraction; |
| 70 | using Multiplication = NEPixelWiseMultiplication; |
| 71 | }; |
| 72 | |
Sheri Zhang | 16dddd2 | 2020-05-27 15:03:48 +0100 | [diff] [blame] | 73 | /** Collection of NEON unary element-wise functions */ |
| 74 | struct NEUnaryEltwiseFunctions |
| 75 | { |
| 76 | using Exp = NEExpLayer; |
| 77 | }; |
| 78 | |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 79 | /** Function and tensor types to be used inside a NEON fused convolution/batch normalization layer */ |
| 80 | struct NEFusedLayerTypes |
| 81 | { |
Manuel Bottini | bffb41e | 2019-06-20 16:00:27 +0100 | [diff] [blame] | 82 | using ConvolutionLayer = NEConvolutionLayer; |
| 83 | using DepthwiseConvolutionLayer = NEDepthwiseConvolutionLayer; |
| 84 | using FuseBatchNormalization = NEFuseBatchNormalization; |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 87 | namespace detail |
| 88 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 89 | template <> |
| 90 | 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] | 91 | { |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 92 | validate_node<NETargetInfo>(node, 1 /* expected inputs */, 1 /* expected outputs */); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 93 | |
| 94 | // Extract IO and info |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 95 | NETargetInfo::TensorType *input = get_backing_tensor<NETargetInfo>(node.input(0)); |
| 96 | NETargetInfo::TensorType *output = get_backing_tensor<NETargetInfo>(node.output(0)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 97 | const NormalizationLayerInfo norm_info = node.normalization_info(); |
| 98 | ARM_COMPUTE_ERROR_ON(input == nullptr); |
| 99 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
| 100 | |
| 101 | // Create and configure function |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 102 | 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] | 103 | func->configure(input, output, norm_info); |
| 104 | |
| 105 | // Log info |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 106 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " |
| 107 | << node.name() |
| 108 | << " Type: " << node.type() |
| 109 | << " Target: " << NETargetInfo::TargetType |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 110 | << " Data Type: " << input->info()->data_type() |
| 111 | << " Input shape: " << input->info()->tensor_shape() |
| 112 | << " Output shape: " << output->info()->tensor_shape() |
| 113 | << " Normalization info: " << norm_info.type() |
| 114 | << std::endl); |
| 115 | |
Georgios Pinitas | 0b192e8 | 2020-02-20 17:09:28 +0000 | [diff] [blame] | 116 | return RETURN_UNIQUE_PTR(func); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 117 | } |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 118 | } // namespace detail |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 119 | |
| 120 | std::unique_ptr<IFunction> NEFunctionFactory::create(INode *node, GraphContext &ctx) |
| 121 | { |
| 122 | if(node == nullptr) |
| 123 | { |
| 124 | return nullptr; |
| 125 | } |
| 126 | |
| 127 | NodeType type = node->type(); |
| 128 | switch(type) |
| 129 | { |
| 130 | case NodeType::ActivationLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 131 | return detail::create_activation_layer<NEActivationLayer, NETargetInfo>(*polymorphic_downcast<ActivationLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 132 | case NodeType::BatchNormalizationLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 133 | return detail::create_batch_normalization_layer<NEBatchNormalizationLayer, NETargetInfo>(*polymorphic_downcast<BatchNormalizationLayerNode *>(node)); |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 134 | case NodeType::ChannelShuffleLayer: |
| 135 | return detail::create_channel_shuffle_layer<NEChannelShuffleLayer, NETargetInfo>(*polymorphic_downcast<ChannelShuffleLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 136 | case NodeType::ConvolutionLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 137 | return detail::create_convolution_layer<NEConvolutionLayerFunctions, NETargetInfo>(*polymorphic_downcast<ConvolutionLayerNode *>(node), ctx); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 138 | case NodeType::DeconvolutionLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 139 | return detail::create_deconvolution_layer<NEDeconvolutionLayer, NETargetInfo>(*polymorphic_downcast<DeconvolutionLayerNode *>(node), ctx); |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 140 | case NodeType::ConcatenateLayer: |
| 141 | return detail::create_concatenate_layer<NEConcatenateLayer, NETargetInfo>(*polymorphic_downcast<ConcatenateLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 142 | case NodeType::DepthwiseConvolutionLayer: |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 143 | return detail::create_depthwise_convolution_layer<NEDepthwiseConvolutionLayer, NETargetInfo>(*polymorphic_downcast<DepthwiseConvolutionLayerNode *>(node)); |
Isabella Gottardi | cd4e9ab | 2019-11-05 17:50:27 +0000 | [diff] [blame] | 144 | case NodeType::DequantizationLayer: |
| 145 | return detail::create_dequantization_layer<NEDequantizationLayer, NETargetInfo>(*polymorphic_downcast<DequantizationLayerNode *>(node)); |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 146 | case NodeType::DetectionOutputLayer: |
| 147 | return detail::create_detection_output_layer<CPPDetectionOutputLayer, NETargetInfo>(*polymorphic_downcast<DetectionOutputLayerNode *>(node)); |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 148 | case NodeType::DetectionPostProcessLayer: |
Giuseppe Rossini | d985378 | 2019-10-25 11:11:44 +0100 | [diff] [blame] | 149 | return detail::create_detection_post_process_layer<NEDetectionPostProcessLayer, NETargetInfo>(*polymorphic_downcast<DetectionPostProcessLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 150 | case NodeType::EltwiseLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 151 | return detail::create_eltwise_layer<NEEltwiseFunctions, NETargetInfo>(*polymorphic_downcast<EltwiseLayerNode *>(node)); |
Sheri Zhang | 16dddd2 | 2020-05-27 15:03:48 +0100 | [diff] [blame] | 152 | case NodeType::UnaryEltwiseLayer: |
| 153 | return detail::create_unary_eltwise_layer<NEUnaryEltwiseFunctions, NETargetInfo>(*polymorphic_downcast<UnaryEltwiseLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 154 | case NodeType::FlattenLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 155 | return detail::create_flatten_layer<NEFlattenLayer, NETargetInfo>(*polymorphic_downcast<FlattenLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 156 | case NodeType::FullyConnectedLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 157 | return detail::create_fully_connected_layer<NEFullyConnectedLayer, NETargetInfo>(*polymorphic_downcast<FullyConnectedLayerNode *>(node), ctx); |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 158 | case NodeType::FusedConvolutionBatchNormalizationLayer: |
Gian Marco Iodice | 5dea19e | 2019-11-08 12:13:48 +0000 | [diff] [blame] | 159 | return detail::create_fused_convolution_batch_normalization_layer<NEFusedLayerTypes, NETargetInfo>(*polymorphic_downcast<FusedConvolutionBatchNormalizationNode *>(node), ctx); |
Manuel Bottini | cf3abde | 2019-07-29 16:59:41 +0100 | [diff] [blame] | 160 | case NodeType::FusedDepthwiseConvolutionBatchNormalizationLayer: |
Gian Marco Iodice | 5dea19e | 2019-11-08 12:13:48 +0000 | [diff] [blame] | 161 | return detail::create_fused_depthwise_convolution_batch_normalization_layer<NEFusedLayerTypes, NETargetInfo>(*polymorphic_downcast<FusedDepthwiseConvolutionBatchNormalizationNode *>(node), ctx); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 162 | case NodeType::NormalizationLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 163 | return detail::create_normalization_layer<NENormalizationLayer, NETargetInfo>(*polymorphic_downcast<NormalizationLayerNode *>(node), ctx); |
Georgios Pinitas | 07fbe37 | 2020-02-04 13:21:02 +0000 | [diff] [blame] | 164 | case NodeType::PadLayer: |
| 165 | return detail::create_pad_layer<NEPadLayer, NETargetInfo>(*polymorphic_downcast<PadLayerNode *>(node)); |
Georgios Pinitas | 57c4824 | 2018-08-02 13:41:49 +0100 | [diff] [blame] | 166 | case NodeType::PermuteLayer: |
| 167 | return detail::create_permute_layer<NEPermute, NETargetInfo>(*polymorphic_downcast<PermuteLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 168 | case NodeType::PoolingLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 169 | return detail::create_pooling_layer<NEPoolingLayer, NETargetInfo>(*polymorphic_downcast<PoolingLayerNode *>(node)); |
Georgios Pinitas | f8c4749 | 2020-02-04 17:39:59 +0000 | [diff] [blame] | 170 | case NodeType::PReluLayer: |
| 171 | return detail::create_prelu_layer<NEPReluLayer, NETargetInfo>(*polymorphic_downcast<PReluLayerNode *>(node)); |
Giorgio Arena | 6e9d0e0 | 2020-01-03 15:02:04 +0000 | [diff] [blame] | 172 | case NodeType::PrintLayer: |
| 173 | return detail::create_print_layer<NETargetInfo>(*polymorphic_downcast<PrintLayerNode *>(node)); |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 174 | case NodeType::PriorBoxLayer: |
| 175 | return detail::create_priorbox_layer<NEPriorBoxLayer, NETargetInfo>(*polymorphic_downcast<PriorBoxLayerNode *>(node)); |
Isabella Gottardi | 3db1ba9 | 2019-05-17 12:35:20 +0100 | [diff] [blame] | 176 | case NodeType::QuantizationLayer: |
| 177 | return detail::create_quantization_layer<NEQuantizationLayer, NETargetInfo>(*polymorphic_downcast<QuantizationLayerNode *>(node)); |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 178 | case NodeType::ReorgLayer: |
| 179 | return detail::create_reorg_layer<NEReorgLayer, NETargetInfo>(*polymorphic_downcast<ReorgLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 180 | case NodeType::ReshapeLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 181 | return detail::create_reshape_layer<NEReshapeLayer, NETargetInfo>(*polymorphic_downcast<ReshapeLayerNode *>(node)); |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 182 | case NodeType::ResizeLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 183 | return detail::create_resize_layer<NEScale, NETargetInfo>(*polymorphic_downcast<ResizeLayerNode *>(node)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 184 | case NodeType::SoftmaxLayer: |
Georgios Pinitas | da2491f | 2018-06-01 17:49:09 +0100 | [diff] [blame] | 185 | return detail::create_softmax_layer<NESoftmaxLayer, NETargetInfo>(*polymorphic_downcast<SoftmaxLayerNode *>(node), ctx); |
Michele Di Giorgio | ec69975 | 2019-03-22 15:25:32 +0000 | [diff] [blame] | 186 | case NodeType::StackLayer: |
| 187 | return detail::create_stack_layer<NEStackLayer, NETargetInfo>(*polymorphic_downcast<StackLayerNode *>(node)); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 188 | case NodeType::UpsampleLayer: |
| 189 | return detail::create_upsample_layer<NEUpsampleLayer, NETargetInfo>(*polymorphic_downcast<UpsampleLayerNode *>(node), ctx); |
Michalis Spyrou | e22aa13 | 2018-09-13 10:35:33 +0100 | [diff] [blame] | 190 | case NodeType::YOLOLayer: |
| 191 | return detail::create_yolo_layer<NEYOLOLayer, NETargetInfo>(*polymorphic_downcast<YOLOLayerNode *>(node), ctx); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 192 | default: |
| 193 | return nullptr; |
| 194 | } |
| 195 | } |
| 196 | } // namespace backends |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 197 | } // namespace graph |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 198 | } // namespace arm_compute |