Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / src / runtime / NEON / functions / NEReduceSum.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/NEReduceSum.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
47 using namespace arm_compute;
48
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()
52 {
53 }
54
55 Status NEReduceSum::validate(const ITensorInfo *input, const Coordinates &reduction_axis,
56                              bool keep_dims, const ITensorInfo *output)
57 {
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,
62                                                        DataType::F32);
63   ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions());
64
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;
69
70   // Convert negative axis
71   for (unsigned int i = 0; i < reduction_ops; ++i)
72   {
73     axis_local[i] = wrap_around(axis_local[i], input_dims);
74   }
75
76   std::sort(axis_local.begin(), axis_local.begin() + reduction_ops);
77   for (unsigned int i = 0; i < reduction_ops; ++i)
78   {
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)
83     {
84       ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1);
85     }
86     if (keep_dims)
87     {
88       out_shape.set(axis_local[i], 1);
89     }
90     else
91     {
92       out_shape.remove_dimension(axis_local[i] - i);
93     }
94   }
95   const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape);
96   ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);
97
98   return Status{};
99 }
100
101 void NEReduceSum::configure(ITensor *input, const Coordinates &reduction_axis, bool keep_dims,
102                             ITensor *output)
103 {
104   ARM_COMPUTE_ERROR_ON_NULLPTR(input);
105
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;
110
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();
114
115   // Convert negative axis
116   for (unsigned int i = 0; i < reduction_ops; ++i)
117   {
118     axis_local[i] = wrap_around(axis_local[i], input_dims);
119   }
120
121   // Perform reduction for every axis
122   for (unsigned int i = 0; i < _reduction_ops; ++i)
123   {
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]);
128
129     if (i == _reduction_ops - 1 && keep_dims)
130     {
131       _reduction_kernels[i].configure(in, output, axis_local[i], ReductionOperation::SUM);
132     }
133     else
134     {
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);
142     }
143   }
144
145   // Allocate intermediate tensors
146   for (unsigned int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i)
147   {
148     _reduced_outs[i].allocator()->allocate();
149   }
150
151   // Configure reshape layer if we want to drop the dimensions
152   if (!keep_dims)
153   {
154     TensorShape out_shape = input->info()->tensor_shape();
155
156     // We have to sort the reduction axis vectors in order for remove_dimension
157     // to work properly
158     std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops);
159     for (unsigned int i = 0; i < _reduction_ops; ++i)
160     {
161       out_shape.remove_dimension(axis_local[i] - i);
162     }
163     auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(out_shape));
164     _reshape.configure(&_reduced_outs[_reduction_ops - 1], output);
165   }
166 }
167
168 void NEReduceSum::run()
169 {
170   MemoryGroupResourceScope scope_mg(_memory_group);
171
172   for (unsigned int i = 0; i < _reduction_ops; ++i)
173   {
174     _reduction_kernels[i].run();
175   }
176
177   if (!_keep_dims)
178   {
179     _reshape.run();
180   }
181 }