blob: 60564a97dd84ab53a9280a8bc449cd4d112dcb27 [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"
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010028#include "arm_compute/core/utils/misc/Utility.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029#include "src/core/CPP/Validate.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010030#include "src/core/NEON/wrapper/wrapper.h"
Sheri Zhang23adc4c2021-01-05 12:48:45 +000031#include "src/core/common/Registrars.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/helpers/AutoConfiguration.h"
33#include "src/core/helpers/ScaleHelpers.h"
34#include "src/core/helpers/WindowHelpers.h"
Sang-Hoon Park3687ee12020-06-24 13:34:04 +010035#include "src/core/utils/ScaleUtils.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010036#include "src/cpu/kernels/scale/neon/list.h"
37#include "src/cpu/kernels/scale/sve/list.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010038#include "support/Rounding.h"
Manuel Bottini10b38262021-02-19 18:16:44 +000039
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040#include <arm_neon.h>
Manuel Bottinifc2f6d02020-08-26 16:28:38 +010041#include <map>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010043namespace arm_compute
44{
Manuel Bottini10b38262021-02-19 18:16:44 +000045namespace cpu
46{
47namespace kernels
48{
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010049namespace
50{
Giorgio Arena5ae8d802021-11-18 18:02:13 +000051static const std::vector<CpuScaleKernel::ScaleKernel> available_kernels =
Sheri Zhang23adc4c2021-01-05 12:48:45 +000052{
Michalis Spyrou20fca522021-06-07 14:23:57 +010053#if defined(ARM_COMPUTE_ENABLE_SVE)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000054 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010055 "sve_fp16_scale",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000056 [](const DataTypeISASelectorData & data) { return data.dt == DataType::F16 && data.isa.sve; },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010057 REGISTER_FP16_SVE(arm_compute::cpu::fp16_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000058 },
59 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010060 "sve_fp32_scale",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000061 [](const DataTypeISASelectorData & data) { return data.dt == DataType::F32 && data.isa.sve; },
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",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000066 [](const DataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8 && data.isa.sve; },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010067 REGISTER_QASYMM8_SVE(arm_compute::cpu::qasymm8_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000068 },
69 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010070 "sve_qs8_scale",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000071 [](const DataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED && data.isa.sve; },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010072 REGISTER_QASYMM8_SIGNED_SVE(arm_compute::cpu::qasymm8_signed_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000073 },
74 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010075 "sve_u8_scale",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000076 [](const DataTypeISASelectorData & data) { return data.dt == DataType::U8 && data.isa.sve; },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010077 REGISTER_INTEGER_SVE(arm_compute::cpu::u8_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000078 },
79 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010080 "sve_s16_scale",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000081 [](const DataTypeISASelectorData & data) { return data.dt == DataType::S16 && data.isa.sve; },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010082 REGISTER_INTEGER_SVE(arm_compute::cpu::s16_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000083 },
Michalis Spyrou20fca522021-06-07 14:23:57 +010084#endif /* defined(ARM_COMPUTE_ENABLE_SVE) */
85#if defined(ARM_COMPUTE_ENABLE_NEON)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000086#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
87 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010088 "neon_fp16_scale",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000089 [](const DataTypeISASelectorData & data) { return data.dt == DataType::F16 && data.isa.fp16; },
Sheri Zhang360f5762021-01-20 12:20:20 +000090 REGISTER_FP16_NEON(arm_compute::cpu::common_neon_scale<float16_t>)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000091 },
92#endif /* !defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) */
93 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010094 "neon_fp32_scale",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000095 [](const DataTypeISASelectorData & data) { return data.dt == DataType::F32; },
Sheri Zhang360f5762021-01-20 12:20:20 +000096 REGISTER_FP32_NEON(arm_compute::cpu::common_neon_scale<float>)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000097 },
98 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010099 "neon_qu8_scale",
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000100 [](const DataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8; },
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000101 REGISTER_QASYMM8_NEON(arm_compute::cpu::qasymm8_neon_scale)
102 },
103 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100104 "neon_qs8_scale",
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000105 [](const DataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000106 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::qasymm8_signed_neon_scale)
107 },
108 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100109 "neon_u8_scale",
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000110 [](const DataTypeISASelectorData & data) { return data.dt == DataType::U8; },
Gian Marco Iodice8b8405a2021-10-01 17:48:02 +0100111 REGISTER_INTEGER_NEON(arm_compute::cpu::u8_neon_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000112 },
113 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100114 "neon_s16_scale",
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000115 [](const DataTypeISASelectorData & data) { return data.dt == DataType::S16; },
Gian Marco Iodice8b8405a2021-10-01 17:48:02 +0100116 REGISTER_INTEGER_NEON(arm_compute::cpu::s16_neon_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000117 },
Michalis Spyrou20fca522021-06-07 14:23:57 +0100118#endif /* defined(ARM_COMPUTE_ENABLE_NEON) */
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000119};
120
Manuel Bottini10b38262021-02-19 18:16:44 +0000121Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy,
122 const ITensorInfo *offsets, ITensorInfo *dst, const ScaleKernelInfo &info)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100123{
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000124 const auto *uk = CpuScaleKernel::get_implementation(DataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa() });
125
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000126 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
127
Manuel Bottini10b38262021-02-19 18:16:44 +0000128 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(dst);
129 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
130 ARM_COMPUTE_RETURN_ERROR_ON(dst == src);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100131 ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100132 ARM_COMPUTE_UNUSED(info.constant_border_value);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100133 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.use_padding, "Padding is not supported");
Georgios Pinitas20b43132018-05-14 16:05:23 +0100134
Manuel Bottini10b38262021-02-19 18:16:44 +0000135 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000136 const auto width_index = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
137 const auto height_index = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Manuel Bottini10b38262021-02-19 18:16:44 +0000138 const auto output_width = dst->dimension(width_index);
139 const auto output_height = dst->dimension(height_index);
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000140 ARM_COMPUTE_RETURN_ERROR_ON(output_width == 0);
141 ARM_COMPUTE_RETURN_ERROR_ON(output_height == 0);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100142
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100143 if(info.interpolation_policy == InterpolationPolicy::NEAREST_NEIGHBOR)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100144 {
145 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32);
146 }
147
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100148 if(info.interpolation_policy == InterpolationPolicy::BILINEAR)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100149 {
150 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32);
Michalis Spyrou7f8caf72021-05-13 13:35:30 +0100151 if(dx != nullptr && dy != nullptr)
152 {
153 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dx, 1, DataType::F32);
154 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dy, 1, DataType::F32);
155 }
Sang-Hoon Parkf025a772020-05-26 11:11:32 +0100156 }
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000157
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100158 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 +0100159
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100160 if(info.interpolation_policy == InterpolationPolicy::AREA)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100161 {
162 ARM_COMPUTE_RETURN_ERROR_ON(data_layout != DataLayout::NCHW);
Manuel Bottini10b38262021-02-19 18:16:44 +0000163 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::U8);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100164 }
165
166 return Status{};
167}
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100168} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169
Manuel Bottini10b38262021-02-19 18:16:44 +0000170void CpuScaleKernel::configure(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy, const ITensorInfo *offsets,
171 ITensorInfo *dst, const ScaleKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172{
Manuel Bottini10b38262021-02-19 18:16:44 +0000173 ARM_COMPUTE_UNUSED(dx, dy, offsets);
174 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100175 // Perform validation step
Manuel Bottini10b38262021-02-19 18:16:44 +0000176 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src,
177 dx,
178 dy,
179 offsets,
180 dst,
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100181 info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000183 const auto *uk = CpuScaleKernel::get_implementation(DataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa() });
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100184 ARM_COMPUTE_ERROR_ON_NULLPTR(uk);
185
186 _run_method = uk->ukernel;
187 _name = std::string("CpuScaleKernel").append("/").append(uk->name).append("_").append(string_from_interpolation_policy(info.interpolation_policy));
188
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100189 // Get data layout and width/height indices
Manuel Bottini10b38262021-02-19 18:16:44 +0000190 _data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000191 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
192 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100194 _policy = info.interpolation_policy;
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100195 _border_mode = info.border_mode;
196 _constant_border_value = info.constant_border_value;
Michele Di Giorgio7a81d2a2020-07-30 14:52:16 +0100197 _align_corners = info.align_corners;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100198
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100199 if(info.sampling_policy == SamplingPolicy::CENTER)
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000200 {
201 _sampling_offset = 0.5f;
202 }
203
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100204 // Compute the ratio between source width/height and destination width/height
Manuel Bottini10b38262021-02-19 18:16:44 +0000205 const auto wr = scale_utils::calculate_resize_ratio(src->dimension(idx_width), dst->dimension(idx_width), _align_corners);
206 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 +0100207
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100208 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000209 _policy = (_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) ? InterpolationPolicy::NEAREST_NEIGHBOR : _policy;
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100210
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100211 if(_border_mode == BorderMode::UNDEFINED)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212 {
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100213 _border_mode = BorderMode::CONSTANT;
214 _constant_border_value = PixelValue();
215 }
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100216
Manuel Bottini10b38262021-02-19 18:16:44 +0000217#ifdef ENABLE_NCHW_KERNELS
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000218 // Configure scale function to run
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000219 if(_data_layout == DataLayout::NCHW)
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100220 {
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000221 std::string function_to_call("scale_");
Manuel Bottini10b38262021-02-19 18:16:44 +0000222 function_to_call += string_from_data_type(src->data_type()) + "_";
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000223 function_to_call += string_from_data_layout(_data_layout) + "_";
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000224 function_to_call += string_from_interpolation_policy(_policy);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100225
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000226 static std::map<std::string, ScaleFunctionPtr> map_function =
227 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000228 { "scale_U8_NCHW_AREA_CONSTANT", &CpuScaleKernel::scale_area_nchw_u8 },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100229
Manuel Bottini10b38262021-02-19 18:16:44 +0000230 { "scale_U8_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<uint8_t> },
231 { "scale_U8_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<uint8_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100232
Manuel Bottini10b38262021-02-19 18:16:44 +0000233 { "scale_QASYMM8_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_qasymm<uint8_t> },
234 { "scale_QASYMM8_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<uint8_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100235
Manuel Bottini10b38262021-02-19 18:16:44 +0000236 { "scale_QASYMM8_SIGNED_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_qasymm<int8_t> },
237 { "scale_QASYMM8_SIGNED_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<int8_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100238
Manuel Bottini10b38262021-02-19 18:16:44 +0000239 { "scale_S16_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<int16_t> },
240 { "scale_S16_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<int16_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100241
242#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Manuel Bottini10b38262021-02-19 18:16:44 +0000243 { "scale_F16_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<float16_t> },
244 { "scale_F16_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<float16_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100245#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
246
Manuel Bottini10b38262021-02-19 18:16:44 +0000247 { "scale_F32_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<float> },
248 { "scale_F32_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<float> },
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000249 };
250 auto it = map_function.find(function_to_call);
251 if(it != map_function.end())
252 {
253 _func = it->second;
254 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100255 }
Manuel Bottini10b38262021-02-19 18:16:44 +0000256#endif // ENABLE_NCHW_KERNELS
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100257
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100258 // Configure window
Manuel Bottini10b38262021-02-19 18:16:44 +0000259 Window win = calculate_max_window(*dst, Steps());
260 ICpuKernel::configure(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100261}
262
Manuel Bottini10b38262021-02-19 18:16:44 +0000263#ifdef ENABLE_NCHW_KERNELS
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100264template <typename T>
Manuel Bottini10b38262021-02-19 18:16:44 +0000265void 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 +0100266{
Manuel Bottini10b38262021-02-19 18:16:44 +0000267 ARM_COMPUTE_UNUSED(dx, dy);
268 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 +0100269
270 // Compute the ratio between source height and destination height
Manuel Bottini10b38262021-02-19 18:16:44 +0000271 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 +0100272
273 // Don't increment in X and Y direction for the input tensor
274 // A pointer to the start of this plane is needed as base for the precomputed offsets
275 Window win_in(window);
276 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
277 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
278
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100279 // Set offsets window
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100280 Window win_off;
281 win_off.set(Window::DimX, window[Window::DimX]);
282 win_off.set(Window::DimY, window[Window::DimY]);
Manuel Bottini10b38262021-02-19 18:16:44 +0000283 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100284 {
285 win_off.set(d, Window::Dimension(0, 0, 0));
286 }
287
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100288 // Create iterators
Manuel Bottini10b38262021-02-19 18:16:44 +0000289 Iterator src_i(src, win_in);
290 Iterator dst_i(dst, window);
291 Iterator offsets_i(offsets, win_off);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100292 execute_window_loop(window, [&](const Coordinates & id)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100293 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000294 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets_i.ptr());
295 const auto in_yi = static_cast<int32_t>(_align_corners ? utils::rounding::round_half_away_from_zero((id.y() + _sampling_offset) * hr) : std::floor((
296 id.y() + _sampling_offset)
297 * hr));
298 const int32_t offset_row = in_yi * in_stride_x;
299 *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 +0100300 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000301 src_i, offsets_i, dst_i);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100302}
303
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100304template <typename T>
Manuel Bottini10b38262021-02-19 18:16:44 +0000305void 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 +0100306{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100307 // Compute the ratio between source height and destination height
Manuel Bottini10b38262021-02-19 18:16:44 +0000308 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 +0100309 Window win_off;
310 win_off.set(Window::DimX, window.x());
311 win_off.set(Window::DimY, window.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100312
313 // Don't increment in X and Y direction for the input tensor
314 // A pointer to the start of this plane is needed as base for the precomputed offsets
315 Window win_in(window);
316 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
317 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
318
Manuel Bottini10b38262021-02-19 18:16:44 +0000319 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100320 {
321 win_off.set(d, Window::Dimension(0, 0, 0));
322 }
323
Manuel Bottini10b38262021-02-19 18:16:44 +0000324 Iterator src_i(src, win_in);
325 Iterator dst_i(dst, window);
326 Iterator offsets_i(offsets, win_off);
327 Iterator dx_i(dx, win_off);
328 Iterator dy_i(dy, win_off);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100329
Manuel Bottini10b38262021-02-19 18:16:44 +0000330 const int32_t in_dim_w = src->info()->dimension(0);
331 const int32_t in_dim_h = src->info()->dimension(1);
332 const int32_t in_stride_w = in_dim_w + src->info()->padding().left + src->info()->padding().right;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100333
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100334 if(_border_mode == BorderMode::CONSTANT)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100335 {
Georgios Pinitasc47ef202018-11-16 18:19:43 +0000336#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100337 using ConstType = typename std::conditional<std::is_same<T, float16_t>::value, half, T>::type;
338#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
339 using ConstType = T;
Georgios Pinitasc47ef202018-11-16 18:19:43 +0000340#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100341 const T const_border_value = static_cast<T>(_constant_border_value.get<ConstType>());
342 execute_window_loop(window, [&](const Coordinates & id)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100343 {
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100344 const int32_t index_h = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
Manuel Bottini10b38262021-02-19 18:16:44 +0000345 const auto index_w = *(reinterpret_cast<const int32_t *>(offsets_i.ptr()));
346 const auto dx_val = *(reinterpret_cast<const float *>(dx_i.ptr()));
347 const auto dy_val = *(reinterpret_cast<const float *>(dy_i.ptr()));
348 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
Georgios Pinitas583137c2017-08-31 18:12:42 +0100349
Manuel Bottini49682852020-10-13 10:25:31 +0100350 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;
351 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 +0100352 const auto a10 = (0 <= index_w && index_w < in_dim_w && -1 <= index_h
353 && index_h < in_dim_h - 1) ?
Manuel Bottini49682852020-10-13 10:25:31 +0100354 (*(pixel_row_ptr + index_w + index_h * in_stride_w + in_stride_w)) :
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100355 const_border_value;
356 const auto a11 = (-1 <= index_w && index_w < in_dim_w - 1 && -1 <= index_h
357 && index_h < in_dim_h - 1) ?
Manuel Bottini49682852020-10-13 10:25:31 +0100358 (*(pixel_row_ptr + index_w + 1 + index_h * in_stride_w + in_stride_w)) :
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100359 const_border_value;
Georgios Pinitas583137c2017-08-31 18:12:42 +0100360
Manuel Bottini10b38262021-02-19 18:16:44 +0000361 *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 +0100362 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000363 src_i, offsets_i, dx_i, dy_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100364 }
365 else if(_border_mode == BorderMode::REPLICATE)
366 {
367 execute_window_loop(window, [&](const Coordinates & id)
368 {
369 const int index_h = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
Manuel Bottini10b38262021-02-19 18:16:44 +0000370 const auto index_w = *(reinterpret_cast<const int32_t *>(offsets_i.ptr()));
371 const auto dx_val = *(reinterpret_cast<const float *>(dx_i.ptr()));
372 const auto dy_val = *(reinterpret_cast<const float *>(dy_i.ptr()));
373 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
Georgios Pinitas583137c2017-08-31 18:12:42 +0100374
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100375 auto clamped_x = utility::clamp<int>(index_w, 0, in_dim_w - 1);
376 auto clamped_x1 = utility::clamp<int>(index_w + 1, 0, in_dim_w - 1);
377 auto clamped_y = utility::clamp<int>(index_h, 0, in_dim_h - 1);
378 auto clamped_y1 = utility::clamp<int>(index_h + 1, 0, in_dim_h - 1);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100379
Manuel Bottini49682852020-10-13 10:25:31 +0100380 const auto a00 = *(pixel_row_ptr + clamped_x + clamped_y * in_stride_w);
381 const auto a01 = *(pixel_row_ptr + clamped_x1 + clamped_y * in_stride_w);
382 const auto a10 = *(pixel_row_ptr + clamped_x + clamped_y1 * in_stride_w);
383 const auto a11 = *(pixel_row_ptr + clamped_x1 + clamped_y1 * in_stride_w);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100384
Manuel Bottini10b38262021-02-19 18:16:44 +0000385 *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 +0100386 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000387 src_i, offsets_i, dx_i, dy_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100388 }
389 else
390 {
391 ARM_COMPUTE_ERROR("Not implemented");
Georgios Pinitas583137c2017-08-31 18:12:42 +0100392 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100393}
394
Manuel Bottini10b38262021-02-19 18:16:44 +0000395void 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 +0100396{
Manuel Bottini10b38262021-02-19 18:16:44 +0000397 ARM_COMPUTE_UNUSED(dx, dy, offsets);
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100398 using namespace scale_helpers;
399
Manuel Bottini10b38262021-02-19 18:16:44 +0000400 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100401
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100402 // Don't increment in width/height/channels for the input tensor
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100403 // A pointer to the start of this plane is needed as base for the precomputed offsets
404 Window win_in(window);
405 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
406 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100407 win_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100408
Manuel Bottini10b38262021-02-19 18:16:44 +0000409 Iterator src_i(src, win_in);
410 Iterator dst_i(dst, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100411
Manuel Bottini10b38262021-02-19 18:16:44 +0000412 const auto wr = scale_utils::calculate_resize_ratio(src->info()->dimension(0), dst->info()->dimension(0), _align_corners);
413 const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), _align_corners);
414 const auto w = src->info()->dimension(0);
415 const auto h = src->info()->dimension(1);
416 const size_t in_stride = src->info()->strides_in_bytes()[1];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100417
418 execute_window_loop(window, [&](const Coordinates & id)
419 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000420 const auto in_ptr = reinterpret_cast<const uint8_t *>(src_i.ptr());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100421
422 uint8x8_t tmp0 = vdup_n_u8(0);
423 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x(), id.y()), tmp0, 0);
424 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 1, id.y()), tmp0, 1);
425 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 2, id.y()), tmp0, 2);
426 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 3, id.y()), tmp0, 3);
427 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 4, id.y()), tmp0, 4);
428 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 5, id.y()), tmp0, 5);
429 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 6, id.y()), tmp0, 6);
430 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 7, id.y()), tmp0, 7);
431
432 uint8x8_t tmp1 = vdup_n_u8(0);
433 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 8, id.y()), tmp1, 0);
434 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 9, id.y()), tmp1, 1);
435 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 10, id.y()), tmp1, 2);
436 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 11, id.y()), tmp1, 3);
437 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 12, id.y()), tmp1, 4);
438 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 13, id.y()), tmp1, 5);
439 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 14, id.y()), tmp1, 6);
440 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 15, id.y()), tmp1, 7);
441
Manuel Bottini10b38262021-02-19 18:16:44 +0000442 vst1q_u8(dst_i.ptr(), vcombine_u8(tmp0, tmp1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100443 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000444 src_i, dst_i);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100445}
446
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100447template <typename T>
Manuel Bottini10b38262021-02-19 18:16:44 +0000448void 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 +0100449{
450 // Get data layout and width/height indices
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000451 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
452 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100453
454 // Compute the ratio between source height and destination height
Manuel Bottini10b38262021-02-19 18:16:44 +0000455 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 +0100456 Window win_off;
457 win_off.set(Window::DimX, Window::Dimension(0, 0, 0));
458 win_off.set(Window::DimY, Window::Dimension(0, 0, 0));
459
460 // Don't increment in X and Y direction for the input tensor
461 // A pointer to the start of this plane is needed as base for the precomputed offsets
462 Window win_in(window);
463 win_in.set(idx_width, Window::Dimension(0, 0, 0));
464 win_in.set(idx_height, Window::Dimension(0, 0, 0));
465
Manuel Bottini10b38262021-02-19 18:16:44 +0000466 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100467 {
468 win_off.set(d, Window::Dimension(0, 0, 0));
469 }
470
Manuel Bottini10b38262021-02-19 18:16:44 +0000471 Iterator src_i(src, win_in);
472 Iterator dst_i(dst, window);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100473
Manuel Bottini10b38262021-02-19 18:16:44 +0000474 const int32_t in_dim_w = src->info()->dimension(idx_width);
475 const int32_t in_dim_h = src->info()->dimension(idx_height);
476 const int32_t stride_w = src->info()->strides_in_bytes()[idx_width];
477 const int32_t stride_h = src->info()->strides_in_bytes()[idx_height];
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100478
Manuel Bottini10b38262021-02-19 18:16:44 +0000479 const UniformQuantizationInfo iq_info = src->info()->quantization_info().uniform();
480 const UniformQuantizationInfo oq_info = dst->info()->quantization_info().uniform();
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100481
482 if(_border_mode == BorderMode::CONSTANT)
483 {
484#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
485 using ConstType = typename std::conditional<std::is_same<T, float16_t>::value, half, T>::type;
486#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
487 using ConstType = T;
488#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
489 const T const_border_value = static_cast<T>(_constant_border_value.get<ConstType>());
490 execute_window_loop(window, [&](const Coordinates & id)
491 {
492 const int32_t index_h = std::floor((id[idx_height] + _sampling_offset) * hr - _sampling_offset);
Manuel Bottini10b38262021-02-19 18:16:44 +0000493 const int32_t index_w = *(reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
494 const auto dx_val = *(reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
495 const auto dy_val = *(reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
496 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100497
498 const auto a00 = (0 <= index_w && index_w < in_dim_w && 0 <= index_h && index_h < in_dim_h) ?
499 (*(pixel_row_ptr + index_w * stride_w + index_h * stride_h)) :
500 const_border_value;
501 const auto a01 = (-1 <= index_w && index_w < in_dim_w - 1 && 0 <= index_h && index_h < in_dim_h) ?
502 (*(pixel_row_ptr + (index_w + 1) * stride_w + index_h * stride_h)) :
503 const_border_value;
504 const auto a10 = (0 <= index_w && index_w < in_dim_w && -1 <= index_h && index_h < in_dim_h - 1) ?
505 (*(pixel_row_ptr + index_w * stride_w + (index_h + 1) * stride_h)) :
506 const_border_value;
507 const auto a11 = (-1 <= index_w && index_w < in_dim_w - 1 && -1 <= index_h && index_h < in_dim_h - 1) ?
508 (*(pixel_row_ptr + (index_w + 1) * stride_w + (index_h + 1) * stride_h)) :
509 const_border_value;
510
Manuel Bottini10b38262021-02-19 18:16:44 +0000511 const float inp00 = Qasymm8QuantizationHelper<T>::dequantize(a00, iq_info);
512 const float inp01 = Qasymm8QuantizationHelper<T>::dequantize(a01, iq_info);
513 const float inp10 = Qasymm8QuantizationHelper<T>::dequantize(a10, iq_info);
514 const float inp11 = Qasymm8QuantizationHelper<T>::dequantize(a11, iq_info);
515 *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 +0100516 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000517 src_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100518 }
519 else if(_border_mode == BorderMode::REPLICATE)
520 {
521 execute_window_loop(window, [&](const Coordinates & id)
522 {
523 const int index_h = std::floor((id[idx_height] + _sampling_offset) * hr - _sampling_offset);
Manuel Bottini10b38262021-02-19 18:16:44 +0000524 const int32_t index_w = *(reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
525 const auto dx_val = *(reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
526 const auto dy_val = *(reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
527 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100528
529 auto clamped_w = utility::clamp<int>(index_w, 0, in_dim_w - 1);
530 auto clamped_w1 = utility::clamp<int>(index_w + 1, 0, in_dim_w - 1);
531 auto clamped_h = utility::clamp<int>(index_h, 0, in_dim_h - 1);
532 auto clamped_h1 = utility::clamp<int>(index_h + 1, 0, in_dim_h - 1);
533
534 const auto a00 = *(pixel_row_ptr + clamped_w * stride_w + clamped_h * stride_h);
535 const auto a01 = *(pixel_row_ptr + clamped_w1 * stride_w + clamped_h * stride_h);
536 const auto a10 = *(pixel_row_ptr + clamped_w * stride_w + clamped_h1 * stride_h);
537 const auto a11 = *(pixel_row_ptr + clamped_w1 * stride_w + clamped_h1 * stride_h);
538
Manuel Bottini10b38262021-02-19 18:16:44 +0000539 const float inp00 = Qasymm8QuantizationHelper<T>::dequantize(a00, iq_info);
540 const float inp01 = Qasymm8QuantizationHelper<T>::dequantize(a01, iq_info);
541 const float inp10 = Qasymm8QuantizationHelper<T>::dequantize(a10, iq_info);
542 const float inp11 = Qasymm8QuantizationHelper<T>::dequantize(a11, iq_info);
543 *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 +0100544 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000545 src_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100546 }
547 else
548 {
549 ARM_COMPUTE_ERROR("Not implemented");
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100550 }
551}
Manuel Bottini10b38262021-02-19 18:16:44 +0000552#endif // ENABLE_NCHW_KERNELS
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100553
Manuel Bottini10b38262021-02-19 18:16:44 +0000554Status CpuScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *dx, const ITensorInfo *dy,
555 const ITensorInfo *offsets, ITensorInfo *output, const ScaleKernelInfo &info)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100556{
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100557 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, dx, dy, offsets, output, info));
Georgios Pinitas20b43132018-05-14 16:05:23 +0100558 return Status{};
559}
560
Manuel Bottini10b38262021-02-19 18:16:44 +0000561void CpuScaleKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100562{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100563 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100564 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottini10b38262021-02-19 18:16:44 +0000565 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000566 ARM_COMPUTE_ERROR_ON(_func == nullptr && _data_layout == DataLayout::NCHW);
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100567 ARM_COMPUTE_ERROR_ON(_run_method == nullptr && _data_layout == DataLayout::NHWC);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100568
Manuel Bottini10b38262021-02-19 18:16:44 +0000569 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
570 auto dst = tensors.get_tensor(TensorType::ACL_DST);
571 const auto dx = tensors.get_const_tensor(TensorType::ACL_INT_0);
572 const auto dy = tensors.get_const_tensor(TensorType::ACL_INT_1);
573 const auto offsets = tensors.get_const_tensor(TensorType::ACL_INT_2);
574
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000575 if(_data_layout == DataLayout::NCHW)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000576 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000577 (this->*_func)(src, dst, dx, dy, offsets, window);
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000578 }
579 else
580 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100581 _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 +0000582 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100583}
Manuel Bottini10b38262021-02-19 18:16:44 +0000584
585const char *CpuScaleKernel::name() const
586{
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100587 return _name.c_str();
Manuel Bottini10b38262021-02-19 18:16:44 +0000588}
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000589
590const std::vector<CpuScaleKernel::ScaleKernel> &CpuScaleKernel::get_available_kernels()
591{
592 return available_kernels;
593}
594
Manuel Bottini10b38262021-02-19 18:16:44 +0000595} // namespace kernels
596} // namespace cpu
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100597} // namespace arm_compute