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