93963a504676248529aa3f89df9848f23230091d
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / src / core / NEON / kernels / NEGatherKernelEx.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) 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/core/NEON/kernels/NEGatherKernelEx.h"
42
43 #include "arm_compute/core/CPP/Validate.h"
44 #include "arm_compute/core/Coordinates.h"
45 #include "arm_compute/core/Error.h"
46 #include "arm_compute/core/Helpers.h"
47 #include "arm_compute/core/IAccessWindow.h"
48 #include "arm_compute/core/TensorInfo.h"
49 #include "arm_compute/core/Validate.h"
50 #include "arm_compute/core/Window.h"
51 #include "arm_compute/core/utils/misc/ShapeCalculatorEx.h"
52
53 namespace arm_compute
54 {
55 namespace
56 {
57 /** Validate the indices
58  *
59  * Validate that indices are not negative
60  *
61  * @param[in] indices Indices tensor info.
62  */
63 template <typename U> void validate_indices(const ITensor *indices)
64 {
65   for (size_t i = 0; i < indices->info()->tensor_shape()[0]; ++i)
66   {
67     ARM_COMPUTE_ERROR_ON(*(reinterpret_cast<U *>(indices->ptr_to_element(Coordinates(i)))) < 0);
68   }
69 }
70
71 } // namespace
72
73 NEGatherKernelEx::NEGatherKernelEx()
74     : _input{}, _indices{}, _axis{}, _indices_rank{}, _output{}, _func{}
75 {
76 }
77
78 template <typename U>
79 inline void NEGatherKernelEx::gather_0_axis(const Window &window, const ThreadInfo &info)
80 {
81   ARM_COMPUTE_UNUSED(info);
82
83   // Validate that the indices are not negative
84   validate_indices<U>(_indices);
85
86   Iterator output_it(_output, window);
87   execute_window_loop(
88       window,
89       [&](const Coordinates &id) {
90         Coordinates gather_id(id);
91         gather_id.collapse(_indices_rank);
92
93         U new_index;
94         switch (_indices_rank)
95         {
96           case 1:
97             new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0]))));
98             break;
99           case 2:
100             new_index =
101                 *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0], id[1]))));
102             break;
103           case 3:
104             new_index = *(
105                 reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0], id[1], id[2]))));
106             break;
107           default:
108             ARM_COMPUTE_ERROR("Wrong num of dimensions");
109             break;
110         }
111
112         gather_id.set(0, new_index);
113
114         std::copy_n(_input->ptr_to_element(gather_id), _output->info()->element_size(),
115                     output_it.ptr());
116       },
117       output_it);
118 }
119
120 template <typename U>
121 void NEGatherKernelEx::gather_n_axis(const Window &window, const ThreadInfo &info)
122 {
123   ARM_COMPUTE_UNUSED(info);
124
125   // Validate that the indices are not negative
126   validate_indices<U>(_indices);
127
128   Window output_window{window};
129   output_window.set(Window::DimX, Window::Dimension(0, 1, 1));
130
131   Iterator output_it(_output, output_window);
132   execute_window_loop(
133       output_window,
134       [&](const Coordinates &id) {
135         Coordinates gather_id(id);
136         gather_id.collapse(_indices_rank, _axis);
137
138         U new_index;
139         switch (_indices_rank)
140         {
141           case 1:
142             new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[_axis]))));
143             break;
144           case 2:
145             new_index = *(reinterpret_cast<U *>(
146                 _indices->ptr_to_element(Coordinates(id[_axis], id[_axis + 1]))));
147             break;
148           case 3:
149             new_index = *(reinterpret_cast<U *>(
150                 _indices->ptr_to_element(Coordinates(id[_axis], id[_axis + 1], id[_axis + 2]))));
151             break;
152           default:
153             ARM_COMPUTE_ERROR("Wrong num of dimensions");
154             break;
155         }
156
157         gather_id.set(_axis, new_index);
158
159         std::copy_n(_input->ptr_to_element(gather_id),
160                     _input->info()->dimension(0) * _output->info()->element_size(),
161                     output_it.ptr());
162       },
163       output_it);
164 }
165
166 void NEGatherKernelEx::configure(const ITensor *input, const ITensor *indices, ITensor *output,
167                                  int axis)
168 {
169   ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices);
170   ARM_COMPUTE_ERROR_ON(indices->info()->num_dimensions() > 3);
171   ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32, DataType::S32);
172   ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(
173       input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, DataType::U16, DataType::S16,
174       DataType::U32, DataType::S32, DataType::F16, DataType::F32);
175
176   _input = input;
177   _indices = indices;
178   _output = output;
179   _axis = axis;
180   _indices_rank = indices->info()->num_dimensions();
181
182   if (_axis < 0)
183   {
184     _axis += input->info()->num_dimensions();
185   }
186   ARM_COMPUTE_ERROR_ON(0 > _axis || _axis >= static_cast<int32_t>(input->info()->num_dimensions()));
187
188   if (0 == _axis)
189   {
190     switch (_indices->info()->data_type())
191     {
192       case DataType::U32:
193         _func = &NEGatherKernelEx::gather_0_axis<uint32_t>;
194         break;
195       case DataType::S32:
196         _func = &NEGatherKernelEx::gather_0_axis<int32_t>;
197         break;
198       default:
199         ARM_COMPUTE_ERROR("Not supported");
200         break;
201     }
202   }
203   else
204   {
205     switch (_indices->info()->data_type())
206     {
207       case DataType::U32:
208         _func = &NEGatherKernelEx::gather_n_axis<uint32_t>;
209         break;
210       case DataType::S32:
211         _func = &NEGatherKernelEx::gather_n_axis<int32_t>;
212         break;
213       default:
214         ARM_COMPUTE_ERROR("Not supported");
215         break;
216     }
217   }
218   // Output auto initialization if not yet initialized
219   TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape_ex(
220       input->info()->tensor_shape(), indices->info()->tensor_shape(), _axis);
221   auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
222
223   // Create window
224   Window win = calculate_max_window(*output->info(), Steps());
225   output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
226
227   INEKernel::configure(win);
228 }
229
230 Status NEGatherKernelEx::validate(const ITensorInfo *input, const ITensorInfo *indices,
231                                   const ITensorInfo *output, int axis)
232 {
233   ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, indices, output);
234   ARM_COMPUTE_RETURN_ERROR_ON(indices->num_dimensions() > 3);
235   ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
236   ARM_COMPUTE_ERROR_ON(input->num_dimensions() + indices->num_dimensions() - 1 > 4);
237
238   if (axis < 0)
239   {
240     axis += input->num_dimensions();
241   }
242
243   ARM_COMPUTE_RETURN_ERROR_ON(0 > axis || axis >= static_cast<int32_t>(input->num_dimensions()));
244   ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
245   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(
246       input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, DataType::U16, DataType::S16,
247       DataType::U32, DataType::S32, DataType::F16, DataType::F32);
248
249   if (output->total_size() != 0)
250   {
251     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
252     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
253     TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape_ex(
254         input->tensor_shape(), indices->tensor_shape(), axis);
255     ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
256   }
257
258   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32, DataType::S32);
259
260   return Status{};
261 }
262
263 void NEGatherKernelEx::run(const Window &window, const ThreadInfo &info)
264 {
265   ARM_COMPUTE_UNUSED(info);
266   ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
267   ARM_COMPUTE_ERROR_ON(_func == nullptr);
268
269   (this->*_func)(window, info);
270 }
271
272 } // namespace arm_compute