blob: f91de3219108decb7007ec9cf2f20affc0e3ead4 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgio655e8c62021-01-28 12:51:02 +00002 * Copyright (c) 2016-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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/runtime/NEON/functions/NEScale.h"
25
26#include "arm_compute/core/Coordinates.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/PixelValue.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Window.h"
Georgios Pinitas658039b2017-09-15 16:30:50 +010033#include "arm_compute/runtime/NEON/NEScheduler.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/runtime/TensorAllocator.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010035#include "src/core/NEON/kernels/NEScaleKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
Sang-Hoon Park3687ee12020-06-24 13:34:04 +010037#include "src/core/utils/ScaleUtils.h"
38
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010039#include "support/Rounding.h"
40
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041#include <cmath>
42#include <cstddef>
43#include <utility>
44
Sang-Hoon Parkccd94962020-06-09 12:09:24 +010045namespace arm_compute
46{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047namespace
48{
Manuel Bottinifc2f6d02020-08-26 16:28:38 +010049void precompute_dx_dy_offsets(ITensor *dx, ITensor *dy, ITensor *offsets, float wr, float hr, SamplingPolicy sampling_policy, bool align_corners)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050{
51 ARM_COMPUTE_ERROR_ON(nullptr == offsets);
Daniil Efremov02bf80d2017-11-22 00:26:51 +070052 ARM_COMPUTE_UNUSED(sampling_policy);
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +000053 float sampling_offset = 0.0f;
54 if(sampling_policy == SamplingPolicy::CENTER)
55 {
56 sampling_offset = 0.5f;
57 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058
59 Window win;
60 win.set(Window::DimX, Window::Dimension(0, offsets->info()->dimension(0), 1));
61 win.set(Window::DimY, Window::Dimension(0, offsets->info()->dimension(1), 1));
62
63 if(dx != nullptr && dy != nullptr)
64 {
65 // Pre-compute the offset and pixel's distance for BILINEAR interpolation
66 Iterator offsets_it(offsets, win);
67 Iterator dx_it(dx, win);
68 Iterator dy_it(dy, win);
69
70 execute_window_loop(win, [&](const Coordinates & id)
71 {
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +000072 const float in_x = (id.x() + sampling_offset) * wr - sampling_offset;
73 const float in_y = (id.y() + sampling_offset) * hr - sampling_offset;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074 const int in_xi = std::floor(in_x);
75 const int in_yi = std::floor(in_y);
76
Manuel Bottinifc2f6d02020-08-26 16:28:38 +010077 *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 *reinterpret_cast<float *>(dx_it.ptr()) = in_x - in_xi;
79 *reinterpret_cast<float *>(dy_it.ptr()) = in_y - in_yi;
80 },
81 offsets_it, dx_it, dy_it);
82 }
83 else
84 {
85 // Pre-compute the offset for NEAREST interpolation
86 Iterator offsets_it(offsets, win);
87
88 execute_window_loop(win, [&](const Coordinates & id)
89 {
Manuel Bottinifc2f6d02020-08-26 16:28:38 +010090 const float float_in_xi = (id.x() + sampling_offset) * wr;
91 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));
92 *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 },
94 offsets_it);
95 }
96}
97} // namespace
98
Manuel Bottinifc2f6d02020-08-26 16:28:38 +010099NEScale::NEScale()
100 : _offsets(), _dx(), _dy()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101{
102}
103
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100104void NEScale::configure(ITensor *input, ITensor *output, const ScaleKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105{
Georgios Pinitas20b43132018-05-14 16:05:23 +0100106 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100107 ARM_COMPUTE_ERROR_THROW_ON(NEScale::validate(input->info(), output->info(), info));
George Wort05398a92019-01-25 15:38:33 +0000108
Michele Di Giorgio7a81d2a2020-07-30 14:52:16 +0100109 const bool is_align_corners_used = info.align_corners && arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(info.sampling_policy);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100111 // Get data layout and width/height indices
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000112 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? input->info()->data_layout() : info.data_layout;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100113 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
114 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115
116 // Get the tensor shape
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100117 TensorShape shape(output->info()->dimension(idx_width));
118 shape.set(1, output->info()->dimension(idx_height), false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119
120 // Compute the ratio between source width/height and destination width/height
Sang-Hoon Park3687ee12020-06-24 13:34:04 +0100121 const auto wr = arm_compute::scale_utils::calculate_resize_ratio(input->info()->dimension(idx_width), output->info()->dimension(idx_width), is_align_corners_used);
122 const auto hr = arm_compute::scale_utils::calculate_resize_ratio(input->info()->dimension(idx_height), output->info()->dimension(idx_height), is_align_corners_used);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100125 const auto policy_to_use = (info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) ? InterpolationPolicy::NEAREST_NEIGHBOR : info.interpolation_policy;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000127 auto scale_kernel = std::make_unique<NEScaleKernel>();
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100128 switch(policy_to_use)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129 {
130 case InterpolationPolicy::NEAREST_NEIGHBOR:
131 {
132 TensorInfo tensor_info_offsets(shape, Format::S32);
133 _offsets.allocator()->init(tensor_info_offsets);
134
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100135 scale_kernel->configure(input, nullptr, nullptr, &_offsets, output, info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136
137 // Allocate once the configure methods have been called
138 _offsets.allocator()->allocate();
139
140 // Pre-compute offsets for nearest interpolation
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100141 precompute_dx_dy_offsets(nullptr, nullptr, &_offsets, wr, hr, info.sampling_policy, is_align_corners_used);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142 break;
143 }
144 case InterpolationPolicy::BILINEAR:
145 {
146 TensorInfo tensor_info_offsets(shape, Format::S32);
147 TensorInfo tensor_info_dxdy(shape, Format::F32);
148
149 _offsets.allocator()->init(tensor_info_offsets);
150 _dx.allocator()->init(tensor_info_dxdy);
151 _dy.allocator()->init(tensor_info_dxdy);
152
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100153 scale_kernel->configure(input, &_dx, &_dy, &_offsets, output, info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154
155 // Allocate once the configure methods have been called
156 _offsets.allocator()->allocate();
157 _dx.allocator()->allocate();
158 _dy.allocator()->allocate();
159
160 // Pre-compute dx, dy and offsets for bilinear interpolation
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100161 precompute_dx_dy_offsets(&_dx, &_dy, &_offsets, wr, hr, info.sampling_policy, is_align_corners_used);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162 break;
163 }
164 case InterpolationPolicy::AREA:
165 {
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100166 scale_kernel->configure(input, nullptr, nullptr, nullptr, output, info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 break;
168 }
169 default:
170 ARM_COMPUTE_ERROR("Unsupported interpolation mode");
171 }
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100172 _kernel = std::move(scale_kernel);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100173}
174
175Status NEScale::validate(const ITensorInfo *input, const ITensorInfo *output, const ScaleKernelInfo &info)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100176{
177 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100178 ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100179
180 ITensorInfo *offsets = nullptr;
181 ITensorInfo *dx = nullptr;
182 ITensorInfo *dy = nullptr;
183
184 // Get data layout and width/height indices
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000185 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? input->data_layout() : info.data_layout;
Georgios Pinitas20b43132018-05-14 16:05:23 +0100186 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
187 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
188
189 // Get the tensor shape of auxilary buffers
190 const TensorShape shape(output->dimension(idx_width), output->dimension(idx_height));
191
192 TensorInfo tensor_info_offsets(shape, Format::S32);
193 TensorInfo tensor_info_dx(shape, Format::F32);
194 TensorInfo tensor_info_dy(shape, Format::F32);
195
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100196 switch(info.interpolation_policy)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100197 {
198 case InterpolationPolicy::NEAREST_NEIGHBOR:
199 offsets = &tensor_info_offsets;
200 break;
201 case InterpolationPolicy::BILINEAR:
202 offsets = &tensor_info_offsets;
203 dx = &tensor_info_dx;
204 dy = &tensor_info_dy;
205 break;
206 default:
207 break;
208 }
209
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100210 ARM_COMPUTE_RETURN_ON_ERROR(NEScaleKernel::validate(input->clone().get(), dx, dy, offsets, output->clone().get(), info));
211 return Status{};
212}
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100213} // namespace arm_compute