blob: d41da4bf7fbdd4029de0049338e92de775747156 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Georgios Pinitasd8734b52017-12-22 15:27:52 +00003 *
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 Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph/backends/CL/CLFunctionFactory.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000025
26#include "arm_compute/core/utils/misc/Cast.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010027#include "arm_compute/graph/Graph.h"
Gian Marco Iodice5dea19e2019-11-08 12:13:48 +000028#include "arm_compute/graph/GraphContext.h"
Georgios Pinitasda2491f2018-06-01 17:49:09 +010029#include "arm_compute/graph/backends/FunctionHelpers.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000030#include "arm_compute/runtime/CL/CLFunctions.h"
Isabella Gottardi7234ed82018-11-27 08:51:10 +000031#include "arm_compute/runtime/CPP/CPPFunctions.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000032
Georgios Pinitasd8734b52017-12-22 15:27:52 +000033using namespace arm_compute::utils::cast;
34
35namespace arm_compute
36{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010037namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000038{
39namespace backends
40{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010041/** Target specific information structure used to pass information to the layer templates */
42struct CLTargetInfo
Georgios Pinitasd8734b52017-12-22 15:27:52 +000043{
giuros01acce5042019-02-21 17:32:34 +000044 using TensorType = arm_compute::ICLTensor;
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +010045 using SrcTensorType = const arm_compute::ICLTensor;
giuros01acce5042019-02-21 17:32:34 +000046 using TensorConcreteType = CLTensor;
Georgios Pinitasda2491f2018-06-01 17:49:09 +010047 static Target TargetType;
48};
49
50Target CLTargetInfo::TargetType = Target::CL;
51
52/** Collection of CL convolution functions */
53struct CLConvolutionLayerFunctions
Georgios Pinitasd8734b52017-12-22 15:27:52 +000054{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010055 using GenericConvolutionLayer = CLConvolutionLayer;
56 using GEMMConvolutionLayer = CLGEMMConvolutionLayer;
57 using DirectConvolutionLayer = CLDirectConvolutionLayer;
58 using WinogradConvolutionLayer = CLWinogradConvolutionLayer;
59};
Georgios Pinitasd8734b52017-12-22 15:27:52 +000060
Georgios Pinitasda2491f2018-06-01 17:49:09 +010061/** Collection of CL element-wise functions */
62struct CLEltwiseFunctions
Georgios Pinitasd8734b52017-12-22 15:27:52 +000063{
Georgios Pinitasda2491f2018-06-01 17:49:09 +010064 using Addition = CLArithmeticAddition;
65 using Subtraction = CLArithmeticSubtraction;
66 using Multiplication = CLPixelWiseMultiplication;
67};
giuros01acce5042019-02-21 17:32:34 +000068
Sheri Zhang16dddd22020-05-27 15:03:48 +010069/** Collection of CL unary element-wise functions */
70struct CLUnaryEltwiseFunctions
71{
72 using Exp = CLExpLayer;
73};
74
giuros01acce5042019-02-21 17:32:34 +000075/** Function and tensor types to be used inside a CL fused convolution/batch normalization layer */
76struct CLFusedLayerTypes
77{
Manuel Bottinibffb41e2019-06-20 16:00:27 +010078 using ConvolutionLayer = CLConvolutionLayer;
79 using DepthwiseConvolutionLayer = CLDepthwiseConvolutionLayer;
80 using FuseBatchNormalization = CLFuseBatchNormalization;
giuros01acce5042019-02-21 17:32:34 +000081};
82
Isabella Gottardi7234ed82018-11-27 08:51:10 +000083// TODO (isagot01): Remove once we support heterogeneous scheduling at function level
84/** Wrapper for the CPP Function in the OpenCL backend **/
85class CPPWrapperFunction : public IFunction
86{
87public:
88 /* Default constructor */
89 CPPWrapperFunction()
90 : _tensors(), _func(nullptr)
91 {
92 }
93
94 void run() override
95 {
96 for(auto &tensor : _tensors)
97 {
98 tensor->map(CLScheduler::get().queue());
99 }
100 _func->run();
101
102 for(auto &tensor : _tensors)
103 {
104 tensor->unmap(CLScheduler::get().queue());
105 }
106 }
107
108 void register_tensor(ICLTensor *tensor)
109 {
110 _tensors.push_back(tensor);
111 }
112
113 void register_function(std::unique_ptr<IFunction> function)
114 {
115 _func = std::move(function);
116 }
117
118private:
119 std::vector<arm_compute::ICLTensor *> _tensors;
120 std::unique_ptr<IFunction> _func;
121};
122
123namespace detail
124{
125// Specialized functions
126template <>
127std::unique_ptr<IFunction> create_detection_output_layer<CPPDetectionOutputLayer, CLTargetInfo>(DetectionOutputLayerNode &node)
128{
129 validate_node<CLTargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
130
131 // Extract IO and info
132 CLTargetInfo::TensorType *input0 = get_backing_tensor<CLTargetInfo>(node.input(0));
133 CLTargetInfo::TensorType *input1 = get_backing_tensor<CLTargetInfo>(node.input(1));
134 CLTargetInfo::TensorType *input2 = get_backing_tensor<CLTargetInfo>(node.input(2));
135 CLTargetInfo::TensorType *output = get_backing_tensor<CLTargetInfo>(node.output(0));
136 const DetectionOutputLayerInfo detect_info = node.detection_output_info();
137
138 ARM_COMPUTE_ERROR_ON(input0 == nullptr);
139 ARM_COMPUTE_ERROR_ON(input1 == nullptr);
140 ARM_COMPUTE_ERROR_ON(input2 == nullptr);
141 ARM_COMPUTE_ERROR_ON(output == nullptr);
142
143 // Create and configure function
144 auto func = support::cpp14::make_unique<CPPDetectionOutputLayer>();
145 func->configure(input0, input1, input2, output, detect_info);
146
147 // Log info
148 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
149 << node.name()
150 << " Type: " << node.type()
151 << " Target: " << CLTargetInfo::TargetType
152 << " Data Type: " << input0->info()->data_type()
153 << " Input0 shape: " << input0->info()->tensor_shape()
154 << " Input1 shape: " << input1->info()->tensor_shape()
155 << " Input2 shape: " << input2->info()->tensor_shape()
156 << " Output shape: " << output->info()->tensor_shape()
157 << " DetectionOutputLayer info: " << detect_info
158 << std::endl);
159
160 auto wrap_function = support::cpp14::make_unique<CPPWrapperFunction>();
Giorgio Arena6e9d0e02020-01-03 15:02:04 +0000161
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000162 wrap_function->register_function(std::move(func));
163 wrap_function->register_tensor(input0);
164 wrap_function->register_tensor(input1);
165 wrap_function->register_tensor(input2);
166 wrap_function->register_tensor(output);
167
Georgios Pinitas0b192e82020-02-20 17:09:28 +0000168 return RETURN_UNIQUE_PTR(wrap_function);
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000169}
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000170template <>
171std::unique_ptr<IFunction> create_detection_post_process_layer<CPPDetectionPostProcessLayer, CLTargetInfo>(DetectionPostProcessLayerNode &node)
172{
173 validate_node<CLTargetInfo>(node, 3 /* expected inputs */, 4 /* expected outputs */);
174
175 // Extract IO and info
176 CLTargetInfo::TensorType *input0 = get_backing_tensor<CLTargetInfo>(node.input(0));
177 CLTargetInfo::TensorType *input1 = get_backing_tensor<CLTargetInfo>(node.input(1));
178 CLTargetInfo::TensorType *input2 = get_backing_tensor<CLTargetInfo>(node.input(2));
179 CLTargetInfo::TensorType *output0 = get_backing_tensor<CLTargetInfo>(node.output(0));
180 CLTargetInfo::TensorType *output1 = get_backing_tensor<CLTargetInfo>(node.output(1));
181 CLTargetInfo::TensorType *output2 = get_backing_tensor<CLTargetInfo>(node.output(2));
182 CLTargetInfo::TensorType *output3 = get_backing_tensor<CLTargetInfo>(node.output(3));
183 const DetectionPostProcessLayerInfo detect_info = node.detection_post_process_info();
184
185 ARM_COMPUTE_ERROR_ON(input0 == nullptr);
186 ARM_COMPUTE_ERROR_ON(input1 == nullptr);
187 ARM_COMPUTE_ERROR_ON(input2 == nullptr);
188 ARM_COMPUTE_ERROR_ON(output0 == nullptr);
189 ARM_COMPUTE_ERROR_ON(output1 == nullptr);
190 ARM_COMPUTE_ERROR_ON(output2 == nullptr);
191 ARM_COMPUTE_ERROR_ON(output3 == nullptr);
192
193 // Create and configure function
194 auto func = support::cpp14::make_unique<CPPDetectionPostProcessLayer>();
195 func->configure(input0, input1, input2, output0, output1, output2, output3, detect_info);
196
197 // Log info
198 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated "
199 << node.name()
200 << " Type: " << node.type()
201 << " Target: " << CLTargetInfo::TargetType
202 << " Data Type: " << input0->info()->data_type()
203 << " Input0 shape: " << input0->info()->tensor_shape()
204 << " Input1 shape: " << input1->info()->tensor_shape()
205 << " Input2 shape: " << input2->info()->tensor_shape()
206 << " Output0 shape: " << output0->info()->tensor_shape()
207 << " Output1 shape: " << output1->info()->tensor_shape()
208 << " Output2 shape: " << output2->info()->tensor_shape()
209 << " Output3 shape: " << output3->info()->tensor_shape()
210 << " DetectionPostProcessLayer info: " << detect_info
211 << std::endl);
212
213 auto wrap_function = support::cpp14::make_unique<CPPWrapperFunction>();
214
215 wrap_function->register_function(std::move(func));
216 wrap_function->register_tensor(input0);
217 wrap_function->register_tensor(input1);
218 wrap_function->register_tensor(input2);
219 wrap_function->register_tensor(output0);
220 wrap_function->register_tensor(output1);
221 wrap_function->register_tensor(output2);
222 wrap_function->register_tensor(output3);
223
Georgios Pinitas0b192e82020-02-20 17:09:28 +0000224 return RETURN_UNIQUE_PTR(wrap_function);
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000225}
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000226} // namespace detail
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000227
228std::unique_ptr<IFunction> CLFunctionFactory::create(INode *node, GraphContext &ctx)
229{
230 if(node == nullptr)
231 {
232 return nullptr;
233 }
234
235 NodeType type = node->type();
236 switch(type)
237 {
238 case NodeType::ActivationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100239 return detail::create_activation_layer<CLActivationLayer, CLTargetInfo>(*polymorphic_downcast<ActivationLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000240 case NodeType::BatchNormalizationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100241 return detail::create_batch_normalization_layer<CLBatchNormalizationLayer, CLTargetInfo>(*polymorphic_downcast<BatchNormalizationLayerNode *>(node));
Manuel Bottinid2048ce2018-10-23 17:00:42 +0100242 case NodeType::BoundingBoxTransformLayer:
243 return detail::create_bounding_box_transform_layer<CLBoundingBoxTransform, CLTargetInfo>(*polymorphic_downcast<BoundingBoxTransformLayerNode *>(node));
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100244 case NodeType::ChannelShuffleLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100245 return detail::create_channel_shuffle_layer<CLChannelShuffleLayer, CLTargetInfo>(*polymorphic_downcast<ChannelShuffleLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000246 case NodeType::ConvolutionLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100247 return detail::create_convolution_layer<CLConvolutionLayerFunctions, CLTargetInfo>(*polymorphic_downcast<ConvolutionLayerNode *>(node), ctx);
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100248 case NodeType::DeconvolutionLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100249 return detail::create_deconvolution_layer<CLDeconvolutionLayer, CLTargetInfo>(*polymorphic_downcast<DeconvolutionLayerNode *>(node), ctx);
Georgios Pinitase2220552018-07-20 13:23:44 +0100250 case NodeType::ConcatenateLayer:
251 return detail::create_concatenate_layer<CLConcatenateLayer, CLTargetInfo>(*polymorphic_downcast<ConcatenateLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000252 case NodeType::DepthwiseConvolutionLayer:
Manuel Bottini05069f02019-09-26 17:18:26 +0100253 return detail::create_depthwise_convolution_layer<CLDepthwiseConvolutionLayer, CLTargetInfo>(*polymorphic_downcast<DepthwiseConvolutionLayerNode *>(node));
Isabella Gottardicd4e9ab2019-11-05 17:50:27 +0000254 case NodeType::DequantizationLayer:
255 return detail::create_dequantization_layer<CLDequantizationLayer, CLTargetInfo>(*polymorphic_downcast<DequantizationLayerNode *>(node));
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000256 case NodeType::DetectionOutputLayer:
257 return detail::create_detection_output_layer<CPPDetectionOutputLayer, CLTargetInfo>(*polymorphic_downcast<DetectionOutputLayerNode *>(node));
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000258 case NodeType::DetectionPostProcessLayer:
259 return detail::create_detection_post_process_layer<CPPDetectionPostProcessLayer, CLTargetInfo>(*polymorphic_downcast<DetectionPostProcessLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000260 case NodeType::EltwiseLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100261 return detail::create_eltwise_layer<CLEltwiseFunctions, CLTargetInfo>(*polymorphic_downcast<EltwiseLayerNode *>(node));
Sheri Zhang16dddd22020-05-27 15:03:48 +0100262 case NodeType::UnaryEltwiseLayer:
263 return detail::create_unary_eltwise_layer<CLUnaryEltwiseFunctions, CLTargetInfo>(*polymorphic_downcast<UnaryEltwiseLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000264 case NodeType::FlattenLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100265 return detail::create_flatten_layer<CLFlattenLayer, CLTargetInfo>(*polymorphic_downcast<FlattenLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000266 case NodeType::FullyConnectedLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100267 return detail::create_fully_connected_layer<CLFullyConnectedLayer, CLTargetInfo>(*polymorphic_downcast<FullyConnectedLayerNode *>(node), ctx);
giuros01acce5042019-02-21 17:32:34 +0000268 case NodeType::FusedConvolutionBatchNormalizationLayer:
Gian Marco Iodice5dea19e2019-11-08 12:13:48 +0000269 return detail::create_fused_convolution_batch_normalization_layer<CLFusedLayerTypes, CLTargetInfo>(*polymorphic_downcast<FusedConvolutionBatchNormalizationNode *>(node), ctx);
Manuel Bottinibffb41e2019-06-20 16:00:27 +0100270 case NodeType::FusedDepthwiseConvolutionBatchNormalizationLayer:
Gian Marco Iodice5dea19e2019-11-08 12:13:48 +0000271 return detail::create_fused_depthwise_convolution_batch_normalization_layer<CLFusedLayerTypes, CLTargetInfo>(*polymorphic_downcast<FusedDepthwiseConvolutionBatchNormalizationNode *>(node), ctx);
Manuel Bottini5209be52019-02-13 16:34:56 +0000272 case NodeType::GenerateProposalsLayer:
273 return detail::create_generate_proposals_layer<CLGenerateProposalsLayer, CLTargetInfo>(*polymorphic_downcast<GenerateProposalsLayerNode *>(node), ctx);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000274 case NodeType::NormalizationLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100275 return detail::create_normalization_layer<CLNormalizationLayer, CLTargetInfo>(*polymorphic_downcast<NormalizationLayerNode *>(node), ctx);
Michele Di Giorgio555d1102018-09-12 13:51:59 +0100276 case NodeType::NormalizePlanarYUVLayer:
277 return detail::create_normalize_planar_yuv_layer<CLNormalizePlanarYUVLayer, CLTargetInfo>(*polymorphic_downcast<NormalizePlanarYUVLayerNode *>(node));
Michele Di Giorgio4bb17332018-09-26 13:56:51 +0100278 case NodeType::PadLayer:
279 return detail::create_pad_layer<CLPadLayer, CLTargetInfo>(*polymorphic_downcast<PadLayerNode *>(node));
Georgios Pinitas57c48242018-08-02 13:41:49 +0100280 case NodeType::PermuteLayer:
281 return detail::create_permute_layer<CLPermute, CLTargetInfo>(*polymorphic_downcast<PermuteLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000282 case NodeType::PoolingLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100283 return detail::create_pooling_layer<CLPoolingLayer, CLTargetInfo>(*polymorphic_downcast<PoolingLayerNode *>(node));
Georgios Pinitasf8c47492020-02-04 17:39:59 +0000284 case NodeType::PReluLayer:
285 return detail::create_prelu_layer<CLPReluLayer, CLTargetInfo>(*polymorphic_downcast<PReluLayerNode *>(node));
Giorgio Arena6e9d0e02020-01-03 15:02:04 +0000286 case NodeType::PrintLayer:
287 return detail::create_print_layer<CLTargetInfo>(*polymorphic_downcast<PrintLayerNode *>(node));
Pablo Tello32521432018-11-15 14:43:10 +0000288 case NodeType::PriorBoxLayer:
289 return detail::create_priorbox_layer<CLPriorBoxLayer, CLTargetInfo>(*polymorphic_downcast<PriorBoxLayerNode *>(node));
Isabella Gottardi3db1ba92019-05-17 12:35:20 +0100290 case NodeType::QuantizationLayer:
291 return detail::create_quantization_layer<CLQuantizationLayer, CLTargetInfo>(*polymorphic_downcast<QuantizationLayerNode *>(node));
Gian Marco Iodice23e24792018-09-07 15:32:14 +0100292 case NodeType::ReorgLayer:
293 return detail::create_reorg_layer<CLReorgLayer, CLTargetInfo>(*polymorphic_downcast<ReorgLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000294 case NodeType::ReshapeLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100295 return detail::create_reshape_layer<CLReshapeLayer, CLTargetInfo>(*polymorphic_downcast<ReshapeLayerNode *>(node));
Georgios Pinitas087eaf62018-05-16 15:52:35 +0100296 case NodeType::ResizeLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100297 return detail::create_resize_layer<CLScale, CLTargetInfo>(*polymorphic_downcast<ResizeLayerNode *>(node));
Manuel Bottini3f9d4d72018-10-19 14:04:42 +0100298 case NodeType::ROIAlignLayer:
299 return detail::create_roi_align_layer<CLROIAlignLayer, CLTargetInfo>(*polymorphic_downcast<ROIAlignLayerNode *>(node));
Michele Di Giorgioc30b6682018-09-12 17:44:08 +0100300 case NodeType::SliceLayer:
301 return detail::create_slice_layer<CLSlice, CLTargetInfo>(*polymorphic_downcast<SliceLayerNode *>(node));
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000302 case NodeType::SoftmaxLayer:
Georgios Pinitasda2491f2018-06-01 17:49:09 +0100303 return detail::create_softmax_layer<CLSoftmaxLayer, CLTargetInfo>(*polymorphic_downcast<SoftmaxLayerNode *>(node), ctx);
Michele Di Giorgioec699752019-03-22 15:25:32 +0000304 case NodeType::StackLayer:
305 return detail::create_stack_layer<CLStackLayer, CLTargetInfo>(*polymorphic_downcast<StackLayerNode *>(node));
Michalis Spyrou4e1c3f32018-09-20 17:14:03 +0100306 case NodeType::UpsampleLayer:
307 return detail::create_upsample_layer<CLUpsampleLayer, CLTargetInfo>(*polymorphic_downcast<UpsampleLayerNode *>(node), ctx);
Michalis Spyrou96f67692018-09-13 11:39:28 +0100308 case NodeType::YOLOLayer:
309 return detail::create_yolo_layer<CLYOLOLayer, CLTargetInfo>(*polymorphic_downcast<YOLOLayerNode *>(node), ctx);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000310 default:
311 return nullptr;
312 }
313}
314} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100315} // namespace graph
Michele Di Giorgio555d1102018-09-12 13:51:59 +0100316} // namespace arm_compute