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