Imported Upstream version 1.12.0
[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 = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0], id[1]))));
101           break;
102         case 3:
103           new_index =
104             *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0], id[1], id[2]))));
105           break;
106         default:
107           ARM_COMPUTE_ERROR("Wrong num of dimensions");
108           break;
109       }
110
111       gather_id.set(0, new_index);
112
113       std::copy_n(_input->ptr_to_element(gather_id), _output->info()->element_size(),
114                   output_it.ptr());
115     },
116     output_it);
117 }
118
119 template <typename U>
120 void NEGatherKernelEx::gather_n_axis(const Window &window, const ThreadInfo &info)
121 {
122   ARM_COMPUTE_UNUSED(info);
123
124   // Validate that the indices are not negative
125   validate_indices<U>(_indices);
126
127   Window output_window{window};
128   output_window.set(Window::DimX, Window::Dimension(0, 1, 1));
129
130   Iterator output_it(_output, output_window);
131   execute_window_loop(
132     output_window,
133     [&](const Coordinates &id) {
134       Coordinates gather_id(id);
135       gather_id.collapse(_indices_rank, _axis);
136
137       U new_index;
138       switch (_indices_rank)
139       {
140         case 1:
141           new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[_axis]))));
142           break;
143         case 2:
144           new_index = *(
145             reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[_axis], id[_axis + 1]))));
146           break;
147         case 3:
148           new_index = *(reinterpret_cast<U *>(
149             _indices->ptr_to_element(Coordinates(id[_axis], id[_axis + 1], id[_axis + 2]))));
150           break;
151         default:
152           ARM_COMPUTE_ERROR("Wrong num of dimensions");
153           break;
154       }
155
156       gather_id.set(_axis, new_index);
157
158       std::copy_n(_input->ptr_to_element(gather_id),
159                   _input->info()->dimension(0) * _output->info()->element_size(), output_it.ptr());
160     },
161     output_it);
162 }
163
164 void NEGatherKernelEx::configure(const ITensor *input, const ITensor *indices, ITensor *output,
165                                  int axis)
166 {
167   ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices);
168   ARM_COMPUTE_ERROR_ON(indices->info()->num_dimensions() > 3);
169   ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32, DataType::S32);
170   ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(
171     input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, DataType::U16, DataType::S16,
172     DataType::U32, DataType::S32, DataType::F16, DataType::F32);
173
174   _input = input;
175   _indices = indices;
176   _output = output;
177   _axis = axis;
178   _indices_rank = indices->info()->num_dimensions();
179
180   if (_axis < 0)
181   {
182     _axis += input->info()->num_dimensions();
183   }
184   ARM_COMPUTE_ERROR_ON(0 > _axis || _axis >= static_cast<int32_t>(input->info()->num_dimensions()));
185
186   if (0 == _axis)
187   {
188     switch (_indices->info()->data_type())
189     {
190       case DataType::U32:
191         _func = &NEGatherKernelEx::gather_0_axis<uint32_t>;
192         break;
193       case DataType::S32:
194         _func = &NEGatherKernelEx::gather_0_axis<int32_t>;
195         break;
196       default:
197         ARM_COMPUTE_ERROR("Not supported");
198         break;
199     }
200   }
201   else
202   {
203     switch (_indices->info()->data_type())
204     {
205       case DataType::U32:
206         _func = &NEGatherKernelEx::gather_n_axis<uint32_t>;
207         break;
208       case DataType::S32:
209         _func = &NEGatherKernelEx::gather_n_axis<int32_t>;
210         break;
211       default:
212         ARM_COMPUTE_ERROR("Not supported");
213         break;
214     }
215   }
216   // Output auto initialization if not yet initialized
217   TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape_ex(
218     input->info()->tensor_shape(), indices->info()->tensor_shape(), _axis);
219   auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
220
221   // Create window
222   Window win = calculate_max_window(*output->info(), Steps());
223   output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
224
225   INEKernel::configure(win);
226 }
227
228 Status NEGatherKernelEx::validate(const ITensorInfo *input, const ITensorInfo *indices,
229                                   const ITensorInfo *output, int axis)
230 {
231   ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, indices, output);
232   ARM_COMPUTE_RETURN_ERROR_ON(indices->num_dimensions() > 3);
233   ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
234   ARM_COMPUTE_ERROR_ON(input->num_dimensions() + indices->num_dimensions() - 1 > 4);
235
236   if (axis < 0)
237   {
238     axis += input->num_dimensions();
239   }
240
241   ARM_COMPUTE_RETURN_ERROR_ON(0 > axis || axis >= static_cast<int32_t>(input->num_dimensions()));
242   ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
243   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(
244     input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, DataType::U16, DataType::S16,
245     DataType::U32, DataType::S32, DataType::F16, DataType::F32);
246
247   if (output->total_size() != 0)
248   {
249     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
250     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
251     TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape_ex(
252       input->tensor_shape(), indices->tensor_shape(), axis);
253     ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
254   }
255
256   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32, DataType::S32);
257
258   return Status{};
259 }
260
261 void NEGatherKernelEx::run(const Window &window, const ThreadInfo &info)
262 {
263   ARM_COMPUTE_UNUSED(info);
264   ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
265   ARM_COMPUTE_ERROR_ON(_func == nullptr);
266
267   (this->*_func)(window, info);
268 }
269
270 } // namespace arm_compute