1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2021-2022 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "src/cpu/operators/CpuScale.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/runtime/NEON/NEScheduler.h"
27*c217d954SCole Faust #include "src/common/utils/Log.h"
28*c217d954SCole Faust #include "src/core/utils/ScaleUtils.h"
29*c217d954SCole Faust #include "src/cpu/kernels/CpuScaleKernel.h"
30*c217d954SCole Faust #include "support/Rounding.h"
31*c217d954SCole Faust
32*c217d954SCole Faust namespace arm_compute
33*c217d954SCole Faust {
34*c217d954SCole Faust namespace cpu
35*c217d954SCole Faust {
36*c217d954SCole Faust namespace
37*c217d954SCole Faust {
precompute_dx_dy_offsets(ITensor * dx,ITensor * dy,ITensor * offsets,float wr,float hr,SamplingPolicy sampling_policy,bool align_corners)38*c217d954SCole Faust void precompute_dx_dy_offsets(ITensor *dx, ITensor *dy, ITensor *offsets, float wr, float hr, SamplingPolicy sampling_policy, bool align_corners)
39*c217d954SCole Faust {
40*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(offsets == nullptr);
41*c217d954SCole Faust float sampling_offset = 0.0f;
42*c217d954SCole Faust if(sampling_policy == SamplingPolicy::CENTER)
43*c217d954SCole Faust {
44*c217d954SCole Faust sampling_offset = 0.5f;
45*c217d954SCole Faust }
46*c217d954SCole Faust
47*c217d954SCole Faust Window win;
48*c217d954SCole Faust win.set(Window::DimX, Window::Dimension(0, offsets->info()->dimension(0), 1));
49*c217d954SCole Faust win.set(Window::DimY, Window::Dimension(0, offsets->info()->dimension(1), 1));
50*c217d954SCole Faust
51*c217d954SCole Faust if(dx != nullptr && dy != nullptr)
52*c217d954SCole Faust {
53*c217d954SCole Faust // Pre-compute the offset and pixel's distance for BILINEAR interpolation
54*c217d954SCole Faust Iterator offsets_it(offsets, win);
55*c217d954SCole Faust Iterator dx_it(dx, win);
56*c217d954SCole Faust Iterator dy_it(dy, win);
57*c217d954SCole Faust
58*c217d954SCole Faust execute_window_loop(win, [&](const Coordinates & id)
59*c217d954SCole Faust {
60*c217d954SCole Faust const float in_x = (id.x() + sampling_offset) * wr - sampling_offset;
61*c217d954SCole Faust const float in_y = (id.y() + sampling_offset) * hr - sampling_offset;
62*c217d954SCole Faust const int in_xi = std::floor(in_x);
63*c217d954SCole Faust const int in_yi = std::floor(in_y);
64*c217d954SCole Faust
65*c217d954SCole Faust *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi;
66*c217d954SCole Faust *reinterpret_cast<float *>(dx_it.ptr()) = in_x - in_xi;
67*c217d954SCole Faust *reinterpret_cast<float *>(dy_it.ptr()) = in_y - in_yi;
68*c217d954SCole Faust },
69*c217d954SCole Faust offsets_it, dx_it, dy_it);
70*c217d954SCole Faust }
71*c217d954SCole Faust else
72*c217d954SCole Faust {
73*c217d954SCole Faust // Pre-compute the offset for NEAREST interpolation
74*c217d954SCole Faust Iterator offsets_it(offsets, win);
75*c217d954SCole Faust
76*c217d954SCole Faust execute_window_loop(win, [&](const Coordinates & id)
77*c217d954SCole Faust {
78*c217d954SCole Faust const float float_in_xi = (id.x() + sampling_offset) * wr;
79*c217d954SCole Faust 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));
80*c217d954SCole Faust *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi;
81*c217d954SCole Faust },
82*c217d954SCole Faust offsets_it);
83*c217d954SCole Faust }
84*c217d954SCole Faust }
85*c217d954SCole Faust } // namespace
86*c217d954SCole Faust
configure(ITensorInfo * src,ITensorInfo * dst,const ScaleKernelInfo & info)87*c217d954SCole Faust void CpuScale::configure(ITensorInfo *src, ITensorInfo *dst, const ScaleKernelInfo &info)
88*c217d954SCole Faust {
89*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
90*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(CpuScale::validate(src, dst, info));
91*c217d954SCole Faust ARM_COMPUTE_LOG_PARAMS(src, dst, info);
92*c217d954SCole Faust
93*c217d954SCole Faust _scale_info = info;
94*c217d954SCole Faust _is_prepared = false;
95*c217d954SCole Faust
96*c217d954SCole Faust // Get data layout and width/height indices
97*c217d954SCole Faust _data_layout = _scale_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : _scale_info.data_layout;
98*c217d954SCole Faust const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
99*c217d954SCole Faust const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
100*c217d954SCole Faust
101*c217d954SCole Faust // Compute the ratio between source width/height and destination width/height
102*c217d954SCole Faust const bool is_align_corners_used = _scale_info.align_corners && arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(_scale_info.sampling_policy);
103*c217d954SCole Faust const auto wr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_width), dst->dimension(idx_width), is_align_corners_used);
104*c217d954SCole Faust const auto hr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_height), dst->dimension(idx_height), is_align_corners_used);
105*c217d954SCole Faust
106*c217d954SCole Faust // Area interpolation behaves as Nearest Neighbour in case of up-sampling
107*c217d954SCole Faust InterpolationPolicy policy_to_use = (_scale_info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f
108*c217d954SCole Faust && hr <= 1.f) ?
109*c217d954SCole Faust InterpolationPolicy::NEAREST_NEIGHBOR :
110*c217d954SCole Faust _scale_info.interpolation_policy;
111*c217d954SCole Faust
112*c217d954SCole Faust // Get the tensor shape
113*c217d954SCole Faust TensorShape shape(dst->dimension(idx_width));
114*c217d954SCole Faust shape.set(1, dst->dimension(idx_height), false);
115*c217d954SCole Faust
116*c217d954SCole Faust TensorInfo tensor_info_offsets(shape, Format::S32);
117*c217d954SCole Faust TensorInfo tensor_info_dxdy(shape, Format::F32);
118*c217d954SCole Faust
119*c217d954SCole Faust auto dx = std::make_unique<TensorInfo>(tensor_info_dxdy);
120*c217d954SCole Faust auto dy = std::make_unique<TensorInfo>(tensor_info_dxdy);
121*c217d954SCole Faust auto offsets = std::make_unique<TensorInfo>(tensor_info_offsets);
122*c217d954SCole Faust auto scale_kernel = std::make_unique<kernels::CpuScaleKernel>();
123*c217d954SCole Faust switch(policy_to_use)
124*c217d954SCole Faust {
125*c217d954SCole Faust case InterpolationPolicy::NEAREST_NEIGHBOR:
126*c217d954SCole Faust {
127*c217d954SCole Faust scale_kernel->configure(src, nullptr, nullptr, offsets.get(), dst, info);
128*c217d954SCole Faust break;
129*c217d954SCole Faust }
130*c217d954SCole Faust case InterpolationPolicy::BILINEAR:
131*c217d954SCole Faust {
132*c217d954SCole Faust scale_kernel->configure(src, dx.get(), dy.get(), offsets.get(), dst, info);
133*c217d954SCole Faust break;
134*c217d954SCole Faust }
135*c217d954SCole Faust case InterpolationPolicy::AREA:
136*c217d954SCole Faust {
137*c217d954SCole Faust scale_kernel->configure(src, nullptr, nullptr, nullptr, dst, info);
138*c217d954SCole Faust break;
139*c217d954SCole Faust }
140*c217d954SCole Faust default:
141*c217d954SCole Faust ARM_COMPUTE_ERROR("Unsupported interpolation mode");
142*c217d954SCole Faust }
143*c217d954SCole Faust _kernel = std::move(scale_kernel);
144*c217d954SCole Faust }
145*c217d954SCole Faust
validate(const ITensorInfo * src,const ITensorInfo * dst,const ScaleKernelInfo & info)146*c217d954SCole Faust Status CpuScale::validate(const ITensorInfo *src, const ITensorInfo *dst, const ScaleKernelInfo &info)
147*c217d954SCole Faust {
148*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
149*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT);
150*c217d954SCole Faust
151*c217d954SCole Faust ITensorInfo *offsets = nullptr;
152*c217d954SCole Faust ITensorInfo *dx = nullptr;
153*c217d954SCole Faust ITensorInfo *dy = nullptr;
154*c217d954SCole Faust
155*c217d954SCole Faust // Get data layout and width/height indices
156*c217d954SCole Faust const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
157*c217d954SCole Faust const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
158*c217d954SCole Faust const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
159*c217d954SCole Faust
160*c217d954SCole Faust // Compute the ratio between source width/height and destination width/height
161*c217d954SCole Faust const bool is_align_corners_used = info.align_corners && arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(info.sampling_policy);
162*c217d954SCole Faust const auto wr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_width), dst->dimension(idx_width), is_align_corners_used);
163*c217d954SCole Faust const auto hr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_height), dst->dimension(idx_height), is_align_corners_used);
164*c217d954SCole Faust
165*c217d954SCole Faust // Area interpolation behaves as Nearest Neighbour in case of up-sampling
166*c217d954SCole Faust InterpolationPolicy policy_to_use = (info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) ? InterpolationPolicy::NEAREST_NEIGHBOR : info.interpolation_policy;
167*c217d954SCole Faust
168*c217d954SCole Faust // Get the tensor shape of auxilary buffers
169*c217d954SCole Faust const TensorShape shape(dst->dimension(idx_width), dst->dimension(idx_height));
170*c217d954SCole Faust TensorInfo tensor_info_offsets(shape, Format::S32);
171*c217d954SCole Faust TensorInfo tensor_info_dx(shape, Format::F32);
172*c217d954SCole Faust TensorInfo tensor_info_dy(shape, Format::F32);
173*c217d954SCole Faust switch(policy_to_use)
174*c217d954SCole Faust {
175*c217d954SCole Faust case InterpolationPolicy::NEAREST_NEIGHBOR:
176*c217d954SCole Faust offsets = &tensor_info_offsets;
177*c217d954SCole Faust break;
178*c217d954SCole Faust case InterpolationPolicy::BILINEAR:
179*c217d954SCole Faust offsets = &tensor_info_offsets;
180*c217d954SCole Faust dx = &tensor_info_dx;
181*c217d954SCole Faust dy = &tensor_info_dy;
182*c217d954SCole Faust break;
183*c217d954SCole Faust default:
184*c217d954SCole Faust break;
185*c217d954SCole Faust }
186*c217d954SCole Faust
187*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuScaleKernel::validate(src->clone().get(), dx, dy, offsets, dst->clone().get(), info));
188*c217d954SCole Faust return Status{};
189*c217d954SCole Faust }
190*c217d954SCole Faust
prepare(ITensorPack & tensors)191*c217d954SCole Faust void CpuScale::prepare(ITensorPack &tensors)
192*c217d954SCole Faust {
193*c217d954SCole Faust if(!_is_prepared)
194*c217d954SCole Faust {
195*c217d954SCole Faust _is_prepared = true;
196*c217d954SCole Faust const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
197*c217d954SCole Faust auto dst = tensors.get_tensor(TensorType::ACL_DST);
198*c217d954SCole Faust auto dx = tensors.get_tensor(TensorType::ACL_INT_0);
199*c217d954SCole Faust auto dy = tensors.get_tensor(TensorType::ACL_INT_1);
200*c217d954SCole Faust auto offsets = tensors.get_tensor(TensorType::ACL_INT_2);
201*c217d954SCole Faust
202*c217d954SCole Faust // Get data layout and width/height indices
203*c217d954SCole Faust const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
204*c217d954SCole Faust const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
205*c217d954SCole Faust
206*c217d954SCole Faust // Compute the ratio between source width/height and destination width/height
207*c217d954SCole Faust const bool is_align_corners_used = _scale_info.align_corners && arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(_scale_info.sampling_policy);
208*c217d954SCole Faust const auto wr = arm_compute::scale_utils::calculate_resize_ratio(src->info()->dimension(idx_width), dst->info()->dimension(idx_width), is_align_corners_used);
209*c217d954SCole Faust const auto hr = arm_compute::scale_utils::calculate_resize_ratio(src->info()->dimension(idx_height), dst->info()->dimension(idx_height), is_align_corners_used);
210*c217d954SCole Faust
211*c217d954SCole Faust // Area interpolation behaves as Nearest Neighbour in case of up-sampling
212*c217d954SCole Faust InterpolationPolicy policy_to_use = (_scale_info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f
213*c217d954SCole Faust && hr <= 1.f) ?
214*c217d954SCole Faust InterpolationPolicy::NEAREST_NEIGHBOR :
215*c217d954SCole Faust _scale_info.interpolation_policy;
216*c217d954SCole Faust const SamplingPolicy sampling_policy = _scale_info.sampling_policy;
217*c217d954SCole Faust
218*c217d954SCole Faust bool precompute_indices_weights = arm_compute::scale_utils::is_precomputation_required(_data_layout, src->info()->data_type(), policy_to_use, _scale_info.border_mode);
219*c217d954SCole Faust
220*c217d954SCole Faust if(precompute_indices_weights)
221*c217d954SCole Faust {
222*c217d954SCole Faust switch(policy_to_use)
223*c217d954SCole Faust {
224*c217d954SCole Faust case InterpolationPolicy::NEAREST_NEIGHBOR:
225*c217d954SCole Faust {
226*c217d954SCole Faust // Pre-compute offsets for nearest interpolation
227*c217d954SCole Faust precompute_dx_dy_offsets(nullptr, nullptr, offsets, wr, hr, sampling_policy, is_align_corners_used);
228*c217d954SCole Faust break;
229*c217d954SCole Faust }
230*c217d954SCole Faust case InterpolationPolicy::BILINEAR:
231*c217d954SCole Faust {
232*c217d954SCole Faust // Pre-compute dx, dy and offsets for bilinear interpolation
233*c217d954SCole Faust precompute_dx_dy_offsets(dx, dy, offsets, wr, hr, sampling_policy, is_align_corners_used);
234*c217d954SCole Faust break;
235*c217d954SCole Faust }
236*c217d954SCole Faust case InterpolationPolicy::AREA:
237*c217d954SCole Faust {
238*c217d954SCole Faust break;
239*c217d954SCole Faust }
240*c217d954SCole Faust default:
241*c217d954SCole Faust ARM_COMPUTE_ERROR("Unsupported interpolation mode");
242*c217d954SCole Faust }
243*c217d954SCole Faust }
244*c217d954SCole Faust else
245*c217d954SCole Faust {
246*c217d954SCole Faust if(policy_to_use != InterpolationPolicy::NEAREST_NEIGHBOR && policy_to_use != InterpolationPolicy::BILINEAR && policy_to_use != InterpolationPolicy::AREA)
247*c217d954SCole Faust {
248*c217d954SCole Faust ARM_COMPUTE_ERROR("Unsupported interpolation mode");
249*c217d954SCole Faust }
250*c217d954SCole Faust }
251*c217d954SCole Faust }
252*c217d954SCole Faust }
253*c217d954SCole Faust
run(ITensorPack & tensors)254*c217d954SCole Faust void CpuScale::run(ITensorPack &tensors)
255*c217d954SCole Faust {
256*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided");
257*c217d954SCole Faust prepare(tensors);
258*c217d954SCole Faust NEScheduler::get().schedule_op(_kernel.get(), Window::DimY, _kernel->window(), tensors);
259*c217d954SCole Faust }
260*c217d954SCole Faust } // namespace cpu
261*c217d954SCole Faust } // namespace arm_compute
262