blob: 4938a56b3f5b25e8f49ec5645f1a3f3573e24ce5 [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * 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"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029#include "src/core/helpers/AutoConfiguration.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010030
Michalis Spyroubcf8a962018-10-12 10:51:31 +010031namespace arm_compute
32{
Georgios Pinitasd9769582017-08-03 10:19:40 +010033namespace
34{
35/** Define dimension to split the window
36 *
37 * @param[in] axis Reduction axis
38 *
39 * @return The dimension to split the window
40 */
41size_t reduction_window_split_dimension(unsigned int axis)
42{
43 switch(axis)
44 {
45 case 0:
46 return Window::DimY;
Michalis Spyroubcf8a962018-10-12 10:51:31 +010047 case 1:
48 case 2:
49 case 3:
50 return Window::DimX;
Georgios Pinitasd9769582017-08-03 10:19:40 +010051 default:
52 ARM_COMPUTE_ERROR("Unsupported reduction axis");
53 }
54}
Georgios Pinitasd9769582017-08-03 10:19:40 +010055} // namespace
56
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010057NEReductionOperation::NEReductionOperation(std::shared_ptr<IMemoryManager> memory_manager)
Sheri Zhang4d91dc62020-09-23 11:22:50 +010058 : _memory_group(memory_manager), _reduction_kernel(), _reshape(), _output_internal(), _window_split(0), _reduction_axis(), _is_reshape_required(false)
Georgios Pinitasd9769582017-08-03 10:19:40 +010059{
60}
61
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010062Status NEReductionOperation::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op, bool keep_dims)
John Richardson73d4aef2018-05-08 14:34:33 +010063{
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010064 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions");
65 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis");
66
67 const auto is_reshape_required = !keep_dims;
68
69 auto *output_internal = output;
70
71 TensorInfo info_before_reshape;
72
73 if(is_reshape_required)
74 {
75 const TensorInfo expected_output_shape = output->clone()->set_tensor_shape(arm_compute::misc::shape_calculator::compute_reduced_shape(input->tensor_shape(), axis, keep_dims));
76 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&expected_output_shape, output);
77
78 auto shape_before_reshape = input->tensor_shape();
79 shape_before_reshape.set(axis, 1);
80
81 const auto input_num_channles = input->num_channels();
82 const auto input_qinfo = input->quantization_info();
83 const auto is_arg_min_max = (op == ReductionOperation::ARG_IDX_MAX) || (op == ReductionOperation::ARG_IDX_MIN);
Sang-Hoon Parkeaa01ab2019-11-11 17:33:28 +000084 const auto output_data_type = is_arg_min_max ? DataType::S32 : output->data_type();
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010085
86 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);
87
88 output_internal = &info_before_reshape;
89 }
90
91 ARM_COMPUTE_RETURN_ON_ERROR(NEReductionOperationKernel::validate(input, output_internal, axis, op));
92
93 if(is_reshape_required)
94 {
Michalis Spyroubcd23522020-05-21 15:02:36 +010095 ARM_COMPUTE_RETURN_ON_ERROR(NEReshapeLayer::validate(output_internal, output));
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010096 }
John Richardson73d4aef2018-05-08 14:34:33 +010097
98 return Status{};
99}
100
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100101void NEReductionOperation::configure(ITensor *input, ITensor *output, unsigned int axis, ReductionOperation op, bool keep_dims)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100102{
giuros01154bc1c2019-03-26 17:44:40 +0000103 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100104
105 _is_reshape_required = !keep_dims;
106
107 auto *output_internal = output;
108 const auto is_arg_min_max = (op == ReductionOperation::ARG_IDX_MAX) || (op == ReductionOperation::ARG_IDX_MIN);
109
110 if(_is_reshape_required)
111 {
112 const auto output_internal_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->info()->tensor_shape(), axis);
113 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 +0000114 const auto output_data_type = is_arg_min_max ? DataType::S32 : input->info()->data_type();
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100115 const auto num_channels = input->info()->num_channels();
116 const auto qinfo = input->info()->quantization_info();
117
118 _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(
119 num_channels).set_quantization_info(qinfo));
120 _memory_group.manage(&_output_internal);
121 output_internal = &_output_internal;
122 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));
123 }
124
125 ARM_COMPUTE_ERROR_THROW_ON(NEReductionOperation::validate(input->info(), output->info(), axis, op, keep_dims));
Georgios Pinitasd9769582017-08-03 10:19:40 +0100126
127 // Configure reduction kernel
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100128 _reduction_kernel.configure(input, output_internal, axis, op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100129 _window_split = reduction_window_split_dimension(axis);
130 _reduction_axis = axis;
Georgios Pinitasd9769582017-08-03 10:19:40 +0100131
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100132 if(_is_reshape_required)
133 {
Michalis Spyroubcd23522020-05-21 15:02:36 +0100134 _reshape.configure(output_internal, output);
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100135 _output_internal.allocator()->allocate();
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100136 }
Georgios Pinitasd9769582017-08-03 10:19:40 +0100137}
138
139void NEReductionOperation::run()
140{
Sheri Zhang245ba052020-09-22 09:50:41 +0100141 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100142 NEScheduler::get().schedule(&_reduction_kernel, _window_split);
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100143 if(_is_reshape_required)
144 {
Michalis Spyroubcd23522020-05-21 15:02:36 +0100145 _reshape.run();
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100146 }
Georgios Pinitasd9769582017-08-03 10:19:40 +0100147}
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100148} // namespace arm_compute