2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Copyright (c) 2018-2019 ARM Limited.
20 * SPDX-License-Identifier: MIT
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to
24 * deal in the Software without restriction, including without limitation the
25 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26 * sell copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
29 * The above copyright notice and this permission notice shall be included in all
30 * copies or substantial portions of the Software.
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41 #include "arm_compute/runtime/NEON/functions/NEReduceSum.h"
43 #include "arm_compute/core/CPP/Validate.h"
44 #include "arm_compute/core/Helpers.h"
45 #include "arm_compute/runtime/NEON/NEScheduler.h"
47 using namespace arm_compute;
49 NEReduceSum::NEReduceSum(std::shared_ptr<IMemoryManager> memory_manager)
50 : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(),
51 _reduction_ops(), _keep_dims()
55 Status NEReduceSum::validate(const ITensorInfo *input, const Coordinates &reduction_axis,
56 bool keep_dims, const ITensorInfo *output)
58 ARM_COMPUTE_UNUSED(keep_dims);
59 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
60 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
61 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16,
63 ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions());
65 TensorShape out_shape = input->tensor_shape();
66 const unsigned int reduction_ops = reduction_axis.num_dimensions();
67 const int input_dims = input->num_dimensions();
68 Coordinates axis_local = reduction_axis;
70 // Convert negative axis
71 for (unsigned int i = 0; i < reduction_ops; ++i)
73 axis_local[i] = wrap_around(axis_local[i], input_dims);
76 std::sort(axis_local.begin(), axis_local.begin() + reduction_ops);
77 for (unsigned int i = 0; i < reduction_ops; ++i)
79 ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] > 3);
80 ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local[i]) >
81 input->num_dimensions() - 1);
82 if (output->total_size() > 0 && keep_dims)
84 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1);
88 out_shape.set(axis_local[i], 1);
92 out_shape.remove_dimension(axis_local[i] - i);
95 const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape);
96 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);
101 void NEReduceSum::configure(ITensor *input, const Coordinates &reduction_axis, bool keep_dims,
104 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
106 _reduction_ops = reduction_axis.num_dimensions();
107 _reduction_kernels.resize(_reduction_ops);
108 _reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0));
109 _keep_dims = keep_dims;
111 Coordinates axis_local = reduction_axis;
112 const int input_dims = input->info()->num_dimensions();
113 const unsigned int reduction_ops = reduction_axis.num_dimensions();
115 // Convert negative axis
116 for (unsigned int i = 0; i < reduction_ops; ++i)
118 axis_local[i] = wrap_around(axis_local[i], input_dims);
121 // Perform reduction for every axis
122 for (unsigned int i = 0; i < _reduction_ops; ++i)
124 TensorShape out_shape =
125 i == 0 ? input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape();
126 out_shape.set(axis_local[i], 1);
127 auto in = (i == 0) ? input : (&_reduced_outs[i - 1]);
129 if (i == _reduction_ops - 1 && keep_dims)
131 _reduction_kernels[i].configure(in, output, axis_local[i], ReductionOperation::SUM);
135 _reduced_outs[i].allocator()->init(TensorInfo(out_shape, input->info()->num_channels(),
136 input->info()->data_type(),
137 input->info()->quantization_info())
138 .set_data_layout(input->info()->data_layout()));
139 _memory_group.manage(&_reduced_outs[i]);
140 _reduction_kernels[i].configure(in, &_reduced_outs[i], axis_local[i],
141 ReductionOperation::SUM);
145 // Allocate intermediate tensors
146 for (unsigned int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i)
148 _reduced_outs[i].allocator()->allocate();
151 // Configure reshape layer if we want to drop the dimensions
154 TensorShape out_shape = input->info()->tensor_shape();
156 // We have to sort the reduction axis vectors in order for remove_dimension
158 std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops);
159 for (unsigned int i = 0; i < _reduction_ops; ++i)
161 out_shape.remove_dimension(axis_local[i] - i);
163 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(out_shape));
164 _reshape.configure(&_reduced_outs[_reduction_ops - 1], output);
168 void NEReduceSum::run()
170 MemoryGroupResourceScope scope_mg(_memory_group);
172 for (unsigned int i = 0; i < _reduction_ops; ++i)
174 _reduction_kernels[i].run();