blob: 906e8a053ea5d04a41b0dcc531a2ddd34d703adf [file] [log] [blame]
John Kesapides2dce6cc2019-01-14 09:47:09 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019 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 */
24#include "arm_compute/core/NEON/kernels/NEGatherKernel.h"
25
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000026#include "arm_compute/core/CPP/Validate.h"
John Kesapides2dce6cc2019-01-14 09:47:09 +000027#include "arm_compute/core/Coordinates.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/IAccessWindow.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
34#include "arm_compute/core/utils/misc/ShapeCalculator.h"
35
36namespace arm_compute
37{
38namespace
39{
40/** Validate the indices
41 *
42 * Validate that indices are not negative
43 *
44 * @param[in] indices Indices tensor info.
45 */
46template <typename U>
47void validate_indices(const ITensor *indices)
48{
49 for(size_t i = 0; i < indices->info()->tensor_shape()[0]; ++i)
50 {
51 ARM_COMPUTE_ERROR_ON(*(reinterpret_cast<U *>(indices->ptr_to_element(Coordinates(i)))) < 0);
52 }
53}
54
Georgios Pinitas33843562019-12-10 13:33:18 +000055Status validate_arguments(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis)
56{
57 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, indices, output);
58 ARM_COMPUTE_RETURN_ERROR_ON(indices->num_dimensions() > 1);
59 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
60
61 if(axis < 0)
62 {
63 axis += input->num_dimensions();
64 }
65
66 ARM_COMPUTE_RETURN_ERROR_ON(0 > axis || axis >= static_cast<int32_t>(input->num_dimensions()));
67 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
68
69 if(output->total_size() != 0)
70 {
71 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
72 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
73 TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input->tensor_shape(), indices->tensor_shape(), axis);
74 ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
75 }
76
77 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32, DataType::S32);
78
79 return Status{};
80}
John Kesapides2dce6cc2019-01-14 09:47:09 +000081} // namespace
82
83NEGatherKernel::NEGatherKernel()
84 : _input{}, _indices{}, _axis{}, _output{}, _func{}
85{
86}
87
88template <typename U>
89inline void NEGatherKernel::gather_0_axis(const Window &window, const ThreadInfo &info)
90{
91 ARM_COMPUTE_UNUSED(info);
92
93 // Validate that the indices are not negative
94 validate_indices<U>(_indices);
95
96 Iterator output_it(_output, window);
97 execute_window_loop(window, [&](const Coordinates & id)
98 {
99 Coordinates gather_id(id);
100
101 auto new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0]))));
102 gather_id.set(0, new_index);
103
104 std::copy_n(_input->ptr_to_element(gather_id), _output->info()->element_size(), output_it.ptr());
105 },
106 output_it);
107}
108
109template <typename U>
110void NEGatherKernel::gather_n_axis(const Window &window, const ThreadInfo &info)
111{
112 ARM_COMPUTE_UNUSED(info);
113
114 // Validate that the indices are not negative
115 validate_indices<U>(_indices);
116
117 Window output_window{ window };
118 output_window.set(Window::DimX, Window::Dimension(0, 1, 1));
119
120 Iterator output_it(_output, output_window);
121 execute_window_loop(output_window, [&](const Coordinates & id)
122 {
123 Coordinates gather_id(id);
124
125 auto new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[_axis]))));
126 gather_id.set(_axis, new_index);
127
128 std::copy_n(_input->ptr_to_element(gather_id), _input->info()->dimension(0) * _output->info()->element_size(), output_it.ptr());
129 },
130 output_it);
131}
132
133void NEGatherKernel::configure(const ITensor *input, const ITensor *indices, ITensor *output, int axis)
134{
135 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices);
Georgios Pinitas33843562019-12-10 13:33:18 +0000136 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), indices->info(), output->info(), axis));
John Kesapides2dce6cc2019-01-14 09:47:09 +0000137
138 _input = input;
139 _indices = indices;
140 _output = output;
141 _axis = axis;
142
143 if(_axis < 0)
144 {
145 _axis += input->info()->num_dimensions();
146 }
147 ARM_COMPUTE_ERROR_ON(0 > _axis || _axis >= static_cast<int32_t>(input->info()->num_dimensions()));
148
149 if(0 == _axis)
150 {
151 switch(_indices->info()->data_type())
152 {
153 case DataType::U32:
154 _func = &NEGatherKernel::gather_0_axis<uint32_t>;
155 break;
156 case DataType::S32:
157 _func = &NEGatherKernel::gather_0_axis<int32_t>;
158 break;
159 default:
160 ARM_COMPUTE_ERROR("Not supported");
161 break;
162 }
163 }
164 else
165 {
166 switch(_indices->info()->data_type())
167 {
168 case DataType::U32:
169 _func = &NEGatherKernel::gather_n_axis<uint32_t>;
170 break;
171 case DataType::S32:
172 _func = &NEGatherKernel::gather_n_axis<int32_t>;
173 break;
174 default:
175 ARM_COMPUTE_ERROR("Not supported");
176 break;
177 }
178 }
179 // Output auto initialization if not yet initialized
180 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 +0000181 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
John Kesapides2dce6cc2019-01-14 09:47:09 +0000182
183 // Create window
184 Window win = calculate_max_window(*output->info(), Steps());
185 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
186
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