Manuel Bottini | 10b3826 | 2021-02-19 18:16:44 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. |
| 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 "src/runtime/cpu/operators/CpuScale.h" |
| 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/TensorInfo.h" |
| 28 | #include "arm_compute/core/Validate.h" |
| 29 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 30 | #include "src/core/cpu/kernels/CpuScaleKernel.h" |
| 31 | #include "src/core/utils/ScaleUtils.h" |
| 32 | #include "support/Rounding.h" |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace cpu |
| 37 | { |
| 38 | namespace |
| 39 | { |
| 40 | void precompute_dx_dy_offsets(ITensor *dx, ITensor *dy, ITensor *offsets, float wr, float hr, SamplingPolicy sampling_policy, bool align_corners) |
| 41 | { |
| 42 | ARM_COMPUTE_ERROR_ON(offsets == nullptr); |
| 43 | float sampling_offset = 0.0f; |
| 44 | if(sampling_policy == SamplingPolicy::CENTER) |
| 45 | { |
| 46 | sampling_offset = 0.5f; |
| 47 | } |
| 48 | |
| 49 | Window win; |
| 50 | win.set(Window::DimX, Window::Dimension(0, offsets->info()->dimension(0), 1)); |
| 51 | win.set(Window::DimY, Window::Dimension(0, offsets->info()->dimension(1), 1)); |
| 52 | |
| 53 | if(dx != nullptr && dy != nullptr) |
| 54 | { |
| 55 | // Pre-compute the offset and pixel's distance for BILINEAR interpolation |
| 56 | Iterator offsets_it(offsets, win); |
| 57 | Iterator dx_it(dx, win); |
| 58 | Iterator dy_it(dy, win); |
| 59 | |
| 60 | execute_window_loop(win, [&](const Coordinates & id) |
| 61 | { |
| 62 | const float in_x = (id.x() + sampling_offset) * wr - sampling_offset; |
| 63 | const float in_y = (id.y() + sampling_offset) * hr - sampling_offset; |
| 64 | const int in_xi = std::floor(in_x); |
| 65 | const int in_yi = std::floor(in_y); |
| 66 | |
| 67 | *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi; |
| 68 | *reinterpret_cast<float *>(dx_it.ptr()) = in_x - in_xi; |
| 69 | *reinterpret_cast<float *>(dy_it.ptr()) = in_y - in_yi; |
| 70 | }, |
| 71 | offsets_it, dx_it, dy_it); |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | // Pre-compute the offset for NEAREST interpolation |
| 76 | Iterator offsets_it(offsets, win); |
| 77 | |
| 78 | execute_window_loop(win, [&](const Coordinates & id) |
| 79 | { |
| 80 | const float float_in_xi = (id.x() + sampling_offset) * wr; |
| 81 | const auto in_xi = static_cast<size_t>(align_corners ? arm_compute::utils::rounding::round_half_away_from_zero(float_in_xi) : std::floor(float_in_xi)); |
| 82 | *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi; |
| 83 | }, |
| 84 | offsets_it); |
| 85 | } |
| 86 | } |
| 87 | } // namespace |
| 88 | |
| 89 | CpuScale::CpuScale() |
| 90 | : _scale_info(InterpolationPolicy::NEAREST_NEIGHBOR, BorderMode::UNDEFINED), _data_layout(DataLayout::UNKNOWN), _is_prepared(false) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | void CpuScale::configure(ITensorInfo *src, ITensorInfo *dst, const ScaleKernelInfo &info) |
| 95 | { |
| 96 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 97 | ARM_COMPUTE_ERROR_THROW_ON(CpuScale::validate(src, dst, info)); |
| 98 | |
| 99 | _scale_info = info; |
| 100 | |
| 101 | // Get data layout and width/height indices |
| 102 | _data_layout = _scale_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : _scale_info.data_layout; |
| 103 | const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH); |
| 104 | const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT); |
| 105 | |
| 106 | // Compute the ratio between source width/height and destination width/height |
| 107 | const bool is_align_corners_used = _scale_info.align_corners && arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(_scale_info.sampling_policy); |
| 108 | const auto wr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_width), dst->dimension(idx_width), is_align_corners_used); |
| 109 | const auto hr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_height), dst->dimension(idx_height), is_align_corners_used); |
| 110 | |
| 111 | // Area interpolation behaves as Nearest Neighbour in case of up-sampling |
| 112 | InterpolationPolicy policy_to_use = (_scale_info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f |
| 113 | && hr <= 1.f) ? |
| 114 | InterpolationPolicy::NEAREST_NEIGHBOR : |
| 115 | _scale_info.interpolation_policy; |
| 116 | |
| 117 | // Get the tensor shape |
| 118 | TensorShape shape(dst->dimension(idx_width)); |
| 119 | shape.set(1, dst->dimension(idx_height), false); |
| 120 | |
| 121 | TensorInfo tensor_info_offsets(shape, Format::S32); |
| 122 | TensorInfo tensor_info_dxdy(shape, Format::F32); |
| 123 | |
| 124 | auto dx = std::make_unique<TensorInfo>(tensor_info_dxdy); |
| 125 | auto dy = std::make_unique<TensorInfo>(tensor_info_dxdy); |
| 126 | auto offsets = std::make_unique<TensorInfo>(tensor_info_offsets); |
| 127 | auto scale_kernel = std::make_unique<kernels::CpuScaleKernel>(); |
| 128 | switch(policy_to_use) |
| 129 | { |
| 130 | case InterpolationPolicy::NEAREST_NEIGHBOR: |
| 131 | { |
| 132 | scale_kernel->configure(src, nullptr, nullptr, offsets.get(), dst, info); |
| 133 | break; |
| 134 | } |
| 135 | case InterpolationPolicy::BILINEAR: |
| 136 | { |
| 137 | scale_kernel->configure(src, dx.get(), dy.get(), offsets.get(), dst, info); |
| 138 | break; |
| 139 | } |
| 140 | case InterpolationPolicy::AREA: |
| 141 | { |
| 142 | scale_kernel->configure(src, nullptr, nullptr, nullptr, dst, info); |
| 143 | break; |
| 144 | } |
| 145 | default: |
| 146 | ARM_COMPUTE_ERROR("Unsupported interpolation mode"); |
| 147 | } |
| 148 | _kernel = std::move(scale_kernel); |
| 149 | } |
| 150 | |
| 151 | Status CpuScale::validate(const ITensorInfo *src, const ITensorInfo *dst, const ScaleKernelInfo &info) |
| 152 | { |
| 153 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 154 | ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT); |
| 155 | |
| 156 | ITensorInfo *offsets = nullptr; |
| 157 | ITensorInfo *dx = nullptr; |
| 158 | ITensorInfo *dy = nullptr; |
| 159 | |
| 160 | // Get data layout and width/height indices |
| 161 | const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout; |
| 162 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 163 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 164 | |
| 165 | // Compute the ratio between source width/height and destination width/height |
| 166 | const bool is_align_corners_used = info.align_corners && arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(info.sampling_policy); |
| 167 | const auto wr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_width), dst->dimension(idx_width), is_align_corners_used); |
| 168 | const auto hr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_height), dst->dimension(idx_height), is_align_corners_used); |
| 169 | |
| 170 | // Area interpolation behaves as Nearest Neighbour in case of up-sampling |
| 171 | InterpolationPolicy policy_to_use = (info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) ? InterpolationPolicy::NEAREST_NEIGHBOR : info.interpolation_policy; |
| 172 | |
| 173 | // Get the tensor shape of auxilary buffers |
| 174 | const TensorShape shape(dst->dimension(idx_width), dst->dimension(idx_height)); |
| 175 | TensorInfo tensor_info_offsets(shape, Format::S32); |
| 176 | TensorInfo tensor_info_dx(shape, Format::F32); |
| 177 | TensorInfo tensor_info_dy(shape, Format::F32); |
| 178 | switch(policy_to_use) |
| 179 | { |
| 180 | case InterpolationPolicy::NEAREST_NEIGHBOR: |
| 181 | offsets = &tensor_info_offsets; |
| 182 | break; |
| 183 | case InterpolationPolicy::BILINEAR: |
| 184 | offsets = &tensor_info_offsets; |
| 185 | dx = &tensor_info_dx; |
| 186 | dy = &tensor_info_dy; |
| 187 | break; |
| 188 | default: |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuScaleKernel::validate(src->clone().get(), dx, dy, offsets, dst->clone().get(), info)); |
| 193 | return Status{}; |
| 194 | } |
| 195 | |
| 196 | void CpuScale::prepare(ITensorPack &tensors) |
| 197 | { |
| 198 | if(!_is_prepared) |
| 199 | { |
| 200 | _is_prepared = true; |
| 201 | const auto src = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 202 | auto dst = tensors.get_tensor(TensorType::ACL_DST); |
| 203 | auto dx = tensors.get_tensor(TensorType::ACL_INT_0); |
| 204 | auto dy = tensors.get_tensor(TensorType::ACL_INT_1); |
| 205 | auto offsets = tensors.get_tensor(TensorType::ACL_INT_2); |
| 206 | |
| 207 | // Get data layout and width/height indices |
| 208 | const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH); |
| 209 | const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT); |
| 210 | |
| 211 | // Compute the ratio between source width/height and destination width/height |
| 212 | const bool is_align_corners_used = _scale_info.align_corners && arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(_scale_info.sampling_policy); |
| 213 | const auto wr = arm_compute::scale_utils::calculate_resize_ratio(src->info()->dimension(idx_width), dst->info()->dimension(idx_width), is_align_corners_used); |
| 214 | const auto hr = arm_compute::scale_utils::calculate_resize_ratio(src->info()->dimension(idx_height), dst->info()->dimension(idx_height), is_align_corners_used); |
| 215 | |
| 216 | // Area interpolation behaves as Nearest Neighbour in case of up-sampling |
| 217 | InterpolationPolicy policy_to_use = (_scale_info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f |
| 218 | && hr <= 1.f) ? |
| 219 | InterpolationPolicy::NEAREST_NEIGHBOR : |
| 220 | _scale_info.interpolation_policy; |
| 221 | const SamplingPolicy sampling_policy = _scale_info.sampling_policy; |
| 222 | |
| 223 | switch(policy_to_use) |
| 224 | { |
| 225 | case InterpolationPolicy::NEAREST_NEIGHBOR: |
| 226 | { |
| 227 | // Pre-compute offsets for nearest interpolation |
| 228 | precompute_dx_dy_offsets(nullptr, nullptr, offsets, wr, hr, sampling_policy, is_align_corners_used); |
| 229 | break; |
| 230 | } |
| 231 | case InterpolationPolicy::BILINEAR: |
| 232 | { |
| 233 | // Pre-compute dx, dy and offsets for bilinear interpolation |
| 234 | precompute_dx_dy_offsets(dx, dy, offsets, wr, hr, sampling_policy, is_align_corners_used); |
| 235 | break; |
| 236 | } |
| 237 | case InterpolationPolicy::AREA: |
| 238 | { |
| 239 | break; |
| 240 | } |
| 241 | default: |
| 242 | ARM_COMPUTE_ERROR("Unsupported interpolation mode"); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void CpuScale::run(ITensorPack &tensors) |
| 248 | { |
| 249 | ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided"); |
| 250 | prepare(tensors); |
| 251 | NEScheduler::get().schedule_op(_kernel.get(), Window::DimY, _kernel->window(), tensors); |
| 252 | } |
| 253 | } // namespace cpu |
| 254 | } // namespace arm_compute |