1 /*
2 * Copyright (c) 2022 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
25 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
26
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/ITensorPack.h"
29 #include "arm_compute/core/Window.h"
30 #include "src/core/NEON/NEMath.h"
31 #include "src/core/NEON/wrapper/wrapper.h"
32 #include "src/core/helpers/ScaleHelpers.h"
33 #include "src/core/utils/ScaleUtils.h"
34 #include "support/Rounding.h"
35
36 #include <arm_neon.h>
37 #include <cmath>
38 #include <cstddef>
39
40 namespace arm_compute
41 {
42 namespace
43 {
fp16_neon_scale_nearest(const ITensor * src,ITensor * dst,const ITensor * offsets,float sampling_offset,bool align_corners,const Window & window)44 void fp16_neon_scale_nearest(const ITensor *src, ITensor *dst, const ITensor *offsets,
45 float sampling_offset, bool align_corners, const Window &window)
46 {
47 const size_t in_stride_c = src->info()->dimension(0) + src->info()->padding().left + src->info()->padding().right;
48 const size_t in_stride_w = src->info()->dimension(1) + src->info()->padding().top + src->info()->padding().bottom;
49 const size_t in_stride_wc = in_stride_w * in_stride_c;
50 const size_t in_dim_h = src->info()->dimension(2);
51
52 // Compute the ratio between source height and destination height
53 const auto hr = scale_utils::calculate_resize_ratio(in_dim_h, dst->info()->dimension(2), align_corners);
54 const auto window_start_x = static_cast<int32_t>(window.x().start());
55 const auto window_end_x = static_cast<int32_t>(window.x().end());
56 const int window_step_x = 8;
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 int32_t x = window_start_x;
71 const float16_t *in_ptr = reinterpret_cast<const float16_t *>(in_ptr_start + in_stride_bytes_hwc * id[3]);
72
73 for(; x <= window_end_x - window_step_x; x += window_step_x)
74 {
75 wrapper::vstore(reinterpret_cast<float16_t *>(out.ptr()) + x,
76 wrapper::vloadq(in_ptr + offset + offset_row + x));
77 }
78 for(; x < window_end_x; ++x)
79 {
80 *(reinterpret_cast<float16_t *>(out.ptr()) + x) = *(in_ptr + offset + offset_row + x);
81 }
82 },
83 out);
84 }
85
fp16_neon_scale_bilinear(const ITensor * src,ITensor * dst,const ITensor * offsets,const ITensor * dx,const ITensor * dy,BorderMode border_mode,PixelValue constant_border_value,float sampling_offset,bool align_corners,const Window & window)86 void fp16_neon_scale_bilinear(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy,
87 BorderMode border_mode, PixelValue constant_border_value, float sampling_offset,
88 bool align_corners, const Window &window)
89 {
90 // Compute the ratio between source height and destination height
91 const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(2), dst->info()->dimension(2), align_corners);
92
93 Iterator out(dst, window);
94 const int in_stride_c = src->info()->dimension(0) + src->info()->padding().left + src->info()->padding().right;
95 const int in_dim_w = src->info()->dimension(1);
96 const int in_dim_h = src->info()->dimension(2);
97 const int in_stride_wc = in_stride_c * (in_dim_w + src->info()->padding().top + src->info()->padding().bottom);
98
99 // Don't increment in Y and Z direction for the input tensor
100 // A pointer to the start of this plane is needed as base for the precomputed offsets
101 Window win_in(window);
102 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
103 win_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
104 Iterator in(src, win_in);
105
106 if(border_mode == BorderMode::CONSTANT)
107 {
108 using ConstType = typename std::conditional<std::is_same<float16_t, float16_t>::value, half, float16_t>::type;
109
110 const float16_t const_border_value = static_cast<float16_t>(constant_border_value.get<ConstType>());
111 execute_window_loop(window, [&](const Coordinates & id)
112 {
113 const auto offset = *reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id.y(), id.z())));
114 const auto dx_val = *reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id.y(), id.z())));
115 const auto dy_val = *reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id.y(), id.z())));
116 const int32_t in_hi = std::floor((id.z() + sampling_offset) * hr - sampling_offset);
117 const float16_t *in_ptr = reinterpret_cast<const float16_t *>(in.ptr()) + offset * in_stride_c + in_hi * in_stride_wc;
118
119 const auto a00 = (0 <= offset && offset < in_dim_w && 0 <= in_hi && in_hi < in_dim_h) ? *in_ptr : const_border_value;
120 const auto a01 = (-1 <= offset && offset < in_dim_w - 1 && 0 <= in_hi && in_hi < in_dim_h) ? *(in_ptr + in_stride_c) : const_border_value;
121 const auto a10 = (0 <= offset && offset < in_dim_w && -1 <= in_hi && in_hi < in_dim_h - 1) ? *(in_ptr + in_stride_wc) : const_border_value;
122 const auto a11 = (-1 <= offset && offset < in_dim_w - 1 && -1 <= in_hi && in_hi < in_dim_h - 1) ? *(in_ptr + in_stride_c + in_stride_wc) : const_border_value;
123
124 *reinterpret_cast<float16_t *>(out.ptr()) = static_cast<float16_t>(scale_helpers::delta_bilinear(a00, a01, a10, a11, dx_val, dy_val));
125 },
126 in, out);
127 }
128 else if(border_mode == BorderMode::REPLICATE)
129 {
130 execute_window_loop(window, [&](const Coordinates & id)
131 {
132 const auto offset = *reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id.y(), id.z())));
133 const auto dx_val = *reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id.y(), id.z())));
134 const auto dy_val = *reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id.y(), id.z())));
135 const int in_hi = std::floor((id.z() + sampling_offset) * hr - sampling_offset);
136
137 auto clamped_w = utility::clamp<int>(offset, 0, in_dim_w - 1);
138 auto clamped_w1 = utility::clamp<int>(offset + 1, 0, in_dim_w - 1);
139 auto clamped_h = utility::clamp<int>(in_hi, 0, in_dim_h - 1);
140 auto clamped_h1 = utility::clamp<int>(in_hi + 1, 0, in_dim_h - 1);
141
142 const auto a00 = *(reinterpret_cast<const float16_t *>(in.ptr()) + clamped_w * in_stride_c + clamped_h * in_stride_wc);
143 const auto a01 = *(reinterpret_cast<const float16_t *>(in.ptr()) + clamped_w1 * in_stride_c + clamped_h * in_stride_wc);
144 const auto a10 = *(reinterpret_cast<const float16_t *>(in.ptr()) + clamped_w * in_stride_c + clamped_h1 * in_stride_wc);
145 const auto a11 = *(reinterpret_cast<const float16_t *>(in.ptr()) + clamped_w1 * in_stride_c + clamped_h1 * in_stride_wc);
146
147 *reinterpret_cast<float16_t *>(out.ptr()) = static_cast<float16_t>(scale_helpers::delta_bilinear(a00, a01, a10, a11, dx_val, dy_val));
148 },
149 in, out);
150 }
151 else
152 {
153 ARM_COMPUTE_ERROR("Not implemented");
154 }
155 }
156 }
157 namespace cpu
158 {
fp16_neon_scale(const ITensor * src,ITensor * dst,const ITensor * offsets,const ITensor * dx,const ITensor * dy,InterpolationPolicy policy,BorderMode border_mode,PixelValue constant_border_value,float sampling_offset,bool align_corners,const Window & window)159 void fp16_neon_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy,
160 InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset,
161 bool align_corners, const Window &window)
162 {
163 if(policy == InterpolationPolicy::BILINEAR)
164 {
165 fp16_neon_scale_bilinear(src, dst, offsets, dx, dy, border_mode, constant_border_value, sampling_offset, align_corners, window);
166 }
167 else if(policy == InterpolationPolicy::NEAREST_NEIGHBOR)
168 {
169 fp16_neon_scale_nearest(src, dst, offsets, sampling_offset, align_corners, window);
170 }
171 }
172 } // namespace cpu
173 } // namespace arm_compute
174
175 #endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */