forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspmmUtilsAvx2.cc
370 lines (331 loc) · 15.6 KB
/
spmmUtilsAvx2.cc
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#define FBGEMM_EXPORTS
#include "fbgemm/spmmUtilsAvx2.h"
#if defined(__x86_64__) || defined(__i386__) || \
(defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86)))
#include <immintrin.h>
#endif
#include <cassert> //for assert
#include "./MaskAvx2.h"
namespace fbgemm {
template <
bool FUSE_RELU,
bool ACT_SYMMETRIC,
bool WEIGHT_SYMMETRIC,
bool HAS_BIAS,
QuantizationGranularity Q_GRAN>
FBGEMM_API void trRequantizeOpt(
uint8_t* out,
const int32_t* inp,
const block_type_t& block,
int ld_out,
int ld_in,
const trRequantizationParams_t& r) {
assert(
(Q_GRAN != QuantizationGranularity::GROUP) &&
"GROUP Granularity is not supported");
// Broadcasted act_times_w_scale / C_scale
__m256 act_times_w_div_c_v;
if (Q_GRAN != QuantizationGranularity::OUT_CHANNEL) {
act_times_w_div_c_v = _mm256_set1_ps(r.act_times_w_scale[0] / r.C_scale);
}
__m256i min_v = _mm256_set1_epi8(static_cast<uint8_t>(0));
__m256i max_v = _mm256_set1_epi8(static_cast<uint8_t>(255));
assert(
(ACT_SYMMETRIC == (r.act_zero_point == 0)) &&
"ACT_SYMMETRIC == true if and only if act_zero_point == 0");
assert(
(WEIGHT_SYMMETRIC ==
((Q_GRAN == QuantizationGranularity::TENSOR &&
r.weight_zero_points[0] == 0) ||
r.act_col_offsets == nullptr)) &&
"WEIGHT_SYMMETRIC == true if and only if weight_zero_point == 0 "
"or r.act_col_offsets == nullptr");
assert(
(HAS_BIAS == (r.bias != nullptr)) &&
"HAS_BIAS == true if and only if bias != nullptr");
__m256i C_zero_point_epi16_v = _mm256_set1_epi16(r.C_zero_point);
__m256i C_zero_point_epi8_v = _mm256_set1_epi8(r.C_zero_point);
__m256i permute_mask_v =
_mm256_set_epi32(0x07, 0x03, 0x06, 0x02, 0x05, 0x01, 0x04, 0x00);
constexpr int VLEN = 8;
for (int i = block.row_start; i < block.row_start + block.row_size; ++i) {
// Scale weight_row_offset with act_zero_point
int32_t row_offset = 0;
if (!ACT_SYMMETRIC) {
row_offset = r.act_zero_point * r.weight_row_offsets[i];
}
__m256i row_offset_v = _mm256_set1_epi32(row_offset);
int weight_zeropoint_idx = 0;
if (Q_GRAN == QuantizationGranularity::OUT_CHANNEL) {
weight_zeropoint_idx = i;
}
__m256 bias_v;
if (HAS_BIAS) {
float bias = r.bias[i] / r.act_times_w_scale[weight_zeropoint_idx];
bias_v = _mm256_set1_ps(bias);
}
if (Q_GRAN == QuantizationGranularity::OUT_CHANNEL) {
float act_times_w_div_c =
r.act_times_w_scale[weight_zeropoint_idx] / r.C_scale;
act_times_w_div_c_v = _mm256_set1_ps(act_times_w_div_c);
}
__m256i weight_zeropoint_v =
_mm256_set1_epi32(r.weight_zero_points[weight_zeropoint_idx]);
int j = block.col_start;
for (; j < block.col_start + (block.col_size / (VLEN * 4) * (VLEN * 4));
j += (VLEN * 4)) {
__m256i x_v = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(
inp + (i - block.row_start) * ld_in + (j - block.col_start)));
__m256i y_v = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(
inp + (i - block.row_start) * ld_in + (j - block.col_start) +
1 * VLEN));
__m256i z_v = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(
inp + (i - block.row_start) * ld_in + (j - block.col_start) +
2 * VLEN));
__m256i w_v = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(
inp + (i - block.row_start) * ld_in + (j - block.col_start) +
3 * VLEN));
if (!ACT_SYMMETRIC) {
x_v = _mm256_sub_epi32(x_v, row_offset_v);
y_v = _mm256_sub_epi32(y_v, row_offset_v);
z_v = _mm256_sub_epi32(z_v, row_offset_v);
w_v = _mm256_sub_epi32(w_v, row_offset_v);
}
if (!WEIGHT_SYMMETRIC) {
__m256i col_offset_v = _mm256_mullo_epi32(
_mm256_loadu_si256(reinterpret_cast<const __m256i*>(
r.act_col_offsets + j - block.col_start)),
weight_zeropoint_v);
x_v = _mm256_sub_epi32(x_v, col_offset_v);
col_offset_v = _mm256_mullo_epi32(
_mm256_loadu_si256(reinterpret_cast<const __m256i*>(
r.act_col_offsets + VLEN + j - block.col_start)),
weight_zeropoint_v);
y_v = _mm256_sub_epi32(y_v, col_offset_v);
col_offset_v = _mm256_mullo_epi32(
_mm256_loadu_si256(reinterpret_cast<const __m256i*>(
r.act_col_offsets + 2 * VLEN + j - block.col_start)),
weight_zeropoint_v);
z_v = _mm256_sub_epi32(z_v, col_offset_v);
col_offset_v = _mm256_mullo_epi32(
_mm256_loadu_si256(reinterpret_cast<const __m256i*>(
r.act_col_offsets + 3 * VLEN + j - block.col_start)),
weight_zeropoint_v);
w_v = _mm256_sub_epi32(w_v, col_offset_v);
}
/*
* Convert int32_t input to FP32 and multiply by FP32 scale.
* Both operations involve statistically unbiased roundings (with
* default MXCSR rounding mode):
* - Large int32_t values can't be exactly represented as FP32.
* CVTDQ2PS instruction on x86 would round it according to nearest
* FP32 value with ties to even (assuming default MXCSR rounding
* mode).
* - Product of two FP32 values is generally not exactly
* representation as an FP32 value, and will be rounded to nearest
* FP32 value with ties to even with default MXCSR rounding mode.
*/
__m256 xf_v, yf_v, zf_v, wf_v;
if (HAS_BIAS) {
xf_v = _mm256_add_ps(_mm256_cvtepi32_ps(x_v), bias_v);
yf_v = _mm256_add_ps(_mm256_cvtepi32_ps(y_v), bias_v);
zf_v = _mm256_add_ps(_mm256_cvtepi32_ps(z_v), bias_v);
wf_v = _mm256_add_ps(_mm256_cvtepi32_ps(w_v), bias_v);
} else {
xf_v = _mm256_cvtepi32_ps(x_v);
yf_v = _mm256_cvtepi32_ps(y_v);
zf_v = _mm256_cvtepi32_ps(z_v);
wf_v = _mm256_cvtepi32_ps(w_v);
}
__m256 x_scaled_v, y_scaled_v, z_scaled_v, w_scaled_v;
x_scaled_v = _mm256_mul_ps(xf_v, act_times_w_div_c_v);
y_scaled_v = _mm256_mul_ps(yf_v, act_times_w_div_c_v);
z_scaled_v = _mm256_mul_ps(zf_v, act_times_w_div_c_v);
w_scaled_v = _mm256_mul_ps(wf_v, act_times_w_div_c_v);
/*
* Convert scaled FP32 result to int32_t using CVTPS2DQ instruction.
* CVTPS2DQ instruction rounds result according to nearest FP32 value
* with ties to even (assuming default MXCSR rounding mode). However,
* when conversion overflows, it produces INT32_MIN as a result. For
* large positive inputs the result of conversion can become negative,
* which affects the final requantization result. Note that on x86
* SSE2 we have e.g. int32_t(float(INT32_MAX)) == INT32_MIN! This
* happens because float(INT32_MAX) rounds to 2**31, which overflows
* int32_t when it is converted back to integer.
*
* Thankfully, we can prove that overflow never happens in this
* requantization scheme. The largest positive input is INT32_MAX
* (2**31 - 1), which turns into 2**31 when converted to float. The
* largest scale value is 0x1.FFFFFEp-1. When multiplied together, the
* result is 2147483520 (compare to INT32_MAX = 2147483647), which
* fits into int32_t without overflow.
*/
__m256i x_rounded_v = _mm256_cvtps_epi32(x_scaled_v);
__m256i y_rounded_v = _mm256_cvtps_epi32(y_scaled_v);
__m256i z_rounded_v = _mm256_cvtps_epi32(z_scaled_v);
__m256i w_rounded_v = _mm256_cvtps_epi32(w_scaled_v);
/*
* Standard final sequence on x86 AVX2:
* - Pack to int16_t and saturate
* - Add zero point
* - Pack to uint8_t and saturate
* - Clamp between qmin and qmax
*/
__m256i xy_packed_v = _mm256_adds_epi16(
_mm256_packs_epi32(x_rounded_v, y_rounded_v), C_zero_point_epi16_v);
__m256i zw_packed_v = _mm256_adds_epi16(
_mm256_packs_epi32(z_rounded_v, w_rounded_v), C_zero_point_epi16_v);
__m256i xyzw_packed_v = _mm256_packus_epi16(xy_packed_v, zw_packed_v);
__m256i xyzw_clamped_v = _mm256_max_epu8(
FUSE_RELU ? C_zero_point_epi8_v : min_v,
_mm256_min_epu8(xyzw_packed_v, max_v));
/*
* xyzw_clamped_v has results in the following layout so we need to
* permute: x0-3 y0-3 z0-3 w0-3 x4-7 y4-7 z4-7 w4-7
*/
xyzw_clamped_v =
_mm256_permutevar8x32_epi32(xyzw_clamped_v, permute_mask_v);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(out + i * ld_out + j), xyzw_clamped_v);
} // j loop vectorized and unrolled 4x
for (; j < block.col_start + (block.col_size / VLEN * VLEN); j += VLEN) {
__m256i x_v = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(
inp + (i - block.row_start) * ld_in + (j - block.col_start)));
if (!ACT_SYMMETRIC) {
x_v = _mm256_sub_epi32(x_v, row_offset_v);
}
if (!WEIGHT_SYMMETRIC) {
__m256i col_offset_v = _mm256_mullo_epi32(
_mm256_loadu_si256(reinterpret_cast<const __m256i*>(
r.act_col_offsets + j - block.col_start)),
weight_zeropoint_v);
x_v = _mm256_sub_epi32(x_v, col_offset_v);
}
__m256 xf_v;
if (HAS_BIAS) {
xf_v = _mm256_add_ps(_mm256_cvtepi32_ps(x_v), bias_v);
} else {
xf_v = _mm256_cvtepi32_ps(x_v);
}
__m256 x_scaled_v = _mm256_mul_ps(xf_v, act_times_w_div_c_v);
__m256i x_rounded_v = _mm256_cvtps_epi32(x_scaled_v);
__m256i x_packed_v = _mm256_adds_epi16(
_mm256_packs_epi32(x_rounded_v, _mm256_setzero_si256()),
C_zero_point_epi16_v);
x_packed_v = _mm256_packus_epi16(x_packed_v, _mm256_setzero_si256());
__m256i x_clamped_v = _mm256_max_epu8(
FUSE_RELU ? C_zero_point_epi8_v : min_v,
_mm256_min_epu8(x_packed_v, max_v));
/*
* x_clamped_v has results in the following layout so we need to
* permute: x0-3 garbage0-11 x4-7 garbage12-23
*/
x_clamped_v = _mm256_permutevar8x32_epi32(x_clamped_v, permute_mask_v);
_mm_storel_epi64(
reinterpret_cast<__m128i*>(out + i * ld_out + j),
_mm256_castsi256_si128(x_clamped_v));
} // j loop vectorized
int remainder = block.col_start + block.col_size - j;
if (remainder > 0) {
__m256i mask_v = _mm256_load_si256(reinterpret_cast<const __m256i*>(
internal::avx2_ps_or_epi32_masks[remainder]));
__m256i x_v = _mm256_maskload_epi32(
inp + (i - block.row_start) * ld_in + (j - block.col_start), mask_v);
if (!ACT_SYMMETRIC) {
x_v = _mm256_sub_epi32(x_v, row_offset_v);
}
if (!WEIGHT_SYMMETRIC) {
__m256i col_offset_v = _mm256_mullo_epi32(
_mm256_maskload_epi32(
r.act_col_offsets + j - block.col_start, mask_v),
weight_zeropoint_v);
x_v = _mm256_sub_epi32(x_v, col_offset_v);
}
__m256 xf_v;
if (HAS_BIAS) {
xf_v = _mm256_add_ps(_mm256_cvtepi32_ps(x_v), bias_v);
} else {
xf_v = _mm256_cvtepi32_ps(x_v);
}
__m256 x_scaled_v = _mm256_mul_ps(xf_v, act_times_w_div_c_v);
__m256i x_rounded_v = _mm256_cvtps_epi32(x_scaled_v);
__m256i x_packed_v = _mm256_adds_epi16(
_mm256_packs_epi32(x_rounded_v, _mm256_setzero_si256()),
C_zero_point_epi16_v);
x_packed_v = _mm256_packus_epi16(x_packed_v, _mm256_setzero_si256());
__m256i x_clamped_v = _mm256_max_epu8(
FUSE_RELU ? C_zero_point_epi8_v : min_v,
_mm256_min_epu8(x_packed_v, max_v));
/*
* x_clamped_v has results in the following layout so we need to
* permute: x0-3 garbage0-11 x4-7 garbage12-23
*/
x_clamped_v = _mm256_permutevar8x32_epi32(x_clamped_v, permute_mask_v);
alignas(64) uint8_t x_clamped_buffer[32];
_mm256_store_si256(
reinterpret_cast<__m256i*>(x_clamped_buffer), x_clamped_v);
for (int k = 0; k < remainder; ++k) {
out[i * ld_out + j + k] = x_clamped_buffer[k];
}
} // j loop remainder
} // i loop
}
#define CREATE_INSTANCE( \
FUSE_RELU, ACT_SYMMETRIC, WEIGHT_SYMMETRIC, HAS_BIAS, QGRAN) \
template FBGEMM_API void trRequantizeOpt< \
FUSE_RELU, \
ACT_SYMMETRIC, \
WEIGHT_SYMMETRIC, \
HAS_BIAS, \
QGRAN>( \
uint8_t * out, \
const int32_t* inp, \
const block_type_t& block, \
int ld_out, \
int ld_in, \
const trRequantizationParams_t& r);
CREATE_INSTANCE(true, true, true, true, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(true, true, true, false, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(true, true, false, true, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(true, true, false, false, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(true, false, true, true, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(true, false, true, false, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(true, false, false, true, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(true, false, false, false, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(false, true, true, true, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(false, true, true, false, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(false, true, false, true, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(false, true, false, false, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(false, false, true, true, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(false, false, true, false, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(false, false, false, true, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(false, false, false, false, QuantizationGranularity::TENSOR)
CREATE_INSTANCE(true, true, true, true, QuantizationGranularity::OUT_CHANNEL)
CREATE_INSTANCE(true, true, true, false, QuantizationGranularity::OUT_CHANNEL)
CREATE_INSTANCE(true, true, false, true, QuantizationGranularity::OUT_CHANNEL)
CREATE_INSTANCE(true, true, false, false, QuantizationGranularity::OUT_CHANNEL)
CREATE_INSTANCE(true, false, true, true, QuantizationGranularity::OUT_CHANNEL)
CREATE_INSTANCE(true, false, true, false, QuantizationGranularity::OUT_CHANNEL)
CREATE_INSTANCE(true, false, false, true, QuantizationGranularity::OUT_CHANNEL)
CREATE_INSTANCE(true, false, false, false, QuantizationGranularity::OUT_CHANNEL)
CREATE_INSTANCE(false, true, true, true, QuantizationGranularity::OUT_CHANNEL)
CREATE_INSTANCE(false, true, true, false, QuantizationGranularity::OUT_CHANNEL)
CREATE_INSTANCE(false, true, false, true, QuantizationGranularity::OUT_CHANNEL)
CREATE_INSTANCE(false, true, false, false, QuantizationGranularity::OUT_CHANNEL)
CREATE_INSTANCE(false, false, true, true, QuantizationGranularity::OUT_CHANNEL)
CREATE_INSTANCE(false, false, true, false, QuantizationGranularity::OUT_CHANNEL)
CREATE_INSTANCE(false, false, false, true, QuantizationGranularity::OUT_CHANNEL)
CREATE_INSTANCE(
false,
false,
false,
false,
QuantizationGranularity::OUT_CHANNEL)
#undef CREATE_INSTANCE
} // namespace fbgemm