blob: 9ef045f472e3a45c7d577c1c35758fdd6cf70772 [file] [log] [blame]
Adnan AlSinan9104cd52022-04-06 16:19:31 +01001/*
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#ifndef SRC_CORE_HELPERS_POOLINGHELPERS_H
25#define SRC_CORE_HELPERS_POOLINGHELPERS_H
26
27#include "src/core/NEON/NEAsymm.h"
28
29namespace arm_compute
30{
31namespace cpu
32{
33namespace
34{
35
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010036inline float calculate_avg_scale_pool3d(bool exclude_padding,
37 const Coordinates &id,
38 const int pool_size_x,
39 const int pool_size_y,
40 const int pool_size_z,
41 const int upper_bound_w,
42 const int upper_bound_h,
43 const int upper_bound_d,
44 const int pad_x,
45 const int pad_y,
46 const int pad_z,
47 const int stride_x,
48 const int stride_y,
49 const int stride_z)
Adnan AlSinan9104cd52022-04-06 16:19:31 +010050{
51 // Based on NDHWC
52 int start_x = id[1] * stride_x - pad_x;
53 int start_y = id[2] * stride_y - pad_y;
54 int start_z = id[3] * stride_z - pad_z;
55
56 const int end_x = std::min(start_x + pool_size_x, upper_bound_w);
57 const int end_y = std::min(start_y + pool_size_y, upper_bound_h);
58 const int end_z = std::min(start_z + pool_size_z, upper_bound_d);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059 if (exclude_padding)
Adnan AlSinan9104cd52022-04-06 16:19:31 +010060 {
61 start_x = std::max(0, start_x);
62 start_y = std::max(0, start_y);
63 start_z = std::max(0, start_z);
64 }
65 return 1.f / ((end_y - start_y) * (end_x - start_x) * (end_z - start_z));
66}
67
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010068inline float calculate_avg_scale_pool2d(bool exclude_padding,
69 DataLayout data_layout,
70 const Coordinates &id,
71 const int pool_size_x,
72 const int pool_size_y,
73 const int upper_bound_w,
74 const int upper_bound_h,
75 const int pad_x,
76 const int pad_y,
77 const int stride_x,
78 const int stride_y)
Adnan AlSinan9104cd52022-04-06 16:19:31 +010079{
80 const unsigned int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
81 const unsigned int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
82
83 int start_x = id[idx_width] * stride_x - pad_x;
84 int start_y = id[idx_height] * stride_y - pad_y;
85
86 const int end_x = std::min(start_x + pool_size_x, upper_bound_w);
87 const int end_y = std::min(start_y + pool_size_y, upper_bound_h);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 if (exclude_padding)
Adnan AlSinan9104cd52022-04-06 16:19:31 +010089 {
90 start_x = std::max(0, start_x);
91 start_y = std::max(0, start_y);
92 }
93 return 1.f / ((end_y - start_y) * (end_x - start_x));
94}
95
96template <typename T>
97inline typename std::enable_if<std::is_same<T, int8_t>::value, int8_t>::type
98quantize(float val, const UniformQuantizationInfo &info)
99{
100 return quantize_qasymm8_signed(val, info);
101}
102
103template <typename T>
104inline typename std::enable_if<std::is_same<T, uint8_t>::value, uint8_t>::type
105quantize(float val, const UniformQuantizationInfo &info)
106{
107 return quantize_qasymm8(val, info);
108}
109
110template <typename T>
111inline T vcvtq_q32_f32(float32x4_t values);
112
113template <>
114inline uint32x4_t vcvtq_q32_f32(float32x4_t values)
115{
116 return vcvtq_u32_f32(values);
117}
118
119template <>
120inline int32x4_t vcvtq_q32_f32(float32x4_t values)
121{
122 return vcvtq_s32_f32(values);
123}
124
125template <typename T>
126inline float32x4_t vcvtq_f32_q32(T values);
127
128template <>
129inline float32x4_t vcvtq_f32_q32(uint32x4_t values)
130{
131 return vcvtq_f32_u32(values);
132}
133
134template <>
135inline float32x4_t vcvtq_f32_q32(int32x4_t values)
136{
137 return vcvtq_f32_s32(values);
138}
139
140template <typename Tout>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100141inline Tout vrequantize_pooling_with_scale(const float32x4x4_t &acc,
142 const float quant_rescale,
143 const float scale_pooling,
144 const int32_t new_offset);
Adnan AlSinan9104cd52022-04-06 16:19:31 +0100145
146template <>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100147inline uint8x16_t vrequantize_pooling_with_scale(const float32x4x4_t &acc,
148 const float quant_rescale,
149 const float scale_pooling,
150 const int32_t new_offset)
Adnan AlSinan9104cd52022-04-06 16:19:31 +0100151{
152 const float new_scale = quant_rescale / scale_pooling;
153 return vquantize(acc, UniformQuantizationInfo(new_scale, new_offset));
154}
155
156template <>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100157inline int8x16_t vrequantize_pooling_with_scale(const float32x4x4_t &acc,
158 const float quant_rescale,
159 const float scale_pooling,
160 const int32_t new_offset)
Adnan AlSinan9104cd52022-04-06 16:19:31 +0100161{
162 const float new_scale = quant_rescale / scale_pooling;
163 return vquantize_signed(acc, UniformQuantizationInfo(new_scale, new_offset));
164}
165
166template <typename Tin, typename Tout>
167inline Tout vrequantize_pooling(Tin vec1, Tin vec2, const UniformQuantizationInfo &requant_qinfo);
168
169template <>
170inline uint8x16_t vrequantize_pooling(uint8x8_t vec1, uint8x8_t vec2, const UniformQuantizationInfo &requant_qinfo)
171{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100172 const float32x4x4_t acc = {{
173 vcvtq_f32_u32(vmovl_u16(vget_low_u16(vmovl_u8((vec1))))),
174 vcvtq_f32_u32(vmovl_u16(vget_high_u16(vmovl_u8((vec1))))),
175 vcvtq_f32_u32(vmovl_u16(vget_low_u16(vmovl_u8((vec2))))),
176 vcvtq_f32_u32(vmovl_u16(vget_high_u16(vmovl_u8((vec2))))),
177 }};
Adnan AlSinan9104cd52022-04-06 16:19:31 +0100178 return vquantize(acc, requant_qinfo);
179}
180
181template <>
182inline int8x16_t vrequantize_pooling(int8x8_t vec1, int8x8_t vec2, const UniformQuantizationInfo &requant_qinfo)
183{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100184 const float32x4x4_t acc = {{
185 vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8((vec1))))),
186 vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8((vec1))))),
187 vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8((vec2))))),
188 vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8((vec2))))),
189 }};
Adnan AlSinan9104cd52022-04-06 16:19:31 +0100190 return vquantize_signed(acc, requant_qinfo);
191}
192
193template <typename T>
194inline T vrequantize_pooling(T &vec, const UniformQuantizationInfo &requant_qinfo);
195
196template <>
197inline uint8x8_t vrequantize_pooling(uint8x8_t &vec, const UniformQuantizationInfo &requant_qinfo)
198{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100199 const float32x4x2_t acc = {{
200 vcvtq_f32_u32(vmovl_u16(vget_low_u16(vmovl_u8((vec))))),
201 vcvtq_f32_u32(vmovl_u16(vget_high_u16(vmovl_u8((vec))))),
202 }};
Adnan AlSinan9104cd52022-04-06 16:19:31 +0100203 return vquantize(acc, requant_qinfo);
204}
205
206template <>
207inline int8x8_t vrequantize_pooling(int8x8_t &vec, const UniformQuantizationInfo &requant_qinfo)
208{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100209 const float32x4x2_t acc = {{
210 vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8((vec))))),
211 vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8((vec))))),
212 }};
Adnan AlSinan9104cd52022-04-06 16:19:31 +0100213 return vquantize_signed(acc, requant_qinfo);
214}
215
216} // namespace
217} // namespace cpu
218} // namespace arm_compute
219#endif /* SRC_CORE_HELPERS_POOLINGHELPERS_H */