1 /*
2 * Copyright (c) 2017-2021 Arm Limited.
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 */
24 #include "arm_compute/runtime/NEON/functions/NEReductionOperation.h"
25
26 #include "arm_compute/core/Helpers.h"
27 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
28 #include "arm_compute/runtime/NEON/NEScheduler.h"
29 #include "src/common/utils/Log.h"
30 #include "src/core/NEON/kernels/NEReductionOperationKernel.h"
31 #include "src/core/helpers/AutoConfiguration.h"
32
33 namespace arm_compute
34 {
35 namespace
36 {
37 /** Define dimension to split the window
38 *
39 * @param[in] axis Reduction axis
40 *
41 * @return The dimension to split the window
42 */
reduction_window_split_dimension(unsigned int axis)43 size_t reduction_window_split_dimension(unsigned int axis)
44 {
45 switch(axis)
46 {
47 case 0:
48 return Window::DimY;
49 case 1:
50 case 2:
51 case 3:
52 return Window::DimX;
53 default:
54 ARM_COMPUTE_ERROR("Unsupported reduction axis");
55 }
56 }
57 } // namespace
58
59 NEReductionOperation::~NEReductionOperation() = default;
60
NEReductionOperation(std::shared_ptr<IMemoryManager> memory_manager)61 NEReductionOperation::NEReductionOperation(std::shared_ptr<IMemoryManager> memory_manager)
62 : _memory_group(memory_manager), _reduction_kernel(), _reshape(), _output_internal(), _window_split(0), _reduction_axis(), _is_reshape_required(false)
63 {
64 }
65
validate(const ITensorInfo * input,const ITensorInfo * output,unsigned int axis,ReductionOperation op,bool keep_dims)66 Status NEReductionOperation::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op, bool keep_dims)
67 {
68 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions");
69 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis");
70
71 const auto is_reshape_required = !keep_dims;
72
73 auto *output_internal = output;
74
75 TensorInfo info_before_reshape;
76
77 if(is_reshape_required)
78 {
79 const TensorInfo expected_output_shape = output->clone()->set_tensor_shape(arm_compute::misc::shape_calculator::compute_reduced_shape(input->tensor_shape(), axis, keep_dims));
80 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&expected_output_shape, output);
81
82 auto shape_before_reshape = input->tensor_shape();
83 shape_before_reshape.set(axis, 1);
84
85 const auto input_num_channles = input->num_channels();
86 const auto input_qinfo = input->quantization_info();
87 const auto is_arg_min_max = (op == ReductionOperation::ARG_IDX_MAX) || (op == ReductionOperation::ARG_IDX_MIN);
88 const auto output_data_type = is_arg_min_max ? DataType::S32 : output->data_type();
89
90 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);
91
92 output_internal = &info_before_reshape;
93 }
94
95 ARM_COMPUTE_RETURN_ON_ERROR(NEReductionOperationKernel::validate(input, output_internal, axis, op));
96
97 if(is_reshape_required)
98 {
99 ARM_COMPUTE_RETURN_ON_ERROR(NEReshapeLayer::validate(output_internal, output));
100 }
101
102 return Status{};
103 }
104
configure(ITensor * input,ITensor * output,unsigned int axis,ReductionOperation op,bool keep_dims)105 void NEReductionOperation::configure(ITensor *input, ITensor *output, unsigned int axis, ReductionOperation op, bool keep_dims)
106 {
107 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
108 ARM_COMPUTE_LOG_PARAMS(input, output, axis, op, keep_dims);
109
110 _is_reshape_required = !keep_dims;
111
112 auto *output_internal = output;
113 const auto is_arg_min_max = (op == ReductionOperation::ARG_IDX_MAX) || (op == ReductionOperation::ARG_IDX_MIN);
114
115 if(_is_reshape_required)
116 {
117 const auto output_internal_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->info()->tensor_shape(), axis);
118 const auto output_external_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->info()->tensor_shape(), axis, false);
119 const auto output_data_type = is_arg_min_max ? DataType::S32 : input->info()->data_type();
120 const auto num_channels = input->info()->num_channels();
121 const auto qinfo = input->info()->quantization_info();
122
123 _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(
124 num_channels).set_quantization_info(qinfo));
125 _memory_group.manage(&_output_internal);
126 output_internal = &_output_internal;
127 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));
128 }
129
130 ARM_COMPUTE_ERROR_THROW_ON(NEReductionOperation::validate(input->info(), output->info(), axis, op, keep_dims));
131
132 // Configure reduction kernel
133 _reduction_kernel = std::make_unique<NEReductionOperationKernel>();
134 _reduction_kernel->configure(input, output_internal, axis, op);
135 _window_split = reduction_window_split_dimension(axis);
136 _reduction_axis = axis;
137
138 if(_is_reshape_required)
139 {
140 _reshape.configure(output_internal, output);
141 _output_internal.allocator()->allocate();
142 }
143 }
144
run()145 void NEReductionOperation::run()
146 {
147 MemoryGroupResourceScope scope_mg(_memory_group);
148 NEScheduler::get().schedule(_reduction_kernel.get(), _window_split);
149 if(_is_reshape_required)
150 {
151 _reshape.run();
152 }
153 }
154 } // namespace arm_compute
155