blob: 54e91fb8d809460d6c47b7573be6a90c1d833fbb [file] [log] [blame]
Michalis Spyrou04f089c2017-08-08 17:42:38 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Michalis Spyrou04f089c2017-08-08 17:42:38 +01003 *
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#include "arm_compute/runtime/CL/functions/CLReductionOperation.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010027#include "arm_compute/core/Helpers.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010028#include "arm_compute/core/PixelValue.h"
29#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Validate.h"
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010032#include "arm_compute/runtime/CL/CLScheduler.h"
Manuel Bottini7b9998d2019-10-21 17:59:07 +010033#include "arm_compute/runtime/Utils.h"
Matthew Bentham92046462020-03-07 22:15:55 +000034#include "support/MemorySupport.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010035
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010036namespace arm_compute
37{
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010038CLReductionOperation::CLReductionOperation(std::shared_ptr<IMemoryManager> memory_manager)
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010039 : _memory_group(std::move(memory_manager)), _results_vector(), _reduction_kernels_vector(), _border_handlers_vector(), _reshape(), _num_of_stages(), _reduction_axis(), _is_serial(),
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010040 _is_reshape_required(false)
Michalis Spyrou04f089c2017-08-08 17:42:38 +010041{
42}
43
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010044Status CLReductionOperation::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op, bool keep_dims)
John Richardson62385bc2018-04-20 13:11:36 +010045{
Manuel Bottini7b9998d2019-10-21 17:59:07 +010046 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010047 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions");
48 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis");
49
Manuel Bottini7b9998d2019-10-21 17:59:07 +010050 const unsigned int num_of_stages = calculate_number_of_stages_only_x_axis(input->dimension(0), axis);
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010051 const bool is_serial = needs_serialized_reduction(op, input->data_type(), axis);
Manuel Bottini7b9998d2019-10-21 17:59:07 +010052 const bool is_reshape_required = !keep_dims;
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010053
Manuel Bottini7b9998d2019-10-21 17:59:07 +010054 if(is_reshape_required && output->total_size() != 0)
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010055 {
56 const TensorInfo expected_output_shape = output->clone()->set_tensor_shape(arm_compute::misc::shape_calculator::compute_reduced_shape(input->tensor_shape(), axis, keep_dims));
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&expected_output_shape, output);
58 }
59
60 auto *output_internal = output;
61
62 TensorInfo output_before_reshape;
63 const auto input_shape = input->tensor_shape();
64 const auto input_data_type = input->data_type();
65 const auto input_num_channles = input->num_channels();
66 const auto input_qinfo = input->quantization_info();
Manuel Bottini7b9998d2019-10-21 17:59:07 +010067 const auto output_data_type = output->data_type();
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010068
69 auto initialize_tensorinfo = [](TensorInfo & ti, TensorShape shape, DataType data_type, int num_channels, QuantizationInfo qinfo)
70 {
71 ti.set_data_type(data_type).set_tensor_shape(shape).set_num_channels(num_channels).set_quantization_info(qinfo);
72 };
73
74 if(is_reshape_required)
75 {
76 auto shape_before_reshape = input_shape;
77 shape_before_reshape.set(axis, 1);
78 initialize_tensorinfo(output_before_reshape, shape_before_reshape, output_data_type, input_num_channles, input_qinfo);
79 output_internal = &output_before_reshape;
80 }
81
Manuel Bottinib412fab2018-12-10 17:40:23 +000082 if(is_serial)
83 {
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010084 ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(input, output_internal, axis, op));
Manuel Bottinib412fab2018-12-10 17:40:23 +000085 }
86 else
John Richardson62385bc2018-04-20 13:11:36 +010087 {
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010088 // Create temporary tensor infos
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010089 std::vector<TensorInfo> sums_vector(num_of_stages - 1);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010090
91 // Create intermediate tensor info
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010092 TensorShape shape{ input_shape };
93
94 shape.set(0, ceil(shape.x() / 128.f));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010095
96 for(unsigned int i = 0; i < num_of_stages - 1; i++)
97 {
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010098 initialize_tensorinfo(sums_vector[i], shape, input_data_type, input_num_channles, input_qinfo);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010099 }
100
Michalis Spyroue55a0132018-10-26 10:48:56 +0100101 ReductionOperation first_kernel_op;
Manuel Bottinib412fab2018-12-10 17:40:23 +0000102 ReductionOperation intermediate_kernel_op;
Michalis Spyroue55a0132018-10-26 10:48:56 +0100103 ReductionOperation last_kernel_op;
104 switch(op)
105 {
106 case ReductionOperation::SUM:
107 case ReductionOperation::MEAN_SUM:
Manuel Bottinib412fab2018-12-10 17:40:23 +0000108 first_kernel_op = ReductionOperation::SUM;
109 intermediate_kernel_op = ReductionOperation::SUM;
110 last_kernel_op = op;
Michalis Spyroue55a0132018-10-26 10:48:56 +0100111 break;
112 case ReductionOperation::SUM_SQUARE:
Manuel Bottinib412fab2018-12-10 17:40:23 +0000113 first_kernel_op = ReductionOperation::SUM_SQUARE;
114 intermediate_kernel_op = ReductionOperation::SUM;
115 last_kernel_op = ReductionOperation::SUM;
116 break;
117 case ReductionOperation::PROD:
118 first_kernel_op = ReductionOperation::PROD;
119 intermediate_kernel_op = ReductionOperation::PROD;
120 last_kernel_op = ReductionOperation::PROD;
Michalis Spyroue55a0132018-10-26 10:48:56 +0100121 break;
Usama Arifb2890502019-05-21 11:48:37 +0100122 case ReductionOperation::MIN:
123 first_kernel_op = ReductionOperation::MIN;
124 intermediate_kernel_op = ReductionOperation::MIN;
125 last_kernel_op = ReductionOperation::MIN;
126 break;
Usama Arif048b0f32019-05-22 16:32:27 +0100127 case ReductionOperation::MAX:
128 first_kernel_op = ReductionOperation::MAX;
129 intermediate_kernel_op = ReductionOperation::MAX;
130 last_kernel_op = ReductionOperation::MAX;
131 break;
Michalis Spyroue55a0132018-10-26 10:48:56 +0100132 default:
133 ARM_COMPUTE_ERROR("Not supported");
134 }
135
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100136 // Validate ReductionOperation only on first kernel
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100137 ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(input, &sums_vector[0], axis, first_kernel_op));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100138
139 // Validate ReductionOperation on intermediate stages
140 for(unsigned int i = 1; i < num_of_stages - 1; ++i)
141 {
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100142 ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(&sums_vector[i - 1], &sums_vector[i], axis, intermediate_kernel_op));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100143 }
144
145 // Validate ReductionOperation on the last stage
146 const unsigned int last_stage = num_of_stages - 1;
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100147 ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(&sums_vector[last_stage - 1], output_internal, axis, last_kernel_op, input->dimension(0)));
148 }
149
150 if(is_reshape_required)
151 {
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100152 ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayer::validate(output_internal, output));
John Richardson62385bc2018-04-20 13:11:36 +0100153 }
John Richardson62385bc2018-04-20 13:11:36 +0100154
John Richardson62385bc2018-04-20 13:11:36 +0100155 return Status{};
156}
157
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100158ICLTensor *CLReductionOperation::configure_intermediate_result_vector(ICLTensor *input, ICLTensor *output)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100159{
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100160 if(!_is_reshape_required && _is_serial)
161 {
162 return output;
163 }
164
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100165 auto intermediate_result_vector_size = _is_serial ? 1 : _num_of_stages;
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100166
167 if(!_is_reshape_required)
168 {
169 --intermediate_result_vector_size;
170 }
171
172 _results_vector.resize(intermediate_result_vector_size);
173 auto shape = input->info()->tensor_shape();
174
175 shape.set(_reduction_axis, _is_serial ? 1 : ceil(shape.x() / 128.f));
176
177 for(auto &v : _results_vector)
178 {
179 if(&v == &_results_vector.back() && _is_reshape_required)
180 {
181 shape.set(_reduction_axis, 1);
182 }
183 v.allocator()->init(input->info()->clone()->set_tensor_shape(shape));
184 }
185
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100186 return _is_reshape_required ? &_results_vector.back() : output;
187}
188
189void CLReductionOperation::configure(ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op, bool keep_dims)
190{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100191 configure(CLKernelLibrary::get().get_compile_context(), input, output, axis, op, keep_dims);
192}
193
194void CLReductionOperation::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op, bool keep_dims)
195{
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100196 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100197 _num_of_stages = calculate_number_of_stages_only_x_axis(input->info()->dimension(0), axis);
198 _reduction_axis = axis;
199 _is_serial = needs_serialized_reduction(op, input->info()->data_type(), axis);
200 _is_reshape_required = !keep_dims;
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100201
202 auto *output_internal = configure_intermediate_result_vector(input, output);
203
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100204 if(_is_reshape_required)
205 {
206 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->info()->tensor_shape(), axis, false);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100207 const auto output_data_type = input->info()->data_type();
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100208 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape).set_data_type(output_data_type).reset_padding().set_is_resizable(true));
209 }
Georgios Pinitasaec513c2017-09-15 19:36:30 +0100210
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100211 // Configure reduction operation kernels
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100212 _reduction_kernels_vector.resize(_num_of_stages);
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100213
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100214 // Create temporary tensors
Manuel Bottinib412fab2018-12-10 17:40:23 +0000215 if(_is_serial)
216 {
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100217 if(_is_reshape_required)
218 {
219 _memory_group.manage(&_results_vector.back());
220 }
221
Manuel Bottini2b84be52020-04-08 10:15:51 +0100222 _reduction_kernels_vector[0].configure(compile_context, input, output_internal, axis, op, 0);
Manuel Bottinib412fab2018-12-10 17:40:23 +0000223 }
224 else
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100225 {
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100226 _border_handlers_vector.resize(_num_of_stages);
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100227 _memory_group.manage(&_results_vector[0]);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100228
229 ReductionOperation first_kernel_op;
Manuel Bottinib412fab2018-12-10 17:40:23 +0000230 ReductionOperation intermediate_kernel_op;
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100231 ReductionOperation last_kernel_op;
Manuel Bottinib412fab2018-12-10 17:40:23 +0000232 PixelValue pixelValue;
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100233 switch(op)
234 {
235 case ReductionOperation::SUM:
236 case ReductionOperation::MEAN_SUM:
Manuel Bottinib412fab2018-12-10 17:40:23 +0000237 first_kernel_op = ReductionOperation::SUM;
238 intermediate_kernel_op = ReductionOperation::SUM;
239 last_kernel_op = op;
Manuel Bottini55e16782019-01-15 13:21:57 +0000240 pixelValue = PixelValue();
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100241 break;
242 case ReductionOperation::SUM_SQUARE:
Manuel Bottinib412fab2018-12-10 17:40:23 +0000243 first_kernel_op = ReductionOperation::SUM_SQUARE;
244 intermediate_kernel_op = ReductionOperation::SUM;
245 last_kernel_op = ReductionOperation::SUM;
Manuel Bottini55e16782019-01-15 13:21:57 +0000246 pixelValue = PixelValue();
Manuel Bottinib412fab2018-12-10 17:40:23 +0000247 break;
248 case ReductionOperation::PROD:
249 first_kernel_op = ReductionOperation::PROD;
250 intermediate_kernel_op = ReductionOperation::PROD;
251 last_kernel_op = ReductionOperation::PROD;
252 pixelValue = PixelValue(1, input->info()->data_type());
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100253 break;
Usama Arifb2890502019-05-21 11:48:37 +0100254 case ReductionOperation::MIN:
255 first_kernel_op = ReductionOperation::MIN;
256 intermediate_kernel_op = ReductionOperation::MIN;
257 last_kernel_op = ReductionOperation::MIN;
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100258 pixelValue = std::get<1>(get_min_max(input->info()->data_type()));
Usama Arifb2890502019-05-21 11:48:37 +0100259 break;
Usama Arif048b0f32019-05-22 16:32:27 +0100260 case ReductionOperation::MAX:
261 first_kernel_op = ReductionOperation::MAX;
262 intermediate_kernel_op = ReductionOperation::MAX;
263 last_kernel_op = ReductionOperation::MAX;
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100264 pixelValue = std::get<0>(get_min_max(input->info()->data_type()));
Usama Arif048b0f32019-05-22 16:32:27 +0100265 break;
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100266 default:
267 ARM_COMPUTE_ERROR("Not supported");
268 }
269
Manuel Bottini2b84be52020-04-08 10:15:51 +0100270 _reduction_kernels_vector[0].configure(compile_context, input, &_results_vector[0], axis, first_kernel_op);
271 _border_handlers_vector[0].configure(compile_context, input, _reduction_kernels_vector[0].border_size(), BorderMode::CONSTANT, pixelValue);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100272
273 // Apply ReductionOperation on intermediate stages
274 for(unsigned int i = 1; i < _num_of_stages - 1; ++i)
275 {
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100276 _memory_group.manage(&_results_vector[i]);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100277 _reduction_kernels_vector[i].configure(compile_context, &_results_vector[i - 1], &_results_vector[i], axis, intermediate_kernel_op);
278 _border_handlers_vector[i].configure(compile_context, &_results_vector[i - 1], _reduction_kernels_vector[i].border_size(), BorderMode::CONSTANT, pixelValue);
Manuel Bottinib412fab2018-12-10 17:40:23 +0000279 _results_vector[i - 1].allocator()->allocate();
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100280 }
281
282 // Apply ReductionOperation on the last stage
283 const unsigned int last_stage = _num_of_stages - 1;
284 const unsigned int input_width = input->info()->dimension(0);
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100285
286 if(_is_reshape_required)
287 {
288 _memory_group.manage(&_results_vector.back());
289 }
290
Manuel Bottini2b84be52020-04-08 10:15:51 +0100291 _reduction_kernels_vector[last_stage].configure(compile_context, &_results_vector[last_stage - 1], output_internal, axis, last_kernel_op, input_width);
292 _border_handlers_vector[last_stage].configure(compile_context, &_results_vector[last_stage - 1], _reduction_kernels_vector[last_stage].border_size(), BorderMode::CONSTANT, pixelValue);
Manuel Bottinib412fab2018-12-10 17:40:23 +0000293 _results_vector[last_stage - 1].allocator()->allocate();
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100294 }
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100295
296 if(_is_reshape_required)
297 {
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100298 _reshape.configure(compile_context, &_results_vector.back(), output);
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100299 _results_vector.back().allocator()->allocate();
300 }
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100301}
302
303void CLReductionOperation::run()
304{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100305 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100306
Manuel Bottinib412fab2018-12-10 17:40:23 +0000307 if(_is_serial)
308 {
309 CLScheduler::get().enqueue(_reduction_kernels_vector[0], false);
310 }
311 else
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100312 {
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100313 for(unsigned int i = 0; i < _num_of_stages; ++i)
314 {
315 CLScheduler::get().enqueue(_border_handlers_vector[i], false);
316 CLScheduler::get().enqueue(_reduction_kernels_vector[i], false);
317 }
318 }
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100319
320 if(_is_reshape_required)
321 {
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100322 _reshape.run();
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100323 }
John Richardson62385bc2018-04-20 13:11:36 +0100324}
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100325} // namespace arm_compute