forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen_fp16fp32.cc
387 lines (331 loc) · 13.2 KB
/
codegen_fp16fp32.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright (c) Facebook, Inc. and its 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.
*/
#include <assert.h>
#include <cpuid.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <array>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <string>
using namespace std;
void addi(ofstream& of, string i, bool disable = false) {
if (disable == false)
of << "\"" + i + "\\t\\n\"" + "\n";
}
struct ISA {
unsigned avx; // 1, 2 or 3
string name;
vector<vector<unsigned>> shapes;
};
int main() {
bool iaca = false;
bool disable = false;
bool fixedA = true, fixedB = true, fixedC = true;
int eax, ebx, ecx, edx;
__cpuid(1 /* ecx = vendor string */, eax, ebx, ecx, edx);
printf("FC16 is %s supported\n", ((ecx & bit_F16C) ? " " : "not"));
string comma = ",";
vector<ISA> isa = {
// {1, "AVX", {{4, 1, 0}, {4, 2, 0}, {4, 3, 0}, {3, 1, 0}, {3, 2, 0}, {3,
// 3, 0}}},
{ 2, "AVX2",
{ { 1, 1, 0 },
{ 2, 1, 0 },
{ 3, 1, 0 },
{ 4, 1, 0 },
{ 5, 1, 0 },
{ 6, 1, 0 },
{ 7, 1, 0 },
{ 8, 1, 0 },
{ 9, 1, 0 },
{ 10, 1, 0 },
{ 11, 1, 0 },
{ 12, 1, 0 },
{ 13, 1, 0 },
{ 14, 1, 0 },
}
}
};
// open all files
ofstream srcfile;
srcfile.open("FbgemmFP16UKernels.cc");
srcfile << "#include \"FbgemmFP16UKernels.h\"\n";
if (iaca)
srcfile << "#include \"iacaMarks.h\"\n";
ofstream hdrfile;
hdrfile.open("FbgemmFP16UKernels.h");
hdrfile << "#ifndef FBGEMM_UKERNELS\n";
hdrfile << "#define FBGEMM_UKERNELS\n";
hdrfile << "#include <cstdint>\n";
hdrfile << "#include <tuple>\n";
hdrfile << "#include <vector>\n";
hdrfile << "#include \"fbgemm/Types.h\"\n";
hdrfile << "using fp16 = fbgemm2::float16;\n";
hdrfile << "using fp32 = float;\n";
hdrfile << "struct GemmParams {uint64_t k; float *A; const fp16 *B;\n"
"float *beta; uint64_t accum; float *C; uint64_t ldc;\n"
"uint64_t b_block_cols; uint64_t b_block_size;};\n";
std::map<string, string> fptr_typedef;
fptr_typedef["fp16"] = "";
fptr_typedef["fp32"] = "";
unsigned labelId = 0;
#if 1
for (auto fixedA : {false})
for (auto fixedB : {false})
for (auto fixedC : {false})
#else
for (auto fixedA : {true})
for (auto fixedB : {true})
for (auto fixedC : {true})
#endif
for (auto s : isa) {
vector<vector<unsigned>>& ukernel_shape = s.shapes;
vector<string> funcname(ukernel_shape.size()),
fheader(ukernel_shape.size());
string fargs;
for (auto fp16 : {true}) {
string B_type = ((fp16) ? "fp16" : "fp32");
string prefix = s.name + /*"_" + B_type */ + "_" + "fA" +
to_string(fixedA) + "fB" + to_string(fixedB) + "fC" +
to_string(fixedC);
cout << "Generating code for " << s.name << " " << B_type << "\n";
for (unsigned k = 0; k < ukernel_shape.size(); k++) {
printf(
"shape: %d x %d * 32\n",
ukernel_shape[k][0],
ukernel_shape[k][1]);
string p1 = "GemmParams *gp";
funcname[k] = "gemmkernel_" + to_string(ukernel_shape[k][0]) +
"x" + to_string(ukernel_shape[k][1]) + "_";
funcname[k] += prefix;
fargs = "(" + p1 + ")";
fheader[k] =
"void __attribute__ ((noinline)) " + funcname[k] + fargs;
srcfile << fheader[k] << "\n";
srcfile << "{\n";
unsigned last_free_ymmreg = 0;
// produce register block of C
vector<vector<string>> vCtile(ukernel_shape[k][0]);
for (auto r = 0; r < ukernel_shape[k][0]; r++)
for (auto c = 0; c < ukernel_shape[k][1]; c++) {
vCtile[r].push_back("ymm" + to_string(last_free_ymmreg));
last_free_ymmreg++;
}
assert(last_free_ymmreg <= 14);
string vAtmp = "ymm" + to_string(last_free_ymmreg++);
// produce register block of B col
assert(ukernel_shape[k][1] == 1);
vector<string> vBcol(ukernel_shape[k][1]);
for (auto c = 0; c < ukernel_shape[k][1]; c++) {
vBcol[c] = ("ymm" + to_string(last_free_ymmreg));
last_free_ymmreg++;
}
assert(last_free_ymmreg <= 16);
srcfile << "asm volatile\n";
srcfile << "(\n";
srcfile << "#if !defined(__clang__)" << "\n";
addi(srcfile, "mov r14, %[gp]");
srcfile << "#else\n";
addi(srcfile, "mov %[gp], %%r14");
addi(srcfile, ".intel_syntax noprefix");
srcfile << "#endif\n";
srcfile << "\n// Copy parameters\n";
srcfile << "// k\n";
addi(srcfile, "mov r8, [r14 + 0]");
srcfile << "// A\n";
addi(srcfile, "mov r9, [r14 + 8]");
srcfile << "// B\n";
addi(srcfile, "mov r10, [r14 + 16]");
srcfile << "// beta\n";
addi(srcfile, "mov r15, [r14 + 24]");
srcfile << "// accum\n";
addi(srcfile, "mov rdx, [r14 + 32]");
srcfile << "// C\n";
addi(srcfile, "mov r12, [r14 + 40]");
srcfile << "// ldc\n";
addi(srcfile, "mov r13, [r14 + 48]");
srcfile << "// b_block_cols\n";
addi(srcfile, "mov rdi, [r14 + 56]");
srcfile << "// b_block_size\n";
addi(srcfile, "mov rsi, [r14 + 64]");
srcfile << "// Make copies of A and C\n";
addi(srcfile, "mov rax, r9");
addi(srcfile, "mov rcx, r12");
srcfile << "\n\n";
addi(srcfile, "mov rbx, 0");
string exitlabel = "L_exit%=";
string label2 = "loop_outter%=";
addi(srcfile, label2 + ":");
addi(srcfile, "mov r14, 0");
// set all vCtile regs to zeros
for (auto r = 0; r < vCtile.size(); r++) {
for (auto c = 0; c < vCtile[r].size(); c++) {
addi(
srcfile,
"vxorps " + vCtile[r][c] + "," + vCtile[r][c] + "," +
vCtile[r][c]);
}
}
// start marker
if (iaca) {
addi(srcfile, "mov ebx, 111");
addi(srcfile, ".byte 0x64, 0x67, 0x90");
}
srcfile << "\n";
if (ukernel_shape[k][0] <= 13) {
addi(srcfile, "vcvtph2ps ymm15, XMMWORD PTR [r10 + 0]");
addi(srcfile, "mov r11, 16");
} else {
addi(srcfile, "mov r11, 0");
}
srcfile << "\n";
string label = "loop_inner%=";
addi(srcfile, label + ":");
srcfile << "\n";
if (ukernel_shape[k][0] <= 13) {
auto a_offset = 0, unroll_factor = 2;
for (auto u = 0; u < unroll_factor; u++) {
string breg = (u == 0) ? "ymm14" : "ymm15";
string breg_rev = (u == 0) ? "ymm15" : "ymm14";
addi(srcfile, "vcvtph2ps " + breg +
",XMMWORD PTR [r10 + r11 + " +
to_string(u * 16) + "]");
addi(srcfile, "inc r14");
for (auto r = 0; r < vCtile.size(); r++) {
addi(srcfile, "vbroadcastss " + vAtmp + ",DWORD PTR [r9+" +
to_string(a_offset) + "]");
addi(srcfile, "vfmadd231ps " + vCtile[r][0] + "," +
breg_rev + "," + vAtmp);
if (u == 1 && r == vCtile.size() / 2)
addi(srcfile, "add r11, 32");
a_offset += 4;
}
if (u < unroll_factor - 1) {
addi(srcfile, "cmp r14, r8");
addi(srcfile, "jge " + exitlabel);
}
}
addi(srcfile, "add r9," + to_string(a_offset));
addi(srcfile, "cmp r14, r8");
addi(srcfile, "jl " + label);
srcfile << "\n";
addi(srcfile, exitlabel + ":");
} else {
addi(srcfile,
"vcvtph2ps " + vBcol[0] + ",XMMWORD PTR [r10 + r11]");
for (auto r = 0; r < vCtile.size(); r++) {
addi(srcfile, "vbroadcastss " + vAtmp + ",DWORD PTR [r9+" +
to_string(4 * r) + "]");
addi(srcfile, "vfmadd231ps " + vCtile[r][0] + "," + vBcol[0] +
"," + vAtmp);
}
addi(srcfile, "add r9," + to_string(4 * ukernel_shape[k][0]),
fixedA); // move A ptr
addi(srcfile, "add r11, 16");
addi(srcfile, "inc r14");
addi(srcfile, "cmp r14, r8");
addi(srcfile, "jl " + label);
}
addi(srcfile, "add r10, rsi");
srcfile << "\n";
// end marker
if (iaca) {
addi(srcfile, "mov ebx, 222");
addi(srcfile, ".byte 0x64, 0x67, 0x90");
}
addi(srcfile, "cmp rdx, 1");
addi(srcfile, "je L_accum%=");
srcfile << "// Dump C\n";
for (auto r = 0; r < vCtile.size(); r++) {
for (auto c = 0; c < vCtile[r].size(); c++) {
addi(srcfile, "vmovups YMMWORD PTR [r12 + " +
to_string(32 * c) + "], " + vCtile[r][c],
fixedC);
}
addi(srcfile, "add r12, r13", fixedC); // move C ptr
}
addi(srcfile, "jmp L_done%=");
srcfile << "\n\n";
addi(srcfile, "L_accum%=:");
srcfile << "// Dump C with accumulate\n";
string r_spare = (s.avx == 1) ? "ymm14" : "ymm15";
addi(srcfile,
"vbroadcastss " + r_spare + string(",DWORD PTR [r15]"),
fixedC);
// store out C
for (auto r = 0; r < vCtile.size(); r++) {
for (auto c = 0; c < vCtile[r].size(); c++) {
switch (s.avx) {
case 1:
addi(srcfile,
string("vmulps ymm15, ") + r_spare + comma +
"YMMWORD PTR [r12 + " + to_string(32 * c) + "]",
fixedC);
addi(srcfile, "vaddps " + vCtile[r][c] + "," +
vCtile[r][c] + "," + "ymm15",
fixedC);
break;
case 2:
addi(srcfile,
"vfmadd231ps " + vCtile[r][c] + "," + r_spare + "," +
"YMMWORD PTR [r12 + " + to_string(32 * c) + "]",
fixedC);
break;
default:
assert(0);
}
addi(srcfile, "vmovups YMMWORD PTR [r12 + " +
to_string(32 * c) + "], " + vCtile[r][c],
fixedC);
}
addi(srcfile, "add r12, r13", fixedC); // move C ptr
}
srcfile << "\n";
addi(srcfile, "L_done%=:");
srcfile << "\n// next outer iteration\n";
// C
addi(srcfile, "add rcx, " + to_string(32 * ukernel_shape[k][1]),
fixedC);
addi(srcfile, "mov r12, rcx", fixedC);
// A
addi(srcfile, "mov r9, rax");
addi(srcfile, "inc rbx");
addi(srcfile, "cmp rbx, rdi");
addi(srcfile, "jl " + label2);
// output
srcfile << ":\n";
// input
srcfile << ":\n";
srcfile << "[gp] \"rm\" (gp)\n";
// clobbered
srcfile
<< (string) ": \"r8\", \"r9\", \"r10\", \"r11\", \"r15\", " +
(string) " \"r13\", \"r14\",\n" +
(string) "\"rax\", \"rcx\", "
"\"rdx\", \"rsi\", \"rdi\", \"rbx\", "
"\"r12\", \"memory\"" +
(string) "\n";
srcfile << ");\n";
srcfile << "}\n";
}
for (unsigned k = 0; k < ukernel_shape.size(); k++) {
hdrfile << fheader[k] << ";\n";
}
fptr_typedef[B_type] =
"typedef void (* funcptr_" + B_type + ") " + fargs;
}
}
srcfile.close();
hdrfile << fptr_typedef["fp16"] << ";\n";
hdrfile << fptr_typedef["fp32"] << ";\n";
hdrfile << "#endif\n";
hdrfile.close();
}