blob: b8bb5ad18a5fd69e5e2a8cedfe9cdf7d6ca712e9 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Giorgio Arena5ae8d802021-11-18 18:02:13 +00002 * Copyright (c) 2016-2022 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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/kernels/CpuScaleKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/core/Helpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Window.h"
Sheri Zhang23adc4c2021-01-05 12:48:45 +000028#include "src/core/common/Registrars.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029#include "src/core/helpers/ScaleHelpers.h"
30#include "src/core/helpers/WindowHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010031#include "src/cpu/kernels/scale/neon/list.h"
32#include "src/cpu/kernels/scale/sve/list.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "support/Rounding.h"
Manuel Bottini10b38262021-02-19 18:16:44 +000034
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include <arm_neon.h>
Manuel Bottinifc2f6d02020-08-26 16:28:38 +010036#include <map>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010038namespace arm_compute
39{
Manuel Bottini10b38262021-02-19 18:16:44 +000040namespace cpu
41{
42namespace kernels
43{
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010044namespace
45{
Giorgio Arena5ae8d802021-11-18 18:02:13 +000046static const std::vector<CpuScaleKernel::ScaleKernel> available_kernels =
Sheri Zhang23adc4c2021-01-05 12:48:45 +000047{
Sheri Zhang23adc4c2021-01-05 12:48:45 +000048 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010049 "sve_fp16_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +010050 [](const ScaleKernelDataTypeISASelectorData & data)
51 {
52 return data.dt == DataType::F16 && data.isa.sve && data.isa.fp16 && data.interpolation_policy != InterpolationPolicy::BILINEAR;
53 },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010054 REGISTER_FP16_SVE(arm_compute::cpu::fp16_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000055 },
56 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010057 "sve_fp32_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +010058 [](const ScaleKernelDataTypeISASelectorData & data)
59 {
60 return data.dt == DataType::F32 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
61 },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010062 REGISTER_FP32_SVE(arm_compute::cpu::fp32_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000063 },
64 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010065 "sve_qu8_scale",
Gunes Bayirc4f27432022-09-11 15:59:19 +010066 [](const ScaleKernelDataTypeISASelectorData & data)
67 {
68 return data.dt == DataType::QASYMM8 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
69 },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010070 REGISTER_QASYMM8_SVE(arm_compute::cpu::qasymm8_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000071 },
72 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010073 "sve_qs8_scale",
Gunes Bayirc4f27432022-09-11 15:59:19 +010074 [](const ScaleKernelDataTypeISASelectorData & data)
75 {
76 return data.dt == DataType::QASYMM8_SIGNED && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
77 },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010078 REGISTER_QASYMM8_SIGNED_SVE(arm_compute::cpu::qasymm8_signed_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000079 },
80 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010081 "sve_u8_scale",
Gunes Bayirc4f27432022-09-11 15:59:19 +010082 [](const ScaleKernelDataTypeISASelectorData & data)
83 {
84 return data.dt == DataType::U8 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
85 },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010086 REGISTER_INTEGER_SVE(arm_compute::cpu::u8_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000087 },
88 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010089 "sve_s16_scale",
Gunes Bayirc4f27432022-09-11 15:59:19 +010090 [](const ScaleKernelDataTypeISASelectorData & data)
91 {
92 return data.dt == DataType::S16 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
93 },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010094 REGISTER_INTEGER_SVE(arm_compute::cpu::s16_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000095 },
Sheri Zhang23adc4c2021-01-05 12:48:45 +000096 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010097 "neon_fp16_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +010098 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::F16 && data.isa.fp16; },
Sheri Zhang360f5762021-01-20 12:20:20 +000099 REGISTER_FP16_NEON(arm_compute::cpu::common_neon_scale<float16_t>)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000100 },
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000101 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100102 "neon_fp32_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +0100103 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::F32; },
Sheri Zhang360f5762021-01-20 12:20:20 +0000104 REGISTER_FP32_NEON(arm_compute::cpu::common_neon_scale<float>)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000105 },
106 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100107 "neon_qu8_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +0100108 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8; },
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000109 REGISTER_QASYMM8_NEON(arm_compute::cpu::qasymm8_neon_scale)
110 },
111 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100112 "neon_qs8_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +0100113 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000114 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::qasymm8_signed_neon_scale)
115 },
116 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100117 "neon_u8_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +0100118 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::U8; },
Gian Marco Iodice8b8405a2021-10-01 17:48:02 +0100119 REGISTER_INTEGER_NEON(arm_compute::cpu::u8_neon_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000120 },
121 {
Gunes Bayirc4f27432022-09-11 15:59:19 +0100122 "neon_s8_scale",
123 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::S8; },
124 REGISTER_INTEGER_NEON(arm_compute::cpu::s8_neon_scale)
125 },
126 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100127 "neon_s16_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +0100128 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::S16; },
Gian Marco Iodice8b8405a2021-10-01 17:48:02 +0100129 REGISTER_INTEGER_NEON(arm_compute::cpu::s16_neon_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000130 },
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000131};
132
Manuel Bottini10b38262021-02-19 18:16:44 +0000133Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy,
134 const ITensorInfo *offsets, ITensorInfo *dst, const ScaleKernelInfo &info)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100135{
Gunes Bayir53929b12022-08-11 12:15:39 +0100136 const auto *uk = CpuScaleKernel::get_implementation(ScaleKernelDataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa(), info.interpolation_policy });
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000137
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000138 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
139
Manuel Bottini10b38262021-02-19 18:16:44 +0000140 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);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100143 ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100144 ARM_COMPUTE_UNUSED(info.constant_border_value);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100145 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.use_padding, "Padding is not supported");
Georgios Pinitas20b43132018-05-14 16:05:23 +0100146
Manuel Bottini10b38262021-02-19 18:16:44 +0000147 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000148 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);
Manuel Bottini10b38262021-02-19 18:16:44 +0000150 const auto output_width = dst->dimension(width_index);
151 const auto output_height = dst->dimension(height_index);
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000152 ARM_COMPUTE_RETURN_ERROR_ON(output_width == 0);
153 ARM_COMPUTE_RETURN_ERROR_ON(output_height == 0);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100154
Gunes Bayirc4f27432022-09-11 15:59:19 +0100155 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
Gunes Bayir0eed3052022-09-04 21:00:10 +0100158 if(info.interpolation_policy == InterpolationPolicy::NEAREST_NEIGHBOR && offsets != nullptr)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100159 {
160 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32);
161 }
162
Gunes Bayir0eed3052022-09-04 21:00:10 +0100163 if(info.interpolation_policy == InterpolationPolicy::BILINEAR && offsets != nullptr)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100164 {
165 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32);
Michalis Spyrou7f8caf72021-05-13 13:35:30 +0100166 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 }
Sang-Hoon Parkf025a772020-05-26 11:11:32 +0100171 }
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000172
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100173 ARM_COMPUTE_RETURN_ERROR_ON(info.align_corners && !scale_utils::is_align_corners_allowed_sampling_policy(info.sampling_policy));
Georgios Pinitas20b43132018-05-14 16:05:23 +0100174
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100175 if(info.interpolation_policy == InterpolationPolicy::AREA)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100176 {
177 ARM_COMPUTE_RETURN_ERROR_ON(data_layout != DataLayout::NCHW);
Manuel Bottini10b38262021-02-19 18:16:44 +0000178 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::U8);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100179 }
180
181 return Status{};
182}
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100183} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184
Manuel Bottini10b38262021-02-19 18:16:44 +0000185void CpuScaleKernel::configure(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy, const ITensorInfo *offsets,
186 ITensorInfo *dst, const ScaleKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187{
Manuel Bottini10b38262021-02-19 18:16:44 +0000188 ARM_COMPUTE_UNUSED(dx, dy, offsets);
189 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100190 // Perform validation step
Manuel Bottini10b38262021-02-19 18:16:44 +0000191 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src,
192 dx,
193 dy,
194 offsets,
195 dst,
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100196 info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197
Gunes Bayir53929b12022-08-11 12:15:39 +0100198 const auto *uk = CpuScaleKernel::get_implementation(ScaleKernelDataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa(), info.interpolation_policy });
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100199 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
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100204 // Get data layout and width/height indices
Manuel Bottini10b38262021-02-19 18:16:44 +0000205 _data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000206 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);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100209 _policy = info.interpolation_policy;
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100210 _border_mode = info.border_mode;
211 _constant_border_value = info.constant_border_value;
Michele Di Giorgio7a81d2a2020-07-30 14:52:16 +0100212 _align_corners = info.align_corners;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100213
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100214 if(info.sampling_policy == SamplingPolicy::CENTER)
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000215 {
216 _sampling_offset = 0.5f;
217 }
218
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100219 // Compute the ratio between source width/height and destination width/height
Manuel Bottini10b38262021-02-19 18:16:44 +0000220 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);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100223 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000224 _policy = (_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) ? InterpolationPolicy::NEAREST_NEIGHBOR : _policy;
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100225
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100226 if(_border_mode == BorderMode::UNDEFINED)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100227 {
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100228 _border_mode = BorderMode::CONSTANT;
229 _constant_border_value = PixelValue();
230 }
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100231
Manuel Bottini10b38262021-02-19 18:16:44 +0000232#ifdef ENABLE_NCHW_KERNELS
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000233 // Configure scale function to run
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000234 if(_data_layout == DataLayout::NCHW)
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100235 {
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000236 std::string function_to_call("scale_");
Manuel Bottini10b38262021-02-19 18:16:44 +0000237 function_to_call += string_from_data_type(src->data_type()) + "_";
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000238 function_to_call += string_from_data_layout(_data_layout) + "_";
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000239 function_to_call += string_from_interpolation_policy(_policy);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100240
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000241 static std::map<std::string, ScaleFunctionPtr> map_function =
242 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000243 { "scale_U8_NCHW_AREA_CONSTANT", &CpuScaleKernel::scale_area_nchw_u8 },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100244
Manuel Bottini10b38262021-02-19 18:16:44 +0000245 { "scale_U8_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<uint8_t> },
246 { "scale_U8_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<uint8_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100247
Manuel Bottini10b38262021-02-19 18:16:44 +0000248 { "scale_QASYMM8_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_qasymm<uint8_t> },
249 { "scale_QASYMM8_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<uint8_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100250
Manuel Bottini10b38262021-02-19 18:16:44 +0000251 { "scale_QASYMM8_SIGNED_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_qasymm<int8_t> },
252 { "scale_QASYMM8_SIGNED_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<int8_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100253
Manuel Bottini10b38262021-02-19 18:16:44 +0000254 { "scale_S16_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<int16_t> },
255 { "scale_S16_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<int16_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100256
257#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Manuel Bottini10b38262021-02-19 18:16:44 +0000258 { "scale_F16_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<float16_t> },
259 { "scale_F16_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<float16_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100260#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
261
Manuel Bottini10b38262021-02-19 18:16:44 +0000262 { "scale_F32_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<float> },
263 { "scale_F32_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<float> },
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000264 };
265 auto it = map_function.find(function_to_call);
266 if(it != map_function.end())
267 {
268 _func = it->second;
269 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100270 }
Manuel Bottini10b38262021-02-19 18:16:44 +0000271#endif // ENABLE_NCHW_KERNELS
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100272
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100273 // Configure window
Manuel Bottini10b38262021-02-19 18:16:44 +0000274 Window win = calculate_max_window(*dst, Steps());
275 ICpuKernel::configure(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100276}
277
Manuel Bottini10b38262021-02-19 18:16:44 +0000278#ifdef ENABLE_NCHW_KERNELS
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100279template <typename T>
Manuel Bottini10b38262021-02-19 18:16:44 +0000280void CpuScaleKernel::scale_nearest_nchw(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100281{
Manuel Bottini10b38262021-02-19 18:16:44 +0000282 ARM_COMPUTE_UNUSED(dx, dy);
283 const size_t in_stride_x = src->info()->dimension(0) + src->info()->padding().left + src->info()->padding().right;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100284
285 // Compute the ratio between source height and destination height
Manuel Bottini10b38262021-02-19 18:16:44 +0000286 const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), _align_corners);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100287
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
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100294 // Set offsets window
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100295 Window win_off;
296 win_off.set(Window::DimX, window[Window::DimX]);
297 win_off.set(Window::DimY, window[Window::DimY]);
Manuel Bottini10b38262021-02-19 18:16:44 +0000298 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100299 {
300 win_off.set(d, Window::Dimension(0, 0, 0));
301 }
302
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100303 // Create iterators
Manuel Bottini10b38262021-02-19 18:16:44 +0000304 Iterator src_i(src, win_in);
305 Iterator dst_i(dst, window);
306 Iterator offsets_i(offsets, win_off);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100307 execute_window_loop(window, [&](const Coordinates & id)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100308 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000309 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);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100315 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000316 src_i, offsets_i, dst_i);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100317}
318
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100319template <typename T>
Manuel Bottini10b38262021-02-19 18:16:44 +0000320void CpuScaleKernel::scale_bilinear_nchw(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100321{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100322 // Compute the ratio between source height and destination height
Manuel Bottini10b38262021-02-19 18:16:44 +0000323 const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), _align_corners);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100324 Window win_off;
325 win_off.set(Window::DimX, window.x());
326 win_off.set(Window::DimY, window.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100327
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
Manuel Bottini10b38262021-02-19 18:16:44 +0000334 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100335 {
336 win_off.set(d, Window::Dimension(0, 0, 0));
337 }
338
Manuel Bottini10b38262021-02-19 18:16:44 +0000339 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);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100344
Manuel Bottini10b38262021-02-19 18:16:44 +0000345 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;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100348
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100349 if(_border_mode == BorderMode::CONSTANT)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100350 {
Georgios Pinitasc47ef202018-11-16 18:19:43 +0000351#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100352 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;
Georgios Pinitasc47ef202018-11-16 18:19:43 +0000355#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100356 const T const_border_value = static_cast<T>(_constant_border_value.get<ConstType>());
357 execute_window_loop(window, [&](const Coordinates & id)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100358 {
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100359 const int32_t index_h = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
Manuel Bottini10b38262021-02-19 18:16:44 +0000360 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());
Georgios Pinitas583137c2017-08-31 18:12:42 +0100364
Manuel Bottini49682852020-10-13 10:25:31 +0100365 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;
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100367 const auto a10 = (0 <= index_w && index_w < in_dim_w && -1 <= index_h
368 && index_h < in_dim_h - 1) ?
Manuel Bottini49682852020-10-13 10:25:31 +0100369 (*(pixel_row_ptr + index_w + index_h * in_stride_w + in_stride_w)) :
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100370 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) ?
Manuel Bottini49682852020-10-13 10:25:31 +0100373 (*(pixel_row_ptr + index_w + 1 + index_h * in_stride_w + in_stride_w)) :
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100374 const_border_value;
Georgios Pinitas583137c2017-08-31 18:12:42 +0100375
Manuel Bottini10b38262021-02-19 18:16:44 +0000376 *reinterpret_cast<T *>(dst_i.ptr()) = static_cast<T>(scale_helpers::delta_bilinear(a00, a01, a10, a11, dx_val, dy_val));
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100377 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000378 src_i, offsets_i, dx_i, dy_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100379 }
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);
Manuel Bottini10b38262021-02-19 18:16:44 +0000385 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());
Georgios Pinitas583137c2017-08-31 18:12:42 +0100389
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100390 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);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100394
Manuel Bottini49682852020-10-13 10:25:31 +0100395 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);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100399
Manuel Bottini10b38262021-02-19 18:16:44 +0000400 *reinterpret_cast<T *>(dst_i.ptr()) = static_cast<T>(scale_helpers::delta_bilinear(a00, a01, a10, a11, dx_val, dy_val));
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100401 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000402 src_i, offsets_i, dx_i, dy_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100403 }
404 else
405 {
406 ARM_COMPUTE_ERROR("Not implemented");
Georgios Pinitas583137c2017-08-31 18:12:42 +0100407 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100408}
409
Manuel Bottini10b38262021-02-19 18:16:44 +0000410void CpuScaleKernel::scale_area_nchw_u8(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100411{
Manuel Bottini10b38262021-02-19 18:16:44 +0000412 ARM_COMPUTE_UNUSED(dx, dy, offsets);
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100413 using namespace scale_helpers;
414
Manuel Bottini10b38262021-02-19 18:16:44 +0000415 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100416
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100417 // Don't increment in width/height/channels for the input tensor
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100418 // 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));
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100422 win_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100423
Manuel Bottini10b38262021-02-19 18:16:44 +0000424 Iterator src_i(src, win_in);
425 Iterator dst_i(dst, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100426
Manuel Bottini10b38262021-02-19 18:16:44 +0000427 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];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100432
433 execute_window_loop(window, [&](const Coordinates & id)
434 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000435 const auto in_ptr = reinterpret_cast<const uint8_t *>(src_i.ptr());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100436
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
Manuel Bottini10b38262021-02-19 18:16:44 +0000457 vst1q_u8(dst_i.ptr(), vcombine_u8(tmp0, tmp1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100458 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000459 src_i, dst_i);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100460}
461
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100462template <typename T>
Manuel Bottini10b38262021-02-19 18:16:44 +0000463void CpuScaleKernel::scale_bilinear_qasymm(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100464{
465 // Get data layout and width/height indices
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000466 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);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100468
469 // Compute the ratio between source height and destination height
Manuel Bottini10b38262021-02-19 18:16:44 +0000470 const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(idx_height), dst->info()->dimension(idx_height), _align_corners);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100471 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
Manuel Bottini10b38262021-02-19 18:16:44 +0000481 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100482 {
483 win_off.set(d, Window::Dimension(0, 0, 0));
484 }
485
Manuel Bottini10b38262021-02-19 18:16:44 +0000486 Iterator src_i(src, win_in);
487 Iterator dst_i(dst, window);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100488
Manuel Bottini10b38262021-02-19 18:16:44 +0000489 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];
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100493
Manuel Bottini10b38262021-02-19 18:16:44 +0000494 const UniformQuantizationInfo iq_info = src->info()->quantization_info().uniform();
495 const UniformQuantizationInfo oq_info = dst->info()->quantization_info().uniform();
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100496
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);
Manuel Bottini10b38262021-02-19 18:16:44 +0000508 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());
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100512
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
Manuel Bottini10b38262021-02-19 18:16:44 +0000526 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);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100531 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000532 src_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100533 }
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);
Manuel Bottini10b38262021-02-19 18:16:44 +0000539 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());
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100543
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
Manuel Bottini10b38262021-02-19 18:16:44 +0000554 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);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100559 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000560 src_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100561 }
562 else
563 {
564 ARM_COMPUTE_ERROR("Not implemented");
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100565 }
566}
Manuel Bottini10b38262021-02-19 18:16:44 +0000567#endif // ENABLE_NCHW_KERNELS
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100568
Manuel Bottini10b38262021-02-19 18:16:44 +0000569Status CpuScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *dx, const ITensorInfo *dy,
570 const ITensorInfo *offsets, ITensorInfo *output, const ScaleKernelInfo &info)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100571{
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100572 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, dx, dy, offsets, output, info));
Georgios Pinitas20b43132018-05-14 16:05:23 +0100573 return Status{};
574}
575
Manuel Bottini10b38262021-02-19 18:16:44 +0000576void CpuScaleKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100577{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100578 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100579 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottini10b38262021-02-19 18:16:44 +0000580 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000581 ARM_COMPUTE_ERROR_ON(_func == nullptr && _data_layout == DataLayout::NCHW);
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100582 ARM_COMPUTE_ERROR_ON(_run_method == nullptr && _data_layout == DataLayout::NHWC);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100583
Manuel Bottini10b38262021-02-19 18:16:44 +0000584 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
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000590 if(_data_layout == DataLayout::NCHW)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000591 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000592 (this->*_func)(src, dst, dx, dy, offsets, window);
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000593 }
594 else
595 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100596 _run_method(src, dst, offsets, dx, dy, _policy, _border_mode, _constant_border_value, _sampling_offset, _align_corners, window);
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000597 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100598}
Manuel Bottini10b38262021-02-19 18:16:44 +0000599
600const char *CpuScaleKernel::name() const
601{
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100602 return _name.c_str();
Manuel Bottini10b38262021-02-19 18:16:44 +0000603}
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000604
605const std::vector<CpuScaleKernel::ScaleKernel> &CpuScaleKernel::get_available_kernels()
606{
607 return available_kernels;
608}
609
Manuel Bottini10b38262021-02-19 18:16:44 +0000610} // namespace kernels
611} // namespace cpu
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100612} // namespace arm_compute