blob: 66408ab94c0d53149485740eca07036ef85d73cf [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitasa5eb5912020-01-07 17:37:14 +00002 * Copyright (c) 2016-2020 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"
Georgios Pinitasc47ef202018-11-16 18:19:43 +000027#include "arm_compute/core/CPP/Validate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Coordinates.h"
29#include "arm_compute/core/Error.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/ITensor.h"
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010032#include "arm_compute/core/NEON/wrapper/wrapper.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/core/TensorInfo.h"
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +000034#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include "arm_compute/core/Validate.h"
36#include "arm_compute/core/Window.h"
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010037#include "arm_compute/core/utils/misc/Utility.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
39#include <arm_neon.h>
40#include <cstddef>
41#include <cstdint>
42
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010043namespace arm_compute
44{
45namespace
46{
Georgios Pinitas20b43132018-05-14 16:05:23 +010047Status validate_arguments(const ITensorInfo *input, const ITensorInfo *dx, const ITensorInfo *dy,
48 const ITensorInfo *offsets, ITensorInfo *output, InterpolationPolicy policy,
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +000049 BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy, bool use_padding, bool align_corners)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010050{
Georgios Pinitasc47ef202018-11-16 18:19:43 +000051 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +000052 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::F16, DataType::F32, DataType::QASYMM8);
Georgios Pinitas20b43132018-05-14 16:05:23 +010053 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
55 ARM_COMPUTE_RETURN_ERROR_ON(output == input);
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +000056 ARM_COMPUTE_RETURN_ERROR_ON(sampling_policy != SamplingPolicy::CENTER && sampling_policy != SamplingPolicy::TOP_LEFT);
George Wort05398a92019-01-25 15:38:33 +000057 ARM_COMPUTE_RETURN_ERROR_ON(!use_padding && border_mode != BorderMode::CONSTANT);
58 ARM_COMPUTE_UNUSED(constant_border_value);
Georgios Pinitas20b43132018-05-14 16:05:23 +010059
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +000060 const DataLayout data_layout = input->data_layout();
61 const auto width_index = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
62 const auto height_index = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
63 const auto output_width = output->dimension(width_index);
64 const auto output_height = output->dimension(height_index);
65 ARM_COMPUTE_RETURN_ERROR_ON(output_width == 0);
66 ARM_COMPUTE_RETURN_ERROR_ON(output_height == 0);
Georgios Pinitas20b43132018-05-14 16:05:23 +010067
68 if(policy == InterpolationPolicy::NEAREST_NEIGHBOR)
69 {
70 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32);
71 }
72
73 if(policy == InterpolationPolicy::BILINEAR)
74 {
75 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32);
76 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dx, 1, DataType::F32);
77 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dy, 1, DataType::F32);
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +000078
79 if(align_corners)
80 {
81 // For bilinear method with aligned corners, the resize ratio will
82 // be calculated by (input_size - 1)/(output_size - 1). Belows are
83 // checking possible overflows.
84 const auto input_width = input->dimension(width_index);
85 const auto input_height = input->dimension(height_index);
86
87 ARM_COMPUTE_RETURN_ERROR_ON(input_width == 0 || input_height == 0);
88 ARM_COMPUTE_RETURN_ERROR_ON((output_width - 1 == 0) || (output_height - 1 == 0));
89 }
Georgios Pinitas20b43132018-05-14 16:05:23 +010090 }
91
92 if(policy == InterpolationPolicy::AREA)
93 {
94 ARM_COMPUTE_RETURN_ERROR_ON(data_layout != DataLayout::NCHW);
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +000095 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
Georgios Pinitas20b43132018-05-14 16:05:23 +010096 }
97
98 return Status{};
99}
100
101std::pair<Status, Window> validate_and_configure_window_nchw(ITensorInfo *input, ITensorInfo *dx, ITensorInfo *dy, ITensorInfo *offsets, ITensorInfo *output,
102 InterpolationPolicy policy, bool border_undefined, SamplingPolicy sampling_policy, BorderSize border_size)
103{
104 bool window_changed{ false };
105 Window win{};
106
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100107 constexpr unsigned int num_elems_processed_per_iteration = 16;
108
109 // Configure kernel window
Georgios Pinitas20b43132018-05-14 16:05:23 +0100110 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100111
Georgios Pinitas20b43132018-05-14 16:05:23 +0100112 const ValidRegion &input_valid_region = input->valid_region();
113
114 if(offsets != nullptr)
115 {
116 AccessWindowHorizontal offsets_access(offsets, 0, num_elems_processed_per_iteration);
117 window_changed = window_changed || update_window_and_padding(win, offsets_access);
118 }
119 if(dx != nullptr && dy != nullptr)
120 {
121 AccessWindowHorizontal dx_access(dx, 0, num_elems_processed_per_iteration);
122 AccessWindowHorizontal dy_access(dy, 0, num_elems_processed_per_iteration);
123 window_changed = window_changed || update_window_and_padding(win, dx_access, dy_access);
124 }
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100125
126 // Reads can occur within the valid region of the input
Georgios Pinitas20b43132018-05-14 16:05:23 +0100127 AccessWindowStatic input_access(input, input_valid_region.anchor[0] - border_size.left,
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100128 input_valid_region.anchor[1] - border_size.top,
129 input_valid_region.anchor[0] + input_valid_region.shape[0] + border_size.right,
130 input_valid_region.anchor[1] + input_valid_region.shape[1] + border_size.bottom);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100131 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
132 window_changed = window_changed || update_window_and_padding(win, input_access, output_access);
133 output_access.set_valid_region(win, calculate_valid_region_scale(*input, output->tensor_shape(),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100134 policy, sampling_policy, border_undefined));
135
Georgios Pinitas20b43132018-05-14 16:05:23 +0100136 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
137 return std::make_pair(err, win);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100138}
Georgios Pinitas20b43132018-05-14 16:05:23 +0100139
140std::pair<Status, Window> validate_and_configure_window_nhwc(ITensorInfo *input, ITensorInfo *output,
141 InterpolationPolicy policy, bool border_undefined,
George Wort05398a92019-01-25 15:38:33 +0000142 SamplingPolicy sampling_policy, BorderSize border_size, bool use_padding)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100143{
Georgios Pinitas20b43132018-05-14 16:05:23 +0100144 bool window_changed{ false };
145 Window win{};
146
George Wort05398a92019-01-25 15:38:33 +0000147 const unsigned int num_elems_processed_per_iteration = (use_padding && policy == InterpolationPolicy::NEAREST_NEIGHBOR) ? 16 / input->element_size() : 1;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100148
149 // Configure kernel window
Georgios Pinitas20b43132018-05-14 16:05:23 +0100150 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100151
George Wort05398a92019-01-25 15:38:33 +0000152 if(use_padding)
153 {
Georgios Pinitas32620422019-06-27 17:14:32 +0100154 AccessWindowStatic input_access(input, 0, -border_size.top, ceil_to_multiple(input->tensor_shape()[0], num_elems_processed_per_iteration), input->tensor_shape()[1]);
George Wort05398a92019-01-25 15:38:33 +0000155 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
156 window_changed = update_window_and_padding(win, input_access, output_access);
157 output->set_valid_region(calculate_valid_region_scale(*input, output->tensor_shape(), policy, sampling_policy, border_undefined));
158 }
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100159
Georgios Pinitas20b43132018-05-14 16:05:23 +0100160 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
161 return std::make_pair(err, win);
162}
163
164std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *dx, ITensorInfo *dy, ITensorInfo *offsets, ITensorInfo *output,
George Wort05398a92019-01-25 15:38:33 +0000165 InterpolationPolicy policy, bool border_undefined, SamplingPolicy sampling_policy, BorderSize border_size, bool use_padding)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100166{
167 std::pair<Status, Window> win_config;
168 switch(input->data_layout())
169 {
170 case DataLayout::NCHW:
George Wort05398a92019-01-25 15:38:33 +0000171 if(!use_padding)
172 {
173 return std::make_pair(ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Padding required for NCHW"), Window{});
174 }
Georgios Pinitas20b43132018-05-14 16:05:23 +0100175 win_config = validate_and_configure_window_nchw(input, dx, dy, offsets, output, policy, border_undefined, sampling_policy, border_size);
176 break;
177 case DataLayout::NHWC:
George Wort05398a92019-01-25 15:38:33 +0000178 win_config = validate_and_configure_window_nhwc(input, output, policy, border_undefined, sampling_policy, border_size, use_padding);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100179 break;
180 default:
181 win_config = std::make_pair(ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported data layout!"), Window{});
182 }
183
184 return win_config;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100185}
186
187template <typename T>
188inline void scale_nearest_nhwc_core(const ITensor *input, const ITensor *offsets, ITensor *output,
Michalis Spyroud4733862019-07-09 14:21:06 +0100189 float hr, Window window, const Window &win_in, size_t stride_w, size_t stride_h, size_t stride_c, float sampling_offset)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100190{
George Wort05398a92019-01-25 15:38:33 +0000191 const int window_step_x = 16 / sizeof(T);
192 const auto window_start_x = static_cast<int32_t>(window.x().start());
193 const auto window_end_x = static_cast<int32_t>(window.x().end());
194
195 window.set(Window::DimX, Window::Dimension(0, 1, 1));
196
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100197 Iterator in(input, win_in);
198 Iterator out(output, window);
199
200 const size_t offsets_stride = stride_w / sizeof(T);
201
202 execute_window_loop(window, [&](const Coordinates & id)
203 {
George Wort05398a92019-01-25 15:38:33 +0000204 const int32_t offset = *reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id.y(), id.z())));
Michalis Spyroud4733862019-07-09 14:21:06 +0100205 const int in_yi = std::floor((id.z() + sampling_offset) * hr);
George Wort05398a92019-01-25 15:38:33 +0000206 const int offset_row = in_yi * stride_h;
207 int32_t x = window_start_x;
208 for(; x < window_end_x - window_step_x; x += window_step_x)
209 {
210 wrapper::vstore(reinterpret_cast<T *>(out.ptr()) + x,
211 wrapper::vloadq(reinterpret_cast<const T *>(in.ptr() + offset * offsets_stride + offset_row + x * stride_c)));
212 }
213 for(; x < window_end_x; ++x)
214 {
215 *(reinterpret_cast<T *>(out.ptr()) + x) =
216 *(reinterpret_cast<const T *>(in.ptr() + offset * offsets_stride + offset_row + x * stride_c));
217 }
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100218 },
219 in, out);
220}
221
George Wort05398a92019-01-25 15:38:33 +0000222template <typename T, typename ConstType>
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100223inline void scale_bilinear_nhwc_core(const ITensor *input, const ITensor *offsets, const ITensor *dx, const ITensor *dy, ITensor *output,
George Wort05398a92019-01-25 15:38:33 +0000224 float hr, float sampling_offset, Window window, const Window &win_in, size_t stride_w, size_t stride_h,
225 size_t stride_c, BorderMode border_mode, PixelValue constant_border_value, bool use_padding)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100226{
227 Iterator in(input, win_in);
228 Iterator out(output, window);
229
230 const size_t stride_w_elems = stride_w / sizeof(T);
231 const size_t stride_h_elems = stride_h / sizeof(T);
232
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100233 const int input_width = input->info()->dimension(1);
234 const int input_height = input->info()->dimension(2);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100235
George Wort05398a92019-01-25 15:38:33 +0000236 T border_value;
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100237 if(use_padding && border_mode != BorderMode::REPLICATE)
George Wort05398a92019-01-25 15:38:33 +0000238 {
Pablo Tello10971472019-05-15 15:14:46 +0100239 // configure() sets top border to 0 for BorderMode::REPLICATE and border_value is not needed in execute_window_loop() for REPLICATE
George Wort05398a92019-01-25 15:38:33 +0000240 border_value = *reinterpret_cast<T *>(input->buffer() + input->info()->offset_first_element_in_bytes() - stride_w);
241 }
242 else
243 {
244 border_value = static_cast<T>(constant_border_value.get<ConstType>());
245 }
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100246
Michalis Spyroufae513c2019-10-16 17:41:33 +0100247 auto is_valid = [](int64_t x, int64_t low_x, int64_t high_x, int64_t y, int64_t low_y, int64_t high_y)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100248 {
249 return !(x < low_x || x > high_x || y < low_y || y > high_y);
250 };
251
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100252 int border_size = (border_mode == BorderMode::UNDEFINED) ? 0 : 1;
253
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100254 const bool is_quantized = (input->info()->data_type() == DataType::QASYMM8);
255 const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform();
256 const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform();
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000257
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100258 execute_window_loop(window, [&](const Coordinates & id)
259 {
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100260 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 +0100261 const auto dx_scale = *reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id.y(), id.z())));
262 const auto dy_scale = *reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id.y(), id.z())));
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000263 const int in_yi = std::floor((id.z() + sampling_offset) * hr - sampling_offset);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100264 const int offset_row = in_yi * stride_h + id.x() * stride_c;
265 const T *in_ptr = reinterpret_cast<T *>(in.ptr() + offset * stride_w + offset_row);
266
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100267 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 +0100268 {
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100269 T a00 = 0;
270 T a01 = 0;
271 T a10 = 0;
272 T a11 = 0;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100273
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100274 if(border_mode == BorderMode::CONSTANT)
275 {
George Wort05398a92019-01-25 15:38:33 +0000276 a00 = is_valid(offset, 0, input_width - 1, in_yi, 0, input_height - 1) ? *in_ptr : border_value;
277 a01 = is_valid(offset + 1, 0, input_width - 1, in_yi, 0, input_height - 1) ? *(in_ptr + stride_w_elems) : border_value;
278 a10 = is_valid(offset, 0, input_width - 1, in_yi + 1, 0, input_height - 1) ? *(in_ptr + stride_h_elems) : border_value;
279 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_value;
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100280 }
281 else if(border_mode == BorderMode::REPLICATE)
282 {
283 auto clamped_x = utility::clamp<int>(offset, 0, input_width - 1);
284 auto clamped_x1 = utility::clamp<int>(offset + 1, 0, input_width - 1);
285 auto clamped_y = utility::clamp<int>(in_yi, 0, input_height - 1);
286 auto clamped_y1 = utility::clamp<int>(in_yi + 1, 0, input_height - 1);
287
288 a00 = *reinterpret_cast<T *>(in.ptr() + clamped_x * stride_w + clamped_y * stride_h + id.x() * stride_c);
289 a01 = *reinterpret_cast<T *>(in.ptr() + clamped_x1 * stride_w + clamped_y * stride_h + id.x() * stride_c);
290 a10 = *reinterpret_cast<T *>(in.ptr() + clamped_x * stride_w + clamped_y1 * stride_h + id.x() * stride_c);
291 a11 = *reinterpret_cast<T *>(in.ptr() + clamped_x1 * stride_w + clamped_y1 * stride_h + id.x() * stride_c);
292 }
293 else
294 {
295 a00 = is_valid(offset, 0, input_width - 1, in_yi, 0, input_height - 1) ? *in_ptr : 0;
296 a01 = is_valid(offset + 1, 0, input_width - 1, in_yi, 0, input_height - 1) ? *(in_ptr + stride_w_elems) : 0;
297 a10 = is_valid(offset, 0, input_width - 1, in_yi + 1, 0, input_height - 1) ? *(in_ptr + stride_h_elems) : 0;
298 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;
299 }
300
301 // Perform interpolation
302 const float dx1 = 1.0f - dx_scale;
303 const float dy1 = 1.0f - dy_scale;
304
305 const float w1 = dx1 * dy1;
306 const float w2 = dx_scale * dy1;
307 const float w3 = dx1 * dy_scale;
308 const float w4 = dx_scale * dy_scale;
309
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000310 T res = 0;
311 //dequantize quantized input
312 if(is_quantized)
313 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100314 float inp00 = dequantize_qasymm8(a00, iq_info);
315 float inp01 = dequantize_qasymm8(a01, iq_info);
316 float inp10 = dequantize_qasymm8(a10, iq_info);
317 float inp11 = dequantize_qasymm8(a11, iq_info);
318 res = static_cast<T>(quantize_qasymm8((inp00 * w1 + inp01 * w2 + inp10 * w3 + inp11 * w4), oq_info));
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000319 }
320 else
321 {
322 res = static_cast<T>(a00 * w1 + a01 * w2 + a10 * w3 + a11 * w4);
323 }
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100324 // Store result
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000325 *reinterpret_cast<T *>(out.ptr()) = res;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100326 }
327 else
328 {
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100329 if(border_mode == BorderMode::CONSTANT)
330 {
George Wort05398a92019-01-25 15:38:33 +0000331 *reinterpret_cast<T *>(out.ptr()) = border_value;
Georgios Pinitasfa7ad562018-05-15 17:38:40 +0100332 }
333 else if(border_mode == BorderMode::REPLICATE)
334 {
335 auto clamped_x = utility::clamp<int>(offset, 0, input_width - 1);
336 auto clamped_y = utility::clamp<int>(in_yi, 0, input_height - 1);
337 *reinterpret_cast<T *>(out.ptr()) = *reinterpret_cast<T *>(in.ptr() + clamped_x * stride_w + clamped_y * stride_h + id.x() * stride_c);
338 }
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100339 }
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100340 },
341 in, out);
342}
343} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100344
345NEScaleKernel::NEScaleKernel()
Manuel Bottinif00d3322019-03-05 15:53:25 +0000346 : _func(nullptr), _offsets(nullptr), _dx(nullptr), _dy(nullptr), _input(nullptr), _output(nullptr), _policy(), _border_size(1), _border_mode(), _constant_border_value(PixelValue()),
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000347 _sampling_offset(0), _use_padding(true), _align_corners(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100348{
349}
350
351BorderSize NEScaleKernel::border_size() const
352{
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100353 return _border_size;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100354}
355
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100356void NEScaleKernel::configure(const ITensor *input, const ITensor *dx, const ITensor *dy, const ITensor *offsets,
George Wort05398a92019-01-25 15:38:33 +0000357 ITensor *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy,
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000358 bool use_padding, bool align_corners)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100359{
Georgios Pinitas20b43132018-05-14 16:05:23 +0100360 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100361 // Perform validation step
362 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(),
363 dx != nullptr ? dx->info() : nullptr,
364 dy != nullptr ? dy->info() : nullptr,
365 offsets != nullptr ? offsets->info() : nullptr,
366 output->info(),
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000367 policy, border_mode, constant_border_value, sampling_policy, use_padding, align_corners));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100368
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100369 // Get data layout and width/height indices
370 const DataLayout data_layout = input->info()->data_layout();
371 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
372 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100373
George Wort05398a92019-01-25 15:38:33 +0000374 _input = input;
375 _output = output;
376 _offsets = offsets;
377 _dx = dx;
378 _dy = dy;
379 _policy = policy;
380 _border_size = BorderSize(1);
381 _border_mode = border_mode;
382 _constant_border_value = constant_border_value;
383 _use_padding = use_padding;
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000384 _align_corners = _policy == InterpolationPolicy::BILINEAR
385 && sampling_policy == SamplingPolicy::TOP_LEFT
386 && align_corners;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100387
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000388 if(sampling_policy == SamplingPolicy::CENTER)
389 {
390 _sampling_offset = 0.5f;
391 }
392
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100393 // Compute the ratio between source width/height and destination width/height
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000394 const auto wr = arm_compute::calculate_resize_ratio(input->info()->dimension(idx_width), output->info()->dimension(idx_width), _align_corners);
395 const auto hr = arm_compute::calculate_resize_ratio(input->info()->dimension(idx_height), output->info()->dimension(idx_height), _align_corners);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100396
397 // Add constant border only on top in case of NHWC layout
398 if(data_layout == DataLayout::NHWC)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100399 {
Michalis Spyrouc050e0c2019-07-25 17:58:30 +0100400 _border_size = (border_mode != BorderMode::REPLICATE && policy == InterpolationPolicy::BILINEAR && use_padding) ? BorderSize(1, 0, 0, 0) : BorderSize(0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100401 }
402
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100403 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100404 if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
405 {
406 policy = InterpolationPolicy::NEAREST_NEIGHBOR;
407 }
408
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100409 // Select interpolation function
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100410 switch(policy)
411 {
412 case InterpolationPolicy::NEAREST_NEIGHBOR:
413 {
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100414 _func = (data_layout == DataLayout::NCHW) ? &NEScaleKernel::scale_nearest_nchw : &NEScaleKernel::scale_nhwc;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100415 break;
416 }
417 case InterpolationPolicy::BILINEAR:
418 {
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100419 _func = (data_layout == DataLayout::NCHW) ? &NEScaleKernel::scale_bilinear_nchw : &NEScaleKernel::scale_nhwc;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100420 break;
421 }
422 case InterpolationPolicy::AREA:
423 {
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100424 _func = &NEScaleKernel::scale_area_nchw;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100425 break;
426 }
427 default:
428 ARM_COMPUTE_ERROR("Unsupported interpolation mode");
429 }
430
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100431 // Configure window
Georgios Pinitas20b43132018-05-14 16:05:23 +0100432 std::pair<Status, Window> win_config = validate_and_configure_window(input->info(),
433 dx != nullptr ? dx->info() : nullptr,
434 dy != nullptr ? dy->info() : nullptr,
435 offsets != nullptr ? offsets->info() : nullptr,
436 output->info(),
George Wort05398a92019-01-25 15:38:33 +0000437 policy, border_mode == BorderMode::UNDEFINED, sampling_policy, border_size(), use_padding);
438
Georgios Pinitas20b43132018-05-14 16:05:23 +0100439 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
440 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100441}
442
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100443void NEScaleKernel::scale_nearest_nchw(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100444{
445 const size_t input_stride = _input->info()->strides_in_bytes()[1];
446
447 // Compute the ratio between source height and destination height
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000448 const auto hr = arm_compute::calculate_resize_ratio(_input->info()->dimension(1), _output->info()->dimension(1), _align_corners);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100449
450 // Don't increment in X and Y direction for the input tensor
451 // A pointer to the start of this plane is needed as base for the precomputed offsets
452 Window win_in(window);
453 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
454 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
455
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100456 // Set offsets window
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100457 Window win_off;
458 win_off.set(Window::DimX, window[Window::DimX]);
459 win_off.set(Window::DimY, window[Window::DimY]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100460 for(size_t d = Window::DimZ; d < _offsets->info()->num_dimensions(); ++d)
461 {
462 win_off.set(d, Window::Dimension(0, 0, 0));
463 }
464
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100465 // Create iterators
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100466 Iterator in(_input, win_in);
467 Iterator out(_output, window);
468 Iterator offsets(_offsets, win_off);
469
470 switch(_input->info()->data_type())
471 {
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000472 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100473 case DataType::U8:
474 {
475 uint8x16_t tmp = vdupq_n_u8(0);
476
477 execute_window_loop(window, [&](const Coordinates & id)
478 {
479 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets.ptr());
480 const uint8_t *const in_ptr = in.ptr();
481
Michalis Spyroud4733862019-07-09 14:21:06 +0100482 const int in_yi = std::floor((id.y() + _sampling_offset) * hr);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100483 const int in_yi_clamped = std::min(static_cast<int>(_input->info()->dimension(1)), std::max(in_yi, -1));
484 ARM_COMPUTE_ERROR_ON(in_yi_clamped < -1 || in_yi_clamped > static_cast<int>(_input->info()->dimension(1)));
485 const int offset_row = in_yi_clamped * input_stride;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100486
487 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[0] + offset_row], tmp, 0);
488 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[1] + offset_row], tmp, 1);
489 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[2] + offset_row], tmp, 2);
490 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[3] + offset_row], tmp, 3);
491 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[4] + offset_row], tmp, 4);
492 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[5] + offset_row], tmp, 5);
493 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[6] + offset_row], tmp, 6);
494 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[7] + offset_row], tmp, 7);
495 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[8] + offset_row], tmp, 8);
496 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[9] + offset_row], tmp, 9);
497 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[10] + offset_row], tmp, 10);
498 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[11] + offset_row], tmp, 11);
499 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[12] + offset_row], tmp, 12);
500 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[13] + offset_row], tmp, 13);
501 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[14] + offset_row], tmp, 14);
502 tmp = vsetq_lane_u8(in_ptr[offsets_ptr[15] + offset_row], tmp, 15);
503
504 vst1q_u8(out.ptr(), tmp);
505 },
506 in, offsets, out);
507 break;
508 }
509 case DataType::S16:
510 {
511 int16x8x2_t tmp =
512 {
513 {
514 vdupq_n_s16(0),
515 vdupq_n_s16(0)
516 }
517 };
518
519 execute_window_loop(window, [&](const Coordinates & id)
520 {
521 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets.ptr());
522
Michalis Spyroud4733862019-07-09 14:21:06 +0100523 const int in_yi = std::floor((id.y() + _sampling_offset) * hr);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100524 const int offset_row = in_yi * input_stride;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100525
526 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[0] + offset_row), tmp.val[0], 0);
527 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[2] + offset_row), tmp.val[0], 1);
528 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[4] + offset_row), tmp.val[0], 2);
529 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[6] + offset_row), tmp.val[0], 3);
530 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[8] + offset_row), tmp.val[0], 4);
531 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[10] + offset_row), tmp.val[0], 5);
532 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[12] + offset_row), tmp.val[0], 6);
533 tmp.val[0] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[14] + offset_row), tmp.val[0], 7);
534
535 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[1] + offset_row), tmp.val[1], 0);
536 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[3] + offset_row), tmp.val[1], 1);
537 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[5] + offset_row), tmp.val[1], 2);
538 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[7] + offset_row), tmp.val[1], 3);
539 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[9] + offset_row), tmp.val[1], 4);
540 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[11] + offset_row), tmp.val[1], 5);
541 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[13] + offset_row), tmp.val[1], 6);
542 tmp.val[1] = vsetq_lane_s16(*reinterpret_cast<const int16_t *>(in.ptr() + offsets_ptr[15] + offset_row), tmp.val[1], 7);
543
544 vst2q_s16(reinterpret_cast<int16_t *>(out.ptr()), tmp);
545 },
546 in, offsets, out);
547 break;
548 }
Georgios Pinitasc47ef202018-11-16 18:19:43 +0000549#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
550 case DataType::F16:
551 {
552 float16x8x2_t tmp =
553 {
554 {
555 vdupq_n_f16(0),
556 vdupq_n_f16(0)
557 }
558 };
559
560 execute_window_loop(window, [&](const Coordinates & id)
561 {
562 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets.ptr());
563
Michalis Spyroud4733862019-07-09 14:21:06 +0100564 const int in_yi = std::floor((id.y() + _sampling_offset) * hr);
Georgios Pinitasc47ef202018-11-16 18:19:43 +0000565 const int offset_row = in_yi * input_stride;
566
567 tmp.val[0] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[0] + offset_row), tmp.val[0], 0);
568 tmp.val[0] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[2] + offset_row), tmp.val[0], 1);
569 tmp.val[0] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[4] + offset_row), tmp.val[0], 2);
570 tmp.val[0] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[6] + offset_row), tmp.val[0], 3);
571 tmp.val[0] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[8] + offset_row), tmp.val[0], 4);
572 tmp.val[0] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[10] + offset_row), tmp.val[0], 5);
573 tmp.val[0] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[12] + offset_row), tmp.val[0], 6);
574 tmp.val[0] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[14] + offset_row), tmp.val[0], 7);
575
576 tmp.val[1] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[1] + offset_row), tmp.val[1], 0);
577 tmp.val[1] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[3] + offset_row), tmp.val[1], 1);
578 tmp.val[1] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[5] + offset_row), tmp.val[1], 2);
579 tmp.val[1] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[7] + offset_row), tmp.val[1], 3);
580 tmp.val[1] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[9] + offset_row), tmp.val[1], 4);
581 tmp.val[1] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[11] + offset_row), tmp.val[1], 5);
582 tmp.val[1] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[13] + offset_row), tmp.val[1], 6);
583 tmp.val[1] = vsetq_lane_f16(*reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[15] + offset_row), tmp.val[1], 7);
584
585 vst2q_f16(reinterpret_cast<__fp16 *>(out.ptr()), tmp);
586 },
587 in, offsets, out);
588 break;
589 }
590#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100591 case DataType::F32:
592 {
593 float32x4x4_t tmp =
594 {
595 {
596 vdupq_n_f32(0),
597 vdupq_n_f32(0),
598 vdupq_n_f32(0),
599 vdupq_n_f32(0)
600 }
601 };
602
603 execute_window_loop(window, [&](const Coordinates & id)
604 {
605 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets.ptr());
606
Michalis Spyroud4733862019-07-09 14:21:06 +0100607 const int in_yi = std::floor((id.y() + _sampling_offset) * hr);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100608 const int offset_row = in_yi * input_stride;
609
610 tmp.val[0] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[0] + offset_row), tmp.val[0], 0);
611 tmp.val[0] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[4] + offset_row), tmp.val[0], 1);
612 tmp.val[0] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[8] + offset_row), tmp.val[0], 2);
613 tmp.val[0] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[12] + offset_row), tmp.val[0], 3);
614
615 tmp.val[1] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[1] + offset_row), tmp.val[1], 0);
616 tmp.val[1] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[5] + offset_row), tmp.val[1], 1);
617 tmp.val[1] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[9] + offset_row), tmp.val[1], 2);
618 tmp.val[1] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[13] + offset_row), tmp.val[1], 3);
619
620 tmp.val[2] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[2] + offset_row), tmp.val[2], 0);
621 tmp.val[2] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[6] + offset_row), tmp.val[2], 1);
622 tmp.val[2] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[10] + offset_row), tmp.val[2], 2);
623 tmp.val[2] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[14] + offset_row), tmp.val[2], 3);
624
625 tmp.val[3] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[3] + offset_row), tmp.val[3], 0);
626 tmp.val[3] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[7] + offset_row), tmp.val[3], 1);
627 tmp.val[3] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[11] + offset_row), tmp.val[3], 2);
628 tmp.val[3] = vsetq_lane_f32(*reinterpret_cast<const float *>(in.ptr() + offsets_ptr[15] + offset_row), tmp.val[3], 3);
629
630 vst4q_f32(reinterpret_cast<float *>(out.ptr()), tmp);
631 },
632 in, offsets, out);
633 break;
634 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100635 default:
636 ARM_COMPUTE_ERROR("Not supported");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100637 }
638}
639
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100640void NEScaleKernel::scale_bilinear_nchw(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100641{
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000642 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(_input, 1, DataType::U8, DataType::QASYMM8, DataType::S16, DataType::F16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100643
644 // Compute the ratio between source height and destination height
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000645 const auto hr = arm_compute::calculate_resize_ratio(_input->info()->dimension(1), _output->info()->dimension(1), _align_corners);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100646
647 // Don't increment in X and Y direction for the input tensor
648 // A pointer to the start of this plane is needed as base for the precomputed offsets
649 Window win_in(window);
650 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
651 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
652
653 Window win_off;
654 win_off.set(Window::DimX, window.x());
655 win_off.set(Window::DimY, window.y());
656
657 for(size_t d = Window::DimZ; d < _offsets->info()->num_dimensions(); ++d)
658 {
659 win_off.set(d, Window::Dimension(0, 0, 0));
660 }
661
662 Iterator in(_input, win_in);
663 Iterator out(_output, window);
664 Iterator offsets(_offsets, win_off);
665 Iterator dx(_dx, win_off);
666 Iterator dy(_dy, win_off);
667
668 /* Input image stride */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100669 const size_t in_stide_in_bytes = _input->info()->strides_in_bytes()[1];
670 const size_t in_stride = in_stide_in_bytes / _input->info()->element_size();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100671
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100672 const bool is_quantized = (_input->info()->data_type() == DataType::QASYMM8);
673 const UniformQuantizationInfo iq_info = _input->info()->quantization_info().uniform();
674 const UniformQuantizationInfo oq_info = _output->info()->quantization_info().uniform();
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000675
Georgios Pinitas583137c2017-08-31 18:12:42 +0100676 switch(_input->info()->data_type())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100677 {
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000678 case DataType::QASYMM8:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100679 case DataType::U8:
680 {
681 execute_window_loop(window, [&](const Coordinates & id)
682 {
683 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets.ptr());
684 const auto dx_ptr = reinterpret_cast<const float *>(dx.ptr());
685 const auto dy_ptr = reinterpret_cast<const float *>(dy.ptr());
686 const auto in_ptr = reinterpret_cast<const uint8_t *>(in.ptr());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100687
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000688 const int in_yi = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100689 const int offset_row = in_yi * in_stide_in_bytes;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100690
Georgios Pinitas583137c2017-08-31 18:12:42 +0100691 uint8x8_t tmp0 = vdup_n_u8(0);
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000692 if(is_quantized)
693 {
694 tmp0 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[0] + offset_row], in_stride, dx_ptr[0], dy_ptr[0], iq_info, oq_info), tmp0, 0);
695 tmp0 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[1] + offset_row], in_stride, dx_ptr[1], dy_ptr[1], iq_info, oq_info), tmp0, 1);
696 tmp0 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[2] + offset_row], in_stride, dx_ptr[2], dy_ptr[2], iq_info, oq_info), tmp0, 2);
697 tmp0 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[3] + offset_row], in_stride, dx_ptr[3], dy_ptr[3], iq_info, oq_info), tmp0, 3);
698 tmp0 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[4] + offset_row], in_stride, dx_ptr[4], dy_ptr[4], iq_info, oq_info), tmp0, 4);
699 tmp0 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[5] + offset_row], in_stride, dx_ptr[5], dy_ptr[5], iq_info, oq_info), tmp0, 5);
700 tmp0 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[6] + offset_row], in_stride, dx_ptr[6], dy_ptr[6], iq_info, oq_info), tmp0, 6);
701 tmp0 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[7] + offset_row], in_stride, dx_ptr[7], dy_ptr[7], iq_info, oq_info), tmp0, 7);
702 }
703 else
704 {
705 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[0] + offset_row], in_stride, dx_ptr[0], dy_ptr[0]), tmp0, 0);
706 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[1] + offset_row], in_stride, dx_ptr[1], dy_ptr[1]), tmp0, 1);
707 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[2] + offset_row], in_stride, dx_ptr[2], dy_ptr[2]), tmp0, 2);
708 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[3] + offset_row], in_stride, dx_ptr[3], dy_ptr[3]), tmp0, 3);
709 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[4] + offset_row], in_stride, dx_ptr[4], dy_ptr[4]), tmp0, 4);
710 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[5] + offset_row], in_stride, dx_ptr[5], dy_ptr[5]), tmp0, 5);
711 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[6] + offset_row], in_stride, dx_ptr[6], dy_ptr[6]), tmp0, 6);
712 tmp0 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[7] + offset_row], in_stride, dx_ptr[7], dy_ptr[7]), tmp0, 7);
713 }
Georgios Pinitas583137c2017-08-31 18:12:42 +0100714 uint8x8_t tmp1 = vdup_n_u8(0);
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000715 if(is_quantized)
716 {
717 tmp1 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[8] + offset_row], in_stride, dx_ptr[8], dy_ptr[8], iq_info, oq_info), tmp1, 0);
718 tmp1 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[9] + offset_row], in_stride, dx_ptr[9], dy_ptr[9], iq_info, oq_info), tmp1, 1);
719 tmp1 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[10] + offset_row], in_stride, dx_ptr[10], dy_ptr[10], iq_info, oq_info), tmp1, 2);
720 tmp1 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[11] + offset_row], in_stride, dx_ptr[11], dy_ptr[11], iq_info, oq_info), tmp1, 3);
721 tmp1 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[12] + offset_row], in_stride, dx_ptr[12], dy_ptr[12], iq_info, oq_info), tmp1, 4);
722 tmp1 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[13] + offset_row], in_stride, dx_ptr[13], dy_ptr[13], iq_info, oq_info), tmp1, 5);
723 tmp1 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[14] + offset_row], in_stride, dx_ptr[14], dy_ptr[14], iq_info, oq_info), tmp1, 6);
724 tmp1 = vset_lane_u8(delta_bilinear_c1_quantized(&in_ptr[offsets_ptr[15] + offset_row], in_stride, dx_ptr[15], dy_ptr[15], iq_info, oq_info), tmp1, 7);
725 }
726 else
727 {
728 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[8] + offset_row], in_stride, dx_ptr[8], dy_ptr[8]), tmp1, 0);
729 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[9] + offset_row], in_stride, dx_ptr[9], dy_ptr[9]), tmp1, 1);
730 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[10] + offset_row], in_stride, dx_ptr[10], dy_ptr[10]), tmp1, 2);
731 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[11] + offset_row], in_stride, dx_ptr[11], dy_ptr[11]), tmp1, 3);
732 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[12] + offset_row], in_stride, dx_ptr[12], dy_ptr[12]), tmp1, 4);
733 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[13] + offset_row], in_stride, dx_ptr[13], dy_ptr[13]), tmp1, 5);
734 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[14] + offset_row], in_stride, dx_ptr[14], dy_ptr[14]), tmp1, 6);
735 tmp1 = vset_lane_u8(delta_bilinear_c1(&in_ptr[offsets_ptr[15] + offset_row], in_stride, dx_ptr[15], dy_ptr[15]), tmp1, 7);
736 }
Georgios Pinitas583137c2017-08-31 18:12:42 +0100737 vst1q_u8(out.ptr(), vcombine_u8(tmp0, tmp1));
738 },
739 in, offsets, dx, dy, out);
740 break;
741 }
742 case DataType::S16:
743 {
744 execute_window_loop(window, [&](const Coordinates & id)
745 {
746 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets.ptr());
747 const auto dx_ptr = reinterpret_cast<const float *>(dx.ptr());
748 const auto dy_ptr = reinterpret_cast<const float *>(dy.ptr());
749
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000750 const int in_yi = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100751 const int offset_row = in_yi * in_stide_in_bytes;
752
753 int16x8x2_t tmp =
754 {
755 {
756 vdupq_n_s16(0),
757 vdupq_n_s16(0)
758 }
759 };
760
761 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);
762 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);
763 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);
764 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);
765 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);
766 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);
767 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);
768 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);
769
770 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);
771 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);
772 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);
773 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);
774 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);
775 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);
776 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);
777 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);
778
779 vst2q_s16(reinterpret_cast<int16_t *>(out.ptr()), tmp);
780 },
781 in, offsets, dx, dy, out);
782 break;
783 }
Georgios Pinitasc47ef202018-11-16 18:19:43 +0000784#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
785 case DataType::F16:
786 {
787 execute_window_loop(window, [&](const Coordinates & id)
788 {
789 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets.ptr());
790 const auto dx_ptr = reinterpret_cast<const float *>(dx.ptr());
791 const auto dy_ptr = reinterpret_cast<const float *>(dy.ptr());
792
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000793 const int in_yi = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
Georgios Pinitasc47ef202018-11-16 18:19:43 +0000794 const int offset_row = in_yi * in_stide_in_bytes;
795
796 float16x8x2_t tmp =
797 {
798 {
799 vdupq_n_f16(0),
800 vdupq_n_f16(0)
801 }
802 };
803
804 tmp.val[0] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[0] + offset_row), in_stride, dx_ptr[0], dy_ptr[0]), tmp.val[0], 0);
805 tmp.val[0] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[2] + offset_row), in_stride, dx_ptr[2], dy_ptr[2]), tmp.val[0], 1);
806 tmp.val[0] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[4] + offset_row), in_stride, dx_ptr[4], dy_ptr[4]), tmp.val[0], 2);
807 tmp.val[0] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[6] + offset_row), in_stride, dx_ptr[6], dy_ptr[6]), tmp.val[0], 3);
808 tmp.val[0] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[8] + offset_row), in_stride, dx_ptr[8], dy_ptr[8]), tmp.val[0], 4);
809 tmp.val[0] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[10] + offset_row), in_stride, dx_ptr[10], dy_ptr[10]), tmp.val[0], 5);
810 tmp.val[0] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[12] + offset_row), in_stride, dx_ptr[12], dy_ptr[12]), tmp.val[0], 6);
811 tmp.val[0] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[14] + offset_row), in_stride, dx_ptr[14], dy_ptr[14]), tmp.val[0], 7);
812
813 tmp.val[1] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[1] + offset_row), in_stride, dx_ptr[1], dy_ptr[1]), tmp.val[1], 0);
814 tmp.val[1] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[3] + offset_row), in_stride, dx_ptr[3], dy_ptr[3]), tmp.val[1], 1);
815 tmp.val[1] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[5] + offset_row), in_stride, dx_ptr[5], dy_ptr[5]), tmp.val[1], 2);
816 tmp.val[1] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[7] + offset_row), in_stride, dx_ptr[7], dy_ptr[7]), tmp.val[1], 3);
817 tmp.val[1] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[9] + offset_row), in_stride, dx_ptr[9], dy_ptr[9]), tmp.val[1], 4);
818 tmp.val[1] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[11] + offset_row), in_stride, dx_ptr[11], dy_ptr[11]), tmp.val[1], 5);
819 tmp.val[1] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[13] + offset_row), in_stride, dx_ptr[13], dy_ptr[13]), tmp.val[1], 6);
820 tmp.val[1] = vsetq_lane_f16(delta_bilinear_c1(reinterpret_cast<const __fp16 *>(in.ptr() + offsets_ptr[15] + offset_row), in_stride, dx_ptr[15], dy_ptr[15]), tmp.val[1], 7);
821
822 vst2q_f16(reinterpret_cast<__fp16 *>(out.ptr()), tmp);
823 },
824 in, offsets, dx, dy, out);
825 break;
826 }
827#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Georgios Pinitas583137c2017-08-31 18:12:42 +0100828 case DataType::F32:
829 {
830 execute_window_loop(window, [&](const Coordinates & id)
831 {
832 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets.ptr());
833 const auto dx_ptr = reinterpret_cast<const float *>(dx.ptr());
834 const auto dy_ptr = reinterpret_cast<const float *>(dy.ptr());
835
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000836 const int in_yi = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100837 const int offset_row = in_yi * in_stide_in_bytes;
838
839 float32x4x4_t tmp =
840 {
841 {
842 vdupq_n_f32(0),
843 vdupq_n_f32(0),
844 vdupq_n_f32(0),
845 vdupq_n_f32(0)
846 }
847 };
848
849 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);
850 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);
851 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);
852 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);
853
854 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);
855 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);
856 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);
857 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);
858
859 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);
860 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);
861 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);
862 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);
863
864 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);
865 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);
866 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);
867 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);
868
869 vst4q_f32(reinterpret_cast<float *>(out.ptr()), tmp);
870 },
871 in, offsets, dx, dy, out);
872 break;
873 }
874 default:
875 ARM_COMPUTE_ERROR("Not supported");
876 break;
877 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100878}
879
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100880void NEScaleKernel::scale_area_nchw(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100881{
882 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(_input, 1, DataType::U8);
883
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100884 // Don't increment in width/height/channels for the input tensor
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100885 // A pointer to the start of this plane is needed as base for the precomputed offsets
886 Window win_in(window);
887 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
888 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100889 win_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100890
891 Iterator in(_input, win_in);
892 Iterator out(_output, window);
893
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000894 const auto wr = arm_compute::calculate_resize_ratio(_input->info()->dimension(0), _output->info()->dimension(0), _align_corners);
895 const auto hr = arm_compute::calculate_resize_ratio(_input->info()->dimension(1), _output->info()->dimension(1), _align_corners);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100896 const auto w = _input->info()->dimension(0);
897 const auto h = _input->info()->dimension(1);
898 const size_t in_stride = _input->info()->strides_in_bytes()[1];
899
900 execute_window_loop(window, [&](const Coordinates & id)
901 {
902 const auto in_ptr = reinterpret_cast<const uint8_t *>(in.ptr());
903
904 uint8x8_t tmp0 = vdup_n_u8(0);
905 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x(), id.y()), tmp0, 0);
906 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 1, id.y()), tmp0, 1);
907 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 2, id.y()), tmp0, 2);
908 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 3, id.y()), tmp0, 3);
909 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 4, id.y()), tmp0, 4);
910 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 5, id.y()), tmp0, 5);
911 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 6, id.y()), tmp0, 6);
912 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 7, id.y()), tmp0, 7);
913
914 uint8x8_t tmp1 = vdup_n_u8(0);
915 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 8, id.y()), tmp1, 0);
916 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 9, id.y()), tmp1, 1);
917 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 10, id.y()), tmp1, 2);
918 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 11, id.y()), tmp1, 3);
919 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 12, id.y()), tmp1, 4);
920 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 13, id.y()), tmp1, 5);
921 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 14, id.y()), tmp1, 6);
922 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 15, id.y()), tmp1, 7);
923
924 vst1q_u8(out.ptr(), vcombine_u8(tmp0, tmp1));
925 },
926 in, out);
927}
928
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100929void NEScaleKernel::scale_nhwc(const Window &window)
930{
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100931 // Get data layout and width/height indices
Georgios Pinitasa5eb5912020-01-07 17:37:14 +0000932 const DataLayout data_layout = DataLayout::NHWC;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100933 const int idx_channels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
934 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
935 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
936
937 const size_t input_stride_w = _input->info()->strides_in_bytes()[idx_width];
938 const size_t input_stride_h = _input->info()->strides_in_bytes()[idx_height];
939 const size_t input_stride_c = _input->info()->strides_in_bytes()[idx_channels];
940
941 // Compute the ratio between source height and destination height
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000942 const auto hr = arm_compute::calculate_resize_ratio(_input->info()->dimension(idx_height), _output->info()->dimension(idx_height), _align_corners);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100943
944 // Don't increment in width/height/channels for the input tensor
945 // A pointer to the start of this plane is needed as base for the precomputed offsets
946 Window win_in(window);
947 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
948 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
949 win_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
950
951 switch(_input->info()->data_type())
952 {
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000953 case DataType::QASYMM8:
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100954 case DataType::U8:
955 {
956 if(_policy == InterpolationPolicy::NEAREST_NEIGHBOR)
957 {
Michalis Spyroud4733862019-07-09 14:21:06 +0100958 scale_nearest_nhwc_core<uint8_t>(_input, _offsets, _output, hr, window, win_in, input_stride_w, input_stride_h, input_stride_c, _sampling_offset);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100959 }
960 else
961 {
George Wort05398a92019-01-25 15:38:33 +0000962 scale_bilinear_nhwc_core<uint8_t, uint8_t>(_input, _offsets, _dx, _dy, _output, hr, _sampling_offset,
963 window, win_in, input_stride_w, input_stride_h, input_stride_c, _border_mode, _constant_border_value, _use_padding);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100964 }
965 break;
966 }
967 case DataType::S16:
968 {
969 if(_policy == InterpolationPolicy::NEAREST_NEIGHBOR)
970 {
Michalis Spyroud4733862019-07-09 14:21:06 +0100971 scale_nearest_nhwc_core<int16_t>(_input, _offsets, _output, hr, window, win_in, input_stride_w, input_stride_h, input_stride_c, _sampling_offset);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100972 }
973 else
974 {
George Wort05398a92019-01-25 15:38:33 +0000975 scale_bilinear_nhwc_core<int16_t, int16_t>(_input, _offsets, _dx, _dy, _output, hr, _sampling_offset,
976 window, win_in, input_stride_w, input_stride_h, input_stride_c, _border_mode, _constant_border_value, _use_padding);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100977 }
978 break;
979 }
Georgios Pinitasc47ef202018-11-16 18:19:43 +0000980#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
981 case DataType::F16:
982 {
983 if(_policy == InterpolationPolicy::NEAREST_NEIGHBOR)
984 {
985 scale_nearest_nhwc_core<float16_t>(_input, _offsets, _output, hr,
Michalis Spyroud4733862019-07-09 14:21:06 +0100986 window, win_in, input_stride_w, input_stride_h, input_stride_c, _sampling_offset);
Georgios Pinitasc47ef202018-11-16 18:19:43 +0000987 }
988 else
989 {
George Wort05398a92019-01-25 15:38:33 +0000990 scale_bilinear_nhwc_core<float16_t, half>(_input, _offsets, _dx, _dy, _output, hr, _sampling_offset,
991 window, win_in, input_stride_w, input_stride_h, input_stride_c, _border_mode, _constant_border_value, _use_padding);
Georgios Pinitasc47ef202018-11-16 18:19:43 +0000992 }
993 break;
994 }
995#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100996 case DataType::F32:
997 {
998 if(_policy == InterpolationPolicy::NEAREST_NEIGHBOR)
999 {
Michalis Spyroud4733862019-07-09 14:21:06 +01001000 scale_nearest_nhwc_core<float>(_input, _offsets, _output, hr, window, win_in, input_stride_w, input_stride_h, input_stride_c, _sampling_offset);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +01001001 }
1002 else
1003 {
George Wort05398a92019-01-25 15:38:33 +00001004 scale_bilinear_nhwc_core<float, float>(_input, _offsets, _dx, _dy, _output, hr, _sampling_offset,
1005 window, win_in, input_stride_w, input_stride_h, input_stride_c, _border_mode, _constant_border_value, _use_padding);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +01001006 }
1007 break;
1008 }
1009 default:
1010 ARM_COMPUTE_ERROR("Not supported");
1011 break;
1012 }
1013}
1014
Georgios Pinitas20b43132018-05-14 16:05:23 +01001015Status NEScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *dx, const ITensorInfo *dy,
1016 const ITensorInfo *offsets, ITensorInfo *output, InterpolationPolicy policy,
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +00001017 BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy, bool use_padding, bool align_corners)
Georgios Pinitas20b43132018-05-14 16:05:23 +01001018{
1019 BorderSize border_size(1);
1020 if(input->data_layout() == DataLayout::NHWC)
1021 {
1022 border_size = (border_mode == BorderMode::CONSTANT && policy == InterpolationPolicy::BILINEAR) ? BorderSize(1, 0, 0, 0) : BorderSize(0);
1023 }
1024
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +00001025 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, dx, dy, offsets, output, policy, border_mode, constant_border_value, sampling_policy, use_padding, align_corners));
Georgios Pinitas20b43132018-05-14 16:05:23 +01001026 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(),
1027 dx != nullptr ? dx->clone().get() : nullptr,
1028 dy != nullptr ? dy->clone().get() : nullptr,
1029 offsets != nullptr ? offsets->clone().get() : nullptr,
1030 output->clone().get(),
George Wort05398a92019-01-25 15:38:33 +00001031 policy, border_mode == BorderMode::UNDEFINED, sampling_policy, border_size, use_padding)
Georgios Pinitas20b43132018-05-14 16:05:23 +01001032 .first);
1033
1034 return Status{};
1035}
1036
Moritz Pflanzerc186b572017-09-07 09:48:04 +01001037void NEScaleKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001038{
Moritz Pflanzerc186b572017-09-07 09:48:04 +01001039 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001040 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
1041 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
1042 ARM_COMPUTE_ERROR_ON(_func == nullptr);
1043
1044 (this->*_func)(window);
1045}
Georgios Pinitas393fa4c2018-05-08 15:54:53 +01001046} // namespace arm_compute