blob: 332304599f628eb65b27e07e24fd7c8985b02f7c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Pablo Marquez Tello48c0ed92023-05-05 15:02:32 +01002 * Copyright (c) 2016-2023 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"
Matthew Bentham314d3e22023-06-23 10:53:52 +000027#include "arm_compute/core/utils/InterpolationPolicyUtils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Window.h"
Sheri Zhang23adc4c2021-01-05 12:48:45 +000029#include "src/core/common/Registrars.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010030#include "src/core/helpers/ScaleHelpers.h"
31#include "src/core/helpers/WindowHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010032#include "src/cpu/kernels/scale/neon/list.h"
33#include "src/cpu/kernels/scale/sve/list.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "support/Rounding.h"
Manuel Bottini10b38262021-02-19 18:16:44 +000035
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include <arm_neon.h>
Manuel Bottinifc2f6d02020-08-26 16:28:38 +010037#include <map>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010039namespace arm_compute
40{
Manuel Bottini10b38262021-02-19 18:16:44 +000041namespace cpu
42{
43namespace kernels
44{
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010045namespace
46{
Giorgio Arena5ae8d802021-11-18 18:02:13 +000047static const std::vector<CpuScaleKernel::ScaleKernel> available_kernels =
Sheri Zhang23adc4c2021-01-05 12:48:45 +000048{
Sheri Zhang23adc4c2021-01-05 12:48:45 +000049 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010050 "sve_fp16_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +010051 [](const ScaleKernelDataTypeISASelectorData & data)
52 {
53 return data.dt == DataType::F16 && data.isa.sve && data.isa.fp16 && data.interpolation_policy != InterpolationPolicy::BILINEAR;
54 },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010055 REGISTER_FP16_SVE(arm_compute::cpu::fp16_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000056 },
57 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010058 "sve_fp32_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +010059 [](const ScaleKernelDataTypeISASelectorData & data)
60 {
61 return data.dt == DataType::F32 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
62 },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010063 REGISTER_FP32_SVE(arm_compute::cpu::fp32_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000064 },
65 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010066 "sve_qu8_scale",
Gunes Bayirc4f27432022-09-11 15:59:19 +010067 [](const ScaleKernelDataTypeISASelectorData & data)
68 {
69 return data.dt == DataType::QASYMM8 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
70 },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010071 REGISTER_QASYMM8_SVE(arm_compute::cpu::qasymm8_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000072 },
73 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010074 "sve_qs8_scale",
Gunes Bayirc4f27432022-09-11 15:59:19 +010075 [](const ScaleKernelDataTypeISASelectorData & data)
76 {
77 return data.dt == DataType::QASYMM8_SIGNED && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
78 },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010079 REGISTER_QASYMM8_SIGNED_SVE(arm_compute::cpu::qasymm8_signed_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000080 },
81 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010082 "sve_u8_scale",
Gunes Bayirc4f27432022-09-11 15:59:19 +010083 [](const ScaleKernelDataTypeISASelectorData & data)
84 {
85 return data.dt == DataType::U8 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
86 },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010087 REGISTER_INTEGER_SVE(arm_compute::cpu::u8_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000088 },
89 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010090 "sve_s16_scale",
Gunes Bayirc4f27432022-09-11 15:59:19 +010091 [](const ScaleKernelDataTypeISASelectorData & data)
92 {
93 return data.dt == DataType::S16 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
94 },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010095 REGISTER_INTEGER_SVE(arm_compute::cpu::s16_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000096 },
Sheri Zhang23adc4c2021-01-05 12:48:45 +000097 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010098 "neon_fp16_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +010099 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::F16 && data.isa.fp16; },
Sheri Zhang360f5762021-01-20 12:20:20 +0000100 REGISTER_FP16_NEON(arm_compute::cpu::common_neon_scale<float16_t>)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000101 },
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000102 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100103 "neon_fp32_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +0100104 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::F32; },
Sheri Zhang360f5762021-01-20 12:20:20 +0000105 REGISTER_FP32_NEON(arm_compute::cpu::common_neon_scale<float>)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000106 },
107 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100108 "neon_qu8_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +0100109 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8; },
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000110 REGISTER_QASYMM8_NEON(arm_compute::cpu::qasymm8_neon_scale)
111 },
112 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100113 "neon_qs8_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +0100114 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000115 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::qasymm8_signed_neon_scale)
116 },
117 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100118 "neon_u8_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +0100119 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::U8; },
Gian Marco Iodice8b8405a2021-10-01 17:48:02 +0100120 REGISTER_INTEGER_NEON(arm_compute::cpu::u8_neon_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000121 },
122 {
Gunes Bayirc4f27432022-09-11 15:59:19 +0100123 "neon_s8_scale",
124 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::S8; },
125 REGISTER_INTEGER_NEON(arm_compute::cpu::s8_neon_scale)
126 },
127 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100128 "neon_s16_scale",
Gunes Bayir53929b12022-08-11 12:15:39 +0100129 [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::S16; },
Gian Marco Iodice8b8405a2021-10-01 17:48:02 +0100130 REGISTER_INTEGER_NEON(arm_compute::cpu::s16_neon_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000131 },
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000132};
133
Manuel Bottini10b38262021-02-19 18:16:44 +0000134Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy,
135 const ITensorInfo *offsets, ITensorInfo *dst, const ScaleKernelInfo &info)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100136{
Gunes Bayir53929b12022-08-11 12:15:39 +0100137 const auto *uk = CpuScaleKernel::get_implementation(ScaleKernelDataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa(), info.interpolation_policy });
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000138
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000139 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
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);
Pablo Marquez Tello48c0ed92023-05-05 15:02:32 +0100143 ARM_COMPUTE_RETURN_ERROR_ON(src->num_channels()!=1);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100144 ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100145 ARM_COMPUTE_UNUSED(info.constant_border_value);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100146 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.use_padding, "Padding is not supported");
Georgios Pinitas20b43132018-05-14 16:05:23 +0100147
Manuel Bottini10b38262021-02-19 18:16:44 +0000148 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000149 const auto width_index = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
150 const auto height_index = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Manuel Bottini10b38262021-02-19 18:16:44 +0000151 const auto output_width = dst->dimension(width_index);
152 const auto output_height = dst->dimension(height_index);
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000153 ARM_COMPUTE_RETURN_ERROR_ON(output_width == 0);
154 ARM_COMPUTE_RETURN_ERROR_ON(output_height == 0);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100155
Gunes Bayirc4f27432022-09-11 15:59:19 +0100156 ARM_COMPUTE_RETURN_ERROR_ON((src->data_type() == DataType::S8) && (data_layout != DataLayout::NHWC || info.interpolation_policy != InterpolationPolicy::BILINEAR
157 || info.border_mode != BorderMode::REPLICATE));
158
Gunes Bayir0eed3052022-09-04 21:00:10 +0100159 if(info.interpolation_policy == InterpolationPolicy::NEAREST_NEIGHBOR && offsets != nullptr)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100160 {
161 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32);
162 }
163
Gunes Bayir0eed3052022-09-04 21:00:10 +0100164 if(info.interpolation_policy == InterpolationPolicy::BILINEAR && offsets != nullptr)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100165 {
166 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32);
Michalis Spyrou7f8caf72021-05-13 13:35:30 +0100167 if(dx != nullptr && dy != nullptr)
168 {
169 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dx, 1, DataType::F32);
170 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dy, 1, DataType::F32);
171 }
Sang-Hoon Parkf025a772020-05-26 11:11:32 +0100172 }
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000173
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100174 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 +0100175
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100176 if(info.interpolation_policy == InterpolationPolicy::AREA)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100177 {
178 ARM_COMPUTE_RETURN_ERROR_ON(data_layout != DataLayout::NCHW);
Manuel Bottini10b38262021-02-19 18:16:44 +0000179 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::U8);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100180 }
181
182 return Status{};
183}
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100184} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185
Manuel Bottini10b38262021-02-19 18:16:44 +0000186void CpuScaleKernel::configure(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy, const ITensorInfo *offsets,
187 ITensorInfo *dst, const ScaleKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188{
Manuel Bottini10b38262021-02-19 18:16:44 +0000189 ARM_COMPUTE_UNUSED(dx, dy, offsets);
190 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100191 // Perform validation step
Manuel Bottini10b38262021-02-19 18:16:44 +0000192 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src,
193 dx,
194 dy,
195 offsets,
196 dst,
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100197 info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198
Gunes Bayir53929b12022-08-11 12:15:39 +0100199 const auto *uk = CpuScaleKernel::get_implementation(ScaleKernelDataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa(), info.interpolation_policy });
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100200 ARM_COMPUTE_ERROR_ON_NULLPTR(uk);
201
202 _run_method = uk->ukernel;
203 _name = std::string("CpuScaleKernel").append("/").append(uk->name).append("_").append(string_from_interpolation_policy(info.interpolation_policy));
204
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100205 // Get data layout and width/height indices
Manuel Bottini10b38262021-02-19 18:16:44 +0000206 _data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000207 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
208 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100210 _policy = info.interpolation_policy;
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100211 _border_mode = info.border_mode;
212 _constant_border_value = info.constant_border_value;
Michele Di Giorgio7a81d2a2020-07-30 14:52:16 +0100213 _align_corners = info.align_corners;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100214
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100215 if(info.sampling_policy == SamplingPolicy::CENTER)
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000216 {
217 _sampling_offset = 0.5f;
218 }
219
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100220 // Compute the ratio between source width/height and destination width/height
Manuel Bottini10b38262021-02-19 18:16:44 +0000221 const auto wr = scale_utils::calculate_resize_ratio(src->dimension(idx_width), dst->dimension(idx_width), _align_corners);
222 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 +0100223
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100224 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000225 _policy = (_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) ? InterpolationPolicy::NEAREST_NEIGHBOR : _policy;
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100226
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100227 if(_border_mode == BorderMode::UNDEFINED)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100228 {
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100229 _border_mode = BorderMode::CONSTANT;
230 _constant_border_value = PixelValue();
231 }
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100232
Manuel Bottini10b38262021-02-19 18:16:44 +0000233#ifdef ENABLE_NCHW_KERNELS
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000234 // Configure scale function to run
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000235 if(_data_layout == DataLayout::NCHW)
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100236 {
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000237 std::string function_to_call("scale_");
Manuel Bottini10b38262021-02-19 18:16:44 +0000238 function_to_call += string_from_data_type(src->data_type()) + "_";
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000239 function_to_call += string_from_data_layout(_data_layout) + "_";
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000240 function_to_call += string_from_interpolation_policy(_policy);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100241
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000242 static std::map<std::string, ScaleFunctionPtr> map_function =
243 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000244 { "scale_U8_NCHW_AREA_CONSTANT", &CpuScaleKernel::scale_area_nchw_u8 },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100245
Manuel Bottini10b38262021-02-19 18:16:44 +0000246 { "scale_U8_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<uint8_t> },
247 { "scale_U8_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<uint8_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100248
Manuel Bottini10b38262021-02-19 18:16:44 +0000249 { "scale_QASYMM8_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_qasymm<uint8_t> },
250 { "scale_QASYMM8_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<uint8_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100251
Manuel Bottini10b38262021-02-19 18:16:44 +0000252 { "scale_QASYMM8_SIGNED_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_qasymm<int8_t> },
253 { "scale_QASYMM8_SIGNED_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<int8_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100254
Manuel Bottini10b38262021-02-19 18:16:44 +0000255 { "scale_S16_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<int16_t> },
256 { "scale_S16_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<int16_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100257
258#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Manuel Bottini10b38262021-02-19 18:16:44 +0000259 { "scale_F16_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<float16_t> },
260 { "scale_F16_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<float16_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100261#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
262
Manuel Bottini10b38262021-02-19 18:16:44 +0000263 { "scale_F32_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<float> },
264 { "scale_F32_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<float> },
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000265 };
266 auto it = map_function.find(function_to_call);
267 if(it != map_function.end())
268 {
269 _func = it->second;
270 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100271 }
Manuel Bottini10b38262021-02-19 18:16:44 +0000272#endif // ENABLE_NCHW_KERNELS
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100273
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100274 // Configure window
Manuel Bottini10b38262021-02-19 18:16:44 +0000275 Window win = calculate_max_window(*dst, Steps());
276 ICpuKernel::configure(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100277}
278
Manuel Bottini10b38262021-02-19 18:16:44 +0000279#ifdef ENABLE_NCHW_KERNELS
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100280template <typename T>
Manuel Bottini10b38262021-02-19 18:16:44 +0000281void 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 +0100282{
Manuel Bottini10b38262021-02-19 18:16:44 +0000283 ARM_COMPUTE_UNUSED(dx, dy);
284 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 +0100285
286 // Compute the ratio between source height and destination height
Manuel Bottini10b38262021-02-19 18:16:44 +0000287 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 +0100288
289 // Don't increment in X and Y direction for the input tensor
290 // A pointer to the start of this plane is needed as base for the precomputed offsets
291 Window win_in(window);
292 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
293 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
294
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100295 // Set offsets window
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100296 Window win_off;
297 win_off.set(Window::DimX, window[Window::DimX]);
298 win_off.set(Window::DimY, window[Window::DimY]);
Manuel Bottini10b38262021-02-19 18:16:44 +0000299 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100300 {
301 win_off.set(d, Window::Dimension(0, 0, 0));
302 }
303
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100304 // Create iterators
Manuel Bottini10b38262021-02-19 18:16:44 +0000305 Iterator src_i(src, win_in);
306 Iterator dst_i(dst, window);
307 Iterator offsets_i(offsets, win_off);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100308 execute_window_loop(window, [&](const Coordinates & id)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100309 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000310 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets_i.ptr());
311 const auto in_yi = static_cast<int32_t>(_align_corners ? utils::rounding::round_half_away_from_zero((id.y() + _sampling_offset) * hr) : std::floor((
312 id.y() + _sampling_offset)
313 * hr));
314 const int32_t offset_row = in_yi * in_stride_x;
315 *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 +0100316 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000317 src_i, offsets_i, dst_i);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100318}
319
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100320template <typename T>
Manuel Bottini10b38262021-02-19 18:16:44 +0000321void 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 +0100322{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100323 // Compute the ratio between source height and destination height
Manuel Bottini10b38262021-02-19 18:16:44 +0000324 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 +0100325 Window win_off;
326 win_off.set(Window::DimX, window.x());
327 win_off.set(Window::DimY, window.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100328
329 // Don't increment in X and Y direction for the input tensor
330 // A pointer to the start of this plane is needed as base for the precomputed offsets
331 Window win_in(window);
332 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
333 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
334
Manuel Bottini10b38262021-02-19 18:16:44 +0000335 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100336 {
337 win_off.set(d, Window::Dimension(0, 0, 0));
338 }
339
Manuel Bottini10b38262021-02-19 18:16:44 +0000340 Iterator src_i(src, win_in);
341 Iterator dst_i(dst, window);
342 Iterator offsets_i(offsets, win_off);
343 Iterator dx_i(dx, win_off);
344 Iterator dy_i(dy, win_off);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100345
Manuel Bottini10b38262021-02-19 18:16:44 +0000346 const int32_t in_dim_w = src->info()->dimension(0);
347 const int32_t in_dim_h = src->info()->dimension(1);
348 const int32_t in_stride_w = in_dim_w + src->info()->padding().left + src->info()->padding().right;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100349
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100350 if(_border_mode == BorderMode::CONSTANT)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100351 {
Georgios Pinitasc47ef202018-11-16 18:19:43 +0000352#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100353 using ConstType = typename std::conditional<std::is_same<T, float16_t>::value, half, T>::type;
354#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
355 using ConstType = T;
Georgios Pinitasc47ef202018-11-16 18:19:43 +0000356#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100357 const T const_border_value = static_cast<T>(_constant_border_value.get<ConstType>());
358 execute_window_loop(window, [&](const Coordinates & id)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100359 {
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100360 const int32_t index_h = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
Manuel Bottini10b38262021-02-19 18:16:44 +0000361 const auto index_w = *(reinterpret_cast<const int32_t *>(offsets_i.ptr()));
362 const auto dx_val = *(reinterpret_cast<const float *>(dx_i.ptr()));
363 const auto dy_val = *(reinterpret_cast<const float *>(dy_i.ptr()));
364 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
Georgios Pinitas583137c2017-08-31 18:12:42 +0100365
Manuel Bottini49682852020-10-13 10:25:31 +0100366 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;
367 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 +0100368 const auto a10 = (0 <= index_w && index_w < in_dim_w && -1 <= index_h
369 && index_h < in_dim_h - 1) ?
Manuel Bottini49682852020-10-13 10:25:31 +0100370 (*(pixel_row_ptr + index_w + index_h * in_stride_w + in_stride_w)) :
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100371 const_border_value;
372 const auto a11 = (-1 <= index_w && index_w < in_dim_w - 1 && -1 <= index_h
373 && index_h < in_dim_h - 1) ?
Manuel Bottini49682852020-10-13 10:25:31 +0100374 (*(pixel_row_ptr + index_w + 1 + index_h * in_stride_w + in_stride_w)) :
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100375 const_border_value;
Georgios Pinitas583137c2017-08-31 18:12:42 +0100376
Manuel Bottini10b38262021-02-19 18:16:44 +0000377 *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 +0100378 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000379 src_i, offsets_i, dx_i, dy_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100380 }
381 else if(_border_mode == BorderMode::REPLICATE)
382 {
383 execute_window_loop(window, [&](const Coordinates & id)
384 {
385 const int index_h = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
Manuel Bottini10b38262021-02-19 18:16:44 +0000386 const auto index_w = *(reinterpret_cast<const int32_t *>(offsets_i.ptr()));
387 const auto dx_val = *(reinterpret_cast<const float *>(dx_i.ptr()));
388 const auto dy_val = *(reinterpret_cast<const float *>(dy_i.ptr()));
389 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
Georgios Pinitas583137c2017-08-31 18:12:42 +0100390
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100391 auto clamped_x = utility::clamp<int>(index_w, 0, in_dim_w - 1);
392 auto clamped_x1 = utility::clamp<int>(index_w + 1, 0, in_dim_w - 1);
393 auto clamped_y = utility::clamp<int>(index_h, 0, in_dim_h - 1);
394 auto clamped_y1 = utility::clamp<int>(index_h + 1, 0, in_dim_h - 1);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100395
Manuel Bottini49682852020-10-13 10:25:31 +0100396 const auto a00 = *(pixel_row_ptr + clamped_x + clamped_y * in_stride_w);
397 const auto a01 = *(pixel_row_ptr + clamped_x1 + clamped_y * in_stride_w);
398 const auto a10 = *(pixel_row_ptr + clamped_x + clamped_y1 * in_stride_w);
399 const auto a11 = *(pixel_row_ptr + clamped_x1 + clamped_y1 * in_stride_w);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100400
Manuel Bottini10b38262021-02-19 18:16:44 +0000401 *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 +0100402 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000403 src_i, offsets_i, dx_i, dy_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100404 }
405 else
406 {
407 ARM_COMPUTE_ERROR("Not implemented");
Georgios Pinitas583137c2017-08-31 18:12:42 +0100408 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100409}
410
Manuel Bottini10b38262021-02-19 18:16:44 +0000411void 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 +0100412{
Manuel Bottini10b38262021-02-19 18:16:44 +0000413 ARM_COMPUTE_UNUSED(dx, dy, offsets);
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100414 using namespace scale_helpers;
415
Manuel Bottini10b38262021-02-19 18:16:44 +0000416 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100417
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100418 // Don't increment in width/height/channels for the input tensor
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100419 // A pointer to the start of this plane is needed as base for the precomputed offsets
420 Window win_in(window);
421 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
422 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100423 win_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100424
Manuel Bottini10b38262021-02-19 18:16:44 +0000425 Iterator src_i(src, win_in);
426 Iterator dst_i(dst, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100427
Manuel Bottini10b38262021-02-19 18:16:44 +0000428 const auto wr = scale_utils::calculate_resize_ratio(src->info()->dimension(0), dst->info()->dimension(0), _align_corners);
429 const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), _align_corners);
430 const auto w = src->info()->dimension(0);
431 const auto h = src->info()->dimension(1);
432 const size_t in_stride = src->info()->strides_in_bytes()[1];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100433
434 execute_window_loop(window, [&](const Coordinates & id)
435 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000436 const auto in_ptr = reinterpret_cast<const uint8_t *>(src_i.ptr());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100437
438 uint8x8_t tmp0 = vdup_n_u8(0);
439 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x(), id.y()), tmp0, 0);
440 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 1, id.y()), tmp0, 1);
441 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 2, id.y()), tmp0, 2);
442 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 3, id.y()), tmp0, 3);
443 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 4, id.y()), tmp0, 4);
444 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 5, id.y()), tmp0, 5);
445 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 6, id.y()), tmp0, 6);
446 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 7, id.y()), tmp0, 7);
447
448 uint8x8_t tmp1 = vdup_n_u8(0);
449 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 8, id.y()), tmp1, 0);
450 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 9, id.y()), tmp1, 1);
451 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 10, id.y()), tmp1, 2);
452 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 11, id.y()), tmp1, 3);
453 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 12, id.y()), tmp1, 4);
454 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 13, id.y()), tmp1, 5);
455 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 14, id.y()), tmp1, 6);
456 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 15, id.y()), tmp1, 7);
457
Manuel Bottini10b38262021-02-19 18:16:44 +0000458 vst1q_u8(dst_i.ptr(), vcombine_u8(tmp0, tmp1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100459 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000460 src_i, dst_i);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100461}
462
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100463template <typename T>
Manuel Bottini10b38262021-02-19 18:16:44 +0000464void 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 +0100465{
466 // Get data layout and width/height indices
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000467 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
468 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100469
470 // Compute the ratio between source height and destination height
Manuel Bottini10b38262021-02-19 18:16:44 +0000471 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 +0100472 Window win_off;
473 win_off.set(Window::DimX, Window::Dimension(0, 0, 0));
474 win_off.set(Window::DimY, Window::Dimension(0, 0, 0));
475
476 // Don't increment in X and Y direction for the input tensor
477 // A pointer to the start of this plane is needed as base for the precomputed offsets
478 Window win_in(window);
479 win_in.set(idx_width, Window::Dimension(0, 0, 0));
480 win_in.set(idx_height, Window::Dimension(0, 0, 0));
481
Manuel Bottini10b38262021-02-19 18:16:44 +0000482 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100483 {
484 win_off.set(d, Window::Dimension(0, 0, 0));
485 }
486
Manuel Bottini10b38262021-02-19 18:16:44 +0000487 Iterator src_i(src, win_in);
488 Iterator dst_i(dst, window);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100489
Manuel Bottini10b38262021-02-19 18:16:44 +0000490 const int32_t in_dim_w = src->info()->dimension(idx_width);
491 const int32_t in_dim_h = src->info()->dimension(idx_height);
492 const int32_t stride_w = src->info()->strides_in_bytes()[idx_width];
493 const int32_t stride_h = src->info()->strides_in_bytes()[idx_height];
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100494
Manuel Bottini10b38262021-02-19 18:16:44 +0000495 const UniformQuantizationInfo iq_info = src->info()->quantization_info().uniform();
496 const UniformQuantizationInfo oq_info = dst->info()->quantization_info().uniform();
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100497
498 if(_border_mode == BorderMode::CONSTANT)
499 {
500#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
501 using ConstType = typename std::conditional<std::is_same<T, float16_t>::value, half, T>::type;
502#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
503 using ConstType = T;
504#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
505 const T const_border_value = static_cast<T>(_constant_border_value.get<ConstType>());
506 execute_window_loop(window, [&](const Coordinates & id)
507 {
508 const int32_t index_h = std::floor((id[idx_height] + _sampling_offset) * hr - _sampling_offset);
Manuel Bottini10b38262021-02-19 18:16:44 +0000509 const int32_t index_w = *(reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
510 const auto dx_val = *(reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
511 const auto dy_val = *(reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
512 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100513
514 const auto a00 = (0 <= index_w && index_w < in_dim_w && 0 <= index_h && index_h < in_dim_h) ?
515 (*(pixel_row_ptr + index_w * stride_w + index_h * stride_h)) :
516 const_border_value;
517 const auto a01 = (-1 <= index_w && index_w < in_dim_w - 1 && 0 <= index_h && index_h < in_dim_h) ?
518 (*(pixel_row_ptr + (index_w + 1) * stride_w + index_h * stride_h)) :
519 const_border_value;
520 const auto a10 = (0 <= index_w && index_w < in_dim_w && -1 <= index_h && index_h < in_dim_h - 1) ?
521 (*(pixel_row_ptr + index_w * stride_w + (index_h + 1) * stride_h)) :
522 const_border_value;
523 const auto a11 = (-1 <= index_w && index_w < in_dim_w - 1 && -1 <= index_h && index_h < in_dim_h - 1) ?
524 (*(pixel_row_ptr + (index_w + 1) * stride_w + (index_h + 1) * stride_h)) :
525 const_border_value;
526
Manuel Bottini10b38262021-02-19 18:16:44 +0000527 const float inp00 = Qasymm8QuantizationHelper<T>::dequantize(a00, iq_info);
528 const float inp01 = Qasymm8QuantizationHelper<T>::dequantize(a01, iq_info);
529 const float inp10 = Qasymm8QuantizationHelper<T>::dequantize(a10, iq_info);
530 const float inp11 = Qasymm8QuantizationHelper<T>::dequantize(a11, iq_info);
531 *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 +0100532 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000533 src_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100534 }
535 else if(_border_mode == BorderMode::REPLICATE)
536 {
537 execute_window_loop(window, [&](const Coordinates & id)
538 {
539 const int index_h = std::floor((id[idx_height] + _sampling_offset) * hr - _sampling_offset);
Manuel Bottini10b38262021-02-19 18:16:44 +0000540 const int32_t index_w = *(reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
541 const auto dx_val = *(reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
542 const auto dy_val = *(reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
543 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100544
545 auto clamped_w = utility::clamp<int>(index_w, 0, in_dim_w - 1);
546 auto clamped_w1 = utility::clamp<int>(index_w + 1, 0, in_dim_w - 1);
547 auto clamped_h = utility::clamp<int>(index_h, 0, in_dim_h - 1);
548 auto clamped_h1 = utility::clamp<int>(index_h + 1, 0, in_dim_h - 1);
549
550 const auto a00 = *(pixel_row_ptr + clamped_w * stride_w + clamped_h * stride_h);
551 const auto a01 = *(pixel_row_ptr + clamped_w1 * stride_w + clamped_h * stride_h);
552 const auto a10 = *(pixel_row_ptr + clamped_w * stride_w + clamped_h1 * stride_h);
553 const auto a11 = *(pixel_row_ptr + clamped_w1 * stride_w + clamped_h1 * stride_h);
554
Manuel Bottini10b38262021-02-19 18:16:44 +0000555 const float inp00 = Qasymm8QuantizationHelper<T>::dequantize(a00, iq_info);
556 const float inp01 = Qasymm8QuantizationHelper<T>::dequantize(a01, iq_info);
557 const float inp10 = Qasymm8QuantizationHelper<T>::dequantize(a10, iq_info);
558 const float inp11 = Qasymm8QuantizationHelper<T>::dequantize(a11, iq_info);
559 *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 +0100560 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000561 src_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100562 }
563 else
564 {
565 ARM_COMPUTE_ERROR("Not implemented");
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100566 }
567}
Manuel Bottini10b38262021-02-19 18:16:44 +0000568#endif // ENABLE_NCHW_KERNELS
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100569
Manuel Bottini10b38262021-02-19 18:16:44 +0000570Status CpuScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *dx, const ITensorInfo *dy,
571 const ITensorInfo *offsets, ITensorInfo *output, const ScaleKernelInfo &info)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100572{
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100573 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, dx, dy, offsets, output, info));
Georgios Pinitas20b43132018-05-14 16:05:23 +0100574 return Status{};
575}
576
Manuel Bottini10b38262021-02-19 18:16:44 +0000577void CpuScaleKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100578{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100579 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100580 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottini10b38262021-02-19 18:16:44 +0000581 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000582 ARM_COMPUTE_ERROR_ON(_func == nullptr && _data_layout == DataLayout::NCHW);
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100583 ARM_COMPUTE_ERROR_ON(_run_method == nullptr && _data_layout == DataLayout::NHWC);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100584
Manuel Bottini10b38262021-02-19 18:16:44 +0000585 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
586 auto dst = tensors.get_tensor(TensorType::ACL_DST);
587 const auto dx = tensors.get_const_tensor(TensorType::ACL_INT_0);
588 const auto dy = tensors.get_const_tensor(TensorType::ACL_INT_1);
589 const auto offsets = tensors.get_const_tensor(TensorType::ACL_INT_2);
590
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000591 if(_data_layout == DataLayout::NCHW)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000592 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000593 (this->*_func)(src, dst, dx, dy, offsets, window);
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000594 }
595 else
596 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100597 _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 +0000598 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100599}
Manuel Bottini10b38262021-02-19 18:16:44 +0000600
601const char *CpuScaleKernel::name() const
602{
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100603 return _name.c_str();
Manuel Bottini10b38262021-02-19 18:16:44 +0000604}
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000605
606const std::vector<CpuScaleKernel::ScaleKernel> &CpuScaleKernel::get_available_kernels()
607{
608 return available_kernels;
609}
610
Manuel Bottini10b38262021-02-19 18:16:44 +0000611} // namespace kernels
612} // namespace cpu
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100613} // namespace arm_compute