blob: 1e027b72923cbb017f934a899d69bdf2b707c13d [file] [log] [blame]
John Kesapides2dce6cc2019-01-14 09:47:09 +00001/*
2 * Copyright (c) 2019 ARM Limited.
3 *
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
55} // namespace
56
57NEGatherKernel::NEGatherKernel()
58 : _input{}, _indices{}, _axis{}, _output{}, _func{}
59{
60}
61
62template <typename U>
63inline void NEGatherKernel::gather_0_axis(const Window &window, const ThreadInfo &info)
64{
65 ARM_COMPUTE_UNUSED(info);
66
67 // Validate that the indices are not negative
68 validate_indices<U>(_indices);
69
70 Iterator output_it(_output, window);
71 execute_window_loop(window, [&](const Coordinates & id)
72 {
73 Coordinates gather_id(id);
74
75 auto new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0]))));
76 gather_id.set(0, new_index);
77
78 std::copy_n(_input->ptr_to_element(gather_id), _output->info()->element_size(), output_it.ptr());
79 },
80 output_it);
81}
82
83template <typename U>
84void NEGatherKernel::gather_n_axis(const Window &window, const ThreadInfo &info)
85{
86 ARM_COMPUTE_UNUSED(info);
87
88 // Validate that the indices are not negative
89 validate_indices<U>(_indices);
90
91 Window output_window{ window };
92 output_window.set(Window::DimX, Window::Dimension(0, 1, 1));
93
94 Iterator output_it(_output, output_window);
95 execute_window_loop(output_window, [&](const Coordinates & id)
96 {
97 Coordinates gather_id(id);
98
99 auto new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[_axis]))));
100 gather_id.set(_axis, new_index);
101
102 std::copy_n(_input->ptr_to_element(gather_id), _input->info()->dimension(0) * _output->info()->element_size(), output_it.ptr());
103 },
104 output_it);
105}
106
107void NEGatherKernel::configure(const ITensor *input, const ITensor *indices, ITensor *output, int axis)
108{
109 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices);
110 ARM_COMPUTE_ERROR_ON(indices->info()->num_dimensions() != 1);
111 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32, DataType::S32);
112 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, DataType::U16, DataType::S16, DataType::U32, DataType::S32, DataType::F16, DataType::F32);
113
114 _input = input;
115 _indices = indices;
116 _output = output;
117 _axis = axis;
118
119 if(_axis < 0)
120 {
121 _axis += input->info()->num_dimensions();
122 }
123 ARM_COMPUTE_ERROR_ON(0 > _axis || _axis >= static_cast<int32_t>(input->info()->num_dimensions()));
124
125 if(0 == _axis)
126 {
127 switch(_indices->info()->data_type())
128 {
129 case DataType::U32:
130 _func = &NEGatherKernel::gather_0_axis<uint32_t>;
131 break;
132 case DataType::S32:
133 _func = &NEGatherKernel::gather_0_axis<int32_t>;
134 break;
135 default:
136 ARM_COMPUTE_ERROR("Not supported");
137 break;
138 }
139 }
140 else
141 {
142 switch(_indices->info()->data_type())
143 {
144 case DataType::U32:
145 _func = &NEGatherKernel::gather_n_axis<uint32_t>;
146 break;
147 case DataType::S32:
148 _func = &NEGatherKernel::gather_n_axis<int32_t>;
149 break;
150 default:
151 ARM_COMPUTE_ERROR("Not supported");
152 break;
153 }
154 }
155 // Output auto initialization if not yet initialized
156 TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input->info()->tensor_shape(), indices->info()->tensor_shape(), _axis);
157 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
158
159 // Create window
160 Window win = calculate_max_window(*output->info(), Steps());
161 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
162
163 INEKernel::configure(win);
164}
165
166Status NEGatherKernel::validate(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis)
167{
168 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, indices, output);
169 ARM_COMPUTE_RETURN_ERROR_ON(indices->num_dimensions() > 1);
170 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
171
172 if(axis < 0)
173 {
174 axis += input->num_dimensions();
175 }
176
177 ARM_COMPUTE_RETURN_ERROR_ON(0 > axis || axis >= static_cast<int32_t>(input->num_dimensions()));
Georgios Pinitas8f5802f2019-02-22 11:08:32 +0000178 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
John Kesapides2dce6cc2019-01-14 09:47:09 +0000179 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8,
180 DataType::U16, DataType::S16,
181 DataType::U32, DataType::S32, DataType::F16, DataType::F32);
182
183 if(output->total_size() != 0)
184 {
185 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
186 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
187 TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input->tensor_shape(), indices->tensor_shape(), axis);
188 ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
189 }
190
191 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32, DataType::S32);
192
193 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