blob: dc6cf59019d80803fa1355614f66313593d49d6c [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
Manuel Bottini1d4f3852019-01-14 15:14:43 +00002 * Copyright (c) 2017-2019 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"
27#include "arm_compute/runtime/NEON/NEScheduler.h"
28
Michalis Spyroubcf8a962018-10-12 10:51:31 +010029namespace arm_compute
30{
Georgios Pinitasd9769582017-08-03 10:19:40 +010031namespace
32{
33/** Define dimension to split the window
34 *
35 * @param[in] axis Reduction axis
36 *
37 * @return The dimension to split the window
38 */
39size_t reduction_window_split_dimension(unsigned int axis)
40{
41 switch(axis)
42 {
43 case 0:
44 return Window::DimY;
Michalis Spyroubcf8a962018-10-12 10:51:31 +010045 case 1:
46 case 2:
47 case 3:
48 return Window::DimX;
Georgios Pinitasd9769582017-08-03 10:19:40 +010049 default:
50 ARM_COMPUTE_ERROR("Unsupported reduction axis");
51 }
52}
Georgios Pinitasd9769582017-08-03 10:19:40 +010053} // namespace
54
55NEReductionOperation::NEReductionOperation()
Michalis Spyroubcf8a962018-10-12 10:51:31 +010056 : _reduction_kernel(), _fill_border_kernel(), _window_split(0), _reduction_axis()
Georgios Pinitasd9769582017-08-03 10:19:40 +010057{
58}
59
John Richardson73d4aef2018-05-08 14:34:33 +010060Status NEReductionOperation::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
61{
62 ARM_COMPUTE_RETURN_ON_ERROR(NEReductionOperationKernel::validate(input, output, axis, op));
63
64 return Status{};
65}
66
Georgios Pinitasd9769582017-08-03 10:19:40 +010067void NEReductionOperation::configure(ITensor *input, ITensor *output, unsigned int axis, ReductionOperation op)
68{
giuros01154bc1c2019-03-26 17:44:40 +000069 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
70 ARM_COMPUTE_ERROR_THROW_ON(NEReductionOperation::validate(input->info(), output->info(), axis, op));
Georgios Pinitasd9769582017-08-03 10:19:40 +010071
72 // Configure reduction kernel
73 _reduction_kernel.configure(input, output, axis, op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +010074 _window_split = reduction_window_split_dimension(axis);
75 _reduction_axis = axis;
Georgios Pinitasd9769582017-08-03 10:19:40 +010076
Michalis Spyroubcf8a962018-10-12 10:51:31 +010077 if(axis == 0)
78 {
79 // Configure fill border kernel
Manuel Bottini1d4f3852019-01-14 15:14:43 +000080 const BorderSize fill_border_size = _reduction_kernel.border_size();
Usama Arifa4a08ad2019-05-20 12:38:33 +010081 PixelValue pixelValue;
82 switch(op)
83 {
84 case ReductionOperation::PROD:
85 {
86 pixelValue = PixelValue(1, input->info()->data_type(), input->info()->quantization_info());
87 break;
88 }
89 case ReductionOperation::MIN:
90 {
91 switch(input->info()->data_type())
92 {
93 case DataType::F32:
94 {
95 pixelValue = PixelValue(std::numeric_limits<float>::max());
96 break;
97 }
98 case DataType::F16:
99 {
100 pixelValue = PixelValue(static_cast<half>(65504.0f));
101 break;
102 }
103 case DataType::QASYMM8:
104 {
105 pixelValue = PixelValue(255, input->info()->data_type(), input->info()->quantization_info());
106 break;
107 }
108 default:
109 {
110 ARM_COMPUTE_ERROR("Unsupported DataType");
111 }
112 }
113 break;
114 }
Usama Arif28f0dd92019-05-20 13:44:34 +0100115 case ReductionOperation::MAX:
116 {
117 switch(input->info()->data_type())
118 {
119 case DataType::F32:
120 {
121 pixelValue = PixelValue(-std::numeric_limits<float>::max());
122 break;
123 }
124 case DataType::F16:
125 {
126 pixelValue = PixelValue(static_cast<half>(-65504.0f));
127 break;
128 }
129 case DataType::QASYMM8:
130 {
131 pixelValue = PixelValue(0, input->info()->data_type(), input->info()->quantization_info());
132 break;
133 }
134 default:
135 {
136 ARM_COMPUTE_ERROR("Unsupported DataType");
137 }
138 }
139 break;
140 }
Usama Arifa4a08ad2019-05-20 12:38:33 +0100141 case ReductionOperation::ARG_IDX_MAX:
142 case ReductionOperation::ARG_IDX_MIN:
143 case ReductionOperation::MEAN_SUM:
144 case ReductionOperation::SUM_SQUARE:
145 case ReductionOperation::SUM:
146 {
147 pixelValue = PixelValue(0, input->info()->data_type());
148 break;
149 }
150 default:
151 ARM_COMPUTE_ERROR("Reduction Operation unsupported");
152 }
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000153 _fill_border_kernel.configure(input, fill_border_size, BorderMode::CONSTANT, pixelValue);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100154 }
Georgios Pinitasd9769582017-08-03 10:19:40 +0100155}
156
157void NEReductionOperation::run()
158{
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100159 if(_reduction_axis == 0)
160 {
161 NEScheduler::get().schedule(&_fill_border_kernel, Window::DimY);
162 }
Georgios Pinitasd9769582017-08-03 10:19:40 +0100163 NEScheduler::get().schedule(&_reduction_kernel, _window_split);
164}
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100165} // namespace arm_compute