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]))));
101 *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0], id[1]))));
105 reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0], id[1], id[2]))));
108 ARM_COMPUTE_ERROR("Wrong num of dimensions");
112 gather_id.set(0, new_index);
114 std::copy_n(_input->ptr_to_element(gather_id), _output->info()->element_size(),
120 template <typename U>
121 void NEGatherKernelEx::gather_n_axis(const Window &window, const ThreadInfo &info)
123 ARM_COMPUTE_UNUSED(info);
125 // Validate that the indices are not negative
126 validate_indices<U>(_indices);
128 Window output_window{window};
129 output_window.set(Window::DimX, Window::Dimension(0, 1, 1));
131 Iterator output_it(_output, output_window);
134 [&](const Coordinates &id) {
135 Coordinates gather_id(id);
136 gather_id.collapse(_indices_rank, _axis);
139 switch (_indices_rank)
142 new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[_axis]))));
145 new_index = *(reinterpret_cast<U *>(
146 _indices->ptr_to_element(Coordinates(id[_axis], id[_axis + 1]))));
149 new_index = *(reinterpret_cast<U *>(
150 _indices->ptr_to_element(Coordinates(id[_axis], id[_axis + 1], id[_axis + 2]))));
153 ARM_COMPUTE_ERROR("Wrong num of dimensions");
157 gather_id.set(_axis, new_index);
159 std::copy_n(_input->ptr_to_element(gather_id),
160 _input->info()->dimension(0) * _output->info()->element_size(),
166 void NEGatherKernelEx::configure(const ITensor *input, const ITensor *indices, ITensor *output,
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);
180 _indices_rank = indices->info()->num_dimensions();
184 _axis += input->info()->num_dimensions();
186 ARM_COMPUTE_ERROR_ON(0 > _axis || _axis >= static_cast<int32_t>(input->info()->num_dimensions()));
190 switch (_indices->info()->data_type())
193 _func = &NEGatherKernelEx::gather_0_axis<uint32_t>;
196 _func = &NEGatherKernelEx::gather_0_axis<int32_t>;
199 ARM_COMPUTE_ERROR("Not supported");
205 switch (_indices->info()->data_type())
208 _func = &NEGatherKernelEx::gather_n_axis<uint32_t>;
211 _func = &NEGatherKernelEx::gather_n_axis<int32_t>;
214 ARM_COMPUTE_ERROR("Not supported");
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());
224 Window win = calculate_max_window(*output->info(), Steps());
225 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
227 INEKernel::configure(win);
230 Status NEGatherKernelEx::validate(const ITensorInfo *input, const ITensorInfo *indices,
231 const ITensorInfo *output, int axis)
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);
240 axis += input->num_dimensions();
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);
249 if (output->total_size() != 0)
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());
258 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32, DataType::S32);
263 void NEGatherKernelEx::run(const Window &window, const ThreadInfo &info)
265 ARM_COMPUTE_UNUSED(info);
266 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
267 ARM_COMPUTE_ERROR_ON(_func == nullptr);
269 (this->*_func)(window, info);
272 } // namespace arm_compute