cb1a2630405e8d5a0281f320b1afeaafe64b8256
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / src / runtime / NEON / functions / NEReduceOperation.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /*
18  * Copyright (c) 2018-2019 ARM Limited.
19  *
20  * SPDX-License-Identifier: MIT
21  *
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:
28  *
29  * The above copyright notice and this permission notice shall be included in all
30  * copies or substantial portions of the Software.
31  *
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
38  * SOFTWARE.
39  */
40
41 #include "arm_compute/runtime/NEON/functions/NEReduceOperation.h"
42
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"
48
49 using namespace arm_compute;
50
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()
54 {
55 }
56
57 Status NEReduceOperation::validate(const ITensorInfo *input, const Coordinates &reduction_axis,
58                                    bool keep_dims, const ITensorInfo *output, ReductionOperation op)
59 {
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,
65                                                        DataType::F32);
66   ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions());
67
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;
72
73   // Convert negative axis
74   for (unsigned int i = 0; i < reduction_ops; ++i)
75   {
76     axis_local[i] = wrap_around(axis_local[i], input_dims);
77   }
78
79   std::sort(axis_local.begin(), axis_local.begin() + reduction_ops);
80   for (unsigned int i = 0; i < reduction_ops; ++i)
81   {
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)
86     {
87       ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1);
88     }
89     if (keep_dims)
90     {
91       out_shape.set(axis_local[i], 1);
92     }
93     else
94     {
95       out_shape.remove_dimension(axis_local[i] - i);
96     }
97   }
98   const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape);
99   ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);
100
101   return Status{};
102 }
103
104 void NEReduceOperation::configure(ITensor *input, const Coordinates &reduction_axis, bool keep_dims,
105                                   ITensor *output, ReductionOperation op)
106 {
107   ARM_COMPUTE_ERROR_ON_NULLPTR(input);
108
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;
113
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();
117
118   // Convert negative axis
119   for (unsigned int i = 0; i < reduction_ops; ++i)
120   {
121     axis_local[i] = wrap_around(axis_local[i], input_dims);
122   }
123
124   // Perform reduction for every axis
125   for (unsigned int i = 0; i < _reduction_ops; ++i)
126   {
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]);
131
132     if (i == _reduction_ops - 1 && keep_dims)
133     {
134       _reduction_kernels[i].configure(in, output, axis_local[i], op);
135     }
136     else
137     {
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);
143     }
144   }
145
146   // Allocate intermediate tensors
147   for (unsigned int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i)
148   {
149     _reduced_outs[i].allocator()->allocate();
150   }
151
152   // Configure reshape layer if we want to drop the dimensions
153   if (!keep_dims)
154   {
155     TensorShape out_shape = input->info()->tensor_shape();
156
157     // We have to sort the reduction axis vectors in order for remove_dimension
158     // to work properly
159     std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops);
160     for (unsigned int i = 0; i < _reduction_ops; ++i)
161     {
162       out_shape.remove_dimension(axis_local[i] - i);
163     }
164     auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(out_shape));
165     _reshape.configure(&_reduced_outs[_reduction_ops - 1], output);
166   }
167 }
168
169 void NEReduceOperation::run()
170 {
171   MemoryGroupResourceScope scope_mg(_memory_group);
172
173   for (unsigned int i = 0; i < _reduction_ops; ++i)
174   {
175     _reduction_kernels[i].run();
176   }
177
178   if (!_keep_dims)
179   {
180     _reshape.run();
181   }
182 }