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