-
Notifications
You must be signed in to change notification settings - Fork 501
/
Tensor.js
420 lines (373 loc) · 14.8 KB
/
Tensor.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
import { webgl2, MAX_TEXTURE_SIZE } from './WebGL2'
import * as tensorUtils from './utils/tensorUtils'
import ndarray from 'ndarray'
import ops from 'ndarray-ops'
import squeeze from 'ndarray-squeeze'
/**
* Tensor class
*/
export default class Tensor {
/**
* Creates a tensor
*
* @param {(TypedArray|Array)} data
* @param {number[]} shape
* @param {Object} [options]
*/
constructor(data, shape, options = {}) {
this.arrayType = options.type || Float32Array
if (data && data.length && (data instanceof this.arrayType || data instanceof Array)) {
tensorUtils.checkShape(data, shape)
if (data instanceof this.arrayType) {
this.tensor = ndarray(data, shape)
} else if (data instanceof Array) {
this.tensor = ndarray(new this.arrayType(data), shape)
}
} else if (!data.length && shape.length) {
// if shape present but data not provided, initialize with 0s
this.tensor = ndarray(new this.arrayType(shape.reduce((a, b) => a * b, 1)), shape)
} else {
this.tensor = ndarray(new this.arrayType([]), [])
}
}
/**
* Creates WebGL2 texture
*
* Without args, defaults to gl.TEXTURE_2D and gl.R32F
*
* @param {string} [opts.type]
* @param {string} [opts.format]
* @param {boolean} [opts.supportsTextureFragments]
*/
createGLTexture({ type = '2d', format = 'float', supportsTextureFragments = false }) {
let shape = []
if (this.tensor.shape.length === 1) {
shape = [1, this.tensor.shape[0]]
this.is1D = true
} else if (this.tensor.shape.length === 2) {
shape = this.tensor.shape
} else if (this.tensor.shape.length === 3 && (type === '2d_array' || type === '3d')) {
shape = this.tensor.shape
} else {
throw new Error('[Tensor] cannot create WebGL2 texture.')
}
this.glTextureShape = shape
this.glTextureType = type
this.glTextureFormat = format
if (type === '2d') {
if (this.glTextureShape[0] > MAX_TEXTURE_SIZE && supportsTextureFragments) {
this._create2DRowFragmentedGLTexture()
} else {
this._create2DGLTexture()
}
} else if (type === '2d_array' || type === '3d') {
this._create3DGLTexture()
} else {
throw new Error(`[Tensor] invalid type ${type}.`)
}
}
/**
* Create 2D WebGL2 texture
*/
_create2DGLTexture() {
const gl = webgl2.context
const textureOptions = webgl2.getWebGLTextureOptions(this.glTextureType, this.glTextureFormat)
const { textureTarget, textureInternalFormat, textureFormat, textureType } = textureOptions
this.glTexture = gl.createTexture()
webgl2.storeRef('texture', this.glTexture)
gl.bindTexture(textureTarget, this.glTexture)
const shape = this.glTextureShape
const data = this.tensor.data
gl.texImage2D(textureTarget, 0, textureInternalFormat, shape[1], shape[0], 0, textureFormat, textureType, data)
// clamp to edge
gl.texParameteri(textureTarget, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.texParameteri(textureTarget, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
// no interpolation
gl.texParameteri(textureTarget, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.texParameteri(textureTarget, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
}
/**
* For 2D WebGL2 texture with first dimension exceeding MAX_TEXTURE_SIZE, creates as array of 2D textures
*/
_create2DRowFragmentedGLTexture() {
const gl = webgl2.context
const textureOptions = webgl2.getWebGLTextureOptions(this.glTextureType, this.glTextureFormat)
const { textureTarget, textureInternalFormat, textureFormat, textureType } = textureOptions
this.glTextureFragments = []
this.glTextureFragmentShape = [MAX_TEXTURE_SIZE, this.glTextureShape[1]]
const shape = this.glTextureFragmentShape
const numFragments = Math.ceil(this.glTextureShape[0] / MAX_TEXTURE_SIZE)
let offset = 0
for (let k = 0; k < numFragments; k++) {
const glTexture = gl.createTexture()
webgl2.storeRef('texture', glTexture)
gl.bindTexture(textureTarget, glTexture)
// append 0s to last fragment
let data
if (k === numFragments - 1) {
data = new this.arrayType(shape[0] * shape[1])
data.set(this.tensor.data.slice(offset, offset + shape[0] * shape[1]), 0)
} else {
data = this.tensor.data.slice(offset, offset + shape[0] * shape[1])
}
gl.texImage2D(textureTarget, 0, textureInternalFormat, shape[1], shape[0], 0, textureFormat, textureType, data)
// clamp to edge
gl.texParameteri(textureTarget, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.texParameteri(textureTarget, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
// no interpolation
gl.texParameteri(textureTarget, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.texParameteri(textureTarget, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
this.glTextureFragments.push(glTexture)
offset += shape[0] * shape[1]
}
}
/**
* Create 3D WebGL2 texture
*/
_create3DGLTexture() {
const gl = webgl2.context
const textureOptions = webgl2.getWebGLTextureOptions(this.glTextureType, this.glTextureFormat)
const { textureTarget, textureInternalFormat, textureFormat, textureType } = textureOptions
this.glTexture = gl.createTexture()
webgl2.storeRef('texture', this.glTexture)
gl.bindTexture(textureTarget, this.glTexture)
const shape = this.glTextureShape
const data = tensorUtils.data3DLayoutForGL(this.arrayType, this.tensor, this.glTextureShape)
gl.texImage3D(
textureTarget,
0,
textureInternalFormat,
shape[1],
shape[0],
shape[2],
0,
textureFormat,
textureType,
data
)
// clamp to edge
gl.texParameteri(textureTarget, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.texParameteri(textureTarget, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
// no interpolation
gl.texParameteri(textureTarget, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.texParameteri(textureTarget, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
}
/**
* Converts an array of row-fragmented glTextureFragments into a single column-stacked texture
*/
convert2DRowFragmentedGLTextureToColStack() {
if (!this.glTextureFragments || !this.glTextureFragmentShape) {
throw new Error('[Tensor] no glTextureFragments available.')
}
const gl = webgl2.context
const textureOptions = webgl2.getWebGLTextureOptions(this.glTextureType, this.glTextureFormat)
const { textureTarget, textureInternalFormat, textureFormat, textureType } = textureOptions
if (!this.glTextureFragmentsAsColStack) {
this.glTextureFragmentsAsColStack = gl.createTexture()
webgl2.storeRef('texture', this.glTextureFragmentsAsColStack)
gl.bindTexture(textureTarget, this.glTextureFragmentsAsColStack)
const numFragments = this.glTextureFragments.length
this.glTextureFragmentsAsColStackShape = [
this.glTextureFragmentShape[0],
this.glTextureFragmentShape[1] * numFragments
]
const shape = this.glTextureFragmentsAsColStackShape
const data = new this.arrayType(shape.reduce((a, b) => a * b, 1))
gl.texImage2D(textureTarget, 0, textureInternalFormat, shape[1], shape[0], 0, textureFormat, textureType, data)
// clamp to edge
gl.texParameteri(textureTarget, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.texParameteri(textureTarget, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
// no interpolation
gl.texParameteri(textureTarget, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.texParameteri(textureTarget, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
} else {
gl.bindTexture(textureTarget, this.glTextureFragmentsAsColStack)
}
const fbo = gl.createFramebuffer()
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo)
this.glTextureFragments.forEach((texture, k) => {
gl.framebufferTexture2D(gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0)
gl.copyTexSubImage2D(
textureTarget,
0,
k * this.glTextureFragmentShape[1],
0,
0,
0,
this.glTextureFragmentShape[1],
this.glTextureFragmentShape[0]
)
})
gl.deleteFramebuffer(fbo)
}
/**
* Removes glTextureFragmentsAsColStack
*/
removeGLTextureFragmentsAsColStack() {
if (this.glTextureFragmentsAsColStack) {
const gl = webgl2.context
gl.deleteTexture(this.glTextureFragmentsAsColStack)
delete this.glTextureFragmentsAsColStack
delete this.glTextureFragmentsAsColStackShape
}
}
/**
* Deletes WebGLTexture
*/
deleteGLTexture() {
const gl = webgl2.context
if (this.glTexture) {
gl.deleteTexture(this.glTexture)
delete this.glTexture
}
if (this.glTextureFragments) {
this.glTextureFragments.forEach(texture => {
gl.deleteTexture(texture)
})
delete this.glTextureFragments
}
}
/**
* Replaces data in the underlying ndarray, and the corresponding WebGLTexture if glTexture is present
*
* @param {number[]} data
*/
replaceTensorData(data) {
if (data && data.length && data instanceof this.arrayType) {
this.tensor.data.set(data)
} else if (data && data.length && data instanceof Array) {
this.tensor.data.set(new this.arrayType(data))
} else {
throw new Error('[Tensor] invalid input for replaceTensorData method.')
}
if (this.glTexture) {
const gl = webgl2.context
const textureOptions = webgl2.getWebGLTextureOptions(this.glTextureType, this.glTextureFormat)
const { textureTarget, textureFormat, textureType } = textureOptions
gl.bindTexture(textureTarget, this.glTexture)
const shape = this.glTextureShape
if (this.glTextureType === '2d') {
const data = this.tensor.data
gl.texSubImage2D(textureTarget, 0, 0, 0, shape[1], shape[0], textureFormat, textureType, data, 0)
} else if (this.glTextureType === '2d_array' || this.glTextureType === '3d') {
const data = tensorUtils.data3DLayoutForGL(this.arrayType, this.tensor, shape)
gl.texSubImage3D(textureTarget, 0, 0, 0, 0, shape[1], shape[0], shape[2], textureFormat, textureType, data, 0)
}
}
}
/**
* Transfer data from webgl texture on GPU to ndarray on CPU
*/
transferFromGLTexture() {
if (this.glTextureFragments) {
this.tensor = ndarray(new this.arrayType(this.glTextureShape[0] * this.glTextureShape[1]), this.glTextureShape)
let offset = 0
for (let k = 0; k < this.glTextureFragments.length; k++) {
webgl2.bindOutputTexture(this.glTextureFragments[k], this.glTextureFragmentShape)
const fragmentData = webgl2.readData(this.glTextureFragmentShape)
// last fragment may need to be truncated
if (k === this.glTextureFragments.length - 1) {
const truncate = this.tensor.data.length - offset
this.tensor.data.set(fragmentData.subarray(0, truncate), offset)
} else {
this.tensor.data.set(fragmentData, offset)
}
offset += fragmentData.length
}
} else {
webgl2.bindOutputTexture(this.glTexture, this.glTextureShape)
this.tensor = ndarray(new this.arrayType([]), this.glTextureShape)
this.tensor.data = webgl2.readData(this.glTextureShape)
}
if (this.is1D && this.glTextureShape[0] === 1) {
// collapse to 1D
this.tensor = squeeze(this.tensor, [0])
}
}
/**
* Reshapes data into 2D representation preserving last axis (typically channel axis)
*/
reshapeTo2D() {
const axis = this.tensor.shape.length - 1
const axisSize = this.tensor.shape[axis]
const otherAxes = this.tensor.shape.slice(0, axis)
const otherAxesSize = otherAxes.reduce((a, b) => a * b, 1)
const reshaped = ndarray(new this.arrayType(otherAxesSize * axisSize), [otherAxesSize, axisSize])
const otherAxesData = ndarray(new this.arrayType(otherAxesSize), otherAxes)
const otherAxesDataRaveled = ndarray(new this.arrayType(otherAxesSize), [otherAxesSize])
const axisSlices = Array(this.tensor.shape.length).fill(null)
for (let n = 0; n < axisSize; n++) {
axisSlices[axis] = n
ops.assign(otherAxesData, this.tensor.pick(...axisSlices))
otherAxesDataRaveled.data = otherAxesData.data
ops.assign(reshaped.pick(null, n), otherAxesDataRaveled)
}
this.originalShape = this.tensor.shape
this.indicesForReshaped = tensorUtils.createIndicesFor2DReshaped(this.tensor.shape, false, axis)
this.tensor = reshaped
this.is2DReshaped = true
}
/**
* Reshapes tensor in 2D representation back to original
*
* Typically called at the end when data is read back from GPU
*
* @param {number} axis
*/
reshapeFrom2D(axis = -1) {
if (!this.is2DReshaped) {
throw new Error('[Tensor] not in reshaped 2D representation.')
}
if (!this.originalShape) {
throw new Error('[Tensor] does not contain originalShape.')
}
if (axis < 0) {
axis = this.originalShape.length + axis
}
// second axis is the channel, or common, axis
const channelDataSize = this.tensor.shape[0]
const channels = this.tensor.shape[1]
const reshaped = ndarray(new this.arrayType(this.originalShape.reduce((a, b) => a * b, 1)), this.originalShape)
const channelDataRaveled = ndarray(new this.arrayType(channelDataSize), [channelDataSize])
const unraveledChannelShape = [...this.originalShape.slice(0, axis), ...this.originalShape.slice(axis + 1)]
const unraveledChannel = ndarray(
new this.arrayType(unraveledChannelShape.reduce((a, b) => a * b, 1)),
unraveledChannelShape
)
const axisSlices = Array(this.originalShape.length).fill(null)
for (let n = 0; n < channels; n++) {
ops.assign(channelDataRaveled, this.tensor.pick(null, n))
unraveledChannel.data = channelDataRaveled.data
axisSlices[axis] = n
ops.assign(reshaped.pick(...axisSlices), unraveledChannel)
}
this.tensor = reshaped
}
/**
* Reshapes data into 2D square representation (underlying data remaining contiguous)
*/
reshapeTo2DSquare() {
const squareDim = Math.ceil(Math.sqrt(this.tensor.size))
const reshaped = ndarray(new this.arrayType(squareDim ** 2), [squareDim, squareDim])
reshaped.data.set(this.tensor.data)
this.originalShape = this.tensor.shape
this.indicesForReshaped = tensorUtils.createIndicesFor2DReshaped(this.tensor.shape, true)
this.tensor = reshaped
this.is2DSquareReshaped = true
}
/**
* Reshapes tensor in 2D square representation back to original (underlying data remains contiguous)
*/
reshapeFrom2DSquare() {
if (!this.is2DSquareReshaped) {
throw new Error('[Tensor] not in reshaped 2D square representation.')
}
if (!this.originalShape) {
throw new Error('[Tensor] does not contain originalShape.')
}
const size = this.originalShape.reduce((a, b) => a * b, 1)
const reshaped = ndarray(new this.arrayType(size), this.originalShape)
reshaped.data.set(this.tensor.data.subarray(0, size))
this.tensor = reshaped
}
}