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/NEReduceOperation.h"
43 #include "arm_compute/core/CPP/Validate.h"
44 #include "arm_compute/core/Helpers.h"
45 #include "arm_compute/runtime/NEON/NEScheduler.h"
46 #include "arm_compute/core/TensorInfo.h"
47 #include "arm_compute/runtime/Tensor.h"
49 using namespace arm_compute;
51 NEReduceOperation::NEReduceOperation(std::shared_ptr<IMemoryManager> memory_manager)
52 : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(),
53 _reduction_ops(), _keep_dims()
57 Status NEReduceOperation::validate(const ITensorInfo *input, const Coordinates &reduction_axis,
58 bool keep_dims, const ITensorInfo *output, ReductionOperation op)
60 ARM_COMPUTE_UNUSED(keep_dims);
61 ARM_COMPUTE_UNUSED(op);
62 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
63 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
64 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16,
66 ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions());
68 TensorShape out_shape = input->tensor_shape();
69 const unsigned int reduction_ops = reduction_axis.num_dimensions();
70 const int input_dims = input->num_dimensions();
71 Coordinates axis_local = reduction_axis;
73 // Convert negative axis
74 for (unsigned int i = 0; i < reduction_ops; ++i)
76 axis_local[i] = wrap_around(axis_local[i], input_dims);
79 std::sort(axis_local.begin(), axis_local.begin() + reduction_ops);
80 for (unsigned int i = 0; i < reduction_ops; ++i)
82 ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] > 3);
83 ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local[i]) >
84 input->num_dimensions() - 1);
85 if (output->total_size() > 0 && keep_dims)
87 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1);
91 out_shape.set(axis_local[i], 1);
95 out_shape.remove_dimension(axis_local[i] - i);
98 const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape);
99 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);
104 void NEReduceOperation::configure(ITensor *input, const Coordinates &reduction_axis, bool keep_dims,
105 ITensor *output, ReductionOperation op)
107 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
109 _reduction_ops = reduction_axis.num_dimensions();
110 _reduction_kernels.resize(_reduction_ops);
111 _reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0));
112 _keep_dims = keep_dims;
114 Coordinates axis_local = reduction_axis;
115 const int input_dims = input->info()->num_dimensions();
116 const unsigned int reduction_ops = reduction_axis.num_dimensions();
118 // Convert negative axis
119 for (unsigned int i = 0; i < reduction_ops; ++i)
121 axis_local[i] = wrap_around(axis_local[i], input_dims);
124 // Perform reduction for every axis
125 for (unsigned int i = 0; i < _reduction_ops; ++i)
127 TensorShape out_shape =
128 i == 0 ? input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape();
129 out_shape.set(axis_local[i], 1);
130 auto in = (i == 0) ? input : (&_reduced_outs[i - 1]);
132 if (i == _reduction_ops - 1 && keep_dims)
134 _reduction_kernels[i].configure(in, output, axis_local[i], op);
138 _reduced_outs[i].allocator()->init(TensorInfo(out_shape, input->info()->num_channels(),
139 input->info()->data_type(),
140 input->info()->quantization_info()));
141 _memory_group.manage(&_reduced_outs[i]);
142 _reduction_kernels[i].configure(in, &_reduced_outs[i], axis_local[i], op);
146 // Allocate intermediate tensors
147 for (unsigned int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i)
149 _reduced_outs[i].allocator()->allocate();
152 // Configure reshape layer if we want to drop the dimensions
155 TensorShape out_shape = input->info()->tensor_shape();
157 // We have to sort the reduction axis vectors in order for remove_dimension
159 std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops);
160 for (unsigned int i = 0; i < _reduction_ops; ++i)
162 out_shape.remove_dimension(axis_local[i] - i);
164 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(out_shape));
165 _reshape.configure(&_reduced_outs[_reduction_ops - 1], output);
169 void NEReduceOperation::run()
171 MemoryGroupResourceScope scope_mg(_memory_group);
173 for (unsigned int i = 0; i < _reduction_ops; ++i)
175 _reduction_kernels[i].run();