-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensor.js
282 lines (255 loc) · 7.19 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
// From https://github.com/webmachinelearning/webnn-baseline/blob/main/src/lib/tensor.js.
'use strict';
/**
* Compute the number of elements given a shape.
* @param {Array} shape
* @return {Number}
*/
function sizeOfShape(shape)
{
return shape.reduce((accumulator, currentValue) => accumulator * currentValue, 1);
}
// Mask any dimensions of length 1 to 0, so that when coordinates are masked via getMaskedCoordinate
// that they have no contribution to the element location, allowing trivial broadcasting.
// e.g. shape [3,1,4] yields a mask [1,0,1].
function makeBroadcastingMask(shape)
{
return shape.slice().map((value) => value > 1 ? 1 : 0);
}
// Apply the mask to the coordinate.
// e.g. coordinate [1,2,3] with mask [1,0,1] yields coordinate [1,0,3].
function getMaskedCoordinate(coordinate, mask)
{
console.assert(coordinate.length == mask.length);
return coordinate.slice().map((value, index) => value * mask[index]);
}
// Bidirectional broadcasting between two shapes.
// Any dimensions of size 1 in the first tensor will be broadcast to the dimension of the other tensor.
// e.g. first [1,3,1] with second [2,1,4] yields shape [2,3,4].
// If two corresponding coordinates are > 1, the first one wins (not an error). e.g. [2,1] and [3,4] yield [2,4].
function broadcastShapeWith(first, second)
{
console.assert(first.length == second.length);
return first.slice().map((value, index) => (value == 1) ? second[index] : value);
}
/**
* TensorElementIterator: iterator over elements.
*/
class TensorCoordinateIterator extends Iterator
{
index; // Current logical element index.
size; // Total number of elements.
shape; // Dimensions. e.g. a 3D tensor [2,3,4]
coordinate; // Current coordinate.
constructor(shape)
{
super();
this.index = 0;
this.size = sizeOfShape(shape);
this.coordinate = new Array(shape.length).fill(0);
this.shape = [...shape];
}
next()
{
if (this.index >= this.size)
{
// The last coordinate was already read, or this is the first call and 0 dimensions were present.
return {done: true, value: undefined};
}
else if (this.index == 0)
{
// Unlike most language iterators, JS calls next() the first iteration too, which would yield the wrong
// value, skipping past coordinate [0,0,0]. So treat the first call specially.
this.index++;
return {done: false, value: this.coordinate};
}
else
{
// Advance to next coordinate. e.g.
// Given shape [2,3,4]:
// Coordinate [0,1,2] -> [0,1,3]
// Coordinate [0,1,3] -> [1,2,0]
this.index++;
for (let i = this.shape.length; i > 0; )
{
--i;
this.coordinate[i]++;
if (this.coordinate[i] < this.shape[i])
break;
this.coordinate[i] = 0;
}
return {done: false, value: this.coordinate};
}
}
}
/**
* Tensor: the multidimensional array.
*/
class Tensor
{
data;
shape;
strides;
/**
* Construct a Tensor object
* @param {Array} shape
* @param {Array} [data]
*/
constructor(shape, data = undefined, shouldCopy = true)
{
const size = sizeOfShape(shape);
if (data !== undefined)
{
if (size !== data.length)
{
throw new Error(`The length of data ${data.length} is invalid. Expected ${size}.`);
}
if (shouldCopy)
{
this.data = data.slice();
}
else
{
this.data = data;
}
}
else
{
this.data = new Array(size).fill(0);
}
// Copy the shape, and calculate the strides.
this.shape = shape.slice();
this.strides = new Array(this.rank);
let stride = 1;
for (let i = this.rank; i > 0; )
{
this.strides[--i] = stride;
stride *= this.shape[i];
}
}
get rank()
{
return this.shape.length;
}
get size()
{
return this.data.length;
}
/**
* Get index in the flat array given the location.
* @param {Array} location
* @return {Number}
*/
indexFromLocation(location)
{
if (location.length !== this.rank)
{
throw new Error(`The location length ${location.length} is not equal to rank ${this.rank}.`);
}
let index = 0;
for (let i = 0; i < this.rank; ++i)
{
if (location[i] >= this.shape[i])
{
throw new Error(`The location value ${location[i]} at axis ${i} is invalid.`);
}
index += this.strides[i] * location[i];
}
return index;
}
/**
* Get location from the index of the flat array.
* @param {Number} index
* @return {Array}
*/
locationFromIndex(index)
{
if (index >= this.size)
{
throw new Error('The index is invalid.');
}
const location = new Array(this.rank);
for (let i = 0; i < location.length; ++i)
{
location[i] = Math.floor(index / this.strides[i]);
index -= location[i] * this.strides[i];
}
return location;
}
/**
* Set value given the location.
* @param {Array} location
* @param {Number} value
*/
setValueByLocation(location, value)
{
this.data[this.indexFromLocation(location)] = value;
}
/**
* Get value given the location.
* @param {Array} location
* @return {Number}
*/
getValueByLocation(location)
{
return this.data[this.indexFromLocation(location)];
}
at(location)
{
return this.data[this.indexFromLocation(location)];
}
setAt(location, value)
{
this.data[this.indexFromLocation(location)] = value;
}
/**
* Set value given the index.
* @param {Number} index
* @param {Number} value
*/
setValueByIndex(index, value)
{
if (index >= this.size)
{
throw new Error('The index is invalid.');
}
this.data[index] = value;
}
/**
* Get value given the index.
* @param {Number} index
* @return {Number}
*/
getValueByIndex(index)
{
if (index >= this.size)
{
throw new Error('The index is invalid.');
}
return this.data[index];
}
/*TensorCoordinateIterator*/ get coordinates()
{
return new TensorCoordinateIterator(this.shape);
}
// Return new tensor view of existing data.
/*Tensor*/ asShape(/*Array*/ shape)
{
// Create a new view of the data, but do not copy the data.
return new Tensor(shape, this.data, false);
}
}
/**
* Scalar: a helper class to create a Tensor with a single value.
*/
class Scalar extends Tensor
{
/**
* Construct a Tensor with a single value.
* @param {Number} value
*/
constructor(value)
{
super([1], [value]);
}
}