1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
#include "caffe2/quantization/server/pool_dnnlowp_op_avx2.h"
#include <immintrin.h>
#include <algorithm>
#include <cmath>
namespace caffe2 {
using namespace std;
void max_pool_avx2(
const uint8_t* Xdata,
int n,
int height,
int width,
int channels,
int pooled_height,
int pooled_width,
int kernel_h,
int kernel_w,
int stride_h,
int stride_w,
int pad_t,
int pad_l,
uint8_t* Ydata) {
const uint8_t* Xdata_temp = Xdata + n * height * width * channels;
uint8_t* Ydata_temp = Ydata + n * pooled_height * pooled_width * channels;
for (int ph = 0; ph < pooled_height; ++ph) {
int hstart = ph * stride_h - pad_t;
int hend = hstart + kernel_h < height ? hstart + kernel_h : height;
hstart = hstart > 0 ? hstart : 0;
for (int pw = 0; pw < pooled_width; ++pw) {
int wstart = pw * stride_w - pad_l;
int wend = wstart + kernel_w < width ? wstart + kernel_w : width;
wstart = wstart > 0 ? wstart : 0;
uint8_t* Yh = Ydata_temp + (ph * pooled_width + pw) * channels;
constexpr int VLEN = 32;
// vectorized loop
for (int c = 0; c < channels / VLEN * VLEN; c += VLEN) {
__m256i Y_v = _mm256_setzero_si256();
for (int h = hstart; h < hend; ++h) {
for (int w = wstart; w < wend; ++w) {
const int input_idx = (h * width + w) * channels + c;
Y_v = _mm256_max_epu8(
_mm256_loadu_si256(
reinterpret_cast<const __m256i*>(Xdata_temp + input_idx)),
Y_v);
}
}
_mm256_storeu_si256(reinterpret_cast<__m256i*>(Yh + c), Y_v);
}
// remainder
for (int c = channels / VLEN * VLEN; c < channels; ++c) {
Yh[c] = 0;
}
for (int h = hstart; h < hend; ++h) {
for (int w = wstart; w < wend; ++w) {
for (int c = channels / VLEN * VLEN; c < channels; ++c) {
const int input_idx = (h * width + w) * channels + c;
Yh[c] =
Xdata_temp[input_idx] > Yh[c] ? Xdata_temp[input_idx] : Yh[c];
}
}
}
} // pw loop
} // ph loop
}
void average_pool_avx2(
const uint8_t* Xdata,
int n,
int height,
int width,
int channels,
int pooled_height,
int pooled_width,
int kernel_h,
int kernel_w,
int stride_h,
int stride_w,
int pad_t,
int pad_l,
uint8_t* Ydata,
float in_scale,
float out_scale,
int32_t in_zero_point,
int32_t out_zero_point,
int32_t minimum,
int32_t maximum) {
const uint8_t* Xdata_temp = Xdata + n * height * width * channels;
uint8_t* Ydata_temp = Ydata + n * pooled_height * pooled_width * channels;
const __m256i shuffle_mask_v = _mm256_set_epi8(
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
0x0c,
0x08,
0x04,
0x00,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
0x0c,
0x08,
0x04,
0x00);
const __m256i permute_mask_v =
_mm256_set_epi32(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00);
const __m256i min_v = _mm256_set1_epi32(minimum);
const __m256i max_v = _mm256_set1_epi32(maximum);
__m256 out_zero_point_v = _mm256_set1_ps(out_zero_point);
for (int ph = 0; ph < pooled_height; ++ph) {
int hstart = ph * stride_h - pad_t;
int hend = hstart + kernel_h < height ? hstart + kernel_h : height;
hstart = hstart > 0 ? hstart : 0;
for (int pw = 0; pw < pooled_width; ++pw) {
int wstart = pw * stride_w - pad_l;
int wend = wstart + kernel_w < width ? wstart + kernel_w : width;
wstart = wstart > 0 ? wstart : 0;
int size = (hend - hstart) * (wend - wstart);
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
float multiplier = in_scale / out_scale / size;
__m256 multiplier_v = _mm256_set1_ps(multiplier);
uint8_t* Yh = Ydata_temp + (ph * pooled_width + pw) * channels;
constexpr int VLEN = 8;
int32_t Yh0 = -in_zero_point * size;
// vectorized loop
for (int c = 0; c < channels / VLEN * VLEN; c += VLEN) {
__m256i Yh0_v = _mm256_set1_epi32(Yh0);
for (int h = hstart; h < hend; ++h) {
for (int w = wstart; w < wend; ++w) {
const int input_idx = (h * width + w) * channels + c;
const __m256i temp_v = _mm256_cvtepu8_epi32(_mm_loadl_epi64(
reinterpret_cast<const __m128i*>(Xdata_temp + input_idx)));
Yh0_v = _mm256_add_epi32(Yh0_v, temp_v);
}
}
__m256 Yh0_fp = _mm256_cvtepi32_ps(Yh0_v);
__m256 Y_float_v =
_mm256_fmadd_ps(Yh0_fp, multiplier_v, out_zero_point_v);
__m256i Y_rounded_v = _mm256_cvtps_epi32(Y_float_v);
__m256i Y_clipped_v =
_mm256_max_epi32(min_v, _mm256_min_epi32(max_v, Y_rounded_v));
Y_clipped_v = _mm256_shuffle_epi8(Y_clipped_v, shuffle_mask_v);
Y_clipped_v = _mm256_permutevar8x32_epi32(Y_clipped_v, permute_mask_v);
*reinterpret_cast<int64_t*>(Yh + c) =
_mm256_extract_epi64(Y_clipped_v, 0);
}
// remainder
for (int c = channels / VLEN * VLEN; c < channels; ++c) {
Yh[c] = 0;
}
for (int c = channels / VLEN * VLEN; c < channels; ++c) {
const int pool_idx = (ph * pooled_width + pw) * channels + c;
int32_t Yh_t = -in_zero_point * size;
for (int h = hstart; h < hend; ++h) {
for (int w = wstart; w < wend; ++w) {
const int input_idx = (h * width + w) * channels + c;
Yh_t += Xdata_temp[input_idx];
}
}
Ydata_temp[pool_idx] = std::min<int32_t>(
std::max<int32_t>(
nearbyint(Yh_t * multiplier + out_zero_point), minimum),
maximum);
}
} // pw loop
} // ph loop
}
void average_pool_3d_avx2(
const uint8_t* Xdata,
int n,
int height,
int width,
int depth,
int channels,
int pooled_height,
int pooled_width,
int pooled_depth,
int kernel_h,
int kernel_w,
int kernel_d,
int stride_h,
int stride_w,
int stride_d,
int pad_t,
int pad_l,
int pad_d,
uint8_t* Ydata,
float in_scale,
float out_scale,
int32_t in_zero_point,
int32_t out_zero_point,
int32_t minimum,
int32_t maximum) {
const uint8_t* Xdata_temp = Xdata + n * height * width * depth * channels;
uint8_t* Ydata_temp =
Ydata + n * pooled_height * pooled_width * pooled_depth * channels;
const __m256i shuffle_mask_v = _mm256_set_epi8(
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
0x0c,
0x08,
0x04,
0x00,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
0xff,
0x0c,
0x08,
0x04,
0x00);
const __m256i permute_mask_v =
_mm256_set_epi32(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00);
const __m256i min_v = _mm256_set1_epi32(minimum);
const __m256i max_v = _mm256_set1_epi32(maximum);
__m256 out_zero_point_v = _mm256_set1_ps(out_zero_point);
for (int ph = 0; ph < pooled_height; ++ph) {
int hstart = ph * stride_h - pad_t;
int hend = hstart + kernel_h < height ? hstart + kernel_h : height;
hstart = hstart > 0 ? hstart : 0;
for (int pw = 0; pw < pooled_width; ++pw) {
int wstart = pw * stride_w - pad_l;
int wend = wstart + kernel_w < width ? wstart + kernel_w : width;
wstart = wstart > 0 ? wstart : 0;
for (int pd = 0; pd < pooled_depth; ++pd) {
int dstart = pd * stride_d - pad_d;
int dend = dstart + kernel_d < depth ? dstart + kernel_d : depth;
dstart = max(dstart, 0);
int size = (hend - hstart) * (wend - wstart) * (dend - dstart);
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
float multiplier = in_scale / out_scale / size;
__m256 multiplier_v = _mm256_set1_ps(multiplier);
uint8_t* Yh = Ydata_temp +
((ph * pooled_width + pw) * pooled_depth + pd) * channels;
constexpr int VLEN = 8;
int32_t Yh0 = -in_zero_point * size;
// vectorized loop
for (int c = 0; c < channels / VLEN * VLEN; c += VLEN) {
__m256i Yh0_v = _mm256_set1_epi32(Yh0);
for (int h = hstart; h < hend; ++h) {
for (int w = wstart; w < wend; ++w) {
for (int d = dstart; d < dend; ++d) {
const int input_idx =
((h * width + w) * depth + d) * channels + c;
const __m256i temp_v = _mm256_cvtepu8_epi32(_mm_loadl_epi64(
reinterpret_cast<const __m128i*>(Xdata_temp + input_idx)));
Yh0_v = _mm256_add_epi32(Yh0_v, temp_v);
}
}
}
__m256 Yh0_fp = _mm256_cvtepi32_ps(Yh0_v);
__m256 Y_float_v =
_mm256_fmadd_ps(Yh0_fp, multiplier_v, out_zero_point_v);
__m256i Y_rounded_v = _mm256_cvtps_epi32(Y_float_v);
__m256i Y_clipped_v =
_mm256_max_epi32(min_v, _mm256_min_epi32(max_v, Y_rounded_v));
Y_clipped_v = _mm256_shuffle_epi8(Y_clipped_v, shuffle_mask_v);
Y_clipped_v =
_mm256_permutevar8x32_epi32(Y_clipped_v, permute_mask_v);
*reinterpret_cast<int64_t*>(Yh + c) =
_mm256_extract_epi64(Y_clipped_v, 0);
}
// remainder
for (int c = channels / VLEN * VLEN; c < channels; ++c) {
Yh[c] = 0;
}
for (int c = channels / VLEN * VLEN; c < channels; ++c) {
const int pool_idx =
((ph * pooled_width + pw) * pooled_depth + pd) * channels + c;
int32_t Yh_t = -in_zero_point * size;
for (int h = hstart; h < hend; ++h) {
for (int w = wstart; w < wend; ++w) {
for (int d = dstart; d < dend; ++d) {
const int input_idx =
((h * width + w) * depth + d) * channels + c;
Yh_t += Xdata_temp[input_idx];
}
}
}
Ydata_temp[pool_idx] = std::min<int32_t>(
std::max<int32_t>(
nearbyint(Yh_t * multiplier + out_zero_point), minimum),
maximum);
}
} // pd loop
} // pw loop
} // ph loop
}
} // namespace caffe2
|