blob: 3063d8f682cc78828e411d1dcea893fa3d2f3cb7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sheri Zhang23adc4c2021-01-05 12:48:45 +00002 * Copyright (c) 2016-2021 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{
Sheri Zhang23adc4c2021-01-05 12:48:45 +000051struct ScaleSelectorData
Manuel Bottinifc2f6d02020-08-26 16:28:38 +010052{
Michalis Spyrou20fca522021-06-07 14:23:57 +010053 DataType dt;
54 const CPUInfo &ci;
Sheri Zhang23adc4c2021-01-05 12:48:45 +000055};
56using ScaleSelectorPtr = std::add_pointer<bool(const ScaleSelectorData &data)>::type;
57using ScaleKernelPtr = std::add_pointer<void(const ITensor *, ITensor *, const ITensor *, const ITensor *, const ITensor *,
58 InterpolationPolicy, BorderMode, PixelValue, float, bool, const Window &)>::type;
59struct ScaleKernel
60{
61 const char *name;
62 const ScaleSelectorPtr is_selected;
63 ScaleKernelPtr ukernel;
64};
Manuel Bottinifc2f6d02020-08-26 16:28:38 +010065
Sheri Zhang23adc4c2021-01-05 12:48:45 +000066static const ScaleKernel available_kernels[] =
67{
Michalis Spyrou20fca522021-06-07 14:23:57 +010068#if defined(ARM_COMPUTE_ENABLE_SVE)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000069 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010070 "sve_fp16_scale",
Michalis Spyrou20fca522021-06-07 14:23:57 +010071 [](const ScaleSelectorData & data) { return data.dt == DataType::F16 && data.ci.has_sve(); },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010072 REGISTER_FP16_SVE(arm_compute::cpu::fp16_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000073 },
74 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010075 "sve_fp32_scale",
Michalis Spyrou20fca522021-06-07 14:23:57 +010076 [](const ScaleSelectorData & data) { return data.dt == DataType::F32 && data.ci.has_sve(); },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010077 REGISTER_FP32_SVE(arm_compute::cpu::fp32_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000078 },
79 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010080 "sve_qu8_scale",
Michalis Spyrou20fca522021-06-07 14:23:57 +010081 [](const ScaleSelectorData & data) { return data.dt == DataType::QASYMM8 && data.ci.has_sve(); },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010082 REGISTER_QASYMM8_SVE(arm_compute::cpu::qasymm8_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000083 },
84 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010085 "sve_qs8_scale",
Michalis Spyrou20fca522021-06-07 14:23:57 +010086 [](const ScaleSelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED && data.ci.has_sve(); },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010087 REGISTER_QASYMM8_SIGNED_SVE(arm_compute::cpu::qasymm8_signed_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000088 },
89 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010090 "sve_u8_scale",
Michalis Spyrou20fca522021-06-07 14:23:57 +010091 [](const ScaleSelectorData & data) { return data.dt == DataType::U8 && data.ci.has_sve(); },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010092 REGISTER_INTEGER_SVE(arm_compute::cpu::u8_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000093 },
94 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010095 "sve_s16_scale",
Michalis Spyrou20fca522021-06-07 14:23:57 +010096 [](const ScaleSelectorData & data) { return data.dt == DataType::S16 && data.ci.has_sve(); },
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010097 REGISTER_INTEGER_SVE(arm_compute::cpu::s16_sve_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +000098 },
Michalis Spyrou20fca522021-06-07 14:23:57 +010099#endif /* defined(ARM_COMPUTE_ENABLE_SVE) */
100#if defined(ARM_COMPUTE_ENABLE_NEON)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000101#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
102 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100103 "neon_fp16_scale",
Michalis Spyrou20fca522021-06-07 14:23:57 +0100104 [](const ScaleSelectorData & data) { return data.dt == DataType::F16 && data.ci.has_fp16(); },
Sheri Zhang360f5762021-01-20 12:20:20 +0000105 REGISTER_FP16_NEON(arm_compute::cpu::common_neon_scale<float16_t>)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000106 },
107#endif /* !defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) */
108 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100109 "neon_fp32_scale",
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000110 [](const ScaleSelectorData & data) { return data.dt == DataType::F32; },
Sheri Zhang360f5762021-01-20 12:20:20 +0000111 REGISTER_FP32_NEON(arm_compute::cpu::common_neon_scale<float>)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000112 },
113 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100114 "neon_qu8_scale",
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000115 [](const ScaleSelectorData & data) { return data.dt == DataType::QASYMM8; },
116 REGISTER_QASYMM8_NEON(arm_compute::cpu::qasymm8_neon_scale)
117 },
118 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100119 "neon_qs8_scale",
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000120 [](const ScaleSelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
121 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::qasymm8_signed_neon_scale)
122 },
123 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100124 "neon_u8_scale",
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000125 [](const ScaleSelectorData & data) { return data.dt == DataType::U8; },
Gian Marco Iodice8b8405a2021-10-01 17:48:02 +0100126 REGISTER_INTEGER_NEON(arm_compute::cpu::u8_neon_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000127 },
128 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100129 "neon_s16_scale",
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000130 [](const ScaleSelectorData & data) { return data.dt == DataType::S16; },
Gian Marco Iodice8b8405a2021-10-01 17:48:02 +0100131 REGISTER_INTEGER_NEON(arm_compute::cpu::s16_neon_scale)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000132 },
Michalis Spyrou20fca522021-06-07 14:23:57 +0100133#endif /* defined(ARM_COMPUTE_ENABLE_NEON) */
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000134};
135
136/** Micro-kernel selector
137 *
138 * @param[in] data Selection data passed to help pick the appropriate micro-kernel
139 *
140 * @return A matching micro-kernel else nullptr
141 */
142const ScaleKernel *get_implementation(const ScaleSelectorData &data)
143{
144 for(const auto &uk : available_kernels)
145 {
146 if(uk.is_selected(data))
147 {
148 return &uk;
149 }
150 }
151 return nullptr;
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100152}
153
Manuel Bottini10b38262021-02-19 18:16:44 +0000154Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy,
155 const ITensorInfo *offsets, ITensorInfo *dst, const ScaleKernelInfo &info)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100156{
Michalis Spyrou20fca522021-06-07 14:23:57 +0100157 const auto *uk = get_implementation(ScaleSelectorData{ src->data_type(), CPUInfo::get() });
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000158 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
159
Manuel Bottini10b38262021-02-19 18:16:44 +0000160 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(dst);
161 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
162 ARM_COMPUTE_RETURN_ERROR_ON(dst == src);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100163 ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100164 ARM_COMPUTE_UNUSED(info.constant_border_value);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100165 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.use_padding, "Padding is not supported");
Georgios Pinitas20b43132018-05-14 16:05:23 +0100166
Manuel Bottini10b38262021-02-19 18:16:44 +0000167 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000168 const auto width_index = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
169 const auto height_index = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Manuel Bottini10b38262021-02-19 18:16:44 +0000170 const auto output_width = dst->dimension(width_index);
171 const auto output_height = dst->dimension(height_index);
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000172 ARM_COMPUTE_RETURN_ERROR_ON(output_width == 0);
173 ARM_COMPUTE_RETURN_ERROR_ON(output_height == 0);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100174
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100175 if(info.interpolation_policy == InterpolationPolicy::NEAREST_NEIGHBOR)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100176 {
177 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32);
178 }
179
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100180 if(info.interpolation_policy == InterpolationPolicy::BILINEAR)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100181 {
182 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(offsets, 1, DataType::S32);
Michalis Spyrou7f8caf72021-05-13 13:35:30 +0100183 if(dx != nullptr && dy != nullptr)
184 {
185 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dx, 1, DataType::F32);
186 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dy, 1, DataType::F32);
187 }
Sang-Hoon Parkf025a772020-05-26 11:11:32 +0100188 }
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000189
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100190 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 +0100191
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100192 if(info.interpolation_policy == InterpolationPolicy::AREA)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100193 {
194 ARM_COMPUTE_RETURN_ERROR_ON(data_layout != DataLayout::NCHW);
Manuel Bottini10b38262021-02-19 18:16:44 +0000195 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::U8);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100196 }
197
198 return Status{};
199}
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100200} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201
Manuel Bottini10b38262021-02-19 18:16:44 +0000202void CpuScaleKernel::configure(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy, const ITensorInfo *offsets,
203 ITensorInfo *dst, const ScaleKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204{
Manuel Bottini10b38262021-02-19 18:16:44 +0000205 ARM_COMPUTE_UNUSED(dx, dy, offsets);
206 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100207 // Perform validation step
Manuel Bottini10b38262021-02-19 18:16:44 +0000208 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src,
209 dx,
210 dy,
211 offsets,
212 dst,
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100213 info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100215 const auto *uk = get_implementation(ScaleSelectorData{ src->data_type(), CPUInfo::get() });
216 ARM_COMPUTE_ERROR_ON_NULLPTR(uk);
217
218 _run_method = uk->ukernel;
219 _name = std::string("CpuScaleKernel").append("/").append(uk->name).append("_").append(string_from_interpolation_policy(info.interpolation_policy));
220
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100221 // Get data layout and width/height indices
Manuel Bottini10b38262021-02-19 18:16:44 +0000222 _data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000223 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
224 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100226 _policy = info.interpolation_policy;
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100227 _border_mode = info.border_mode;
228 _constant_border_value = info.constant_border_value;
Michele Di Giorgio7a81d2a2020-07-30 14:52:16 +0100229 _align_corners = info.align_corners;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100230
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100231 if(info.sampling_policy == SamplingPolicy::CENTER)
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000232 {
233 _sampling_offset = 0.5f;
234 }
235
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100236 // Compute the ratio between source width/height and destination width/height
Manuel Bottini10b38262021-02-19 18:16:44 +0000237 const auto wr = scale_utils::calculate_resize_ratio(src->dimension(idx_width), dst->dimension(idx_width), _align_corners);
238 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 +0100239
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100240 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000241 _policy = (_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) ? InterpolationPolicy::NEAREST_NEIGHBOR : _policy;
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100242
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100243 if(_border_mode == BorderMode::UNDEFINED)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100244 {
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100245 _border_mode = BorderMode::CONSTANT;
246 _constant_border_value = PixelValue();
247 }
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100248
Manuel Bottini10b38262021-02-19 18:16:44 +0000249#ifdef ENABLE_NCHW_KERNELS
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000250 // Configure scale function to run
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000251 if(_data_layout == DataLayout::NCHW)
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100252 {
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000253 std::string function_to_call("scale_");
Manuel Bottini10b38262021-02-19 18:16:44 +0000254 function_to_call += string_from_data_type(src->data_type()) + "_";
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000255 function_to_call += string_from_data_layout(_data_layout) + "_";
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000256 function_to_call += string_from_interpolation_policy(_policy);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100257
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000258 static std::map<std::string, ScaleFunctionPtr> map_function =
259 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000260 { "scale_U8_NCHW_AREA_CONSTANT", &CpuScaleKernel::scale_area_nchw_u8 },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100261
Manuel Bottini10b38262021-02-19 18:16:44 +0000262 { "scale_U8_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<uint8_t> },
263 { "scale_U8_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<uint8_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100264
Manuel Bottini10b38262021-02-19 18:16:44 +0000265 { "scale_QASYMM8_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_qasymm<uint8_t> },
266 { "scale_QASYMM8_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<uint8_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100267
Manuel Bottini10b38262021-02-19 18:16:44 +0000268 { "scale_QASYMM8_SIGNED_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_qasymm<int8_t> },
269 { "scale_QASYMM8_SIGNED_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<int8_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100270
Manuel Bottini10b38262021-02-19 18:16:44 +0000271 { "scale_S16_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<int16_t> },
272 { "scale_S16_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<int16_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100273
274#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Manuel Bottini10b38262021-02-19 18:16:44 +0000275 { "scale_F16_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<float16_t> },
276 { "scale_F16_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<float16_t> },
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100277#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
278
Manuel Bottini10b38262021-02-19 18:16:44 +0000279 { "scale_F32_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<float> },
280 { "scale_F32_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<float> },
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000281 };
282 auto it = map_function.find(function_to_call);
283 if(it != map_function.end())
284 {
285 _func = it->second;
286 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100287 }
Manuel Bottini10b38262021-02-19 18:16:44 +0000288#endif // ENABLE_NCHW_KERNELS
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100289
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100290 // Configure window
Manuel Bottini10b38262021-02-19 18:16:44 +0000291 Window win = calculate_max_window(*dst, Steps());
292 ICpuKernel::configure(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100293}
294
Manuel Bottini10b38262021-02-19 18:16:44 +0000295#ifdef ENABLE_NCHW_KERNELS
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100296template <typename T>
Manuel Bottini10b38262021-02-19 18:16:44 +0000297void 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 +0100298{
Manuel Bottini10b38262021-02-19 18:16:44 +0000299 ARM_COMPUTE_UNUSED(dx, dy);
300 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 +0100301
302 // Compute the ratio between source height and destination height
Manuel Bottini10b38262021-02-19 18:16:44 +0000303 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 +0100304
305 // Don't increment in X and Y direction for the input tensor
306 // A pointer to the start of this plane is needed as base for the precomputed offsets
307 Window win_in(window);
308 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
309 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
310
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100311 // Set offsets window
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100312 Window win_off;
313 win_off.set(Window::DimX, window[Window::DimX]);
314 win_off.set(Window::DimY, window[Window::DimY]);
Manuel Bottini10b38262021-02-19 18:16:44 +0000315 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100316 {
317 win_off.set(d, Window::Dimension(0, 0, 0));
318 }
319
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100320 // Create iterators
Manuel Bottini10b38262021-02-19 18:16:44 +0000321 Iterator src_i(src, win_in);
322 Iterator dst_i(dst, window);
323 Iterator offsets_i(offsets, win_off);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100324 execute_window_loop(window, [&](const Coordinates & id)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100325 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000326 const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets_i.ptr());
327 const auto in_yi = static_cast<int32_t>(_align_corners ? utils::rounding::round_half_away_from_zero((id.y() + _sampling_offset) * hr) : std::floor((
328 id.y() + _sampling_offset)
329 * hr));
330 const int32_t offset_row = in_yi * in_stride_x;
331 *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 +0100332 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000333 src_i, offsets_i, dst_i);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100334}
335
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100336template <typename T>
Manuel Bottini10b38262021-02-19 18:16:44 +0000337void 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 +0100338{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100339 // Compute the ratio between source height and destination height
Manuel Bottini10b38262021-02-19 18:16:44 +0000340 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 +0100341 Window win_off;
342 win_off.set(Window::DimX, window.x());
343 win_off.set(Window::DimY, window.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100344
345 // Don't increment in X and Y direction for the input tensor
346 // A pointer to the start of this plane is needed as base for the precomputed offsets
347 Window win_in(window);
348 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
349 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
350
Manuel Bottini10b38262021-02-19 18:16:44 +0000351 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100352 {
353 win_off.set(d, Window::Dimension(0, 0, 0));
354 }
355
Manuel Bottini10b38262021-02-19 18:16:44 +0000356 Iterator src_i(src, win_in);
357 Iterator dst_i(dst, window);
358 Iterator offsets_i(offsets, win_off);
359 Iterator dx_i(dx, win_off);
360 Iterator dy_i(dy, win_off);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100361
Manuel Bottini10b38262021-02-19 18:16:44 +0000362 const int32_t in_dim_w = src->info()->dimension(0);
363 const int32_t in_dim_h = src->info()->dimension(1);
364 const int32_t in_stride_w = in_dim_w + src->info()->padding().left + src->info()->padding().right;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100366 if(_border_mode == BorderMode::CONSTANT)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100367 {
Georgios Pinitasc47ef202018-11-16 18:19:43 +0000368#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100369 using ConstType = typename std::conditional<std::is_same<T, float16_t>::value, half, T>::type;
370#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
371 using ConstType = T;
Georgios Pinitasc47ef202018-11-16 18:19:43 +0000372#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100373 const T const_border_value = static_cast<T>(_constant_border_value.get<ConstType>());
374 execute_window_loop(window, [&](const Coordinates & id)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100375 {
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100376 const int32_t index_h = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
Manuel Bottini10b38262021-02-19 18:16:44 +0000377 const auto index_w = *(reinterpret_cast<const int32_t *>(offsets_i.ptr()));
378 const auto dx_val = *(reinterpret_cast<const float *>(dx_i.ptr()));
379 const auto dy_val = *(reinterpret_cast<const float *>(dy_i.ptr()));
380 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
Georgios Pinitas583137c2017-08-31 18:12:42 +0100381
Manuel Bottini49682852020-10-13 10:25:31 +0100382 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;
383 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 +0100384 const auto a10 = (0 <= index_w && index_w < in_dim_w && -1 <= index_h
385 && index_h < in_dim_h - 1) ?
Manuel Bottini49682852020-10-13 10:25:31 +0100386 (*(pixel_row_ptr + index_w + index_h * in_stride_w + in_stride_w)) :
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100387 const_border_value;
388 const auto a11 = (-1 <= index_w && index_w < in_dim_w - 1 && -1 <= index_h
389 && index_h < in_dim_h - 1) ?
Manuel Bottini49682852020-10-13 10:25:31 +0100390 (*(pixel_row_ptr + index_w + 1 + index_h * in_stride_w + in_stride_w)) :
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100391 const_border_value;
Georgios Pinitas583137c2017-08-31 18:12:42 +0100392
Manuel Bottini10b38262021-02-19 18:16:44 +0000393 *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 +0100394 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000395 src_i, offsets_i, dx_i, dy_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100396 }
397 else if(_border_mode == BorderMode::REPLICATE)
398 {
399 execute_window_loop(window, [&](const Coordinates & id)
400 {
401 const int index_h = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
Manuel Bottini10b38262021-02-19 18:16:44 +0000402 const auto index_w = *(reinterpret_cast<const int32_t *>(offsets_i.ptr()));
403 const auto dx_val = *(reinterpret_cast<const float *>(dx_i.ptr()));
404 const auto dy_val = *(reinterpret_cast<const float *>(dy_i.ptr()));
405 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
Georgios Pinitas583137c2017-08-31 18:12:42 +0100406
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100407 auto clamped_x = utility::clamp<int>(index_w, 0, in_dim_w - 1);
408 auto clamped_x1 = utility::clamp<int>(index_w + 1, 0, in_dim_w - 1);
409 auto clamped_y = utility::clamp<int>(index_h, 0, in_dim_h - 1);
410 auto clamped_y1 = utility::clamp<int>(index_h + 1, 0, in_dim_h - 1);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100411
Manuel Bottini49682852020-10-13 10:25:31 +0100412 const auto a00 = *(pixel_row_ptr + clamped_x + clamped_y * in_stride_w);
413 const auto a01 = *(pixel_row_ptr + clamped_x1 + clamped_y * in_stride_w);
414 const auto a10 = *(pixel_row_ptr + clamped_x + clamped_y1 * in_stride_w);
415 const auto a11 = *(pixel_row_ptr + clamped_x1 + clamped_y1 * in_stride_w);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100416
Manuel Bottini10b38262021-02-19 18:16:44 +0000417 *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 +0100418 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000419 src_i, offsets_i, dx_i, dy_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100420 }
421 else
422 {
423 ARM_COMPUTE_ERROR("Not implemented");
Georgios Pinitas583137c2017-08-31 18:12:42 +0100424 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100425}
426
Manuel Bottini10b38262021-02-19 18:16:44 +0000427void 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 +0100428{
Manuel Bottini10b38262021-02-19 18:16:44 +0000429 ARM_COMPUTE_UNUSED(dx, dy, offsets);
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100430 using namespace scale_helpers;
431
Manuel Bottini10b38262021-02-19 18:16:44 +0000432 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100433
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100434 // Don't increment in width/height/channels for the input tensor
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100435 // A pointer to the start of this plane is needed as base for the precomputed offsets
436 Window win_in(window);
437 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
438 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100439 win_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100440
Manuel Bottini10b38262021-02-19 18:16:44 +0000441 Iterator src_i(src, win_in);
442 Iterator dst_i(dst, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100443
Manuel Bottini10b38262021-02-19 18:16:44 +0000444 const auto wr = scale_utils::calculate_resize_ratio(src->info()->dimension(0), dst->info()->dimension(0), _align_corners);
445 const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), _align_corners);
446 const auto w = src->info()->dimension(0);
447 const auto h = src->info()->dimension(1);
448 const size_t in_stride = src->info()->strides_in_bytes()[1];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100449
450 execute_window_loop(window, [&](const Coordinates & id)
451 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000452 const auto in_ptr = reinterpret_cast<const uint8_t *>(src_i.ptr());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100453
454 uint8x8_t tmp0 = vdup_n_u8(0);
455 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x(), id.y()), tmp0, 0);
456 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 1, id.y()), tmp0, 1);
457 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 2, id.y()), tmp0, 2);
458 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 3, id.y()), tmp0, 3);
459 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 4, id.y()), tmp0, 4);
460 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 5, id.y()), tmp0, 5);
461 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 6, id.y()), tmp0, 6);
462 tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 7, id.y()), tmp0, 7);
463
464 uint8x8_t tmp1 = vdup_n_u8(0);
465 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 8, id.y()), tmp1, 0);
466 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 9, id.y()), tmp1, 1);
467 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 10, id.y()), tmp1, 2);
468 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 11, id.y()), tmp1, 3);
469 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 12, id.y()), tmp1, 4);
470 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 13, id.y()), tmp1, 5);
471 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 14, id.y()), tmp1, 6);
472 tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 15, id.y()), tmp1, 7);
473
Manuel Bottini10b38262021-02-19 18:16:44 +0000474 vst1q_u8(dst_i.ptr(), vcombine_u8(tmp0, tmp1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100475 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000476 src_i, dst_i);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100477}
478
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100479template <typename T>
Manuel Bottini10b38262021-02-19 18:16:44 +0000480void 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 +0100481{
482 // Get data layout and width/height indices
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000483 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
484 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100485
486 // Compute the ratio between source height and destination height
Manuel Bottini10b38262021-02-19 18:16:44 +0000487 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 +0100488 Window win_off;
489 win_off.set(Window::DimX, Window::Dimension(0, 0, 0));
490 win_off.set(Window::DimY, Window::Dimension(0, 0, 0));
491
492 // Don't increment in X and Y direction for the input tensor
493 // A pointer to the start of this plane is needed as base for the precomputed offsets
494 Window win_in(window);
495 win_in.set(idx_width, Window::Dimension(0, 0, 0));
496 win_in.set(idx_height, Window::Dimension(0, 0, 0));
497
Manuel Bottini10b38262021-02-19 18:16:44 +0000498 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100499 {
500 win_off.set(d, Window::Dimension(0, 0, 0));
501 }
502
Manuel Bottini10b38262021-02-19 18:16:44 +0000503 Iterator src_i(src, win_in);
504 Iterator dst_i(dst, window);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100505
Manuel Bottini10b38262021-02-19 18:16:44 +0000506 const int32_t in_dim_w = src->info()->dimension(idx_width);
507 const int32_t in_dim_h = src->info()->dimension(idx_height);
508 const int32_t stride_w = src->info()->strides_in_bytes()[idx_width];
509 const int32_t stride_h = src->info()->strides_in_bytes()[idx_height];
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100510
Manuel Bottini10b38262021-02-19 18:16:44 +0000511 const UniformQuantizationInfo iq_info = src->info()->quantization_info().uniform();
512 const UniformQuantizationInfo oq_info = dst->info()->quantization_info().uniform();
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100513
514 if(_border_mode == BorderMode::CONSTANT)
515 {
516#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
517 using ConstType = typename std::conditional<std::is_same<T, float16_t>::value, half, T>::type;
518#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
519 using ConstType = T;
520#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
521 const T const_border_value = static_cast<T>(_constant_border_value.get<ConstType>());
522 execute_window_loop(window, [&](const Coordinates & id)
523 {
524 const int32_t index_h = std::floor((id[idx_height] + _sampling_offset) * hr - _sampling_offset);
Manuel Bottini10b38262021-02-19 18:16:44 +0000525 const int32_t index_w = *(reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
526 const auto dx_val = *(reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
527 const auto dy_val = *(reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
528 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100529
530 const auto a00 = (0 <= index_w && index_w < in_dim_w && 0 <= index_h && index_h < in_dim_h) ?
531 (*(pixel_row_ptr + index_w * stride_w + index_h * stride_h)) :
532 const_border_value;
533 const auto a01 = (-1 <= index_w && index_w < in_dim_w - 1 && 0 <= index_h && index_h < in_dim_h) ?
534 (*(pixel_row_ptr + (index_w + 1) * stride_w + index_h * stride_h)) :
535 const_border_value;
536 const auto a10 = (0 <= index_w && index_w < in_dim_w && -1 <= index_h && index_h < in_dim_h - 1) ?
537 (*(pixel_row_ptr + index_w * stride_w + (index_h + 1) * stride_h)) :
538 const_border_value;
539 const auto a11 = (-1 <= index_w && index_w < in_dim_w - 1 && -1 <= index_h && index_h < in_dim_h - 1) ?
540 (*(pixel_row_ptr + (index_w + 1) * stride_w + (index_h + 1) * stride_h)) :
541 const_border_value;
542
Manuel Bottini10b38262021-02-19 18:16:44 +0000543 const float inp00 = Qasymm8QuantizationHelper<T>::dequantize(a00, iq_info);
544 const float inp01 = Qasymm8QuantizationHelper<T>::dequantize(a01, iq_info);
545 const float inp10 = Qasymm8QuantizationHelper<T>::dequantize(a10, iq_info);
546 const float inp11 = Qasymm8QuantizationHelper<T>::dequantize(a11, iq_info);
547 *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 +0100548 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000549 src_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100550 }
551 else if(_border_mode == BorderMode::REPLICATE)
552 {
553 execute_window_loop(window, [&](const Coordinates & id)
554 {
555 const int index_h = std::floor((id[idx_height] + _sampling_offset) * hr - _sampling_offset);
Manuel Bottini10b38262021-02-19 18:16:44 +0000556 const int32_t index_w = *(reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
557 const auto dx_val = *(reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
558 const auto dy_val = *(reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
559 const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100560
561 auto clamped_w = utility::clamp<int>(index_w, 0, in_dim_w - 1);
562 auto clamped_w1 = utility::clamp<int>(index_w + 1, 0, in_dim_w - 1);
563 auto clamped_h = utility::clamp<int>(index_h, 0, in_dim_h - 1);
564 auto clamped_h1 = utility::clamp<int>(index_h + 1, 0, in_dim_h - 1);
565
566 const auto a00 = *(pixel_row_ptr + clamped_w * stride_w + clamped_h * stride_h);
567 const auto a01 = *(pixel_row_ptr + clamped_w1 * stride_w + clamped_h * stride_h);
568 const auto a10 = *(pixel_row_ptr + clamped_w * stride_w + clamped_h1 * stride_h);
569 const auto a11 = *(pixel_row_ptr + clamped_w1 * stride_w + clamped_h1 * stride_h);
570
Manuel Bottini10b38262021-02-19 18:16:44 +0000571 const float inp00 = Qasymm8QuantizationHelper<T>::dequantize(a00, iq_info);
572 const float inp01 = Qasymm8QuantizationHelper<T>::dequantize(a01, iq_info);
573 const float inp10 = Qasymm8QuantizationHelper<T>::dequantize(a10, iq_info);
574 const float inp11 = Qasymm8QuantizationHelper<T>::dequantize(a11, iq_info);
575 *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 +0100576 },
Manuel Bottini10b38262021-02-19 18:16:44 +0000577 src_i, dst_i);
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100578 }
579 else
580 {
581 ARM_COMPUTE_ERROR("Not implemented");
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100582 }
583}
Manuel Bottini10b38262021-02-19 18:16:44 +0000584#endif // ENABLE_NCHW_KERNELS
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100585
Manuel Bottini10b38262021-02-19 18:16:44 +0000586Status CpuScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *dx, const ITensorInfo *dy,
587 const ITensorInfo *offsets, ITensorInfo *output, const ScaleKernelInfo &info)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100588{
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100589 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, dx, dy, offsets, output, info));
Georgios Pinitas20b43132018-05-14 16:05:23 +0100590 return Status{};
591}
592
Manuel Bottini10b38262021-02-19 18:16:44 +0000593void CpuScaleKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100594{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100595 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100596 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottini10b38262021-02-19 18:16:44 +0000597 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000598 ARM_COMPUTE_ERROR_ON(_func == nullptr && _data_layout == DataLayout::NCHW);
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100599 ARM_COMPUTE_ERROR_ON(_run_method == nullptr && _data_layout == DataLayout::NHWC);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100600
Manuel Bottini10b38262021-02-19 18:16:44 +0000601 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
602 auto dst = tensors.get_tensor(TensorType::ACL_DST);
603 const auto dx = tensors.get_const_tensor(TensorType::ACL_INT_0);
604 const auto dy = tensors.get_const_tensor(TensorType::ACL_INT_1);
605 const auto offsets = tensors.get_const_tensor(TensorType::ACL_INT_2);
606
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000607 if(_data_layout == DataLayout::NCHW)
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000608 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000609 (this->*_func)(src, dst, dx, dy, offsets, window);
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000610 }
611 else
612 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100613 _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 +0000614 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100615}
Manuel Bottini10b38262021-02-19 18:16:44 +0000616
617const char *CpuScaleKernel::name() const
618{
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100619 return _name.c_str();
Manuel Bottini10b38262021-02-19 18:16:44 +0000620}
621} // namespace kernels
622} // namespace cpu
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100623} // namespace arm_compute