blob: 8a712bf088a28489c7a93852643952ef41954639 [file] [log] [blame]
Manuel Bottini10b38262021-02-19 18:16:44 +00001/*
Matthew Bentham1d062042023-07-06 13:13:59 +00002 * Copyright (c) 2021-2023 Arm Limited.
Manuel Bottini10b38262021-02-19 18:16:44 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/operators/CpuScale.h"
Manuel Bottini10b38262021-02-19 18:16:44 +000025
Matthew Bentham1d062042023-07-06 13:13:59 +000026#include "arm_compute/core/Helpers.h"
Manuel Bottini10b38262021-02-19 18:16:44 +000027#include "arm_compute/runtime/NEON/NEScheduler.h"
Matthew Bentham1d062042023-07-06 13:13:59 +000028#include "arm_compute/core/TensorInfo.h"
ramelg013ae3d882021-09-12 23:07:47 +010029#include "src/common/utils/Log.h"
Manuel Bottini10b38262021-02-19 18:16:44 +000030#include "src/core/utils/ScaleUtils.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010031#include "src/cpu/kernels/CpuScaleKernel.h"
Manuel Bottini10b38262021-02-19 18:16:44 +000032#include "support/Rounding.h"
33
34namespace arm_compute
35{
36namespace cpu
37{
38namespace
39{
40void 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
Manuel Bottini10b38262021-02-19 18:16:44 +000089void CpuScale::configure(ITensorInfo *src, ITensorInfo *dst, const ScaleKernelInfo &info)
90{
91 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
92 ARM_COMPUTE_ERROR_THROW_ON(CpuScale::validate(src, dst, info));
ramelg013ae3d882021-09-12 23:07:47 +010093 ARM_COMPUTE_LOG_PARAMS(src, dst, info);
Manuel Bottini10b38262021-02-19 18:16:44 +000094
Georgios Pinitas2eb5d162021-07-02 09:01:49 +010095 _scale_info = info;
96 _is_prepared = false;
Manuel Bottini10b38262021-02-19 18:16:44 +000097
98 // Get data layout and width/height indices
Georgios Pinitas2eb5d162021-07-02 09:01:49 +010099 _data_layout = _scale_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : _scale_info.data_layout;
100 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
101 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
Manuel Bottini10b38262021-02-19 18:16:44 +0000102
103 // Compute the ratio between source width/height and destination width/height
104 const bool is_align_corners_used = _scale_info.align_corners && arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(_scale_info.sampling_policy);
105 const auto wr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_width), dst->dimension(idx_width), is_align_corners_used);
106 const auto hr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_height), dst->dimension(idx_height), is_align_corners_used);
107
108 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
109 InterpolationPolicy policy_to_use = (_scale_info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f
110 && hr <= 1.f) ?
111 InterpolationPolicy::NEAREST_NEIGHBOR :
112 _scale_info.interpolation_policy;
113
114 // Get the tensor shape
115 TensorShape shape(dst->dimension(idx_width));
116 shape.set(1, dst->dimension(idx_height), false);
117
118 TensorInfo tensor_info_offsets(shape, Format::S32);
119 TensorInfo tensor_info_dxdy(shape, Format::F32);
120
121 auto dx = std::make_unique<TensorInfo>(tensor_info_dxdy);
122 auto dy = std::make_unique<TensorInfo>(tensor_info_dxdy);
123 auto offsets = std::make_unique<TensorInfo>(tensor_info_offsets);
124 auto scale_kernel = std::make_unique<kernels::CpuScaleKernel>();
125 switch(policy_to_use)
126 {
127 case InterpolationPolicy::NEAREST_NEIGHBOR:
128 {
129 scale_kernel->configure(src, nullptr, nullptr, offsets.get(), dst, info);
130 break;
131 }
132 case InterpolationPolicy::BILINEAR:
133 {
134 scale_kernel->configure(src, dx.get(), dy.get(), offsets.get(), dst, info);
135 break;
136 }
137 case InterpolationPolicy::AREA:
138 {
139 scale_kernel->configure(src, nullptr, nullptr, nullptr, dst, info);
140 break;
141 }
142 default:
143 ARM_COMPUTE_ERROR("Unsupported interpolation mode");
144 }
145 _kernel = std::move(scale_kernel);
146}
147
148Status CpuScale::validate(const ITensorInfo *src, const ITensorInfo *dst, const ScaleKernelInfo &info)
149{
150 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
151 ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT);
152
153 ITensorInfo *offsets = nullptr;
154 ITensorInfo *dx = nullptr;
155 ITensorInfo *dy = nullptr;
156
157 // Get data layout and width/height indices
158 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
159 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
160 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
161
162 // Compute the ratio between source width/height and destination width/height
163 const bool is_align_corners_used = info.align_corners && arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(info.sampling_policy);
164 const auto wr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_width), dst->dimension(idx_width), is_align_corners_used);
165 const auto hr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_height), dst->dimension(idx_height), is_align_corners_used);
166
167 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
168 InterpolationPolicy policy_to_use = (info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) ? InterpolationPolicy::NEAREST_NEIGHBOR : info.interpolation_policy;
169
170 // Get the tensor shape of auxilary buffers
171 const TensorShape shape(dst->dimension(idx_width), dst->dimension(idx_height));
172 TensorInfo tensor_info_offsets(shape, Format::S32);
173 TensorInfo tensor_info_dx(shape, Format::F32);
174 TensorInfo tensor_info_dy(shape, Format::F32);
175 switch(policy_to_use)
176 {
177 case InterpolationPolicy::NEAREST_NEIGHBOR:
178 offsets = &tensor_info_offsets;
179 break;
180 case InterpolationPolicy::BILINEAR:
181 offsets = &tensor_info_offsets;
182 dx = &tensor_info_dx;
183 dy = &tensor_info_dy;
184 break;
185 default:
186 break;
187 }
188
189 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuScaleKernel::validate(src->clone().get(), dx, dy, offsets, dst->clone().get(), info));
190 return Status{};
191}
192
193void CpuScale::prepare(ITensorPack &tensors)
194{
195 if(!_is_prepared)
196 {
197 _is_prepared = true;
198 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
199 auto dst = tensors.get_tensor(TensorType::ACL_DST);
200 auto dx = tensors.get_tensor(TensorType::ACL_INT_0);
201 auto dy = tensors.get_tensor(TensorType::ACL_INT_1);
202 auto offsets = tensors.get_tensor(TensorType::ACL_INT_2);
203
204 // Get data layout and width/height indices
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100205 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
206 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
Manuel Bottini10b38262021-02-19 18:16:44 +0000207
208 // Compute the ratio between source width/height and destination width/height
209 const bool is_align_corners_used = _scale_info.align_corners && arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(_scale_info.sampling_policy);
210 const auto wr = arm_compute::scale_utils::calculate_resize_ratio(src->info()->dimension(idx_width), dst->info()->dimension(idx_width), is_align_corners_used);
211 const auto hr = arm_compute::scale_utils::calculate_resize_ratio(src->info()->dimension(idx_height), dst->info()->dimension(idx_height), is_align_corners_used);
212
213 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
214 InterpolationPolicy policy_to_use = (_scale_info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f
215 && hr <= 1.f) ?
216 InterpolationPolicy::NEAREST_NEIGHBOR :
217 _scale_info.interpolation_policy;
218 const SamplingPolicy sampling_policy = _scale_info.sampling_policy;
219
Gunes Bayirc4f27432022-09-11 15:59:19 +0100220 bool precompute_indices_weights = arm_compute::scale_utils::is_precomputation_required(_data_layout, src->info()->data_type(), policy_to_use, _scale_info.border_mode);
Gunes Bayir0eed3052022-09-04 21:00:10 +0100221
Gunes Bayirc4f27432022-09-11 15:59:19 +0100222 if(precompute_indices_weights)
Manuel Bottini10b38262021-02-19 18:16:44 +0000223 {
Gunes Bayir0eed3052022-09-04 21:00:10 +0100224 switch(policy_to_use)
Manuel Bottini10b38262021-02-19 18:16:44 +0000225 {
Gunes Bayir0eed3052022-09-04 21:00:10 +0100226 case InterpolationPolicy::NEAREST_NEIGHBOR:
227 {
228 // Pre-compute offsets for nearest interpolation
229 precompute_dx_dy_offsets(nullptr, nullptr, offsets, wr, hr, sampling_policy, is_align_corners_used);
230 break;
231 }
232 case InterpolationPolicy::BILINEAR:
233 {
234 // Pre-compute dx, dy and offsets for bilinear interpolation
235 precompute_dx_dy_offsets(dx, dy, offsets, wr, hr, sampling_policy, is_align_corners_used);
236 break;
237 }
238 case InterpolationPolicy::AREA:
239 {
240 break;
241 }
242 default:
243 ARM_COMPUTE_ERROR("Unsupported interpolation mode");
Manuel Bottini10b38262021-02-19 18:16:44 +0000244 }
Gunes Bayir0eed3052022-09-04 21:00:10 +0100245 }
246 else
247 {
248 if(policy_to_use != InterpolationPolicy::NEAREST_NEIGHBOR && policy_to_use != InterpolationPolicy::BILINEAR && policy_to_use != InterpolationPolicy::AREA)
Manuel Bottini10b38262021-02-19 18:16:44 +0000249 {
Manuel Bottini10b38262021-02-19 18:16:44 +0000250 ARM_COMPUTE_ERROR("Unsupported interpolation mode");
Gunes Bayir0eed3052022-09-04 21:00:10 +0100251 }
Manuel Bottini10b38262021-02-19 18:16:44 +0000252 }
253 }
254}
255
256void CpuScale::run(ITensorPack &tensors)
257{
258 ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided");
259 prepare(tensors);
260 NEScheduler::get().schedule_op(_kernel.get(), Window::DimY, _kernel->window(), tensors);
261}
262} // namespace cpu
263} // namespace arm_compute