blob: 676ca94fb06ee1171bac54cf538043bd8caae5bd [file] [log] [blame]
Sheri Zhang23adc4c2021-01-05 12:48:45 +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 */
24#include "arm_compute/core/Helpers.h"
25#include "arm_compute/core/ITensorPack.h"
26#include "arm_compute/core/Window.h"
27#include "src/core/NEON/NEMath.h"
28#include "src/core/NEON/wrapper/wrapper.h"
29#include "src/core/common/Validate.h"
30#include "src/core/helpers/ScaleHelpers.h"
31#include "src/core/helpers/ScaleHelpers.h"
32#include "src/core/utils/ScaleUtils.h"
33#include "support/Rounding.h"
34
35#include <cmath>
36#include <cstddef>
37
38#if defined(__ARM_FEATURE_SVE)
39#include <arm_sve.h>
40
41namespace arm_compute
42{
43namespace
44{
45void qasymm8_signed_sve_scale_nearest(const ITensor *src, ITensor *dst, const ITensor *offsets,
46 float sampling_offset, bool align_corners, const Window &window)
47{
48 const size_t in_stride_c = src->info()->dimension(0) + src->info()->padding().left + src->info()->padding().right;
49 const size_t in_stride_w = src->info()->dimension(1) + src->info()->padding().top + src->info()->padding().bottom;
50 const size_t in_stride_wc = in_stride_w * in_stride_c;
51 const size_t in_dim_h = src->info()->dimension(2);
52
53 // Compute the ratio between source height and destination height
54 const auto hr = scale_utils::calculate_resize_ratio(in_dim_h, dst->info()->dimension(2), align_corners);
55 const auto window_start_x = static_cast<int32_t>(window.x().start());
56 const auto window_end_x = static_cast<int32_t>(window.x().end());
57
58 Window win(window);
59 win.set(Window::DimX, Window::Dimension(0, 1, 1));
60 Iterator out(dst, win);
61
62 const uint8_t *in_ptr_start = src->buffer() + src->info()->offset_first_element_in_bytes();
63 const unsigned int in_stride_bytes_hwc = src->info()->strides_in_bytes()[3];
64
65 execute_window_loop(win, [&](const Coordinates & id)
66 {
67 const int32_t offset = *reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id.y(), id.z()))) * in_stride_c;
68 const auto in_hi = static_cast<int>(align_corners ? utils::rounding::round_half_away_from_zero((id.z() + sampling_offset) * hr) : std::floor((id.z() + sampling_offset) * hr));
69 const int offset_row = in_hi * in_stride_wc;
70 const auto in_ptr = reinterpret_cast<const int8_t *>(in_ptr_start + in_stride_bytes_hwc * id[3]);
71 const auto out_ptr = reinterpret_cast<int8_t *>(out.ptr());
72
73 // Compute S elements per iteration
74 int x = window_start_x;
75 svbool_t pg = svwhilelt_b8(x, window_end_x);
76 do
77 {
78 // Store results
79 svst1_s8(pg, out_ptr + x, svld1_s8(pg, in_ptr + offset + offset_row + x));
80
81 x += svcntw();
82 pg = svwhilelt_b8(x, window_end_x);
83 }
84 while(svptest_any(svptrue_b8(), pg));
85 },
86 out);
87}
88
89void qasymm8_signed_sve_scale_bilinear(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy,
90 BorderMode border_mode, PixelValue constant_border_value, float sampling_offset,
91 bool align_corners, const Window &window)
92{
93 // Get data layout and width/height indices
94 const DataLayout data_layout = src->info()->data_layout();
95 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
96 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
97
98 // Compute the ratio between source height and destination height
99 const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(idx_height), dst->info()->dimension(idx_height), align_corners);
100 Window win_off;
101 win_off.set(Window::DimX, Window::Dimension(0, 0, 0));
102 win_off.set(Window::DimY, Window::Dimension(0, 0, 0));
103
104 // Don't increment in X and Y direction for the input tensor
105 // A pointer to the start of this plane is needed as base for the precomputed offsets
106 Window win_in(window);
107 win_in.set(idx_width, Window::Dimension(0, 0, 0));
108 win_in.set(idx_height, Window::Dimension(0, 0, 0));
109
110 for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
111 {
112 win_off.set(d, Window::Dimension(0, 0, 0));
113 }
114
115 Iterator in(src, win_in);
116 Iterator out(dst, window);
117
118 const int32_t in_dim_w = src->info()->dimension(idx_width);
119 const int32_t in_dim_h = src->info()->dimension(idx_height);
120 const int32_t stride_w = src->info()->strides_in_bytes()[idx_width];
121 const int32_t stride_h = src->info()->strides_in_bytes()[idx_height];
122
123 const UniformQuantizationInfo iq_info = src->info()->quantization_info().uniform();
124 const UniformQuantizationInfo oq_info = dst->info()->quantization_info().uniform();
125
126 if(border_mode == BorderMode::CONSTANT)
127 {
128 const int8_t const_border_value = static_cast<int8_t>(constant_border_value.get<int8_t>());
129 execute_window_loop(window, [&](const Coordinates & id)
130 {
131 const int32_t index_h = std::floor((id[idx_height] + sampling_offset) * hr - sampling_offset);
132 const int32_t index_w = *(reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
133 const auto dx_val = *(reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
134 const auto dy_val = *(reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
135 const auto pixel_row_ptr = reinterpret_cast<const int8_t *>(in.ptr());
136
137 const auto a00 = (0 <= index_w && index_w < in_dim_w && 0 <= index_h && index_h < in_dim_h) ?
138 (*(pixel_row_ptr + index_w * stride_w + index_h * stride_h)) :
139 const_border_value;
140 const auto a01 = (-1 <= index_w && index_w < in_dim_w - 1 && 0 <= index_h && index_h < in_dim_h) ?
141 (*(pixel_row_ptr + (index_w + 1) * stride_w + index_h * stride_h)) :
142 const_border_value;
143 const auto a10 = (0 <= index_w && index_w < in_dim_w && -1 <= index_h && index_h < in_dim_h - 1) ?
144 (*(pixel_row_ptr + index_w * stride_w + (index_h + 1) * stride_h)) :
145 const_border_value;
146 const auto a11 = (-1 <= index_w && index_w < in_dim_w - 1 && -1 <= index_h && index_h < in_dim_h - 1) ?
147 (*(pixel_row_ptr + (index_w + 1) * stride_w + (index_h + 1) * stride_h)) :
148 const_border_value;
149
150 const float inp00 = Qasymm8QuantizationHelper<int8_t>::dequantize(a00, iq_info);
151 const float inp01 = Qasymm8QuantizationHelper<int8_t>::dequantize(a01, iq_info);
152 const float inp10 = Qasymm8QuantizationHelper<int8_t>::dequantize(a10, iq_info);
153 const float inp11 = Qasymm8QuantizationHelper<int8_t>::dequantize(a11, iq_info);
154 *reinterpret_cast<int8_t *>(out.ptr()) = Qasymm8QuantizationHelper<int8_t>::quantize(scale_helpers::delta_bilinear(inp00, inp01, inp10, inp11, dx_val, dy_val), oq_info);
155 },
156 in, out);
157 }
158 else if(border_mode == BorderMode::REPLICATE)
159 {
160 execute_window_loop(window, [&](const Coordinates & id)
161 {
162 const int index_h = std::floor((id[idx_height] + sampling_offset) * hr - sampling_offset);
163 const int32_t index_w = *(reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
164 const auto dx_val = *(reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
165 const auto dy_val = *(reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
166 const auto pixel_row_ptr = reinterpret_cast<const int8_t *>(in.ptr());
167
168 auto clamped_w = utility::clamp<int>(index_w, 0, in_dim_w - 1);
169 auto clamped_w1 = utility::clamp<int>(index_w + 1, 0, in_dim_w - 1);
170 auto clamped_h = utility::clamp<int>(index_h, 0, in_dim_h - 1);
171 auto clamped_h1 = utility::clamp<int>(index_h + 1, 0, in_dim_h - 1);
172
173 const auto a00 = *(pixel_row_ptr + clamped_w * stride_w + clamped_h * stride_h);
174 const auto a01 = *(pixel_row_ptr + clamped_w1 * stride_w + clamped_h * stride_h);
175 const auto a10 = *(pixel_row_ptr + clamped_w * stride_w + clamped_h1 * stride_h);
176 const auto a11 = *(pixel_row_ptr + clamped_w1 * stride_w + clamped_h1 * stride_h);
177
178 const float inp00 = Qasymm8QuantizationHelper<int8_t>::dequantize(a00, iq_info);
179 const float inp01 = Qasymm8QuantizationHelper<int8_t>::dequantize(a01, iq_info);
180 const float inp10 = Qasymm8QuantizationHelper<int8_t>::dequantize(a10, iq_info);
181 const float inp11 = Qasymm8QuantizationHelper<int8_t>::dequantize(a11, iq_info);
182 *reinterpret_cast<int8_t *>(out.ptr()) = Qasymm8QuantizationHelper<int8_t>::quantize(scale_helpers::delta_bilinear(inp00, inp01, inp10, inp11, dx_val, dy_val), oq_info);
183 },
184 in, out);
185 }
186 else
187 {
188 ARM_COMPUTE_ERROR("Not implemented");
189 }
190}
191}
192namespace cpu
193{
194void qasymm8_signed_sve_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy,
195 InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset,
196 bool align_corners, const Window &window)
197{
198 if(policy == InterpolationPolicy::BILINEAR)
199 {
200 qasymm8_signed_sve_scale_bilinear(src, dst, offsets, dx, dy, border_mode, constant_border_value, sampling_offset, align_corners, window);
201 }
202 else if(policy == InterpolationPolicy::NEAREST_NEIGHBOR)
203 {
204 qasymm8_signed_sve_scale_nearest(src, dst, offsets, sampling_offset, align_corners, window);
205 }
206}
207} // namespace cpu
208} // namespace arm_compute
209
210#endif // __ARM_FEATURE_SVE