-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsgemm_ocl2.h
446 lines (398 loc) · 14.3 KB
/
sgemm_ocl2.h
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* public domain Simple, Minimalistic, Fast GEMM library
* ©2019-2021 Yuichiro Nakada
*
* Basic usage:
* sgemm_ocl_init(platform, device, max_buffer_size);
* sgemm_ocl('N', 'N', M, N, K, A, B, C);
* sgemm_ocl_finish();
* */
#include "ocl.h"
#define TS 16 // Threadblock sizes
char sgemm_kcode[] = OCLSTRINGIFY(
// Tiled and coalesced version
__kernel void gemm_rnn(__global float* restrict gm, const int8 _info, const float4 _param)
{
const int M = _info.s0;
const int N = _info.s1;
const int K = _info.s2;
__global float* restrict A = (__global float* restrict)(gm + _info.s3);
__global float* restrict B = (__global float* restrict)(gm + _info.s4);
__global float* restrict C = (__global float* restrict)(gm + _info.s5);
// Thread identifiers
const int row = get_local_id(0); // Local row ID (max: TS)
const int col = get_local_id(1); // Local col ID (max: TS)
const int globalRow = TS*get_group_id(0) + row; // Row ID of C (0..M)
const int globalCol = TS*get_group_id(1) + col; // Col ID of C (0..N)
// Local memory to fit a tile of TS*TS elements of A and B
__local float Asub[TS][TS];
__local float Bsub[TS][TS];
// Initialise the accumulation register
float acc = 0.0f;
// Loop over all tiles
const int numTiles = K/TS /*+1*/;
for (int t=0; t<numTiles; t++) {
// Load one tile of A and B into local memory
const int tiledRow = TS*t + row;
const int tiledCol = TS*t + col;
// Asub[col][row] = A[tiledCol*M + globalRow]; // Column major
// Bsub[col][row] = B[globalCol*K + tiledRow];
Asub[col][row] = A[tiledCol + globalRow*K]; // Row major
Bsub[col][row] = B[globalCol + N*tiledRow];
// Synchronise to make sure the tile is loaded
barrier(CLK_LOCAL_MEM_FENCE);
// Perform the computation for a single tile
for (int k=0; k<TS; k++) {
acc += Asub[k][row] * Bsub[col][k];
// acc += Asub[row][k] * Bsub[k][col];
}
// Synchronise before loading the next tile
barrier(CLK_LOCAL_MEM_FENCE);
}
{
int t = numTiles;
// Load one tile of A and B into local memory
const int tiledRow = TS*t + row;
const int tiledCol = TS*t + col;
Asub[col][row] = (tiledCol>=K || globalRow>=M) ? 0 : A[tiledCol + globalRow*K]; // Row major
Bsub[col][row] = (tiledRow>=K || globalCol>=N) ? 0 : B[globalCol + N*tiledRow];
// Asub[col][row] = (tiledCol>=K || globalRow>=M) ? 0 : col + row*K;
// Synchronise to make sure the tile is loaded
barrier(CLK_LOCAL_MEM_FENCE);
// Perform the computation for a single tile
for (int k=0; k<TS; k++) {
acc += Asub[k][row] * Bsub[col][k];
}
// Synchronise before loading the next tile
barrier(CLK_LOCAL_MEM_FENCE);
}
if (globalRow >= M || globalCol >= N) return;
// Store the final result in C
// C[globalCol*M + globalRow] = acc; // Column major
float z = _param.s1;
if (z) z *= C[globalCol + globalRow*N];
C[globalCol + globalRow*N] = _param.s0 * acc + z; // Row major
// C[globalCol + globalRow*N] = Asub[col][row];
}
#define TRANSPOSEX 16
#define TRANSPOSEY 16
// Simple transpose kernel for a P * Q matrix
__kernel void transpose(__global float* gm, const int8 _info, const float4 _param)
{
const int P = _info.s0;
const int Q = _info.s1;
__global float* input = (__global float*)(gm + _info.s2);
__global float* output = (__global float*)(gm + _info.s3);
// Thread identifiers
const int tx = get_local_id(0);
const int ty = get_local_id(1);
const int ID0 = get_group_id(0)*TRANSPOSEX + tx; // 0..P
const int ID1 = get_group_id(1)*TRANSPOSEY + ty; // 0..Q
// Set-up the local memory for shuffling
__local float buffer[TRANSPOSEX][TRANSPOSEY];
// Swap the x and y coordinates to perform the rotation (coalesced)
if (ID0 < P && ID1 < Q) {
buffer[ty][tx] = input[ID1*P + ID0];
}
// Synchronise all threads
barrier(CLK_LOCAL_MEM_FENCE);
// We don't have to swap the x and y thread indices here,
// because that's already done in the local memory
const int newID0 = get_group_id(1)*TRANSPOSEY + tx;
const int newID1 = get_group_id(0)*TRANSPOSEX + ty;
// Store the transposed result (coalesced)
if (newID0 < Q && newID1 < P) {
output[newID1*Q + newID0] = buffer[tx][ty];
}
}
__kernel void im2col(__global float* gm, const int8 _info, const float4 _param)
{
__global float* im_src = (__global float*)(gm + _info.s0);
int channels = _info.s1;
int height_inp = _info.s2;
int width_inp = _info.s3;
int kernel_h = _info.s4;
int kernel_w = _info.s4;
int pad_h = _info.s5;
int pad_w = _info.s5;
int stride_h = _info.s6;
int stride_w = _info.s6;
__global float* im_col = (__global float*)(gm + _info.s7);
int height_out = (height_inp + 2 * pad_h - kernel_h) / stride_h + 1;
int width_out = (width_inp + 2 * pad_w - kernel_w) / stride_w + 1;
int index = get_global_id(0);
if (index >= height_out * width_out * channels) return;
int j_out = index % width_out;
int i_out = (index / width_out) % height_out;
int c_inp = (index / width_out) / height_out;
int c_out = c_inp * kernel_h * kernel_w;
int i_inp = i_out * stride_h - pad_h;
int j_inp = j_out * stride_w - pad_w;
im_src += (c_inp * height_inp + i_inp) * width_inp + j_inp;
im_col += (c_out * height_out + i_out) * width_out + j_out;
for (int ki = 0; ki < kernel_h; ++ki) {
for (int kj = 0; kj < kernel_w; ++kj) {
int i = i_inp + ki;
int j = j_inp + kj;
*im_col = (i >= 0 && j >= 0 && i < height_inp && j < width_inp) ? im_src[ki * width_inp + kj] : 0;
im_col += height_out * width_out;
}
}
}
);
//#define OPENCL_SVM
int _info[8];
float _param[4];
args_t _args[] = {
#ifdef OPENCL_SVM
{ CL_MEM_READ_WRITE|CL_MEM_SVM_FINE_GRAIN_BUFFER, 0, 0, OCL_SVM },
#else
{ CL_MEM_READ_WRITE, 0, 0, OCL_BUFFER },
// { CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, 0, 0, OCL_BUFFER },
// { CL_MEM_READ_WRITE|CL_MEM_ALLOC_HOST_PTR, 0, 0, OCL_HOST_PTR },
#endif
{ 0, sizeof(int)*8, _info },
{ 0, sizeof(float)*4, _param },
{ 0, 0, 0, 0, 0 },
};
ocl_t _kernel[] = {
// global: m*MDIMC/MWG, n*NDIMC/NWG
{ _args, "gemm_rnn", 0, 2,{TS,TS} },
// global: k, n
{ _args, "transpose", 0, 2,{TRANSPOSEX,TRANSPOSEY} },
{ _args, "im2col", 0, 1,{16} },
};
int _ksz = sizeof(_kernel)/sizeof(_kernel[0]);
#define KGEMM_RNN _kernel[0]
#define KTRANSPOSE _kernel[1]
#define KIM2COL _kernel[2]
void sgemm_ocl_init(int platform, int device, size_t size)
{
_args[0].size = oclSetup(platform, device);
oclKernel(_kernel, _ksz, "-cl-denorms-are-zero -cl-finite-math-only -cl-fast-relaxed-math -Werror", sgemm_kcode);
oclKernelArgs(_kernel, _ksz);
if (size) _args[0].size = size;
}
static inline void sgemm_ocl(char ta, char tb, int m, int n, int k, float alpha, float *a, float *b, float beta, float *c)
{
int mk = m*k;
int kn = k*n;
int mn = m*n;
int off_a = 0;
int off_b = mk;
#ifndef OPENCL_SVM
oclWrite(_args[0].p, 0, sizeof(float)*mk, a);
oclWrite(_args[0].p, sizeof(float)*mk, sizeof(float)*kn, b);
if (beta!=0) oclWrite(_args[0].p, sizeof(float)*(mk+kn), sizeof(float)*mn, c);
#endif
if (ta=='T') {
_info[0] = m; // a
_info[1] = k; // ta
_info[2] = 0; // input a
_info[3] = off_a = mk +kn +mn;
KTRANSPOSE.global_size[0] = ceil_int(m, TRANSPOSEX);
KTRANSPOSE.global_size[1] = ceil_int(k, TRANSPOSEY);
oclRun(&KTRANSPOSE);
}
if (tb=='T') {
_info[0] = k; // b
_info[1] = n; // tb
_info[2] = mk; // input b
_info[3] = off_b = mk +kn +mn +mk;
KTRANSPOSE.global_size[0] = ceil_int(k, TRANSPOSEX);
KTRANSPOSE.global_size[1] = ceil_int(n, TRANSPOSEY);
oclRun(&KTRANSPOSE);
}
_info[0] = m;
_info[1] = n;
_info[2] = k;
_info[3] = off_a; // a
_info[4] = off_b; // b
_info[5] = mk +kn; // c
_param[0] = alpha;
_param[1] = beta;
KGEMM_RNN.global_size[0] = ceil_int(m, TS);
KGEMM_RNN.global_size[1] = ceil_int(n, TS);
// KGEMM_RNN.global_size[0] = ((m+1)/TS)*TS;
// KGEMM_RNN.global_size[1] = ((n+1)/TS)*TS;
// printf("M:%zu N:%zu ", KGEMM_RNN.global_size[0], KGEMM_RNN.global_size[1]);
oclRun(&KGEMM_RNN);
#ifndef OPENCL_SVM
oclRead(_args[0].p, sizeof(float)*(mk+kn), sizeof(float)*mn, c);
#endif
}
void sgemm_ocl_finish()
{
oclReleaseKernel(_kernel, _ksz);
oclFinish();
}
static inline void ocl_im2col(float *inputs, int ich, int w, int h, int k, int pad, int stride, float *outputs)
{
// im2col(pix, 3, h, w, 4, 4, 2, 2, 1, 1, workspace);
int hcol = (h + 2 * pad - k) / stride + 1;
int wcol = (w + 2 * pad - k) / stride + 1;
_info[0] = wcol*hcol*ich*k*k; // inputs
_info[1] = ich;
_info[2] = h;
_info[3] = w;
_info[4] = k;
_info[5] = pad;
_info[6] = stride;
_info[7] = 0; // outputs
KIM2COL.global_size[0] = ceil_int(_info[0], 16);
oclWrite(_args[0].p, sizeof(float)*_info[0], sizeof(float)*w*h*ich, inputs);
oclRun(&KIM2COL);
oclRead(_args[0].p, sizeof(float)*_info[7], sizeof(float)*_info[0], outputs);
}
#ifndef CATS_OPENCL
static inline void ocl_convolution(float *inputs, int ich, int w, int h, float *weights, int k, int pad, int stride, float *outputs, int ch)
{
// im2col(pix, 3, h, w, 4, 4, 2, 2, 1, 1, workspace);
int hcol = (h + 2 * pad - k) / stride + 1;
int wcol = (w + 2 * pad - k) / stride + 1;
oclWrite(_args[0].p, sizeof(float)*wcol*hcol*ich*k*k, sizeof(float)*w*h*ich, inputs);
_info[0] = wcol*hcol*ich*k*k; // inputs
_info[1] = ich;
_info[2] = h;
_info[3] = w;
_info[4] = k;
_info[5] = pad;
_info[6] = stride;
_info[7] = 0; // outputs
KIM2COL.global_size[0] = ceil_int(_info[0], 16);
oclRun(&KIM2COL);
// sgemm_ocl('N', 'T', ch, wcol*hcol, k*k, magic_kernel, workspace, pix);
oclWrite(_args[0].p, sizeof(float)*(wcol*hcol*ich*k*k), sizeof(float)*k*k*ich*ch, weights);
_info[0] = ch;
_info[1] = wcol*hcol /* *batch */;
_info[2] = k*k*ich;
_info[3] = wcol*hcol*ich*k*k; // a (weights)
_info[4] = 0; // b (col)
_info[5] = wcol*hcol*ich*k*k +k*k*ich*ch; // c
KGEMM_RNN.global_size[0] = ceil_int(_info[0], TS);
KGEMM_RNN.global_size[1] = ceil_int(_info[1], TS);
oclRun(&KGEMM_RNN);
oclRead(_args[0].p, sizeof(float)*_info[5], sizeof(float)*wcol*hcol*ch, outputs);
}
static inline void im2col(const float *im, const int channels,
const int height, const int width, const int kernel_h, const int kernel_w,
const int pad_h, const int pad_w, const int stride_h, const int stride_w, float *col)
{
int height_col = (height + 2 * pad_h - kernel_h) / stride_h + 1;
int width_col = (width + 2 * pad_w - kernel_w) / stride_w + 1;
int channels_col = channels * kernel_h * kernel_w;
for (int c=0; c<channels_col; c++) {
int w_offset = c % kernel_w;
int h_offset = (c / kernel_w) % kernel_h;
int c_im = c / kernel_h / kernel_w;
for (int h=0; h<height_col; h++) {
for (int w=0; w<width_col; w++) {
int h_pad = h * stride_h - pad_h + h_offset;
int w_pad = w * stride_w - pad_w + w_offset;
if (h_pad >= 0 && h_pad < height && w_pad >= 0 && w_pad < width)
col[(c * height_col + h) * width_col + w] =
im[(c_im * height + h_pad) * width + w_pad];
else
col[(c * height_col + h) * width_col + w] = 0;
}
}
}
}
float workspace[256*256*128*64];
static inline void ocl_convolution_LReLU(float *inputs, int ich, int w, int h, float *weights, int k, int pad, int stride, float *outputs, int ch, float *bias)
{
// im2col(pix, 3, h, w, 4, 4, 2, 2, 1, 1, workspace);
int hcol = (h + 2 * pad - k) / stride + 1;
int wcol = (w + 2 * pad - k) / stride + 1;
/* _info[0] = wcol*hcol*ich*k*k; // inputs
_info[1] = ich;
_info[2] = h;
_info[3] = w;
_info[4] = k;
_info[5] = pad;
_info[6] = stride;
_info[7] = 0; // outputs
KIM2COL.global_size[0] = ceil_int(_info[0], 16);
// printf("clEnqueueWriteBuffer: %lu %lu\n", sizeof(float)*_info[0], sizeof(float)*w*h*ich);
oclWrite(_args[0].p, sizeof(float)*_info[0], sizeof(float)*w*h*ich, inputs);
oclRun(&KIM2COL);*/
im2col(inputs, ich, h, w, k, k, pad, pad, stride, stride, workspace);
sgemm_ocl('N', 'N', ch, wcol*hcol, k*k*ich, 1.0, weights, workspace, 0, outputs);
// oclWrite(_args[0].p, 0, sizeof(float)*wcol*hcol*ich*k*k, workspace);
#if 0
// sgemm_ocl('N', 'T', ch, wcol*hcol, k*k, magic_kernel, workspace, pix);
_info[0] = ch;
_info[1] = wcol*hcol /* *batch */;
_info[2] = k*k*ich;
_info[3] = wcol*hcol*ich*k*k; // a (weights)
_info[4] = 0; // b (col)
_info[5] = wcol*hcol*ich*k*k +k*k*ich*ch; // c
_info[6] = _info[5] + wcol*hcol*ch;
KGEMM_RNN.global_size[0] = ceil_int(_info[0], TS);
KGEMM_RNN.global_size[1] = ceil_int(_info[1], TS);
// printf("clEnqueueWriteBuffer: %lu %lu\n", sizeof(float)*_info[3], sizeof(float)*k*k*ich*ch);
oclWrite(_args[0].p, sizeof(float)*_info[3], sizeof(float)*k*k*ich*ch, weights);
// printf("clEnqueueWriteBuffer: %lu %lu\n", sizeof(float)*_info[6], sizeof(float)*ch);
//oclWrite(_args[0].p, sizeof(float)*_info[6], sizeof(float)*ch, bias);
oclRun(&KGEMM_RNN);
// printf("clEnqueueReadBuffer: %lu %lu\n", sizeof(float)*_info[5], sizeof(float)*wcol*hcol*ch);
oclRead(_args[0].p, sizeof(float)*_info[5], sizeof(float)*wcol*hcol*ch, outputs);
#endif
// +bias LReLU
float *p = outputs;
for (int i=0; i<ch; i++) {
for (int n=0; n<wcol*hcol; n++) {
*p += bias[i];
*p = *p>0 ? (*p) : (*p)*0.1;
p++;
}
}
}
static int ocl_wsize;
static int ocl_off;
static int ocl_woff;
static inline void ocl_conv_init(float *weights, int wsize, float *bias, int bsize, /*float *X, int size,*/ int woff)
{
oclWrite(_args[0].p, 0, sizeof(float)*wsize, weights);
oclWrite(_args[0].p, sizeof(float)*wsize, sizeof(float)*bsize, bias);
// oclWrite(_args[0].p, sizeof(float)*(wsize+bsize), sizeof(float)*size, X);
ocl_wsize = wsize;
ocl_off = wsize+bsize;
ocl_woff = ocl_off + woff;
}
static inline void ocl_conv_LReLU(int inputs, int ich, int w, int h, int weights, int k, int pad, int stride, int outputs, int ch, int bias)
{
// im2col(pix, 3, h, w, 4, 4, 2, 2, 1, 1, workspace);
int hcol = (h + 2 * pad - k) / stride + 1;
int wcol = (w + 2 * pad - k) / stride + 1;
_info[0] = ocl_off + inputs; // inputs
_info[1] = ich;
_info[2] = h;
_info[3] = w;
_info[4] = k;
_info[5] = pad;
_info[6] = stride;
_info[7] = ocl_woff; // outputs
KIM2COL.global_size[0] = ceil_int(_info[0], 16);
oclRun(&KIM2COL);
// sgemm_ocl('N', 'T', ch, wcol*hcol, k*k, magic_kernel, workspace, pix);
_info[0] = ch;
_info[1] = wcol*hcol /* *batch */;
_info[2] = k*k*ich;
_info[3] = weights; // a (weights)
_info[4] = ocl_woff; // b (col)
_info[5] = ocl_off + outputs; // c
_info[6] = ocl_wsize + bias; // bias
KGEMM_RNN.global_size[0] = ceil_int(_info[0], TS);
KGEMM_RNN.global_size[1] = ceil_int(_info[1], TS);
oclRun(&KGEMM_RNN);
// +bias LReLU
/* float *p = outputs;
for (int i=0; i<ch; i++) {
for (int n=0; n<wcol*hcol; n++) {
*p += bias[i];
*p = *p>0 ? (*p) : (*p)*0.1;
p++;
}
}*/
}
#endif