-
Notifications
You must be signed in to change notification settings - Fork 62
/
gpu.js
533 lines (412 loc) · 13.1 KB
/
gpu.js
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
var ndarray = require('ndarray')
var cpuSoftmax = require('./common.js').cpuSoftmax
module.exports = function (regl, d) {
var WIDTH = 28
var HEIGHT = 28
/*
We use RGBA32f textures throughout the computations.
And we use RGBA, because
you can't render to a single-channel texture in WebGL.
*/
function createTexture (width, height) {
return regl.texture({
width: width,
height: height,
format: 'rgba',
type: 'float',
mag: 'nearest',
min: 'nearest',
wrap: 'clamp'
})
}
function getFbo (fbo) {
var a = []
regl({framebuffer: fbo})(function () {
var arr = regl.read()
for (var i = 0; i < arr.length; i += 4) {
a.push(arr[i])
}
})
return ndarray(a, [fbo.color[0].height, fbo.color[0].width])
}
// get all the pixel values of the red channel in the texture.
function getTex (tex) {
return getFbo(regl.framebuffer({color: tex}))
}
// this is the basis of all the GPGPU commands that we write.
var baseGpu = {
vert: [
'precision mediump float;',
'attribute vec2 position;',
'varying vec2 vUv;',
'void main () {',
' vUv = 0.5 * (position + 1.0);',
' gl_Position = vec4(position, 0, 1);',
'}'
].join('\n'),
attributes: {
// we render a full-screen triangle.
position: [ -4, -4, 4, -4, 0, 4 ]
},
count: 3,
// and we always output to an FBO
framebuffer: regl.prop('outFbo')
}
// gpu matrix multiplication. Returns a*b
function gpuMul (a, b) {
if (a.width !== b.height) {
throw Error('Impossible matrix mul!')
}
var outTex = createTexture(b.width, a.height)
var obj = baseGpu
obj.frag = [
'precision mediump float;',
'uniform sampler2D aTex;',
'uniform sampler2D bTex;',
'varying vec2 vUv;',
'#define oDim vec2(' + b.width + ',' + a.height + ')',
'#define aDim vec2(' + a.width + ',' + a.height + ')',
'#define bDim vec2(' + b.width + ',' + b.height + ')',
'#define aDimi ivec2(' + a.width + ',' + a.height + ')',
'void main () {',
// set uv to top-left corner of texel.
' vec2 uv = vUv;',
' float x = uv.x * oDim.x;',
' float y = uv.y * oDim.y;',
' float sum = 0.0;',
' for (int i = 0; i < aDimi.x; i++) {',
// multiply row i of a with col i of b.
' float a = texture2D(aTex, vec2(float(i) / aDim.x, y / aDim.y)).x;',
' float b = texture2D(bTex, vec2(x / bDim.x, float(i) / bDim.y)).x;',
' sum += a*b;',
' }',
'gl_FragColor = vec4(sum);',
'}'].join('\n')
obj.uniforms = {
aTex: regl.prop('aTex'),
bTex: regl.prop('bTex')
}
var cmd = regl(obj)
return {
out: outTex,
func: function () {
var outFbo = regl.framebuffer({color: outTex})
cmd({aTex: a, bTex: b, outFbo: outFbo})
}
}
}
// gpu component-wise matrix add.
function gpuAdd (a, b) {
if (a.width !== b.width || a.height !== b.height) {
throw Error('Impossible matrix add!')
}
var outTex = createTexture(a.width, a.height)
var obj = baseGpu
obj.frag = [
'precision mediump float;',
'uniform sampler2D aTex;',
'uniform sampler2D bTex;',
'varying vec2 vUv;',
'void main () {',
' float a = texture2D(aTex, vUv).x;',
' float b = texture2D(bTex, vUv).x;',
' float res = a + b;',
'gl_FragColor = vec4(res);',
'}'
].join('\n')
obj.uniforms = {
aTex: regl.prop('aTex'),
bTex: regl.prop('bTex')
}
var cmd = regl(obj)
return {
out: outTex,
func: function () {
var outFbo = regl.framebuffer({color: outTex})
cmd({aTex: a, bTex: b, outFbo: outFbo})
}
}
}
// do relu on every element in a.
// where relu = max(0,x)
function gpuRelu (a) {
var outTex = createTexture(a.width, a.height)
var obj = baseGpu
obj.frag = [
'precision mediump float;',
'uniform sampler2D aTex;',
'varying vec2 vUv;',
'void main () {',
' float a = texture2D(aTex, vUv).x;',
' float res = max(0.0, a);',
' gl_FragColor = vec4(res);',
'}'
].join('\n')
obj.uniforms = {
aTex: regl.prop('aTex')
}
var cmd = regl(obj)
return {
out: outTex,
func: function () {
var outFbo = regl.framebuffer({color: outTex})
cmd({aTex: a, outFbo: outFbo})
return outTex
}
}
}
// flatten a tensor into a column vector,
// and put that column vector in a texture.
function gpuFlatten (W) {
// output texture dimensions.
var oWidth = W.length * W[0].width * W[0].height
var oHeight = 1
var outTex = createTexture(oWidth, oHeight)
var obj = baseGpu
obj.frag = [
'precision mediump float;',
'uniform sampler2D wTex;',
'varying vec2 vUv;',
'uniform float i;', // which layer we are processing.
'#define oDim vec2(' + oWidth + ',' + oHeight + ')',
'#define wDim vec2(' + W[0].width + ',' + W[0].height + ')',
'#define wRcp vec2(1.0 / float(' + W[0].width + '), 1.0 / float(' + W[0].height + '))',
'#define wSize float(' + W[0].width + '*' + W[0].height + ')',
'void main () {',
' vec2 uv = vUv;',
' float elem = uv.x * oDim.x;',
// are we processing the right layer?
' if(((floor(elem / wSize)) - i) < 0.001) {',
' vec4 res = vec4(1.0);',
' elem = mod(elem, wSize);',
// find the value in the original texture, that is corresponding
// to the value in the flattened texture.
' float x = (mod(elem, wDim.x)) / wDim.x;',
' float y = (elem / wDim.x) / wDim.y;',
' res = vec4(texture2D(wTex, vec2(x,y) ).x);',
' gl_FragColor = vec4(res);',
' } else {',
// wrong layer, so do nothing!
' discard;',
' }',
'}'
].join('\n')
obj.uniforms = {
i: regl.prop('i'),
wTex: regl.prop('tex')
}
var pass = regl(obj)
return {
out: outTex,
func: function () {
var outFbo = regl.framebuffer({color: outTex})
for (var i = 0; i < W.length; i++) {
// NOTE: To simplify the implementation, we use 'discard'.
// But that also results in a bit of overdraw.
// But by rewriting the algorithm so that it does not use
// 'discard', some performance can be gained.
// But for this demo simple demo, that performance gain will
// be minimal, so we do this simpler implementation.
pass({tex: W[i], outFbo: outFbo, i: i})
}
}
}
}
// implements max-pool layer.
// with stride 2x2, and patch size 2x2.
function gpuMaxPool (W) {
var outLayers = []
for (var i = 0; i < W.length; i++) {
var outTex = createTexture(WIDTH / 2, HEIGHT / 2)
outLayers.push(outTex)
}
var obj = baseGpu
obj.frag = [
'precision mediump float;',
'uniform sampler2D wTex;',
'varying vec2 vUv;',
'#define wDim vec2(' + W[0].width + ',' + W[0].height + ')',
'void main () {',
' vec2 wRcp = vec2(1.0 / wDim.x, 1.0 / wDim.y);',
' vec2 uv = vUv;',
' float a = texture2D(wTex, uv + wRcp * vec2(0.0, 0.0)).x;',
' float b = texture2D(wTex, uv + wRcp * vec2(1.0, 0.0)).x;',
' float c = texture2D(wTex, uv + wRcp * vec2(0.0, 1.0)).x;',
' float d = texture2D(wTex, uv + wRcp * vec2(1.0, 1.0)).x;',
' float res = max(max(a, b), max(c, d));',
' gl_FragColor = vec4(res);',
'}'
].join('\n')
obj.uniforms = {
wTex: regl.prop('tex')
}
var cmd = regl(obj)
return {
out: outLayers,
func: function () {
var outFbo = regl.framebuffer({})
for (var i = 0; i < W.length; i++) {
outFbo({color: outLayers[i]})
// do a max-pool operations for all the layers.
cmd({tex: W[i], outFbo: outFbo})
}
}
}
}
// Do conv2d, bias and relu in a single pass, for increased performance.
// CONV patch size is 2x2, and the stride is 1x1.
function gpuConv2dBiasRelu (W, bias) {
var outLayers = []
for (var i = 0; i < W.length; i++) {
var outTex = createTexture(WIDTH, HEIGHT)
outLayers.push(outTex)
}
var obj = baseGpu
var kernelRad = Math.floor(W[0].width / 2.0)
obj.frag = [
'precision mediump float;',
'uniform sampler2D tex;',
'uniform sampler2D kernel;',
'uniform sampler2D bias;',
'varying vec2 vUv;',
'uniform float i;', // current layer index
'#define N float(' + W.length + ')',
'#define kRad int(' + kernelRad + ')',
'#define kDim vec2(' + W[0].width + ',' + W[0].height + ')',
'#define tDim vec2(' + WIDTH + ',' + HEIGHT + ')',
'void main () {',
' vec2 tRcp = vec2(1.0 / tDim.x, 1.0 / tDim.y);',
' vec2 kRcp = vec2(1.0 / kDim.x, 1.0 / kDim.y);',
' vec2 uv = vUv;',
' float sum = 0.0;',
' for( int ir = -kRad; ir <= +kRad; ir++) {',
' for( int ic = -kRad; ic <= +kRad; ic++) {',
' vec2 tUv = uv + vec2(float(ic), float(ir)) * tRcp;',
' float a = texture2D(tex, tUv).x;',
' vec2 kUv = (vec2(float(ic), float(ir)) + vec2(float(kRad))) * kRcp;',
' kUv += kRcp * 0.5;', // make sure we sample from the texel center!
' float b = texture2D(kernel, kUv).x;',
' float e = 0.01;',
// we use zero padding!
' if(tUv.x < (0.0-e) || tUv.y < (0.0-e) || tUv.x > (1.0+e) || tUv.y > (1.0+e)) {',
' a = 0.0;',
' }',
' sum += a * b;',
' }',
' }',
// add bias
' sum += texture2D(bias, vec2(i / N, 0.0)).x;',
// relu
' sum = max(sum, 0.0);',
' gl_FragColor = vec4(sum);',
'}'
].join('\n')
obj.uniforms = {
tex: regl.prop('tex'),
kernel: regl.prop('kernel'),
bias: regl.prop('bias'),
i: regl.prop('i')
}
var pass = regl(obj)
return {
out: outLayers,
func: function (x) {
var outFbo = regl.framebuffer({})
for (var i = 0; i < W.length; i++) {
outFbo({color: outLayers[i]})
pass({tex: x, kernel: W[i], bias: bias, outFbo: outFbo, i: i})
}
return outLayers
}
}
}
function createTensor (W, shape) {
var i
var textureData = []
var g
if (shape.length === 3) {
var textureSize = shape[1] * shape[2]
var layers = []
for (var l = 0; l < shape[0]; l++) {
// go through all the layers,
// and create a texture for each layer.
textureData = []
for (i = 0; i < textureSize; i++) {
g = W[textureSize * l + i]
textureData.push(g, g, g, g)
}
var layer = regl.texture({
width: shape[1],
height: shape[2],
data: textureData,
format: 'rgba',
type: 'float',
mag: 'nearest',
min: 'nearest'
})
layers.push(layer)
}
return layers
} else {
// create 2D tensor. So a single texture.
textureData = []
for (i = 0; i < W.length; i++) {
g = W[i]
textureData.push(g, g, g, g)
}
var tex = regl.texture({
width: shape[1],
height: shape[0],
data: textureData,
format: 'rgba',
type: 'float',
mag: 'nearest',
min: 'nearest'
})
return tex
}
}
//
// In advance, we create and cache all the tensors that we need
//
var wConv1 = createTensor(d.wConv1Data, [16, 5, 5])
var bConv1 = createTensor(d.bConv1Data, [1, 16])
var wFc1 = createTensor(d.wFc1Data, [14 * 14 * 16, 64])
var wFc2 = createTensor(d.wFc2Data, [64, 10])
var bFc2 = createTensor(d.bFc2Data, [1, 10])
var bFc1 = createTensor(d.bFc1Data, [1, 64])
//
// We also create all draw commands.
//
var gpuConv2dBiasReluFunc = gpuConv2dBiasRelu(wConv1, bConv1)
var gpuMaxPoolFunc = gpuMaxPool(gpuConv2dBiasReluFunc.out)
var gpuFlattenFunc = gpuFlatten(gpuMaxPoolFunc.out)
var gpuMulFunc1 = gpuMul(gpuFlattenFunc.out, wFc1)
var gpuAddFunc1 = gpuAdd(gpuMulFunc1.out, bFc1)
var gpuReluFunc = gpuRelu(gpuAddFunc1.out)
var gpuMulFunc2 = gpuMul(gpuReluFunc.out, wFc2)
var gpuAddFunc2 = gpuAdd(gpuMulFunc2.out, bFc2)
// warm up the regl drawing commands, by running the entire network once,
// for an array of zeros.
cnnGpu(Array.apply(null, Array(WIDTH * HEIGHT)).map(Number.prototype.valueOf, 0))
function cnnGpu (xSrc) {
var x = createTensor(xSrc, [28, 28])
// since we have already created the resources and draw commands,
// running CNN will be fast.
gpuConv2dBiasReluFunc.func(x)
gpuMaxPoolFunc.func()
gpuFlattenFunc.func()
gpuMulFunc1.func()
gpuAddFunc1.func()
gpuReluFunc.func()
gpuMulFunc2.func()
gpuAddFunc2.func()
// we will only be doing softmax on 10 elements, so very little will be
// gained from GPU accelerating softmax. So let us simplify things
// and do it on the CPU:
var res = getTex(gpuAddFunc2.out)
res = cpuSoftmax(res)
return res.data
}
return cnnGpu
}