blob: 43205ef7624946c7770c8d3c714145b44b0644fc [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
Michalis Spyrou19bd4122020-01-22 10:27:06 +00002 * Copyright (c) 2017-2020 ARM Limited.
Georgios Pinitasd9769582017-08-03 10:19:40 +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/NEON/functions/NEReductionOperation.h"
25
26#include "arm_compute/core/Helpers.h"
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010027#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010028#include "arm_compute/runtime/NEON/NEScheduler.h"
29
Michalis Spyroubcf8a962018-10-12 10:51:31 +010030namespace arm_compute
31{
Georgios Pinitasd9769582017-08-03 10:19:40 +010032namespace
33{
34/** Define dimension to split the window
35 *
36 * @param[in] axis Reduction axis
37 *
38 * @return The dimension to split the window
39 */
40size_t reduction_window_split_dimension(unsigned int axis)
41{
42 switch(axis)
43 {
44 case 0:
45 return Window::DimY;
Michalis Spyroubcf8a962018-10-12 10:51:31 +010046 case 1:
47 case 2:
48 case 3:
49 return Window::DimX;
Georgios Pinitasd9769582017-08-03 10:19:40 +010050 default:
51 ARM_COMPUTE_ERROR("Unsupported reduction axis");
52 }
53}
Georgios Pinitasd9769582017-08-03 10:19:40 +010054} // namespace
55
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010056NEReductionOperation::NEReductionOperation(std::shared_ptr<IMemoryManager> memory_manager)
57 : _memory_group(memory_manager), _reduction_kernel(), _fill_border_kernel(), _reshape_kernel(), _output_internal(), _window_split(0), _reduction_axis(), _is_reshape_required(false)
Georgios Pinitasd9769582017-08-03 10:19:40 +010058{
59}
60
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010061Status NEReductionOperation::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op, bool keep_dims)
John Richardson73d4aef2018-05-08 14:34:33 +010062{
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010063 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions");
64 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis");
65
66 const auto is_reshape_required = !keep_dims;
67
68 auto *output_internal = output;
69
70 TensorInfo info_before_reshape;
71
72 if(is_reshape_required)
73 {
74 const TensorInfo expected_output_shape = output->clone()->set_tensor_shape(arm_compute::misc::shape_calculator::compute_reduced_shape(input->tensor_shape(), axis, keep_dims));
75 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&expected_output_shape, output);
76
77 auto shape_before_reshape = input->tensor_shape();
78 shape_before_reshape.set(axis, 1);
79
80 const auto input_num_channles = input->num_channels();
81 const auto input_qinfo = input->quantization_info();
82 const auto is_arg_min_max = (op == ReductionOperation::ARG_IDX_MAX) || (op == ReductionOperation::ARG_IDX_MIN);
Sang-Hoon Parkeaa01ab2019-11-11 17:33:28 +000083 const auto output_data_type = is_arg_min_max ? DataType::S32 : output->data_type();
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010084
85 info_before_reshape.set_data_type(output_data_type).set_tensor_shape(shape_before_reshape).set_num_channels(input_num_channles).set_quantization_info(input_qinfo);
86
87 output_internal = &info_before_reshape;
88 }
89
90 ARM_COMPUTE_RETURN_ON_ERROR(NEReductionOperationKernel::validate(input, output_internal, axis, op));
91
92 if(is_reshape_required)
93 {
94 ARM_COMPUTE_RETURN_ON_ERROR(NEReshapeLayerKernel::validate(output_internal, output));
95 }
John Richardson73d4aef2018-05-08 14:34:33 +010096
97 return Status{};
98}
99
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100100void NEReductionOperation::configure(ITensor *input, ITensor *output, unsigned int axis, ReductionOperation op, bool keep_dims)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100101{
giuros01154bc1c2019-03-26 17:44:40 +0000102 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100103
104 _is_reshape_required = !keep_dims;
105
106 auto *output_internal = output;
107 const auto is_arg_min_max = (op == ReductionOperation::ARG_IDX_MAX) || (op == ReductionOperation::ARG_IDX_MIN);
108
109 if(_is_reshape_required)
110 {
111 const auto output_internal_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->info()->tensor_shape(), axis);
112 const auto output_external_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->info()->tensor_shape(), axis, false);
Sang-Hoon Parkeaa01ab2019-11-11 17:33:28 +0000113 const auto output_data_type = is_arg_min_max ? DataType::S32 : input->info()->data_type();
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100114 const auto num_channels = input->info()->num_channels();
115 const auto qinfo = input->info()->quantization_info();
116
117 _output_internal.allocator()->init(input->info()->clone()->set_data_type(output_data_type).set_tensor_shape(output_internal_shape).reset_padding().set_is_resizable(true).set_num_channels(
118 num_channels).set_quantization_info(qinfo));
119 _memory_group.manage(&_output_internal);
120 output_internal = &_output_internal;
121 auto_init_if_empty(*output->info(), input->info()->clone()->set_data_type(output_data_type).set_tensor_shape(output_external_shape).reset_padding().set_is_resizable(true));
122 }
123
124 ARM_COMPUTE_ERROR_THROW_ON(NEReductionOperation::validate(input->info(), output->info(), axis, op, keep_dims));
Georgios Pinitasd9769582017-08-03 10:19:40 +0100125
126 // Configure reduction kernel
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100127 _reduction_kernel.configure(input, output_internal, axis, op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100128 _window_split = reduction_window_split_dimension(axis);
129 _reduction_axis = axis;
Georgios Pinitasd9769582017-08-03 10:19:40 +0100130
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100131 if(axis == 0)
132 {
133 // Configure fill border kernel
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000134 const BorderSize fill_border_size = _reduction_kernel.border_size();
Usama Arifa4a08ad2019-05-20 12:38:33 +0100135 PixelValue pixelValue;
136 switch(op)
137 {
138 case ReductionOperation::PROD:
139 {
140 pixelValue = PixelValue(1, input->info()->data_type(), input->info()->quantization_info());
141 break;
142 }
143 case ReductionOperation::MIN:
144 {
145 switch(input->info()->data_type())
146 {
147 case DataType::F32:
148 {
149 pixelValue = PixelValue(std::numeric_limits<float>::max());
150 break;
151 }
152 case DataType::F16:
153 {
154 pixelValue = PixelValue(static_cast<half>(65504.0f));
155 break;
156 }
157 case DataType::QASYMM8:
158 {
159 pixelValue = PixelValue(255, input->info()->data_type(), input->info()->quantization_info());
160 break;
161 }
162 default:
163 {
164 ARM_COMPUTE_ERROR("Unsupported DataType");
165 }
166 }
167 break;
168 }
Usama Arif28f0dd92019-05-20 13:44:34 +0100169 case ReductionOperation::MAX:
170 {
171 switch(input->info()->data_type())
172 {
173 case DataType::F32:
174 {
175 pixelValue = PixelValue(-std::numeric_limits<float>::max());
176 break;
177 }
178 case DataType::F16:
179 {
180 pixelValue = PixelValue(static_cast<half>(-65504.0f));
181 break;
182 }
183 case DataType::QASYMM8:
184 {
185 pixelValue = PixelValue(0, input->info()->data_type(), input->info()->quantization_info());
186 break;
187 }
188 default:
189 {
190 ARM_COMPUTE_ERROR("Unsupported DataType");
191 }
192 }
193 break;
194 }
Usama Arifa4a08ad2019-05-20 12:38:33 +0100195 case ReductionOperation::ARG_IDX_MAX:
196 case ReductionOperation::ARG_IDX_MIN:
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000197 {
198 pixelValue = PixelValue(0, input->info()->data_type(), input->info()->quantization_info());
199 break;
200 }
Usama Arifa4a08ad2019-05-20 12:38:33 +0100201 case ReductionOperation::MEAN_SUM:
202 case ReductionOperation::SUM_SQUARE:
203 case ReductionOperation::SUM:
204 {
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000205 pixelValue = PixelValue(static_cast<uint32_t>(0));
Usama Arifa4a08ad2019-05-20 12:38:33 +0100206 break;
207 }
208 default:
209 ARM_COMPUTE_ERROR("Reduction Operation unsupported");
210 }
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100211 _fill_border_kernel.configure(input, fill_border_size, (is_arg_min_max ? BorderMode::REPLICATE : BorderMode::CONSTANT), pixelValue);
212 }
213
214 if(_is_reshape_required)
215 {
216 _reshape_kernel.configure(output_internal, output);
217 _output_internal.allocator()->allocate();
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100218 }
Georgios Pinitasd9769582017-08-03 10:19:40 +0100219}
220
221void NEReductionOperation::run()
222{
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100223 if(_reduction_axis == 0)
224 {
225 NEScheduler::get().schedule(&_fill_border_kernel, Window::DimY);
226 }
Georgios Pinitasd9769582017-08-03 10:19:40 +0100227 NEScheduler::get().schedule(&_reduction_kernel, _window_split);
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100228 if(_is_reshape_required)
229 {
230 NEScheduler::get().schedule(&_reshape_kernel, Window::DimY);
231 }
Georgios Pinitasd9769582017-08-03 10:19:40 +0100232}
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100233} // namespace arm_compute