-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsgemm_convolution.c
220 lines (188 loc) · 5.69 KB
/
sgemm_convolution.c
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
// ©2020 Yuichiro Nakada
// clang convolution.c -o convolution -lm -lOpenCL
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
//#define STB_IMAGE_RESIZE_IMPLEMENTATION
//#include "stb_image_resize.h"
#define _DEBUG
//#include "sgemm_ocl.h"
#include "sgemm_ocl1.h"
#define real float
// gemm('N', 'N', 1, w*h, 4*4, magic_kernel, workspace, pix);
void gemm(char ta, char tb, int M, int N, int K, float *a, float *b, float *c)
{
/* int lda = M*K;
int ldb = K*N;
int ldc = M*N;*/
for (int m=0; m<M; m++) {
for (int n=0; n<N; n++) {
float sum = 0.0;
/* for (int k=0; k<K; k++) {
sum += a[m + k * lda] * b[k + n * ldb];
}
// Z[m + n * ldc] = alpha * sum + beta * Z[m + n * ldc];
c[m + n * ldc] = sum;*/
/* // CTN
for (int k=0; k<K; k++) {
sum += a[k + m * lda] * b[k + n * ldb];
}
//Z[m + n * ldc] = alpha * sum + beta * Z[m + n * ldc];
c[m + n * ldc] = sum;*/
/* // CTT
for (int k=0; k<K; k++) {
sum += a[k + m * lda] * b[n + k * ldb];
}
//Z[m + n * ldc] = alpha * sum + beta * Z[m + n * ldc];
c[m + n * ldc] = sum;*/
for (int k=0; k<K; k++) {
// sum += a[k*M + m] * b[n*K + k];
sum += a[k*M + m] * b[n + N*k]; // NT
// sum += a[k + m*K] * b[n + N*k]; // TT
// sum += a[k + m*K] * b[n*K + k]; // TN
}
//Z[m + n * ldc] = alpha * sum + beta * Z[m + n * ldc];
c[n*M + m] = sum; // Column Major
}
}
}
static inline void im2col(const real *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, real *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;
}
}
}
}
real magic_kernel[4*4 *3*3] = {
1/64.0, 3/64.0, 3/64.0, 1/64.0,
3/64.0, 9/64.0, 9/64.0, 3/64.0,
3/64.0, 9/64.0, 9/64.0, 3/64.0,
1/64.0, 3/64.0, 3/64.0, 1/64.0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
1/64.0, 3/64.0, 3/64.0, 1/64.0,
3/64.0, 9/64.0, 9/64.0, 3/64.0,
3/64.0, 9/64.0, 9/64.0, 3/64.0,
1/64.0, 3/64.0, 3/64.0, 1/64.0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
1/64.0, 3/64.0, 3/64.0, 1/64.0,
3/64.0, 9/64.0, 9/64.0, 3/64.0,
3/64.0, 9/64.0, 9/64.0, 3/64.0,
1/64.0, 3/64.0, 3/64.0, 1/64.0,
};
int main(int argc, char* argv[])
{
char *name = argv[1];
char *outfile = "convolution.jpg";
/* if (argc <=1) {
usage(stderr, argv);
return 0;
}*/
int platform = 0;
int device = 0;
for (int i=1; i<argc; i++) {
if (!strcmp(argv[i], "-p")) {
platform = atoi(argv[++i]);
} else if (!strcmp(argv[i], "-d")) {
device = atoi(argv[++i]);
} else {
name = argv[i];
}
}
uint8_t *pixels;
int w, h, bpp;
pixels = stbi_load(name, &w, &h, &bpp, 3);
assert(pixels);
real *pix = malloc(sizeof(real)*w*h*3*10);
for (int sy=0; sy<h; sy++) {
for (int sx=0; sx<w; sx++) {
pix[(w*sy+sx)] = pixels[(w*sy+sx)*3 ] /255.0;
pix[(w*sy+sx)+w*h] = pixels[(w*sy+sx)*3+1] /255.0;
pix[(w*sy+sx)+w*h*2] = pixels[(w*sy+sx)*3+2] /255.0;
}
}
// sgemm_ocl_init(platform, device, (w*h*1+w*h*1*4*4+4*4*1*1+w*h*1)*sizeof(float));
sgemm_ocl_init(platform, device, (w*h*3+w*h*3*4*4+4*4*1*1+w*h*3)*sizeof(float));
/* real *workspace = malloc(sizeof(real)*w*h*3*4*4*3);
// im2col(pix, 3, h, w, 4, 4, 2, 2, 1, 1, workspace);
// w += 1;
// h += 1;
im2col(pix, 3, h, w, 4, 4, 1, 1, 1, 1, workspace);
w -= 1;
h -= 1;
// https://qiita.com/t-tkd3a/items/6b17f296d61d14e12953
sgemm_ocl('N', 'T', 1, w*h, 4*4, magic_kernel, workspace, pix);
// gemm('N', 'T', 1, w*h, 4*4, magic_kernel, workspace, pix);
// gemm('N', 'N', w*h, 1, 4*4, workspace, magic_kernel, pix);
free(workspace);*/
/* real *workspace = malloc(sizeof(real)*w*h*3*4*4*3);
ocl_im2col(pix, 1, w, h, 4, 1, 1, workspace);
w -= 1;
h -= 1;
gemm('N', 'T', 1, w*h, 4*4, magic_kernel, workspace, pix);
// gemm('N', 'T', w*h, 1, 4*4, workspace, magic_kernel, pix);
// gemm('N', 'N', w*h, 1, 4*4, workspace, magic_kernel, pix);
free(workspace);*/
// real *workspace = malloc(sizeof(real)*w*h*3*4*4*3);
// im2col(pix, 1, h, w, 4, 4, 1, 1, 1, 1, workspace);
// ocl_convolution(workspace, 1, w, h, magic_kernel, 4, 1, 1, pix, 1);
//ocl_convolution(pix, 1, w, h, magic_kernel, 4, 1, 1, pix, 1);
// float bias[] = { 10 };
// ocl_convolution_LReLU(pix, 1, w, h, magic_kernel, 4, 1, 1, pix, 1, bias);
float bias[] = { 0,0,0 };
ocl_convolution_LReLU(pix, 3, w, h, magic_kernel, 4, 1, 1, pix, 3, bias);
w -= 1;
h -= 1;
sgemm_ocl_finish();
for (int sy=0; sy<h; sy++) {
for (int sx=0; sx<w; sx++) {
pixels[(w*sy+sx)*3 ] = pix[(w*sy+sx)] *255.0;
// pixels[(w*sy+sx)*3+1] = pix[(w*sy+sx)] *255.0;
// pixels[(w*sy+sx)*3+2] = pix[(w*sy+sx)] *255.0;
pixels[(w*sy+sx)*3+1] = pix[(w*sy+sx)+w*h ] *255.0;
pixels[(w*sy+sx)*3+2] = pix[(w*sy+sx)+w*h*2] *255.0;
}
}
stbi_write_jpg(outfile, w, h, 3, pixels, 0);
free(pix);
stbi_image_free(pixels);
}