blob: 71116447f446093a37153f9d23e1711ea4b605dc [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Diego Lopez Recasff860cf2018-02-22 13:08:01 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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/NEScaleKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#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/ITensor.h"
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010031#include "arm_compute/core/NEON/wrapper/wrapper.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010035#include "arm_compute/core/utils/misc/Utility.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37#include <arm_neon.h>
38#include <cstddef>
39#include <cstdint>
40
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010041namespace arm_compute
42{
43namespace
44{
Georgios Pinitas20b43132018-05-14 16:05:23 +010045Status validate_arguments(const ITensorInfo *input, const ITensorInfo *dx, const ITensorInfo *dy,
46 const ITensorInfo *offsets, ITensorInfo *output, InterpolationPolicy policy,
47 BorderMode border_mode, SamplingPolicy sampling_policy)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010048{
Georgios Pinitas20b43132018-05-14 16:05:23 +010049 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::F32);
50 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
52 ARM_COMPUTE_RETURN_ERROR_ON(output == input);
53 ARM_COMPUTE_RETURN_ERROR_ON(sampling_policy != SamplingPolicy::CENTER);
54 ARM_COMPUTE_UNUSED(border_mode);
55
56 const DataLayout data_layout = input->data_layout();
57 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH)) == 0);
58 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT)) == 0);
59
60 if(policy == InterpolationPolicy::NEAREST_NEIGHBOR)
61 {
62 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32);
63 }
64
65 if(policy == InterpolationPolicy::BILINEAR)
66 {
67 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32);
68 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dx, 1, DataType::F32);
69 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dy, 1, DataType::F32);
70 }
71
72 if(policy == InterpolationPolicy::AREA)
73 {
74 ARM_COMPUTE_RETURN_ERROR_ON(data_layout != DataLayout::NCHW);
75 }
76
77 return Status{};
78}
79
80std::pair<Status, Window> validate_and_configure_window_nchw(ITensorInfo *input, ITensorInfo *dx, ITensorInfo *dy, ITensorInfo *offsets, ITensorInfo *output,
81 InterpolationPolicy policy, bool border_undefined, SamplingPolicy sampling_policy, BorderSize border_size)
82{
83 bool window_changed{ false };
84 Window win{};
85
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010086 constexpr unsigned int num_elems_processed_per_iteration = 16;
87
88 // Configure kernel window
Georgios Pinitas20b43132018-05-14 16:05:23 +010089 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010090
Georgios Pinitas20b43132018-05-14 16:05:23 +010091 const ValidRegion &input_valid_region = input->valid_region();
92
93 if(offsets != nullptr)
94 {
95 AccessWindowHorizontal offsets_access(offsets, 0, num_elems_processed_per_iteration);
96 window_changed = window_changed || update_window_and_padding(win, offsets_access);
97 }
98 if(dx != nullptr && dy != nullptr)
99 {
100 AccessWindowHorizontal dx_access(dx, 0, num_elems_processed_per_iteration);
101 AccessWindowHorizontal dy_access(dy, 0, num_elems_processed_per_iteration);
102 window_changed = window_changed || update_window_and_padding(win, dx_access, dy_access);
103 }
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100104
105 // Reads can occur within the valid region of the input
Georgios Pinitas20b43132018-05-14 16:05:23 +0100106 AccessWindowStatic input_access(input, input_valid_region.anchor[0] - border_size.left,
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100107 input_valid_region.anchor[1] - border_size.top,
108 input_valid_region.anchor[0] + input_valid_region.shape[0] + border_size.right,
109 input_valid_region.anchor[1] + input_valid_region.shape[1] + border_size.bottom);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100110 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
111 window_changed = window_changed || update_window_and_padding(win, input_access, output_access);
112 output_access.set_valid_region(win, calculate_valid_region_scale(*input, output->tensor_shape(),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100113 policy, sampling_policy, border_undefined));
114
Georgios Pinitas20b43132018-05-14 16:05:23 +0100115 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
116 return std::make_pair(err, win);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100117}
Georgios Pinitas20b43132018-05-14 16:05:23 +0100118
119std::pair<Status, Window> validate_and_configure_window_nhwc(ITensorInfo *input, ITensorInfo *output,
120 InterpolationPolicy policy, bool border_undefined,
121 SamplingPolicy sampling_policy, BorderSize border_size)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100122{
Georgios Pinitas20b43132018-05-14 16:05:23 +0100123 bool window_changed{ false };
124 Window win{};
125
126 const unsigned int num_elems_processed_per_iteration = (policy == InterpolationPolicy::NEAREST_NEIGHBOR) ? 16 / input->element_size() : 1;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100127
128 // Configure kernel window
Georgios Pinitas20b43132018-05-14 16:05:23 +0100129 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100130
Georgios Pinitas20b43132018-05-14 16:05:23 +0100131 AccessWindowStatic input_access(input, 0, -border_size.top,
132 ceil_to_multiple(input->tensor_shape()[0], num_elems_processed_per_iteration),
133 input->tensor_shape()[1]);
134 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100135
Georgios Pinitas20b43132018-05-14 16:05:23 +0100136 window_changed = update_window_and_padding(win, input_access, output_access);
137 output->set_valid_region(calculate_valid_region_scale(*input, output->tensor_shape(),
138 policy, sampling_policy, border_undefined));
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100139
Georgios Pinitas20b43132018-05-14 16:05:23 +0100140 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
141 return std::make_pair(err, win);
142}
143
144std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *dx, ITensorInfo *dy, ITensorInfo *offsets, ITensorInfo *output,
145 InterpolationPolicy policy, bool border_undefined, SamplingPolicy sampling_policy, BorderSize border_size)
146{
147 std::pair<Status, Window> win_config;
148 switch(input->data_layout())
149 {
150 case DataLayout::NCHW:
151 win_config = validate_and_configure_window_nchw(input, dx, dy, offsets, output, policy, border_undefined, sampling_policy, border_size);
152 break;
153 case DataLayout::NHWC:
154 win_config = validate_and_configure_window_nhwc(input, output, policy, border_undefined, sampling_policy, border_size);
155 break;
156 default:
157 win_config = std::make_pair(ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported data layout!"), Window{});
158 }
159
160 return win_config;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100161}
162
163template <typename T>
164inline void scale_nearest_nhwc_core(const ITensor *input, const ITensor *offsets, ITensor *output,
165 float hr, Window window, const Window &win_in, size_t stride_w, size_t stride_h, size_t stride_c)
166{
167 Iterator in(input, win_in);
168 Iterator out(output, window);
169
170 const size_t offsets_stride = stride_w / sizeof(T);
171
172 execute_window_loop(window, [&](const Coordinates & id)
173 {
174 const auto offset = *reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id.y(), id.z())));
175 const int in_yi = (id.z() + 0.5f) * hr;
176 const int offset_row = in_yi * stride_h + id.x() * stride_c;
177 wrapper::vstore(reinterpret_cast<T *>(out.ptr()),
178 wrapper::vloadq(reinterpret_cast<const T *>(in.ptr() + offset * offsets_stride + offset_row)));
179 },
180 in, out);
181}
182
183template <typename T>
184inline void scale_bilinear_nhwc_core(const ITensor *input, const ITensor *offsets, const ITensor *dx, const ITensor *dy, ITensor *output,
185 float hr, Window window, const Window &win_in, size_t stride_w, size_t stride_h, size_t stride_c, BorderMode border_mode)
186{
187 Iterator in(input, win_in);
188 Iterator out(output, window);
189
190 const size_t stride_w_elems = stride_w / sizeof(T);
191 const size_t stride_h_elems = stride_h / sizeof(T);
192
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100193 const int input_width = input->info()->dimension(1);
194 const int input_height = input->info()->dimension(2);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100195
196 const T *border_area = reinterpret_cast<T *>(input->buffer() + input->info()->offset_first_element_in_bytes() - stride_w);
197
198 auto is_valid = [](int x, int low_x, int high_x, int y, int low_y, int high_y)
199 {
200 return !(x < low_x || x > high_x || y < low_y || y > high_y);
201 };
202
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100203 int border_size = (border_mode == BorderMode::UNDEFINED) ? 0 : 1;
204
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100205 execute_window_loop(window, [&](const Coordinates & id)
206 {
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100207 const auto offset = (*reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id.y(), id.z())))) / static_cast<int>(sizeof(T));
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100208 const auto dx_scale = *reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id.y(), id.z())));
209 const auto dy_scale = *reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id.y(), id.z())));
210 const int in_yi = std::floor((id.z() + 0.5f) * hr - 0.5f);
211 const int offset_row = in_yi * stride_h + id.x() * stride_c;
212 const T *in_ptr = reinterpret_cast<T *>(in.ptr() + offset * stride_w + offset_row);
213
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100214 if(is_valid(offset, -border_size, input_width - 1 + border_size, in_yi, -border_size, input_height - 1 + border_size))
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100215 {
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100216 T a00 = 0, a01 = 0, a10 = 0, a11 = 0;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100217
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100218 if(border_mode == BorderMode::CONSTANT)
219 {
220 a00 = is_valid(offset, 0, input_width - 1, in_yi, 0, input_height - 1) ? *in_ptr : *border_area;
221 a01 = is_valid(offset + 1, 0, input_width - 1, in_yi, 0, input_height - 1) ? *(in_ptr + stride_w_elems) : *border_area;
222 a10 = is_valid(offset, 0, input_width - 1, in_yi + 1, 0, input_height - 1) ? *(in_ptr + stride_h_elems) : *border_area;
223 a11 = is_valid(offset + 1, 0, input_width - 1, in_yi + 1, 0, input_height - 1) ? *(in_ptr + stride_h_elems + stride_w_elems) : *border_area;
224 }
225 else if(border_mode == BorderMode::REPLICATE)
226 {
227 auto clamped_x = utility::clamp<int>(offset, 0, input_width - 1);
228 auto clamped_x1 = utility::clamp<int>(offset + 1, 0, input_width - 1);
229 auto clamped_y = utility::clamp<int>(in_yi, 0, input_height - 1);
230 auto clamped_y1 = utility::clamp<int>(in_yi + 1, 0, input_height - 1);
231
232 a00 = *reinterpret_cast<T *>(in.ptr() + clamped_x * stride_w + clamped_y * stride_h + id.x() * stride_c);
233 a01 = *reinterpret_cast<T *>(in.ptr() + clamped_x1 * stride_w + clamped_y * stride_h + id.x() * stride_c);
234 a10 = *reinterpret_cast<T *>(in.ptr() + clamped_x * stride_w + clamped_y1 * stride_h + id.x() * stride_c);
235 a11 = *reinterpret_cast<T *>(in.ptr() + clamped_x1 * stride_w + clamped_y1 * stride_h + id.x() * stride_c);
236 }
237 else
238 {
239 a00 = is_valid(offset, 0, input_width - 1, in_yi, 0, input_height - 1) ? *in_ptr : 0;
240 a01 = is_valid(offset + 1, 0, input_width - 1, in_yi, 0, input_height - 1) ? *(in_ptr + stride_w_elems) : 0;
241 a10 = is_valid(offset, 0, input_width - 1, in_yi + 1, 0, input_height - 1) ? *(in_ptr + stride_h_elems) : 0;
242 a11 = is_valid(offset + 1, 0, input_width - 1, in_yi + 1, 0, input_height - 1) ? *(in_ptr + stride_h_elems + stride_w_elems) : 0;
243 }
244
245 // Perform interpolation
246 const float dx1 = 1.0f - dx_scale;
247 const float dy1 = 1.0f - dy_scale;
248
249 const float w1 = dx1 * dy1;
250 const float w2 = dx_scale * dy1;
251 const float w3 = dx1 * dy_scale;
252 const float w4 = dx_scale * dy_scale;
253
254 // Store result
255 *reinterpret_cast<T *>(out.ptr()) = static_cast<T>(a00 * w1 + a01 * w2 + a10 * w3 + a11 * w4);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100256 }
257 else
258 {
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100259 if(border_mode == BorderMode::CONSTANT)
260 {
261 *reinterpret_cast<T *>(out.ptr()) = *border_area;
262 }
263 else if(border_mode == BorderMode::REPLICATE)
264 {
265 auto clamped_x = utility::clamp<int>(offset, 0, input_width - 1);
266 auto clamped_y = utility::clamp<int>(in_yi, 0, input_height - 1);
267 *reinterpret_cast<T *>(out.ptr()) = *reinterpret_cast<T *>(in.ptr() + clamped_x * stride_w + clamped_y * stride_h + id.x() * stride_c);
268 }
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100269 }
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100270 },
271 in, out);
272}
273} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100274
275NEScaleKernel::NEScaleKernel()
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100276 : _func(nullptr), _offsets(nullptr), _dx(nullptr), _dy(nullptr), _input(nullptr), _output(nullptr), _policy(), _border_size(1), _border_mode()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100277{
278}
279
280BorderSize NEScaleKernel::border_size() const
281{
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100282 return _border_size;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100283}
284
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100285void NEScaleKernel::configure(const ITensor *input, const ITensor *dx, const ITensor *dy, const ITensor *offsets,
286 ITensor *output, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100287{
Georgios Pinitas20b43132018-05-14 16:05:23 +0100288 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100289
Georgios Pinitas20b43132018-05-14 16:05:23 +0100290 // Perform validation step
291 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(),
292 dx != nullptr ? dx->info() : nullptr,
293 dy != nullptr ? dy->info() : nullptr,
294 offsets != nullptr ? offsets->info() : nullptr,
295 output->info(),
296 policy, border_mode, sampling_policy));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100297
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100298 // Get data layout and width/height indices
299 const DataLayout data_layout = input->info()->data_layout();
300 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
301 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100302
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100303 _input = input;
304 _output = output;
305 _offsets = offsets;
306 _dx = dx;
307 _dy = dy;
308 _policy = policy;
309 _border_size = BorderSize(1);
310 _border_mode = border_mode;
311
312 // Compute the ratio between source width/height and destination width/height
313 const auto wr = static_cast<float>(input->info()->dimension(idx_width)) / static_cast<float>(output->info()->dimension(idx_width));
314 const auto hr = static_cast<float>(input->info()->dimension(idx_height)) / static_cast<float>(output->info()->dimension(idx_height));
315
316 // Add constant border only on top in case of NHWC layout
317 if(data_layout == DataLayout::NHWC)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100318 {
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100319 _border_size = (border_mode == BorderMode::CONSTANT && policy == InterpolationPolicy::BILINEAR) ? BorderSize(1, 0, 0, 0) : BorderSize(0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100320 }
321
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100322 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100323 if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
324 {
325 policy = InterpolationPolicy::NEAREST_NEIGHBOR;
326 }
327
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100328 // Select interpolation function
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100329 switch(policy)
330 {
331 case InterpolationPolicy::NEAREST_NEIGHBOR:
332 {
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100333 _func = (data_layout == DataLayout::NCHW) ? &NEScaleKernel::scale_nearest_nchw : &NEScaleKernel::scale_nhwc;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100334 break;
335 }
336 case InterpolationPolicy::BILINEAR:
337 {
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100338 _func = (data_layout == DataLayout::NCHW) ? &NEScaleKernel::scale_bilinear_nchw : &NEScaleKernel::scale_nhwc;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100339 break;
340 }
341 case InterpolationPolicy::AREA:
342 {
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100343 _func = &NEScaleKernel::scale_area_nchw;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100344 break;
345 }
346 default:
347 ARM_COMPUTE_ERROR("Unsupported interpolation mode");
348 }
349
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100350 // Configure window
Georgios Pinitas20b43132018-05-14 16:05:23 +0100351 std::pair<Status, Window> win_config = validate_and_configure_window(input->info(),
352 dx != nullptr ? dx->info() : nullptr,
353 dy != nullptr ? dy->info() : nullptr,
354 offsets != nullptr ? offsets->info() : nullptr,
355 output->info(),
356 policy, border_mode == BorderMode::UNDEFINED, sampling_policy, border_size());
357 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
358 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100359}
360
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100361void NEScaleKernel::scale_nearest_nchw(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100362{
363 const size_t input_stride = _input->info()->strides_in_bytes()[1];
364
365 // Compute the ratio between source height and destination height
366 const auto hr = static_cast<float>(_input->info()->dimension(1)) / static_cast<float>(_output->info()->dimension(1));
367
368 // Don't increment in X and Y direction for the input tensor
369 // A pointer to the start of this plane is needed as base for the precomputed offsets
370 Window win_in(window);
371 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
372 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
373
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100374 // Set offsets window
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100375 Window win_off;
376 win_off.set(Window::DimX, window[Window::DimX]);
377 win_off.set(Window::DimY, window[Window::DimY]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100378 for(size_t d = Window::DimZ; d < _offsets->info()->num_dimensions(); ++d)
379 {
380 win_off.set(d, Window::Dimension(0, 0, 0));
381 }
382
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100383 // Create iterators
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100384 Iterator in(_input, win_in);
385 Iterator out(_output, window);
386 Iterator offsets(_offsets, win_off);
387
388 switch(_input->info()->data_type())
389 {
390 case DataType::U8:
391 {
392 uint8x16_t tmp = vdupq_n_u8(0);
393
394 execute_window_loop(window, [&](const Coordinates & id)
395 {
396 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets.ptr());
397 const uint8_t *const in_ptr = in.ptr();
398
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100399 const int in_yi = std::floor((id.y() + 0.5f) * hr);
400 const int in_yi_clamped = std::min(static_cast<int>(_input->info()->dimension(1)), std::max(in_yi, -1));
401 ARM_COMPUTE_ERROR_ON(in_yi_clamped < -1 || in_yi_clamped > static_cast<int>(_input->info()->dimension(1)));
402 const int offset_row = in_yi_clamped * input_stride;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100403
404 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[0] + offset_row], tmp, 0);
405 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[1] + offset_row], tmp, 1);
406 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[2] + offset_row], tmp, 2);
407 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[3] + offset_row], tmp, 3);
408 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[4] + offset_row], tmp, 4);
409 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[5] + offset_row], tmp, 5);
410 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[6] + offset_row], tmp, 6);
411 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[7] + offset_row], tmp, 7);
412 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[8] + offset_row], tmp, 8);
413 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[9] + offset_row], tmp, 9);
414 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[10] + offset_row], tmp, 10);
415 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[11] + offset_row], tmp, 11);
416 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[12] + offset_row], tmp, 12);
417 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[13] + offset_row], tmp, 13);
418 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[14] + offset_row], tmp, 14);
419 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[15] + offset_row], tmp, 15);
420
421 vst1q_u8(out.ptr(), tmp);
422 },
423 in, offsets, out);
424 break;
425 }
426 case DataType::S16:
427 {
428 int16x8x2_t tmp =
429 {
430 {
431 vdupq_n_s16(0),
432 vdupq_n_s16(0)
433 }
434 };
435
436 execute_window_loop(window, [&](const Coordinates & id)
437 {
438 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets.ptr());
439
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100440 const int in_yi = (id.y() + 0.5f) * hr;
441 const int offset_row = in_yi * input_stride;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100442
443 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[0] + offset_row), tmp.val[0], 0);
444 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[2] + offset_row), tmp.val[0], 1);
445 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[4] + offset_row), tmp.val[0], 2);
446 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[6] + offset_row), tmp.val[0], 3);
447 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[8] + offset_row), tmp.val[0], 4);
448 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[10] + offset_row), tmp.val[0], 5);
449 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[12] + offset_row), tmp.val[0], 6);
450 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[14] + offset_row), tmp.val[0], 7);
451
452 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[1] + offset_row), tmp.val[1], 0);
453 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[3] + offset_row), tmp.val[1], 1);
454 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[5] + offset_row), tmp.val[1], 2);
455 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[7] + offset_row), tmp.val[1], 3);
456 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[9] + offset_row), tmp.val[1], 4);
457 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[11] + offset_row), tmp.val[1], 5);
458 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[13] + offset_row), tmp.val[1], 6);
459 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[15] + offset_row), tmp.val[1], 7);
460
461 vst2q_s16(reinterpret_cast<int16_t *>(out.ptr()), tmp);
462 },
463 in, offsets, out);
464 break;
465 }
Georgios Pinitas583137c2017-08-31 18:12:42 +0100466 case DataType::F32:
467 {
468 float32x4x4_t tmp =
469 {
470 {
471 vdupq_n_f32(0),
472 vdupq_n_f32(0),
473 vdupq_n_f32(0),
474 vdupq_n_f32(0)
475 }
476 };
477
478 execute_window_loop(window, [&](const Coordinates & id)
479 {
480 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets.ptr());
481
482 const int in_yi = (id.y() + 0.5f) * hr;
483 const int offset_row = in_yi * input_stride;
484
485 tmp.val[0] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[0] + offset_row), tmp.val[0], 0);
486 tmp.val[0] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[4] + offset_row), tmp.val[0], 1);
487 tmp.val[0] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[8] + offset_row), tmp.val[0], 2);
488 tmp.val[0] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[12] + offset_row), tmp.val[0], 3);
489
490 tmp.val[1] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[1] + offset_row), tmp.val[1], 0);
491 tmp.val[1] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[5] + offset_row), tmp.val[1], 1);
492 tmp.val[1] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[9] + offset_row), tmp.val[1], 2);
493 tmp.val[1] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[13] + offset_row), tmp.val[1], 3);
494
495 tmp.val[2] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[2] + offset_row), tmp.val[2], 0);
496 tmp.val[2] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[6] + offset_row), tmp.val[2], 1);
497 tmp.val[2] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[10] + offset_row), tmp.val[2], 2);
498 tmp.val[2] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[14] + offset_row), tmp.val[2], 3);
499
500 tmp.val[3] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[3] + offset_row), tmp.val[3], 0);
501 tmp.val[3] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[7] + offset_row), tmp.val[3], 1);
502 tmp.val[3] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[11] + offset_row), tmp.val[3], 2);
503 tmp.val[3] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[15] + offset_row), tmp.val[3], 3);
504
505 vst4q_f32(reinterpret_cast<float *>(out.ptr()), tmp);
506 },
507 in, offsets, out);
508 break;
509 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100510 default:
511 ARM_COMPUTE_ERROR("Not supported");
512 break;
513 }
514}
515
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100516void NEScaleKernel::scale_bilinear_nchw(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100517{
Georgios Pinitas583137c2017-08-31 18:12:42 +0100518 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(_input, 1, DataType::U8, DataType::S16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100519
520 // Compute the ratio between source height and destination height
521 const auto hr = static_cast<float>(_input->info()->dimension(1)) / static_cast<float>(_output->info()->dimension(1));
522
523 // Don't increment in X and Y direction for the input tensor
524 // A pointer to the start of this plane is needed as base for the precomputed offsets
525 Window win_in(window);
526 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
527 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
528
529 Window win_off;
530 win_off.set(Window::DimX, window.x());
531 win_off.set(Window::DimY, window.y());
532
533 for(size_t d = Window::DimZ; d < _offsets->info()->num_dimensions(); ++d)
534 {
535 win_off.set(d, Window::Dimension(0, 0, 0));
536 }
537
538 Iterator in(_input, win_in);
539 Iterator out(_output, window);
540 Iterator offsets(_offsets, win_off);
541 Iterator dx(_dx, win_off);
542 Iterator dy(_dy, win_off);
543
544 /* Input image stride */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100545 const size_t in_stide_in_bytes = _input->info()->strides_in_bytes()[1];
546 const size_t in_stride = in_stide_in_bytes / _input->info()->element_size();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100547
Georgios Pinitas583137c2017-08-31 18:12:42 +0100548 switch(_input->info()->data_type())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100549 {
Georgios Pinitas583137c2017-08-31 18:12:42 +0100550 case DataType::U8:
551 {
552 execute_window_loop(window, [&](const Coordinates & id)
553 {
554 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets.ptr());
555 const auto dx_ptr = reinterpret_cast<const float *>(dx.ptr());
556 const auto dy_ptr = reinterpret_cast<const float *>(dy.ptr());
557 const auto in_ptr = reinterpret_cast<const uint8_t *>(in.ptr());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100558
Georgios Pinitas583137c2017-08-31 18:12:42 +0100559 const int in_yi = std::floor((id.y() + 0.5f) * hr - 0.5f);
560 const int offset_row = in_yi * in_stide_in_bytes;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100561
Georgios Pinitas583137c2017-08-31 18:12:42 +0100562 uint8x8_t tmp0 = vdup_n_u8(0);
563 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[0] + offset_row], in_stride, dx_ptr[0], dy_ptr[0]), tmp0, 0);
564 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[1] + offset_row], in_stride, dx_ptr[1], dy_ptr[1]), tmp0, 1);
565 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[2] + offset_row], in_stride, dx_ptr[2], dy_ptr[2]), tmp0, 2);
566 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[3] + offset_row], in_stride, dx_ptr[3], dy_ptr[3]), tmp0, 3);
567 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[4] + offset_row], in_stride, dx_ptr[4], dy_ptr[4]), tmp0, 4);
568 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[5] + offset_row], in_stride, dx_ptr[5], dy_ptr[5]), tmp0, 5);
569 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[6] + offset_row], in_stride, dx_ptr[6], dy_ptr[6]), tmp0, 6);
570 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[7] + offset_row], in_stride, dx_ptr[7], dy_ptr[7]), tmp0, 7);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100571
Georgios Pinitas583137c2017-08-31 18:12:42 +0100572 uint8x8_t tmp1 = vdup_n_u8(0);
573 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[8] + offset_row], in_stride, dx_ptr[8], dy_ptr[8]), tmp1, 0);
574 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[9] + offset_row], in_stride, dx_ptr[9], dy_ptr[9]), tmp1, 1);
575 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[10] + offset_row], in_stride, dx_ptr[10], dy_ptr[10]), tmp1, 2);
576 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[11] + offset_row], in_stride, dx_ptr[11], dy_ptr[11]), tmp1, 3);
577 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[12] + offset_row], in_stride, dx_ptr[12], dy_ptr[12]), tmp1, 4);
578 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[13] + offset_row], in_stride, dx_ptr[13], dy_ptr[13]), tmp1, 5);
579 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[14] + offset_row], in_stride, dx_ptr[14], dy_ptr[14]), tmp1, 6);
580 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[15] + offset_row], in_stride, dx_ptr[15], dy_ptr[15]), tmp1, 7);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100581
Georgios Pinitas583137c2017-08-31 18:12:42 +0100582 vst1q_u8(out.ptr(), vcombine_u8(tmp0, tmp1));
583 },
584 in, offsets, dx, dy, out);
585 break;
586 }
587 case DataType::S16:
588 {
589 execute_window_loop(window, [&](const Coordinates & id)
590 {
591 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets.ptr());
592 const auto dx_ptr = reinterpret_cast<const float *>(dx.ptr());
593 const auto dy_ptr = reinterpret_cast<const float *>(dy.ptr());
594
595 const int in_yi = std::floor((id.y() + 0.5f) * hr - 0.5f);
596 const int offset_row = in_yi * in_stide_in_bytes;
597
598 int16x8x2_t tmp =
599 {
600 {
601 vdupq_n_s16(0),
602 vdupq_n_s16(0)
603 }
604 };
605
606 tmp.val[0] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[0] + offset_row), in_stride, dx_ptr[0], dy_ptr[0]), tmp.val[0], 0);
607 tmp.val[0] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[2] + offset_row), in_stride, dx_ptr[2], dy_ptr[2]), tmp.val[0], 1);
608 tmp.val[0] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[4] + offset_row), in_stride, dx_ptr[4], dy_ptr[4]), tmp.val[0], 2);
609 tmp.val[0] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[6] + offset_row), in_stride, dx_ptr[6], dy_ptr[6]), tmp.val[0], 3);
610 tmp.val[0] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[8] + offset_row), in_stride, dx_ptr[8], dy_ptr[8]), tmp.val[0], 4);
611 tmp.val[0] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[10] + offset_row), in_stride, dx_ptr[10], dy_ptr[10]), tmp.val[0], 5);
612 tmp.val[0] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[12] + offset_row), in_stride, dx_ptr[12], dy_ptr[12]), tmp.val[0], 6);
613 tmp.val[0] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[14] + offset_row), in_stride, dx_ptr[14], dy_ptr[14]), tmp.val[0], 7);
614
615 tmp.val[1] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[1] + offset_row), in_stride, dx_ptr[1], dy_ptr[1]), tmp.val[1], 0);
616 tmp.val[1] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[3] + offset_row), in_stride, dx_ptr[3], dy_ptr[3]), tmp.val[1], 1);
617 tmp.val[1] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[5] + offset_row), in_stride, dx_ptr[5], dy_ptr[5]), tmp.val[1], 2);
618 tmp.val[1] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[7] + offset_row), in_stride, dx_ptr[7], dy_ptr[7]), tmp.val[1], 3);
619 tmp.val[1] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[9] + offset_row), in_stride, dx_ptr[9], dy_ptr[9]), tmp.val[1], 4);
620 tmp.val[1] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[11] + offset_row), in_stride, dx_ptr[11], dy_ptr[11]), tmp.val[1], 5);
621 tmp.val[1] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[13] + offset_row), in_stride, dx_ptr[13], dy_ptr[13]), tmp.val[1], 6);
622 tmp.val[1] = vsetq_lane_s16(delta_bilinear_c1(reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[15] + offset_row), in_stride, dx_ptr[15], dy_ptr[15]), tmp.val[1], 7);
623
624 vst2q_s16(reinterpret_cast<int16_t *>(out.ptr()), tmp);
625 },
626 in, offsets, dx, dy, out);
627 break;
628 }
629 case DataType::F32:
630 {
631 execute_window_loop(window, [&](const Coordinates & id)
632 {
633 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets.ptr());
634 const auto dx_ptr = reinterpret_cast<const float *>(dx.ptr());
635 const auto dy_ptr = reinterpret_cast<const float *>(dy.ptr());
636
637 const int in_yi = std::floor((id.y() + 0.5f) * hr - 0.5f);
638 const int offset_row = in_yi * in_stide_in_bytes;
639
640 float32x4x4_t tmp =
641 {
642 {
643 vdupq_n_f32(0),
644 vdupq_n_f32(0),
645 vdupq_n_f32(0),
646 vdupq_n_f32(0)
647 }
648 };
649
650 tmp.val[0] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[0] + offset_row), in_stride, dx_ptr[0], dy_ptr[0]), tmp.val[0], 0);
651 tmp.val[0] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[4] + offset_row), in_stride, dx_ptr[4], dy_ptr[4]), tmp.val[0], 1);
652 tmp.val[0] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[8] + offset_row), in_stride, dx_ptr[8], dy_ptr[8]), tmp.val[0], 2);
653 tmp.val[0] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[12] + offset_row), in_stride, dx_ptr[12], dy_ptr[12]), tmp.val[0], 3);
654
655 tmp.val[1] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[1] + offset_row), in_stride, dx_ptr[1], dy_ptr[1]), tmp.val[1], 0);
656 tmp.val[1] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[5] + offset_row), in_stride, dx_ptr[5], dy_ptr[5]), tmp.val[1], 1);
657 tmp.val[1] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[9] + offset_row), in_stride, dx_ptr[9], dy_ptr[9]), tmp.val[1], 2);
658 tmp.val[1] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[13] + offset_row), in_stride, dx_ptr[13], dy_ptr[13]), tmp.val[1], 3);
659
660 tmp.val[2] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[2] + offset_row), in_stride, dx_ptr[2], dy_ptr[2]), tmp.val[2], 0);
661 tmp.val[2] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[6] + offset_row), in_stride, dx_ptr[6], dy_ptr[6]), tmp.val[2], 1);
662 tmp.val[2] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[10] + offset_row), in_stride, dx_ptr[10], dy_ptr[10]), tmp.val[2], 2);
663 tmp.val[2] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[14] + offset_row), in_stride, dx_ptr[14], dy_ptr[14]), tmp.val[2], 3);
664
665 tmp.val[3] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[3] + offset_row), in_stride, dx_ptr[3], dy_ptr[3]), tmp.val[3], 0);
666 tmp.val[3] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[7] + offset_row), in_stride, dx_ptr[7], dy_ptr[7]), tmp.val[3], 1);
667 tmp.val[3] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[11] + offset_row), in_stride, dx_ptr[11], dy_ptr[11]), tmp.val[3], 2);
668 tmp.val[3] = vsetq_lane_f32(delta_bilinear_c1(reinterpret_cast<const float *>(in.ptr() + offsets_ptr[15] + offset_row), in_stride, dx_ptr[15], dy_ptr[15]), tmp.val[3], 3);
669
670 vst4q_f32(reinterpret_cast<float *>(out.ptr()), tmp);
671 },
672 in, offsets, dx, dy, out);
673 break;
674 }
675 default:
676 ARM_COMPUTE_ERROR("Not supported");
677 break;
678 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100679}
680
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100681void NEScaleKernel::scale_area_nchw(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100682{
683 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(_input, 1, DataType::U8);
684
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100685 // Don't increment in width/height/channels for the input tensor
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100686 // A pointer to the start of this plane is needed as base for the precomputed offsets
687 Window win_in(window);
688 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
689 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100690 win_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100691
692 Iterator in(_input, win_in);
693 Iterator out(_output, window);
694
695 const auto wr = static_cast<float>(_input->info()->dimension(0)) / static_cast<float>(_output->info()->dimension(0));
696 const auto hr = static_cast<float>(_input->info()->dimension(1)) / static_cast<float>(_output->info()->dimension(1));
697 const auto w = _input->info()->dimension(0);
698 const auto h = _input->info()->dimension(1);
699 const size_t in_stride = _input->info()->strides_in_bytes()[1];
700
701 execute_window_loop(window, [&](const Coordinates & id)
702 {
703 const auto in_ptr = reinterpret_cast<const uint8_t *>(in.ptr());
704
705 uint8x8_t tmp0 = vdup_n_u8(0);
706 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x(), id.y()), tmp0, 0);
707 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 1, id.y()), tmp0, 1);
708 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 2, id.y()), tmp0, 2);
709 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 3, id.y()), tmp0, 3);
710 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 4, id.y()), tmp0, 4);
711 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 5, id.y()), tmp0, 5);
712 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 6, id.y()), tmp0, 6);
713 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 7, id.y()), tmp0, 7);
714
715 uint8x8_t tmp1 = vdup_n_u8(0);
716 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 8, id.y()), tmp1, 0);
717 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 9, id.y()), tmp1, 1);
718 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 10, id.y()), tmp1, 2);
719 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 11, id.y()), tmp1, 3);
720 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 12, id.y()), tmp1, 4);
721 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 13, id.y()), tmp1, 5);
722 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 14, id.y()), tmp1, 6);
723 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 15, id.y()), tmp1, 7);
724
725 vst1q_u8(out.ptr(), vcombine_u8(tmp0, tmp1));
726 },
727 in, out);
728}
729
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100730void NEScaleKernel::scale_nhwc(const Window &window)
731{
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100732 // Get data layout and width/height indices
733 const DataLayout data_layout = _input->info()->data_layout();
734 const int idx_channels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
735 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
736 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
737
738 const size_t input_stride_w = _input->info()->strides_in_bytes()[idx_width];
739 const size_t input_stride_h = _input->info()->strides_in_bytes()[idx_height];
740 const size_t input_stride_c = _input->info()->strides_in_bytes()[idx_channels];
741
742 // Compute the ratio between source height and destination height
743 const auto hr = static_cast<float>(_input->info()->dimension(idx_height)) / static_cast<float>(_output->info()->dimension(idx_height));
744
745 // Don't increment in width/height/channels for the input tensor
746 // A pointer to the start of this plane is needed as base for the precomputed offsets
747 Window win_in(window);
748 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
749 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
750 win_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
751
752 switch(_input->info()->data_type())
753 {
754 case DataType::U8:
755 {
756 if(_policy == InterpolationPolicy::NEAREST_NEIGHBOR)
757 {
758 scale_nearest_nhwc_core<uint8_t>(_input, _offsets, _output, hr, window, win_in, input_stride_w, input_stride_h, input_stride_c);
759 }
760 else
761 {
762 scale_bilinear_nhwc_core<uint8_t>(_input, _offsets, _dx, _dy, _output, hr,
763 window, win_in, input_stride_w, input_stride_h, input_stride_c, _border_mode);
764 }
765 break;
766 }
767 case DataType::S16:
768 {
769 if(_policy == InterpolationPolicy::NEAREST_NEIGHBOR)
770 {
771 scale_nearest_nhwc_core<int16_t>(_input, _offsets, _output, hr, window, win_in, input_stride_w, input_stride_h, input_stride_c);
772 }
773 else
774 {
775 scale_bilinear_nhwc_core<int16_t>(_input, _offsets, _dx, _dy, _output, hr,
776 window, win_in, input_stride_w, input_stride_h, input_stride_c, _border_mode);
777 }
778 break;
779 }
780 case DataType::F32:
781 {
782 if(_policy == InterpolationPolicy::NEAREST_NEIGHBOR)
783 {
784 scale_nearest_nhwc_core<float>(_input, _offsets, _output, hr, window, win_in, input_stride_w, input_stride_h, input_stride_c);
785 }
786 else
787 {
788 scale_bilinear_nhwc_core<float>(_input, _offsets, _dx, _dy, _output, hr,
789 window, win_in, input_stride_w, input_stride_h, input_stride_c, _border_mode);
790 }
791 break;
792 }
793 default:
794 ARM_COMPUTE_ERROR("Not supported");
795 break;
796 }
797}
798
Georgios Pinitas20b43132018-05-14 16:05:23 +0100799Status NEScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *dx, const ITensorInfo *dy,
800 const ITensorInfo *offsets, ITensorInfo *output, InterpolationPolicy policy,
801 BorderMode border_mode, SamplingPolicy sampling_policy)
802{
803 BorderSize border_size(1);
804 if(input->data_layout() == DataLayout::NHWC)
805 {
806 border_size = (border_mode == BorderMode::CONSTANT && policy == InterpolationPolicy::BILINEAR) ? BorderSize(1, 0, 0, 0) : BorderSize(0);
807 }
808
809 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, dx, dy, offsets, output, policy, border_mode, sampling_policy));
810 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(),
811 dx != nullptr ? dx->clone().get() : nullptr,
812 dy != nullptr ? dy->clone().get() : nullptr,
813 offsets != nullptr ? offsets->clone().get() : nullptr,
814 output->clone().get(),
815 policy, border_mode == BorderMode::UNDEFINED, sampling_policy, border_size)
816 .first);
817
818 return Status{};
819}
820
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100821void NEScaleKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100822{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100823 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100824 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
825 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
826 ARM_COMPUTE_ERROR_ON(_func == nullptr);
827
828 (this->*_func)(window);
829}
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100830} // namespace arm_compute