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) 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/core/NEON/kernels/NEGatherKernelEx.h"
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"
57 /** Validate the indices
59 * Validate that indices are not negative
61 * @param[in] indices Indices tensor info.
63 template <typename U> void validate_indices(const ITensor *indices)
65 for (size_t i = 0; i < indices->info()->tensor_shape()[0]; ++i)
67 ARM_COMPUTE_ERROR_ON(*(reinterpret_cast<U *>(indices->ptr_to_element(Coordinates(i)))) < 0);
73 NEGatherKernelEx::NEGatherKernelEx()
74 : _input{}, _indices{}, _axis{}, _indices_rank{}, _output{}, _func{}
79 inline void NEGatherKernelEx::gather_0_axis(const Window &window, const ThreadInfo &info)
81 ARM_COMPUTE_UNUSED(info);
83 // Validate that the indices are not negative
84 validate_indices<U>(_indices);
86 Iterator output_it(_output, window);
89 [&](const Coordinates &id) {
90 Coordinates gather_id(id);
91 gather_id.collapse(_indices_rank);
94 switch (_indices_rank)
97 new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0]))));
100 new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0], id[1]))));
104 *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0], id[1], id[2]))));
107 ARM_COMPUTE_ERROR("Wrong num of dimensions");
111 gather_id.set(0, new_index);
113 std::copy_n(_input->ptr_to_element(gather_id), _output->info()->element_size(),
119 template <typename U>
120 void NEGatherKernelEx::gather_n_axis(const Window &window, const ThreadInfo &info)
122 ARM_COMPUTE_UNUSED(info);
124 // Validate that the indices are not negative
125 validate_indices<U>(_indices);
127 Window output_window{window};
128 output_window.set(Window::DimX, Window::Dimension(0, 1, 1));
130 Iterator output_it(_output, output_window);
133 [&](const Coordinates &id) {
134 Coordinates gather_id(id);
135 gather_id.collapse(_indices_rank, _axis);
138 switch (_indices_rank)
141 new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[_axis]))));
145 reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[_axis], id[_axis + 1]))));
148 new_index = *(reinterpret_cast<U *>(
149 _indices->ptr_to_element(Coordinates(id[_axis], id[_axis + 1], id[_axis + 2]))));
152 ARM_COMPUTE_ERROR("Wrong num of dimensions");
156 gather_id.set(_axis, new_index);
158 std::copy_n(_input->ptr_to_element(gather_id),
159 _input->info()->dimension(0) * _output->info()->element_size(), output_it.ptr());
164 void NEGatherKernelEx::configure(const ITensor *input, const ITensor *indices, ITensor *output,
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);
178 _indices_rank = indices->info()->num_dimensions();
182 _axis += input->info()->num_dimensions();
184 ARM_COMPUTE_ERROR_ON(0 > _axis || _axis >= static_cast<int32_t>(input->info()->num_dimensions()));
188 switch (_indices->info()->data_type())
191 _func = &NEGatherKernelEx::gather_0_axis<uint32_t>;
194 _func = &NEGatherKernelEx::gather_0_axis<int32_t>;
197 ARM_COMPUTE_ERROR("Not supported");
203 switch (_indices->info()->data_type())
206 _func = &NEGatherKernelEx::gather_n_axis<uint32_t>;
209 _func = &NEGatherKernelEx::gather_n_axis<int32_t>;
212 ARM_COMPUTE_ERROR("Not supported");
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());
222 Window win = calculate_max_window(*output->info(), Steps());
223 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
225 INEKernel::configure(win);
228 Status NEGatherKernelEx::validate(const ITensorInfo *input, const ITensorInfo *indices,
229 const ITensorInfo *output, int axis)
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);
238 axis += input->num_dimensions();
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);
247 if (output->total_size() != 0)
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());
256 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32, DataType::S32);
261 void NEGatherKernelEx::run(const Window &window, const ThreadInfo &info)
263 ARM_COMPUTE_UNUSED(info);
264 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
265 ARM_COMPUTE_ERROR_ON(_func == nullptr);
267 (this->*_func)(window, info);
270 } // namespace arm_compute