blob: 7090da8015d3f1f90293db7d5fdf0e07243dc6ac [file] [log] [blame]
John Kesapides2dce6cc2019-01-14 09:47:09 +00001/*
SiCongLib88272e2021-02-24 15:40:57 +00002 * Copyright (c) 2019-2021 Arm Limited.
John Kesapides2dce6cc2019-01-14 09:47:09 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEGatherKernel.h"
John Kesapides2dce6cc2019-01-14 09:47:09 +000025
26#include "arm_compute/core/Coordinates.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
John Kesapides2dce6cc2019-01-14 09:47:09 +000029#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/core/Window.h"
32#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/CPP/Validate.h"
34#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
John Kesapides2dce6cc2019-01-14 09:47:09 +000036
37namespace arm_compute
38{
39namespace
40{
41/** Validate the indices
42 *
43 * Validate that indices are not negative
44 *
45 * @param[in] indices Indices tensor info.
46 */
47template <typename U>
48void validate_indices(const ITensor *indices)
49{
50 for(size_t i = 0; i < indices->info()->tensor_shape()[0]; ++i)
51 {
52 ARM_COMPUTE_ERROR_ON(*(reinterpret_cast<U *>(indices->ptr_to_element(Coordinates(i)))) < 0);
53 }
54}
55
Georgios Pinitas33843562019-12-10 13:33:18 +000056Status validate_arguments(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis)
57{
58 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, indices, output);
59 ARM_COMPUTE_RETURN_ERROR_ON(indices->num_dimensions() > 1);
60 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
61
62 if(axis < 0)
63 {
64 axis += input->num_dimensions();
65 }
66
67 ARM_COMPUTE_RETURN_ERROR_ON(0 > axis || axis >= static_cast<int32_t>(input->num_dimensions()));
68 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
69
70 if(output->total_size() != 0)
71 {
72 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
73 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
74 TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input->tensor_shape(), indices->tensor_shape(), axis);
75 ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
76 }
77
78 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32, DataType::S32);
79
80 return Status{};
81}
John Kesapides2dce6cc2019-01-14 09:47:09 +000082} // namespace
83
84NEGatherKernel::NEGatherKernel()
85 : _input{}, _indices{}, _axis{}, _output{}, _func{}
86{
87}
88
89template <typename U>
90inline void NEGatherKernel::gather_0_axis(const Window &window, const ThreadInfo &info)
91{
92 ARM_COMPUTE_UNUSED(info);
93
94 // Validate that the indices are not negative
95 validate_indices<U>(_indices);
96
97 Iterator output_it(_output, window);
98 execute_window_loop(window, [&](const Coordinates & id)
99 {
100 Coordinates gather_id(id);
101
102 auto new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0]))));
103 gather_id.set(0, new_index);
104
105 std::copy_n(_input->ptr_to_element(gather_id), _output->info()->element_size(), output_it.ptr());
106 },
107 output_it);
108}
109
110template <typename U>
111void NEGatherKernel::gather_n_axis(const Window &window, const ThreadInfo &info)
112{
113 ARM_COMPUTE_UNUSED(info);
114
115 // Validate that the indices are not negative
116 validate_indices<U>(_indices);
117
118 Window output_window{ window };
119 output_window.set(Window::DimX, Window::Dimension(0, 1, 1));
120
121 Iterator output_it(_output, output_window);
122 execute_window_loop(output_window, [&](const Coordinates & id)
123 {
124 Coordinates gather_id(id);
125
126 auto new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[_axis]))));
127 gather_id.set(_axis, new_index);
128
129 std::copy_n(_input->ptr_to_element(gather_id), _input->info()->dimension(0) * _output->info()->element_size(), output_it.ptr());
130 },
131 output_it);
132}
133
134void NEGatherKernel::configure(const ITensor *input, const ITensor *indices, ITensor *output, int axis)
135{
136 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices);
Georgios Pinitas33843562019-12-10 13:33:18 +0000137 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), indices->info(), output->info(), axis));
John Kesapides2dce6cc2019-01-14 09:47:09 +0000138
139 _input = input;
140 _indices = indices;
141 _output = output;
142 _axis = axis;
143
144 if(_axis < 0)
145 {
146 _axis += input->info()->num_dimensions();
147 }
148 ARM_COMPUTE_ERROR_ON(0 > _axis || _axis >= static_cast<int32_t>(input->info()->num_dimensions()));
149
150 if(0 == _axis)
151 {
152 switch(_indices->info()->data_type())
153 {
154 case DataType::U32:
155 _func = &NEGatherKernel::gather_0_axis<uint32_t>;
156 break;
157 case DataType::S32:
158 _func = &NEGatherKernel::gather_0_axis<int32_t>;
159 break;
160 default:
161 ARM_COMPUTE_ERROR("Not supported");
162 break;
163 }
164 }
165 else
166 {
167 switch(_indices->info()->data_type())
168 {
169 case DataType::U32:
170 _func = &NEGatherKernel::gather_n_axis<uint32_t>;
171 break;
172 case DataType::S32:
173 _func = &NEGatherKernel::gather_n_axis<int32_t>;
174 break;
175 default:
176 ARM_COMPUTE_ERROR("Not supported");
177 break;
178 }
179 }
180 // Output auto initialization if not yet initialized
181 TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input->info()->tensor_shape(), indices->info()->tensor_shape(), _axis);
Georgios Pinitas33843562019-12-10 13:33:18 +0000182 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
John Kesapides2dce6cc2019-01-14 09:47:09 +0000183
184 // Create window
185 Window win = calculate_max_window(*output->info(), Steps());
John Kesapides2dce6cc2019-01-14 09:47:09 +0000186
187 INEKernel::configure(win);
188}
189
190Status NEGatherKernel::validate(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis)
191{
Georgios Pinitas33843562019-12-10 13:33:18 +0000192 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, indices, output, axis));
John Kesapides2dce6cc2019-01-14 09:47:09 +0000193 return Status{};
194}
195
196void NEGatherKernel::run(const Window &window, const ThreadInfo &info)
197{
198 ARM_COMPUTE_UNUSED(info);
199 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
200 ARM_COMPUTE_ERROR_ON(_func == nullptr);
201
202 (this->*_func)(window, info);
203}
204
205} // namespace arm_compute