1 /*
2 * Copyright (c) 2016-2022 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24 #include "src/cpu/kernels/CpuScaleKernel.h"
25
26 #include "arm_compute/core/Helpers.h"
27 #include "arm_compute/core/Window.h"
28 #include "src/core/common/Registrars.h"
29 #include "src/core/helpers/ScaleHelpers.h"
30 #include "src/core/helpers/WindowHelpers.h"
31 #include "src/cpu/kernels/scale/neon/list.h"
32 #include "src/cpu/kernels/scale/sve/list.h"
33 #include "support/Rounding.h"
34
35 #include <arm_neon.h>
36 #include <map>
37
38 namespace arm_compute
39 {
40 namespace cpu
41 {
42 namespace kernels
43 {
44 namespace
45 {
46 static const std::vector<CpuScaleKernel::ScaleKernel> available_kernels =
47 {
48 {
49 "sve_fp16_scale",
50 [](const ScaleKernelDataTypeISASelectorData & data)
__anone5d1f65b0202() 51 {
52 return data.dt == DataType::F16 && data.isa.sve && data.isa.fp16 && data.interpolation_policy != InterpolationPolicy::BILINEAR;
53 },
54 REGISTER_FP16_SVE(arm_compute::cpu::fp16_sve_scale)
55 },
56 {
57 "sve_fp32_scale",
58 [](const ScaleKernelDataTypeISASelectorData & data)
__anone5d1f65b0302() 59 {
60 return data.dt == DataType::F32 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
61 },
62 REGISTER_FP32_SVE(arm_compute::cpu::fp32_sve_scale)
63 },
64 {
65 "sve_qu8_scale",
66 [](const ScaleKernelDataTypeISASelectorData & data)
__anone5d1f65b0402() 67 {
68 return data.dt == DataType::QASYMM8 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
69 },
70 REGISTER_QASYMM8_SVE(arm_compute::cpu::qasymm8_sve_scale)
71 },
72 {
73 "sve_qs8_scale",
74 [](const ScaleKernelDataTypeISASelectorData & data)
__anone5d1f65b0502() 75 {
76 return data.dt == DataType::QASYMM8_SIGNED && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
77 },
78 REGISTER_QASYMM8_SIGNED_SVE(arm_compute::cpu::qasymm8_signed_sve_scale)
79 },
80 {
81 "sve_u8_scale",
82 [](const ScaleKernelDataTypeISASelectorData & data)
__anone5d1f65b0602() 83 {
84 return data.dt == DataType::U8 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
85 },
86 REGISTER_INTEGER_SVE(arm_compute::cpu::u8_sve_scale)
87 },
88 {
89 "sve_s16_scale",
90 [](const ScaleKernelDataTypeISASelectorData & data)
__anone5d1f65b0702() 91 {
92 return data.dt == DataType::S16 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
93 },
94 REGISTER_INTEGER_SVE(arm_compute::cpu::s16_sve_scale)
95 },
96 {
97 "neon_fp16_scale",
__anone5d1f65b0802() 98 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::F16 && data.isa.fp16; },
99 REGISTER_FP16_NEON(arm_compute::cpu::common_neon_scale<float16_t>)
100 },
101 {
102 "neon_fp32_scale",
__anone5d1f65b0902() 103 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::F32; },
104 REGISTER_FP32_NEON(arm_compute::cpu::common_neon_scale<float>)
105 },
106 {
107 "neon_qu8_scale",
__anone5d1f65b0a02() 108 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8; },
109 REGISTER_QASYMM8_NEON(arm_compute::cpu::qasymm8_neon_scale)
110 },
111 {
112 "neon_qs8_scale",
__anone5d1f65b0b02() 113 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
114 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::qasymm8_signed_neon_scale)
115 },
116 {
117 "neon_u8_scale",
__anone5d1f65b0c02() 118 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::U8; },
119 REGISTER_INTEGER_NEON(arm_compute::cpu::u8_neon_scale)
120 },
121 {
122 "neon_s8_scale",
__anone5d1f65b0d02() 123 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::S8; },
124 REGISTER_INTEGER_NEON(arm_compute::cpu::s8_neon_scale)
125 },
126 {
127 "neon_s16_scale",
__anone5d1f65b0e02() 128 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::S16; },
129 REGISTER_INTEGER_NEON(arm_compute::cpu::s16_neon_scale)
130 },
131 };
132
validate_arguments(const ITensorInfo * src,const ITensorInfo * dx,const ITensorInfo * dy,const ITensorInfo * offsets,ITensorInfo * dst,const ScaleKernelInfo & info)133 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy,
134 const ITensorInfo *offsets, ITensorInfo *dst, const ScaleKernelInfo &info)
135 {
136 const auto *uk = CpuScaleKernel::get_implementation(ScaleKernelDataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa(), info.interpolation_policy });
137
138 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
139
140 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(dst);
141 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
142 ARM_COMPUTE_RETURN_ERROR_ON(dst == src);
143 ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT);
144 ARM_COMPUTE_UNUSED(info.constant_border_value);
145 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.use_padding, "Padding is not supported");
146
147 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
148 const auto width_index = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
149 const auto height_index = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
150 const auto output_width = dst->dimension(width_index);
151 const auto output_height = dst->dimension(height_index);
152 ARM_COMPUTE_RETURN_ERROR_ON(output_width == 0);
153 ARM_COMPUTE_RETURN_ERROR_ON(output_height == 0);
154
155 ARM_COMPUTE_RETURN_ERROR_ON((src->data_type() == DataType::S8) && (data_layout != DataLayout::NHWC || info.interpolation_policy != InterpolationPolicy::BILINEAR
156 || info.border_mode != BorderMode::REPLICATE));
157
158 if(info.interpolation_policy == InterpolationPolicy::NEAREST_NEIGHBOR && offsets != nullptr)
159 {
160 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32);
161 }
162
163 if(info.interpolation_policy == InterpolationPolicy::BILINEAR && offsets != nullptr)
164 {
165 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32);
166 if(dx != nullptr && dy != nullptr)
167 {
168 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dx, 1, DataType::F32);
169 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dy, 1, DataType::F32);
170 }
171 }
172
173 ARM_COMPUTE_RETURN_ERROR_ON(info.align_corners && !scale_utils::is_align_corners_allowed_sampling_policy(info.sampling_policy));
174
175 if(info.interpolation_policy == InterpolationPolicy::AREA)
176 {
177 ARM_COMPUTE_RETURN_ERROR_ON(data_layout != DataLayout::NCHW);
178 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::U8);
179 }
180
181 return Status{};
182 }
183 } // namespace
184
configure(const ITensorInfo * src,const ITensorInfo * dx,const ITensorInfo * dy,const ITensorInfo * offsets,ITensorInfo * dst,const ScaleKernelInfo & info)185 void CpuScaleKernel::configure(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy, const ITensorInfo *offsets,
186 ITensorInfo *dst, const ScaleKernelInfo &info)
187 {
188 ARM_COMPUTE_UNUSED(dx, dy, offsets);
189 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
190 // Perform validation step
191 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src,
192 dx,
193 dy,
194 offsets,
195 dst,
196 info));
197
198 const auto *uk = CpuScaleKernel::get_implementation(ScaleKernelDataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa(), info.interpolation_policy });
199 ARM_COMPUTE_ERROR_ON_NULLPTR(uk);
200
201 _run_method = uk->ukernel;
202 _name = std::string("CpuScaleKernel").append("/").append(uk->name).append("_").append(string_from_interpolation_policy(info.interpolation_policy));
203
204 // Get data layout and width/height indices
205 _data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
206 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
207 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
208
209 _policy = info.interpolation_policy;
210 _border_mode = info.border_mode;
211 _constant_border_value = info.constant_border_value;
212 _align_corners = info.align_corners;
213
214 if(info.sampling_policy == SamplingPolicy::CENTER)
215 {
216 _sampling_offset = 0.5f;
217 }
218
219 // Compute the ratio between source width/height and destination width/height
220 const auto wr = scale_utils::calculate_resize_ratio(src->dimension(idx_width), dst->dimension(idx_width), _align_corners);
221 const auto hr = scale_utils::calculate_resize_ratio(src->dimension(idx_height), dst->dimension(idx_height), _align_corners);
222
223 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
224 _policy = (_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) ? InterpolationPolicy::NEAREST_NEIGHBOR : _policy;
225
226 if(_border_mode == BorderMode::UNDEFINED)
227 {
228 _border_mode = BorderMode::CONSTANT;
229 _constant_border_value = PixelValue();
230 }
231
232 #ifdef ENABLE_NCHW_KERNELS
233 // Configure scale function to run
234 if(_data_layout == DataLayout::NCHW)
235 {
236 std::string function_to_call("scale_");
237 function_to_call += string_from_data_type(src->data_type()) + "_";
238 function_to_call += string_from_data_layout(_data_layout) + "_";
239 function_to_call += string_from_interpolation_policy(_policy);
240
241 static std::map<std::string, ScaleFunctionPtr> map_function =
242 {
243 { "scale_U8_NCHW_AREA_CONSTANT", &CpuScaleKernel::scale_area_nchw_u8 },
244
245 { "scale_U8_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<uint8_t> },
246 { "scale_U8_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<uint8_t> },
247
248 { "scale_QASYMM8_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_qasymm<uint8_t> },
249 { "scale_QASYMM8_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<uint8_t> },
250
251 { "scale_QASYMM8_SIGNED_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_qasymm<int8_t> },
252 { "scale_QASYMM8_SIGNED_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<int8_t> },
253
254 { "scale_S16_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<int16_t> },
255 { "scale_S16_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<int16_t> },
256
257 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
258 { "scale_F16_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<float16_t> },
259 { "scale_F16_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<float16_t> },
260 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
261
262 { "scale_F32_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<float> },
263 { "scale_F32_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<float> },
264 };
265 auto it = map_function.find(function_to_call);
266 if(it != map_function.end())
267 {
268 _func = it->second;
269 }
270 }
271 #endif // ENABLE_NCHW_KERNELS
272
273 // Configure window
274 Window win = calculate_max_window(*dst, Steps());
275 ICpuKernel::configure(win);
276 }
277
278 #ifdef ENABLE_NCHW_KERNELS
279 template <typename T>
scale_nearest_nchw(const ITensor * src,ITensor * dst,const ITensor * dx,const ITensor * dy,const ITensor * offsets,const Window & window)280 void CpuScaleKernel::scale_nearest_nchw(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
281 {
282 ARM_COMPUTE_UNUSED(dx, dy);
283 const size_t in_stride_x = src->info()->dimension(0) + src->info()->padding().left + src->info()->padding().right;
284
285 // Compute the ratio between source height and destination height
286 const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), _align_corners);
287
288 // Don't increment in X and Y direction for the input tensor
289 // A pointer to the start of this plane is needed as base for the precomputed offsets
290 Window win_in(window);
291 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
292 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
293
294 // Set offsets window
295 Window win_off;
296 win_off.set(Window::DimX, window[Window::DimX]);
297 win_off.set(Window::DimY, window[Window::DimY]);
298 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
299 {
300 win_off.set(d, Window::Dimension(0, 0, 0));
301 }
302
303 // Create iterators
304 Iterator src_i(src, win_in);
305 Iterator dst_i(dst, window);
306 Iterator offsets_i(offsets, win_off);
307 execute_window_loop(window, [&](const Coordinates & id)
308 {
309 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets_i.ptr());
310 const auto in_yi = static_cast<int32_t>(_align_corners ? utils::rounding::round_half_away_from_zero((id.y() + _sampling_offset) * hr) : std::floor((
311 id.y() + _sampling_offset)
312 * hr));
313 const int32_t offset_row = in_yi * in_stride_x;
314 *reinterpret_cast<T *>(dst_i.ptr()) = *(reinterpret_cast<const T *>(src_i.ptr()) + offsets_ptr[0] + offset_row);
315 },
316 src_i, offsets_i, dst_i);
317 }
318
319 template <typename T>
scale_bilinear_nchw(const ITensor * src,ITensor * dst,const ITensor * dx,const ITensor * dy,const ITensor * offsets,const Window & window)320 void CpuScaleKernel::scale_bilinear_nchw(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
321 {
322 // Compute the ratio between source height and destination height
323 const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), _align_corners);
324 Window win_off;
325 win_off.set(Window::DimX, window.x());
326 win_off.set(Window::DimY, window.y());
327
328 // Don't increment in X and Y direction for the input tensor
329 // A pointer to the start of this plane is needed as base for the precomputed offsets
330 Window win_in(window);
331 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
332 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
333
334 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
335 {
336 win_off.set(d, Window::Dimension(0, 0, 0));
337 }
338
339 Iterator src_i(src, win_in);
340 Iterator dst_i(dst, window);
341 Iterator offsets_i(offsets, win_off);
342 Iterator dx_i(dx, win_off);
343 Iterator dy_i(dy, win_off);
344
345 const int32_t in_dim_w = src->info()->dimension(0);
346 const int32_t in_dim_h = src->info()->dimension(1);
347 const int32_t in_stride_w = in_dim_w + src->info()->padding().left + src->info()->padding().right;
348
349 if(_border_mode == BorderMode::CONSTANT)
350 {
351 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
352 using ConstType = typename std::conditional<std::is_same<T, float16_t>::value, half, T>::type;
353 #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
354 using ConstType = T;
355 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
356 const T const_border_value = static_cast<T>(_constant_border_value.get<ConstType>());
357 execute_window_loop(window, [&](const Coordinates & id)
358 {
359 const int32_t index_h = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
360 const auto index_w = *(reinterpret_cast<const int32_t *>(offsets_i.ptr()));
361 const auto dx_val = *(reinterpret_cast<const float *>(dx_i.ptr()));
362 const auto dy_val = *(reinterpret_cast<const float *>(dy_i.ptr()));
363 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
364
365 const auto a00 = (0 <= index_w && index_w < in_dim_w && 0 <= index_h && index_h < in_dim_h) ? (*(pixel_row_ptr + index_w + index_h * in_stride_w)) : const_border_value;
366 const auto a01 = (-1 <= index_w && index_w < in_dim_w - 1 && 0 <= index_h && index_h < in_dim_h) ? (*(pixel_row_ptr + index_w + 1 + index_h * in_stride_w)) : const_border_value;
367 const auto a10 = (0 <= index_w && index_w < in_dim_w && -1 <= index_h
368 && index_h < in_dim_h - 1) ?
369 (*(pixel_row_ptr + index_w + index_h * in_stride_w + in_stride_w)) :
370 const_border_value;
371 const auto a11 = (-1 <= index_w && index_w < in_dim_w - 1 && -1 <= index_h
372 && index_h < in_dim_h - 1) ?
373 (*(pixel_row_ptr + index_w + 1 + index_h * in_stride_w + in_stride_w)) :
374 const_border_value;
375
376 *reinterpret_cast<T *>(dst_i.ptr()) = static_cast<T>(scale_helpers::delta_bilinear(a00, a01, a10, a11, dx_val, dy_val));
377 },
378 src_i, offsets_i, dx_i, dy_i, dst_i);
379 }
380 else if(_border_mode == BorderMode::REPLICATE)
381 {
382 execute_window_loop(window, [&](const Coordinates & id)
383 {
384 const int index_h = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
385 const auto index_w = *(reinterpret_cast<const int32_t *>(offsets_i.ptr()));
386 const auto dx_val = *(reinterpret_cast<const float *>(dx_i.ptr()));
387 const auto dy_val = *(reinterpret_cast<const float *>(dy_i.ptr()));
388 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
389
390 auto clamped_x = utility::clamp<int>(index_w, 0, in_dim_w - 1);
391 auto clamped_x1 = utility::clamp<int>(index_w + 1, 0, in_dim_w - 1);
392 auto clamped_y = utility::clamp<int>(index_h, 0, in_dim_h - 1);
393 auto clamped_y1 = utility::clamp<int>(index_h + 1, 0, in_dim_h - 1);
394
395 const auto a00 = *(pixel_row_ptr + clamped_x + clamped_y * in_stride_w);
396 const auto a01 = *(pixel_row_ptr + clamped_x1 + clamped_y * in_stride_w);
397 const auto a10 = *(pixel_row_ptr + clamped_x + clamped_y1 * in_stride_w);
398 const auto a11 = *(pixel_row_ptr + clamped_x1 + clamped_y1 * in_stride_w);
399
400 *reinterpret_cast<T *>(dst_i.ptr()) = static_cast<T>(scale_helpers::delta_bilinear(a00, a01, a10, a11, dx_val, dy_val));
401 },
402 src_i, offsets_i, dx_i, dy_i, dst_i);
403 }
404 else
405 {
406 ARM_COMPUTE_ERROR("Not implemented");
407 }
408 }
409
scale_area_nchw_u8(const ITensor * src,ITensor * dst,const ITensor * dx,const ITensor * dy,const ITensor * offsets,const Window & window)410 void CpuScaleKernel::scale_area_nchw_u8(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
411 {
412 ARM_COMPUTE_UNUSED(dx, dy, offsets);
413 using namespace scale_helpers;
414
415 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::U8);
416
417 // Don't increment in width/height/channels for the input tensor
418 // A pointer to the start of this plane is needed as base for the precomputed offsets
419 Window win_in(window);
420 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
421 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
422 win_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
423
424 Iterator src_i(src, win_in);
425 Iterator dst_i(dst, window);
426
427 const auto wr = scale_utils::calculate_resize_ratio(src->info()->dimension(0), dst->info()->dimension(0), _align_corners);
428 const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), _align_corners);
429 const auto w = src->info()->dimension(0);
430 const auto h = src->info()->dimension(1);
431 const size_t in_stride = src->info()->strides_in_bytes()[1];
432
433 execute_window_loop(window, [&](const Coordinates & id)
434 {
435 const auto in_ptr = reinterpret_cast<const uint8_t *>(src_i.ptr());
436
437 uint8x8_t tmp0 = vdup_n_u8(0);
438 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x(), id.y()), tmp0, 0);
439 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 1, id.y()), tmp0, 1);
440 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 2, id.y()), tmp0, 2);
441 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 3, id.y()), tmp0, 3);
442 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 4, id.y()), tmp0, 4);
443 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 5, id.y()), tmp0, 5);
444 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 6, id.y()), tmp0, 6);
445 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 7, id.y()), tmp0, 7);
446
447 uint8x8_t tmp1 = vdup_n_u8(0);
448 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 8, id.y()), tmp1, 0);
449 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 9, id.y()), tmp1, 1);
450 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 10, id.y()), tmp1, 2);
451 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 11, id.y()), tmp1, 3);
452 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 12, id.y()), tmp1, 4);
453 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 13, id.y()), tmp1, 5);
454 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 14, id.y()), tmp1, 6);
455 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 15, id.y()), tmp1, 7);
456
457 vst1q_u8(dst_i.ptr(), vcombine_u8(tmp0, tmp1));
458 },
459 src_i, dst_i);
460 }
461
462 template <typename T>
scale_bilinear_qasymm(const ITensor * src,ITensor * dst,const ITensor * dx,const ITensor * dy,const ITensor * offsets,const Window & window)463 void CpuScaleKernel::scale_bilinear_qasymm(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
464 {
465 // Get data layout and width/height indices
466 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
467 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
468
469 // Compute the ratio between source height and destination height
470 const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(idx_height), dst->info()->dimension(idx_height), _align_corners);
471 Window win_off;
472 win_off.set(Window::DimX, Window::Dimension(0, 0, 0));
473 win_off.set(Window::DimY, Window::Dimension(0, 0, 0));
474
475 // Don't increment in X and Y direction for the input tensor
476 // A pointer to the start of this plane is needed as base for the precomputed offsets
477 Window win_in(window);
478 win_in.set(idx_width, Window::Dimension(0, 0, 0));
479 win_in.set(idx_height, Window::Dimension(0, 0, 0));
480
481 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
482 {
483 win_off.set(d, Window::Dimension(0, 0, 0));
484 }
485
486 Iterator src_i(src, win_in);
487 Iterator dst_i(dst, window);
488
489 const int32_t in_dim_w = src->info()->dimension(idx_width);
490 const int32_t in_dim_h = src->info()->dimension(idx_height);
491 const int32_t stride_w = src->info()->strides_in_bytes()[idx_width];
492 const int32_t stride_h = src->info()->strides_in_bytes()[idx_height];
493
494 const UniformQuantizationInfo iq_info = src->info()->quantization_info().uniform();
495 const UniformQuantizationInfo oq_info = dst->info()->quantization_info().uniform();
496
497 if(_border_mode == BorderMode::CONSTANT)
498 {
499 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
500 using ConstType = typename std::conditional<std::is_same<T, float16_t>::value, half, T>::type;
501 #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
502 using ConstType = T;
503 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
504 const T const_border_value = static_cast<T>(_constant_border_value.get<ConstType>());
505 execute_window_loop(window, [&](const Coordinates & id)
506 {
507 const int32_t index_h = std::floor((id[idx_height] + _sampling_offset) * hr - _sampling_offset);
508 const int32_t index_w = *(reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
509 const auto dx_val = *(reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
510 const auto dy_val = *(reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
511 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
512
513 const auto a00 = (0 <= index_w && index_w < in_dim_w && 0 <= index_h && index_h < in_dim_h) ?
514 (*(pixel_row_ptr + index_w * stride_w + index_h * stride_h)) :
515 const_border_value;
516 const auto a01 = (-1 <= index_w && index_w < in_dim_w - 1 && 0 <= index_h && index_h < in_dim_h) ?
517 (*(pixel_row_ptr + (index_w + 1) * stride_w + index_h * stride_h)) :
518 const_border_value;
519 const auto a10 = (0 <= index_w && index_w < in_dim_w && -1 <= index_h && index_h < in_dim_h - 1) ?
520 (*(pixel_row_ptr + index_w * stride_w + (index_h + 1) * stride_h)) :
521 const_border_value;
522 const auto a11 = (-1 <= index_w && index_w < in_dim_w - 1 && -1 <= index_h && index_h < in_dim_h - 1) ?
523 (*(pixel_row_ptr + (index_w + 1) * stride_w + (index_h + 1) * stride_h)) :
524 const_border_value;
525
526 const float inp00 = Qasymm8QuantizationHelper<T>::dequantize(a00, iq_info);
527 const float inp01 = Qasymm8QuantizationHelper<T>::dequantize(a01, iq_info);
528 const float inp10 = Qasymm8QuantizationHelper<T>::dequantize(a10, iq_info);
529 const float inp11 = Qasymm8QuantizationHelper<T>::dequantize(a11, iq_info);
530 *reinterpret_cast<T *>(dst_i.ptr()) = Qasymm8QuantizationHelper<T>::quantize(scale_helpers::delta_bilinear(inp00, inp01, inp10, inp11, dx_val, dy_val), oq_info);
531 },
532 src_i, dst_i);
533 }
534 else if(_border_mode == BorderMode::REPLICATE)
535 {
536 execute_window_loop(window, [&](const Coordinates & id)
537 {
538 const int index_h = std::floor((id[idx_height] + _sampling_offset) * hr - _sampling_offset);
539 const int32_t index_w = *(reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
540 const auto dx_val = *(reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
541 const auto dy_val = *(reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
542 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
543
544 auto clamped_w = utility::clamp<int>(index_w, 0, in_dim_w - 1);
545 auto clamped_w1 = utility::clamp<int>(index_w + 1, 0, in_dim_w - 1);
546 auto clamped_h = utility::clamp<int>(index_h, 0, in_dim_h - 1);
547 auto clamped_h1 = utility::clamp<int>(index_h + 1, 0, in_dim_h - 1);
548
549 const auto a00 = *(pixel_row_ptr + clamped_w * stride_w + clamped_h * stride_h);
550 const auto a01 = *(pixel_row_ptr + clamped_w1 * stride_w + clamped_h * stride_h);
551 const auto a10 = *(pixel_row_ptr + clamped_w * stride_w + clamped_h1 * stride_h);
552 const auto a11 = *(pixel_row_ptr + clamped_w1 * stride_w + clamped_h1 * stride_h);
553
554 const float inp00 = Qasymm8QuantizationHelper<T>::dequantize(a00, iq_info);
555 const float inp01 = Qasymm8QuantizationHelper<T>::dequantize(a01, iq_info);
556 const float inp10 = Qasymm8QuantizationHelper<T>::dequantize(a10, iq_info);
557 const float inp11 = Qasymm8QuantizationHelper<T>::dequantize(a11, iq_info);
558 *reinterpret_cast<T *>(dst_i.ptr()) = Qasymm8QuantizationHelper<T>::quantize(scale_helpers::delta_bilinear(inp00, inp01, inp10, inp11, dx_val, dy_val), oq_info);
559 },
560 src_i, dst_i);
561 }
562 else
563 {
564 ARM_COMPUTE_ERROR("Not implemented");
565 }
566 }
567 #endif // ENABLE_NCHW_KERNELS
568
validate(const ITensorInfo * input,const ITensorInfo * dx,const ITensorInfo * dy,const ITensorInfo * offsets,ITensorInfo * output,const ScaleKernelInfo & info)569 Status CpuScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *dx, const ITensorInfo *dy,
570 const ITensorInfo *offsets, ITensorInfo *output, const ScaleKernelInfo &info)
571 {
572 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, dx, dy, offsets, output, info));
573 return Status{};
574 }
575
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)576 void CpuScaleKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
577 {
578 ARM_COMPUTE_UNUSED(info);
579 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
580 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
581 ARM_COMPUTE_ERROR_ON(_func == nullptr && _data_layout == DataLayout::NCHW);
582 ARM_COMPUTE_ERROR_ON(_run_method == nullptr && _data_layout == DataLayout::NHWC);
583
584 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
585 auto dst = tensors.get_tensor(TensorType::ACL_DST);
586 const auto dx = tensors.get_const_tensor(TensorType::ACL_INT_0);
587 const auto dy = tensors.get_const_tensor(TensorType::ACL_INT_1);
588 const auto offsets = tensors.get_const_tensor(TensorType::ACL_INT_2);
589
590 if(_data_layout == DataLayout::NCHW)
591 {
592 (this->*_func)(src, dst, dx, dy, offsets, window);
593 }
594 else
595 {
596 _run_method(src, dst, offsets, dx, dy, _policy, _border_mode, _constant_border_value, _sampling_offset, _align_corners, window);
597 }
598 }
599
name() const600 const char *CpuScaleKernel::name() const
601 {
602 return _name.c_str();
603 }
604
get_available_kernels()605 const std::vector<CpuScaleKernel::ScaleKernel> &CpuScaleKernel::get_available_kernels()
606 {
607 return available_kernels;
608 }
609
610 } // namespace kernels
611 } // namespace cpu
612 } // namespace arm_compute
613