-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi8mm.cpp
337 lines (287 loc) · 14.4 KB
/
i8mm.cpp
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
#include "gemmology/gemmology.h"
#include "xsimd/xsimd.hpp"
#include <cassert>
#include <chrono> // For timing
#include <cstdint>
#include <iomanip> // For formatted output
#include <iostream>
#include <vector>
using vuint8_t = xsimd::batch<uint8_t>;
using vint8_t = xsimd::batch<int8_t>;
using vint16_t = xsimd::batch<int16_t>;
using vint32_t = xsimd::batch<int32_t>;
using vuint32_t = xsimd::batch<uint32_t>;
/**
* Naive implementation
*/
__attribute__((noinline))
void NaiveMatMul(const uint8_t *inputMatrixA, const int8_t *inputMatrixB,
size_t rowsA, size_t width, size_t colsB, int32_t *output) {
std::fill(output, output+ rowsA * colsB, 0);
for (size_t rowIndex = 0; rowIndex < rowsA; ++rowIndex) {
for (size_t k = 0; k < width; ++k) {
for (size_t colIndex = 0; colIndex < colsB; ++colIndex) {
output[rowIndex * colsB + colIndex] += inputMatrixA[rowIndex * width + k] * inputMatrixB[k * colsB + colIndex];
}
}
}
}
/**
* Sequential implementation
*/
__attribute__((noinline))
void TransposedMatMul(const uint8_t *inputMatrixA, const int8_t *inputMatrixB,
size_t rowsA, size_t width, size_t colsB, int32_t *output) {
int8_t * transposedB;
posix_memalign((void**)&transposedB, 64, width * colsB);
for (size_t k = 0; k < width; ++k) {
for (size_t colIndex = 0; colIndex < colsB; ++colIndex) {
transposedB[colIndex * width + k] = inputMatrixB[k * colsB + colIndex];
}
}
std::fill(output, output+ rowsA * colsB, 0);
for (size_t rowIndex = 0; rowIndex < rowsA; ++rowIndex) {
for (size_t colIndex = 0; colIndex < colsB; ++colIndex) {
for (size_t k = 0; k < width; ++k) {
output[rowIndex * colsB + colIndex] += inputMatrixA[rowIndex * width + k] * transposedB[colIndex * width + k];
}
}
}
free(transposedB);
}
__attribute__((noinline))
void VecSatMatMul(const uint8_t *inputMatrixA, const int8_t *inputMatrixB,
size_t rowsA, size_t width, size_t colsB, int32_t *output) {
int8_t * transposedB;
posix_memalign((void**)&transposedB, 64, width * colsB);
for (size_t k = 0; k < width; ++k) {
for (size_t colIndex = 0; colIndex < colsB; ++colIndex) {
transposedB[colIndex * width + k] = inputMatrixB[k * colsB + colIndex];
}
}
for (size_t rowIndex = 0; rowIndex < rowsA; ++rowIndex) {
for (size_t colIndex = 0; colIndex < colsB; ++colIndex ) {
vint32_t vacc = 0;
for (size_t k = 0; k < width; k += vint8_t::size) {
vacc = gemmology::maddw(vuint8_t::load_unaligned(&inputMatrixA[rowIndex * width + k]),
vint8_t::load_unaligned(&transposedB[colIndex * width + k]),
vacc);
}
output[rowIndex * colsB + colIndex] = reduce_add(vacc);
}
}
free(transposedB);
}
__attribute__((noinline))
void VecMatMul(const uint8_t *inputMatrixA, const int8_t *inputMatrixB,
size_t rowsA, size_t width, size_t colsB, int32_t *output) {
int8_t * transposedB;
posix_memalign((void**)&transposedB, 64, width * colsB);
for (size_t k = 0; k < width; ++k) {
for (size_t colIndex = 0; colIndex < colsB; ++colIndex) {
transposedB[colIndex * width + k] = inputMatrixB[k * colsB + colIndex];
}
}
for (size_t rowIndex = 0; rowIndex < rowsA; ++rowIndex) {
for (size_t colIndex = 0; colIndex < colsB; ++colIndex ) {
vint32_t vacc = 0;
for (size_t k = 0; k < width; k += vint8_t::size) {
vuint8_t vinputMatrixA = vuint8_t::load_unaligned(&inputMatrixA[rowIndex * width + k]);
vint8_t vtransposedB = vint8_t::load_unaligned(&transposedB[colIndex * width + k]);
vacc = gemmology::maddw(vinputMatrixA & vuint8_t(+0xA), vtransposedB, vacc);
vacc = gemmology::maddw(vinputMatrixA & vuint8_t(~0xA), vtransposedB, vacc);
}
output[rowIndex * colsB + colIndex] = reduce_add(vacc);
}
}
free(transposedB);
}
__attribute__((noinline))
void VecSatLayoutMatMul(const uint8_t *inputMatrixA, const int8_t *inputMatrixB,
size_t rowsA, size_t width, size_t colsB, int32_t *output) {
int8_t * transposedB;
posix_memalign((void**)&transposedB, 64, width * colsB);
for (size_t k = 0; k < width; k += 4) {
for (size_t colIndex = 0; colIndex < colsB; colIndex += 4 * vint32_t::size) {
vint8_t vinputMatrixB0 = vint8_t::load_unaligned(&inputMatrixB[(k + 0) * colsB + colIndex]);
vint8_t vinputMatrixB1 = vint8_t::load_unaligned(&inputMatrixB[(k + 1) * colsB + colIndex]);
vint8_t vinputMatrixB2 = vint8_t::load_unaligned(&inputMatrixB[(k + 2) * colsB + colIndex]);
vint8_t vinputMatrixB3 = vint8_t::load_unaligned(&inputMatrixB[(k + 3) * colsB + colIndex]);
vint16_t vinputMatrixB_lo0 = xsimd::bit_cast<vint16_t>(zip_lo(vinputMatrixB0, vinputMatrixB1));
vint16_t vinputMatrixB_lo1 = xsimd::bit_cast<vint16_t>(zip_lo(vinputMatrixB2, vinputMatrixB3));
vint16_t vinputMatrixB_hi0 = xsimd::bit_cast<vint16_t>(zip_hi(vinputMatrixB0, vinputMatrixB1));
vint16_t vinputMatrixB_hi1 = xsimd::bit_cast<vint16_t>(zip_hi(vinputMatrixB2, vinputMatrixB3));
xsimd::bit_cast<vint8_t>(zip_lo(vinputMatrixB_lo0, vinputMatrixB_lo1)).store_unaligned(&transposedB[(k+0) * colsB + colIndex]);
xsimd::bit_cast<vint8_t>(zip_hi(vinputMatrixB_lo0, vinputMatrixB_lo1)).store_unaligned(&transposedB[(k+1) * colsB + colIndex]);
xsimd::bit_cast<vint8_t>(zip_lo(vinputMatrixB_hi0, vinputMatrixB_hi1)).store_unaligned(&transposedB[(k+2) * colsB + colIndex]);
xsimd::bit_cast<vint8_t>(zip_hi(vinputMatrixB_hi0, vinputMatrixB_hi1)).store_unaligned(&transposedB[(k+3) * colsB + colIndex]);
}
}
for (size_t rowIndex = 0; rowIndex < rowsA; ++rowIndex) {
for (size_t colIndex = 0; colIndex < colsB; colIndex += 4 * vint32_t::size) {
vint32_t vacc[4] = {};
for (size_t k = 0; k < width; k += 4) {
vuint8_t vinputMatrixA = xsimd::bitwise_cast<uint8_t>(vuint32_t(*(uint32_t*)(inputMatrixA + rowIndex * width + k)));
vacc[0] = gemmology::maddw(
vinputMatrixA,
vint8_t::load_unaligned(&transposedB[(k + 0) * colsB + colIndex]),
vacc[0]);
vacc[1] = gemmology::maddw(
vinputMatrixA,
vint8_t::load_unaligned(&transposedB[(k + 1) * colsB + colIndex]),
vacc[1]);
vacc[2] = gemmology::maddw(
vinputMatrixA,
vint8_t::load_unaligned(&transposedB[(k + 2) * colsB + colIndex]),
vacc[2]);
vacc[3] = gemmology::maddw(
vinputMatrixA,
vint8_t::load_unaligned(&transposedB[(k + 3) * colsB + colIndex]),
vacc[3]);
}
vacc[0].store_aligned(&output[rowIndex * colsB + colIndex + 0 * vint32_t::size]);
vacc[1].store_aligned(&output[rowIndex * colsB + colIndex + 1 * vint32_t::size]);
vacc[2].store_aligned(&output[rowIndex * colsB + colIndex + 2 * vint32_t::size]);
vacc[3].store_aligned(&output[rowIndex * colsB + colIndex + 3 * vint32_t::size]);
}
}
free(transposedB);
}
__attribute__((noinline))
void VecLayoutMatMul(const uint8_t *inputMatrixA, const int8_t *inputMatrixB,
size_t rowsA, size_t width, size_t colsB, int32_t *output) {
int8_t * transposedB;
posix_memalign((void**)&transposedB, 64, width * colsB);
for (size_t k = 0; k < width; k += 4) {
for (size_t colIndex = 0; colIndex < colsB; colIndex += 4 * vint32_t::size) {
vint8_t vinputMatrixB0 = vint8_t::load_unaligned(&inputMatrixB[(k + 0) * colsB + colIndex]);
vint8_t vinputMatrixB1 = vint8_t::load_unaligned(&inputMatrixB[(k + 1) * colsB + colIndex]);
vint8_t vinputMatrixB2 = vint8_t::load_unaligned(&inputMatrixB[(k + 2) * colsB + colIndex]);
vint8_t vinputMatrixB3 = vint8_t::load_unaligned(&inputMatrixB[(k + 3) * colsB + colIndex]);
vint16_t vinputMatrixB_lo0 = xsimd::bit_cast<vint16_t>(zip_lo(vinputMatrixB0, vinputMatrixB1));
vint16_t vinputMatrixB_lo1 = xsimd::bit_cast<vint16_t>(zip_lo(vinputMatrixB2, vinputMatrixB3));
vint16_t vinputMatrixB_hi0 = xsimd::bit_cast<vint16_t>(zip_hi(vinputMatrixB0, vinputMatrixB1));
vint16_t vinputMatrixB_hi1 = xsimd::bit_cast<vint16_t>(zip_hi(vinputMatrixB2, vinputMatrixB3));
xsimd::bit_cast<vint8_t>(zip_lo(vinputMatrixB_lo0, vinputMatrixB_lo1)).store_unaligned(&transposedB[(k+0) * colsB + colIndex]);
xsimd::bit_cast<vint8_t>(zip_hi(vinputMatrixB_lo0, vinputMatrixB_lo1)).store_unaligned(&transposedB[(k+1) * colsB + colIndex]);
xsimd::bit_cast<vint8_t>(zip_lo(vinputMatrixB_hi0, vinputMatrixB_hi1)).store_unaligned(&transposedB[(k+2) * colsB + colIndex]);
xsimd::bit_cast<vint8_t>(zip_hi(vinputMatrixB_hi0, vinputMatrixB_hi1)).store_unaligned(&transposedB[(k+3) * colsB + colIndex]);
}
}
for (size_t rowIndex = 0; rowIndex < rowsA; ++rowIndex) {
for (size_t colIndex = 0; colIndex < colsB; colIndex += 4 * vint32_t::size) {
vint32_t vacc[4] = {};
for (size_t k = 0; k < width; k += 4) {
vuint8_t vinputMatrixA = xsimd::bitwise_cast<uint8_t>(vuint32_t(*(uint32_t*)(inputMatrixA + rowIndex * width + k)));
vint8_t vtransposedB0 = vint8_t::load_unaligned(&transposedB[(k + 0) * colsB + colIndex]);
vint8_t vtransposedB1 = vint8_t::load_unaligned(&transposedB[(k + 1) * colsB + colIndex]);
vint8_t vtransposedB2 = vint8_t::load_unaligned(&transposedB[(k + 2) * colsB + colIndex]);
vint8_t vtransposedB3 = vint8_t::load_unaligned(&transposedB[(k + 3) * colsB + colIndex]);
vacc[0] = gemmology::maddw(vinputMatrixA & vuint8_t(+0xA), vtransposedB0, vacc[0]);
vacc[0] = gemmology::maddw(vinputMatrixA & vuint8_t(~0xA), vtransposedB0, vacc[0]);
vacc[1] = gemmology::maddw(vinputMatrixA & vuint8_t(+0xA), vtransposedB1, vacc[1]);
vacc[1] = gemmology::maddw(vinputMatrixA & vuint8_t(~0xA), vtransposedB1, vacc[1]);
vacc[2] = gemmology::maddw(vinputMatrixA & vuint8_t(+0xA), vtransposedB2, vacc[2]);
vacc[2] = gemmology::maddw(vinputMatrixA & vuint8_t(~0xA), vtransposedB2, vacc[2]);
vacc[3] = gemmology::maddw(vinputMatrixA & vuint8_t(+0xA), vtransposedB3, vacc[3]);
vacc[3] = gemmology::maddw(vinputMatrixA & vuint8_t(~0xA), vtransposedB3, vacc[3]);
}
vacc[0].store_aligned(&output[rowIndex * colsB + colIndex + 0 * vint32_t::size]);
vacc[1].store_aligned(&output[rowIndex * colsB + colIndex + 1 * vint32_t::size]);
vacc[2].store_aligned(&output[rowIndex * colsB + colIndex + 2 * vint32_t::size]);
vacc[3].store_aligned(&output[rowIndex * colsB + colIndex + 3 * vint32_t::size]);
}
}
free(transposedB);
}
/**
* gemmology implementation
*/
__attribute__((noinline))
void GemmologyMatMul(const uint8_t *inputMatrixA, const int8_t *inputMatrixB,
size_t rowsA, size_t width, size_t colsB, int32_t *output) {
int8_t * transposedB;
posix_memalign((void**)&transposedB, 64, width * colsB);
gemmology::SequentialExecutionEngine engine;
gemmology::PrepareBQuantized(inputMatrixB, transposedB, width, colsB);
gemmology::Shift::Multiply(inputMatrixA, transposedB, rowsA, width, colsB,
gemmology::callbacks::Write(output), engine);
free(transposedB);
}
int main(int argc, char **argv) {
const size_t rowsA = 128, width=64, colsB=256;
size_t count = 1000;
uint8_t *inputMatrixA;
int8_t *inputMatrixB;
int32_t *output;
posix_memalign((void**)&inputMatrixA, 64, rowsA * width);
posix_memalign((void**)&inputMatrixB, 64, width * colsB);
posix_memalign((void**)&output, 64, rowsA * colsB * sizeof(int32_t));
for(size_t i = 0; i < rowsA; ++i)
for(size_t j = 0; j < width; ++j)
inputMatrixA[i * width + j] = i * width + j;
for(size_t i = 0; i < width; ++i)
for(size_t j = 0; j < colsB; ++j)
inputMatrixB[i * colsB + j] = (i * colsB + j) % 255 - 127;
if (argc == 2) {
if (std::strcmp(argv[1], "--check") == 0) {
int32_t *output_ref;
posix_memalign((void **)&output_ref, 64, rowsA * colsB * sizeof(int32_t));
NaiveMatMul(inputMatrixA, inputMatrixB, rowsA, width, colsB, output_ref);
TransposedMatMul(inputMatrixA, inputMatrixB, rowsA, width, colsB, output);
if (!std::equal(output, output + rowsA * colsB, output_ref)) {
std::cerr << "failed comparison for TransposedMatMul\n";
return 1;
}
VecSatMatMul(inputMatrixA, inputMatrixB, rowsA, width, colsB, output);
if (!std::equal(output, output + rowsA * colsB, output_ref)) {
std::cerr << "failed comparison for VecSatMatMul\n";
return 1;
}
VecMatMul(inputMatrixA, inputMatrixB, rowsA, width, colsB, output);
if (!std::equal(output, output + rowsA * colsB, output_ref)) {
std::cerr << "failed comparison for VecMatMul\n";
return 1;
}
VecSatLayoutMatMul(inputMatrixA, inputMatrixB, rowsA, width, colsB, output);
if (!std::equal(output, output + rowsA * colsB, output_ref)) {
std::cerr << "failed comparison for VecSatLayoutMatMul\n";
return 1;
}
VecLayoutMatMul(inputMatrixA, inputMatrixB, rowsA, width, colsB, output);
if (!std::equal(output, output + rowsA * colsB, output_ref)) {
std::cerr << "failed comparison for VecLayoutMatMul\n";
return 1;
}
GemmologyMatMul(inputMatrixA, inputMatrixB, rowsA, width, colsB, output);
if (!std::equal(output, output + rowsA * colsB, output_ref)) {
std::cerr << "failed comparison for GemmologyMatMul\n";
return 1;
}
std::cerr << "ok\n";
return 0;
}
return 1;
}
typedef void (*mm_t)(const uint8_t *, const int8_t *, size_t, size_t, size_t, int32_t *);
struct { const char* name; mm_t matrix_multiplier;} matrix_multipliers[] = {
{" naive mat mul", NaiveMatMul},
{" transposed mat mul", TransposedMatMul},
{" vec mat mul", VecMatMul},
{" vec layout mat mul", VecLayoutMatMul},
{" gemmology mat mul", GemmologyMatMul},
{" vec + sat mat mul", VecSatMatMul},
{"vec + sat layout mat mul", VecSatLayoutMatMul},
};
for(auto [name, matrix_multiplier] : matrix_multipliers) {
auto start = std::chrono::high_resolution_clock::now();
for(int i = 0; i < count; ++i)
matrix_multiplier(inputMatrixA, inputMatrixB, rowsA, width, colsB, output);
auto end = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(end - start)
.count();
std::cout << name << ": " << duration << " microseconds" << std::endl;
}
return 0;
}