24 #ifndef SRC_CORE_HELPERS_POOLINGHELPERS_H
25 #define SRC_CORE_HELPERS_POOLINGHELPERS_H
36 inline 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,
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;
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);
61 start_x = std::max(0, start_x);
62 start_y = std::max(0, start_y);
63 start_z = std::max(0, start_z);
65 return 1.f / ((end_y - start_y) * (end_x - start_x) * (end_z - start_z));
68 inline float calculate_avg_scale_pool2d(
bool exclude_padding,
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,
83 int start_x =
id[
idx_width] * stride_x - pad_x;
84 int start_y =
id[
idx_height] * stride_y - pad_y;
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);
90 start_x = std::max(0, start_x);
91 start_y = std::max(0, start_y);
93 return 1.f / ((end_y - start_y) * (end_x - start_x));
97 inline typename std::enable_if<std::is_same<T, int8_t>::value, int8_t>
::type
98 quantize(
float val,
const UniformQuantizationInfo &
info)
103 template <
typename T>
104 inline typename std::enable_if<std::is_same<T, uint8_t>::value, uint8_t>
::type
105 quantize(
float val,
const UniformQuantizationInfo &
info)
110 template <
typename T>
111 inline T vcvtq_q32_f32(float32x4_t values);
114 inline uint32x4_t vcvtq_q32_f32(float32x4_t values)
116 return vcvtq_u32_f32(values);
120 inline int32x4_t vcvtq_q32_f32(float32x4_t values)
122 return vcvtq_s32_f32(values);
125 template <
typename T>
126 inline float32x4_t vcvtq_f32_q32(T values);
129 inline float32x4_t vcvtq_f32_q32(uint32x4_t values)
131 return vcvtq_f32_u32(values);
135 inline float32x4_t vcvtq_f32_q32(int32x4_t values)
137 return vcvtq_f32_s32(values);
140 template <
typename Tout>
141 inline 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);
147 inline 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)
152 const float new_scale = quant_rescale / scale_pooling;
153 return vquantize(acc, UniformQuantizationInfo(new_scale, new_offset));
157 inline 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)
162 const float new_scale = quant_rescale / scale_pooling;
163 return vquantize_signed(acc, UniformQuantizationInfo(new_scale, new_offset));
166 template <
typename Tin,
typename Tout>
167 inline Tout vrequantize_pooling(Tin vec1, Tin vec2,
const UniformQuantizationInfo &requant_qinfo);
170 inline uint8x16_t vrequantize_pooling(uint8x8_t vec1, uint8x8_t vec2,
const UniformQuantizationInfo &requant_qinfo)
172 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))))),
182 inline int8x16_t vrequantize_pooling(int8x8_t vec1, int8x8_t vec2,
const UniformQuantizationInfo &requant_qinfo)
184 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))))),
193 template <
typename T>
194 inline T vrequantize_pooling(T &vec,
const UniformQuantizationInfo &requant_qinfo);
197 inline uint8x8_t vrequantize_pooling(uint8x8_t &vec,
const UniformQuantizationInfo &requant_qinfo)
199 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))))),
207 inline int8x8_t vrequantize_pooling(int8x8_t &vec,
const UniformQuantizationInfo &requant_qinfo)
209 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))))),