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