forked from waylonflinn/weblas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
359 lines (269 loc) · 8.6 KB
/
index.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
var globals = require("./lib/globals"),
pipeline = require("./lib/pipeline"),
SGEMMCalculator = require("./lib/sgemmcalculator"),
SAXPYCalculator = require("./lib/saxpycalculator"),
SSCALCalculator = require("./lib/sscalcalculator"),
SDWNSCalculator = require("./lib/sdwnscalculator"),
SCLMPCalculator = require("./lib/sclmpcalculator");
// do we have a WebGL context?
if(globals.gl){
// yes, load the library
module.exports = createModule(globals.gl);
} else {
// no, abort and export null
module.exports = null;
}
function createModule(gl){
var sgemmcalculator = new SGEMMCalculator(gl),
saxpycalculator = new SAXPYCalculator(gl),
sscalcalculator = new SSCALCalculator(gl),
sdwnscalculator = new SDWNSCalculator(gl),
sclmpcalculator = new SCLMPCalculator(gl);
return {
// level one
"saxpy" : saxpy,
"sscal" : sscal, // single precision matrix scale
// level two
// level three
"sgemm" : sgemm, // single precision generalized matrix multiply
// extra
"sstd" : sstd, // single precision Standard Score normalization
"sdwns": sdwns,
"sclmp": sclmp,
// pipeline
"pipeline" : pipeline,
// internals
"gpu" : {
"gl" : gl,
"sgemm": pipeline.sgemmcalculator.calculate.bind(pipeline.sgemmcalculator),
"sscal" : pipeline.sscalcalculator.calculate.bind(pipeline.sscalcalculator),
"sclmp" : pipeline.sclmpcalculator.calculate.bind(pipeline.sclmpcalculator),
"sdwns" : pipeline.sdwnscalculator.calculate.bind(pipeline.sdwnscalculator),
"encode" : gl.encode.bind(gl)
},
"util" : { "fromArray" : fromArray, "transpose" : transpose}
};
/* Wrap the GL calculation object in a (relatively) user friendly function that
accepts TypedArrays
* convert the data to (padded) textures in GPU memory
* execute calculation
* read result into an array, and return
*/
function sgemm(M, N, K, alpha, A, B, beta, C){
if(C != null && C.length != N){
throw new Error("Only vector C with length matching rows in A is currently supported.");
}
// pack each matrix into a single RGBA texel array, with the second transposed
var texels0 = A,
texels1,
texels2 = C;
texels1 = transpose(K, N, B);
// create input textures from data
var texture0 = gl.createDataTexture(M, K, texels0);
var texture1 = gl.createDataTexture(N, K, texels1);
var texture2 = null;
if(texels2 != null){
texture2 = gl.createDataTexture(1, N, texels2);
}
var texture3 = gl.createOutputTexture(M, N);
sgemmcalculator.calculate(M, N, K, alpha, texture0, texture1, beta, texture2, texture3);
// retrieve data
rawBuffer = gl.readData(M, N);
// clean up
gl.context.deleteTexture(texture0);
gl.context.deleteTexture(texture1);
if(texture2 != null){
gl.context.deleteTexture(texture2);
}
gl.context.deleteTexture(texture3);
// return result
return new Float32Array(rawBuffer);
}
function saxpy(N, a, X, Y){
var rawBuffer;
var texels0 = X,
texels1;
// TODO: special shader for constant Y
if(isFloat32Array(Y)){
texels1 = Y;
} else {
texels1 = new Float32Array(N);
texels1.fill(Y);
}
// create input textures from data
var texture0 = gl.createDataTexture(1, N, texels0);
var texture1 = gl.createDataTexture(1, N, texels1);
var texture3 = gl.createOutputTexture(1, N);
saxpycalculator.calculate(N, a, texture0, texture1, texture3);
// retrieve data
rawBuffer = gl.readData(1, N);
// clean up
gl.context.deleteTexture(texture0);
gl.context.deleteTexture(texture1);
gl.context.deleteTexture(texture3);
// return result
return new Float32Array(rawBuffer);
}
function isFloat32Array(obj){
return Object.prototype.toString.call(obj) === "[object Float32Array]";
}
/* a more general version of the BLAS Level 1 scale, that works on matrices
and includes an elementwise scalar addition
a * X + b
a - multiplicative scalar
b - additive scalar
X - matrix (M x N)
to get the standard BLAS scal set M = 1 and b = 0
this function is generally only cost effective to use in a pipeline
*/
function sscal(M, N, a, b, X){
var rawBuffer;
var texels0 = X;
var texture0 = gl.createDataTexture(M, N, texels0);
var texture3 = gl.createOutputTexture(M, N);
sscalcalculator.calculate(M, N, a, b, texture0, texture3);
// retrieve data
rawBuffer = gl.readData(M, N);
// clean up
gl.context.deleteTexture(texture0);
gl.context.deleteTexture(texture3);
// return result
return new Float32Array(rawBuffer);
}
/* Calculate the Standard Score normalization (subtract mean
,divide by standard deviation).
*/
function sstd(M, N, mu, sigma, X){
var rawBuffer;
var texels0 = X;
var texture0 = gl.createDataTexture(M, N, texels0);
var texture3 = gl.createOutputTexture(M, N);
// adjust the parameters (for inverse) and call the standard score normalization
sscalcalculator.calculate(M, N, 1.0/sigma, -1.0 * mu/sigma, texture0, texture3);
// retrieve data
rawBuffer = gl.readData(M, N);
// clean up
gl.context.deleteTexture(texture0);
gl.context.deleteTexture(texture3);
// return result
return new Float32Array(rawBuffer);
}
/* downsample an image (taking the max) for Pooling
M - rows in input
N - columns in input
c - channels in input
factor - the downsample factor (width of patch to sample)
stride - width between pooling regions
X - input image
*/
function sdwns(M, N, channels, factor, stride, X){
var texels0 = X;
var texture0 = gl.createDataTexture(M, N * channels, X);
var N_out = Math.floor((N - factor) / stride) + 1;
var M_out = Math.floor((M - factor) / stride) + 1;
var texture3 = gl.createOutputTexture(M_out, N_out * channels);
sdwnscalculator.calculate(M, N, channels, factor, stride, texture0, texture3);
// retrieve data
rawBuffer = gl.readData(M_out, N_out * channels);
// clean up
gl.context.deleteTexture(texture0);
gl.context.deleteTexture(texture3);
// return result
return new Float32Array(rawBuffer);
}
/* Elementwise clamp function for matrices on the interval [a, b]. Can also be
used for min or max, by passing Number.MIN_VALUE for the first parameter and
Number.MAX_VALUE for the second parameter, respectively.
Passing `null` for either of these parameters will default to it's
respective min or max value.
M - number of rows in X
N - number of columns in X
a - lower bound (inclusize)
b - upper bound (inclusive)
X - matrix
to get the standard BLAS scal set M = 1 and b = 0
this function is generally only cost effective to use in a pipeline
*/
function sclmp(M, N, a, b, X){
a = (a != null) ? a : Number.MIN_VALUE;
b = (b != null) ? b : Number.MAX_VALUE;
var rawBuffer;
var texels0 = X;
var texture0 = gl.createDataTexture(M, N, texels0);
var texture3 = gl.createOutputTexture(M, N);
sclmpcalculator.calculate(M, N, a, b, texture0, texture3);
// retrieve data
rawBuffer = gl.readData(M, N);
// clean up
gl.context.deleteTexture(texture0);
gl.context.deleteTexture(texture3);
// return result
return new Float32Array(rawBuffer);
}
/*
function saxpy(n, a, x, y){
var i = 0,
result = new Float32Array(n);
// assert n = x.length
// assert a is scalar
// assert x is Float32Array
if(isNumeric(y)){
// shortcut for scalar y
for(; i < n; i++){
result[i] = a * x[i] + y;
}
} else {
for(; i < n; i++){
result[i] = a * x[i] + y[i];
}
}
return result;
}*/
// add a String.format method, if none exists
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
function isNumeric( obj ) { return (obj - parseFloat( obj ) + 1) >= 0; }
/* create a typed array from a 2D javascript array */
function fromArray(array, type, tranpose) {
var shape = [],
data,
c; // number of columns
if(!tranpose){
shape[0] = array.length;
shape[1] = array[0].length;
} else {
shape[1] = array.length;
shape[0] = array[0].length;
}
c = shape[1];
type = type || Float32Array;
data = new type(shape[0]*shape[1]);
for (var ii = 0; ii < shape[0]; ++ii)
for (var jj = 0; jj < shape[1]; ++jj)
if(!tranpose)
data[ii*c + jj] = array[ii][jj];
else
data[ii*c + jj] = array[jj][ii];
return data;
};
// tranpose a typed array in row major order, with the given row and column
// numers
function transpose(r, c, typedArray){
var result = new typedArray.constructor(r*c);
for(var i = 0; i < r; i++){
for(var j = 0; j < c; j++){
result[j * r + i] = typedArray[i * c + j];
}
}
return result;
}
}