-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathutil_base.ts
747 lines (678 loc) · 20.3 KB
/
util_base.ts
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import {BackendValues, DataType, DataTypeMap, FlatVector, NumericDataType, TensorLike, TypedArray, WebGLData, WebGPUData} from './types';
/**
* Shuffles the array in-place using Fisher-Yates algorithm.
*
* ```js
* const a = [1, 2, 3, 4, 5];
* tf.util.shuffle(a);
* console.log(a);
* ```
*
* @param array The array to shuffle in-place.
*
* @doc {heading: 'Util', namespace: 'util'}
*/
// tslint:disable-next-line:no-any
export function shuffle(array: any[]|Uint32Array|Int32Array|
Float32Array): void {
let counter = array.length;
let index = 0;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
index = (Math.random() * counter) | 0;
// Decrease counter by 1
counter--;
// And swap the last element with it
swap(array, counter, index);
}
}
/**
* Shuffles two arrays in-place the same way using Fisher-Yates algorithm.
*
* ```js
* const a = [1,2,3,4,5];
* const b = [11,22,33,44,55];
* tf.util.shuffleCombo(a, b);
* console.log(a, b);
* ```
*
* @param array The first array to shuffle in-place.
* @param array2 The second array to shuffle in-place with the same permutation
* as the first array.
*
* @doc {heading: 'Util', namespace: 'util'}
*/
export function shuffleCombo(
// tslint:disable-next-line:no-any
array: any[]|Uint32Array|Int32Array|Float32Array,
// tslint:disable-next-line:no-any
array2: any[]|Uint32Array|Int32Array|Float32Array): void {
if (array.length !== array2.length) {
throw new Error(
`Array sizes must match to be shuffled together ` +
`First array length was ${array.length}` +
`Second array length was ${array2.length}`);
}
let counter = array.length;
let index = 0;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
index = (Math.random() * counter) | 0;
// Decrease counter by 1
counter--;
// And swap the last element of each array with it
swap(array, counter, index);
swap(array2, counter, index);
}
}
/** Clamps a value to a specified range. */
export function clamp(min: number, x: number, max: number): number {
return Math.max(min, Math.min(x, max));
}
export function nearestLargerEven(val: number): number {
return val % 2 === 0 ? val : val + 1;
}
export function swap<T>(
object: {[index: number]: T}, left: number, right: number) {
const temp = object[left];
object[left] = object[right];
object[right] = temp;
}
export function sum(arr: number[]): number {
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
/**
* Returns a sample from a uniform [a, b) distribution.
*
* @param a The minimum support (inclusive).
* @param b The maximum support (exclusive).
* @return A pseudorandom number on the half-open interval [a,b).
*/
export function randUniform(a: number, b: number) {
const r = Math.random();
return (b * r) + (1 - r) * a;
}
/** Returns the squared Euclidean distance between two vectors. */
export function distSquared(a: FlatVector, b: FlatVector): number {
let result = 0;
for (let i = 0; i < a.length; i++) {
const diff = Number(a[i]) - Number(b[i]);
result += diff * diff;
}
return result;
}
/**
* Asserts that the expression is true. Otherwise throws an error with the
* provided message.
*
* ```js
* const x = 2;
* tf.util.assert(x === 2, 'x is not 2');
* ```
*
* @param expr The expression to assert (as a boolean).
* @param msg A function that returns the message to report when throwing an
* error. We use a function for performance reasons.
*
* @doc {heading: 'Util', namespace: 'util'}
*/
export function assert(expr: boolean, msg: () => string) {
if (!expr) {
throw new Error(typeof msg === 'string' ? msg : msg());
}
}
export function assertShapesMatch(
shapeA: number[], shapeB: number[], errorMessagePrefix = ''): void {
assert(
arraysEqual(shapeA, shapeB),
() => errorMessagePrefix + ` Shapes ${shapeA} and ${shapeB} must match`);
}
export function assertNonNull(a: TensorLike): void {
assert(
a != null,
() => `The input to the tensor constructor must be a non-null value.`);
}
/**
* Returns the size (number of elements) of the tensor given its shape.
*
* ```js
* const shape = [3, 4, 2];
* const size = tf.util.sizeFromShape(shape);
* console.log(size);
* ```
*
* @doc {heading: 'Util', namespace: 'util'}
*/
export function sizeFromShape(shape: number[]): number {
if (shape.length === 0) {
// Scalar.
return 1;
}
let size = shape[0];
for (let i = 1; i < shape.length; i++) {
size *= shape[i];
}
return size;
}
export function isScalarShape(shape: number[]): boolean {
return shape.length === 0;
}
export function arraysEqualWithNull(n1: number[], n2: number[]) {
if (n1 === n2) {
return true;
}
if (n1 == null || n2 == null) {
return false;
}
if (n1.length !== n2.length) {
return false;
}
for (let i = 0; i < n1.length; i++) {
if (n1[i] !== null && n2[i] !== null && n1[i] !== n2[i]) {
return false;
}
}
return true;
}
export function arraysEqual(n1: FlatVector, n2: FlatVector) {
if (n1 === n2) {
return true;
}
if (n1 == null || n2 == null) {
return false;
}
if (n1.length !== n2.length) {
return false;
}
for (let i = 0; i < n1.length; i++) {
if (n1[i] !== n2[i]) {
return false;
}
}
return true;
}
export function isInt(a: number): boolean {
return a % 1 === 0;
}
export function tanh(x: number): number {
// tslint:disable-next-line:no-any
if ((Math as any).tanh != null) {
// tslint:disable-next-line:no-any
return (Math as any).tanh(x);
}
if (x === Infinity) {
return 1;
} else if (x === -Infinity) {
return -1;
} else {
const e2x = Math.exp(2 * x);
return (e2x - 1) / (e2x + 1);
}
}
export function sizeToSquarishShape(size: number): [number, number] {
const width = Math.ceil(Math.sqrt(size));
return [width, Math.ceil(size / width)];
}
/**
* Creates a new array with randomized indices to a given quantity.
*
* ```js
* const randomTen = tf.util.createShuffledIndices(10);
* console.log(randomTen);
* ```
*
* @param number Quantity of how many shuffled indices to create.
*
* @doc {heading: 'Util', namespace: 'util'}
*/
export function createShuffledIndices(n: number): Uint32Array {
const shuffledIndices = new Uint32Array(n);
for (let i = 0; i < n; ++i) {
shuffledIndices[i] = i;
}
shuffle(shuffledIndices);
return shuffledIndices;
}
export function rightPad(a: string, size: number): string {
if (size <= a.length) {
return a;
}
return a + ' '.repeat(size - a.length);
}
export function repeatedTry(
checkFn: () => boolean, delayFn = (counter: number) => 0,
maxCounter?: number,
scheduleFn?: (functionRef: Function, delay: number) =>
void): Promise<void> {
return new Promise<void>((resolve, reject) => {
let tryCount = 0;
const tryFn = () => {
if (checkFn()) {
resolve();
return;
}
tryCount++;
const nextBackoff = delayFn(tryCount);
if (maxCounter != null && tryCount >= maxCounter) {
reject();
return;
}
if (scheduleFn != null) {
scheduleFn(tryFn, nextBackoff);
} else {
// google3 does not allow assigning another variable to setTimeout.
// Don't refactor this so scheduleFn has a default value of setTimeout.
setTimeout(tryFn, nextBackoff);
}
};
tryFn();
});
}
/**
* Given the full size of the array and a shape that may contain -1 as the
* implicit dimension, returns the inferred shape where -1 is replaced.
* E.g. For shape=[2, -1, 3] and size=24, it will return [2, 4, 3].
*
* @param shape The shape, which may contain -1 in some dimension.
* @param size The full size (number of elements) of the array.
* @return The inferred shape where -1 is replaced with the inferred size.
*/
export function inferFromImplicitShape(
shape: number[], size: number): number[] {
let shapeProd = 1;
let implicitIdx = -1;
for (let i = 0; i < shape.length; ++i) {
if (shape[i] >= 0) {
shapeProd *= shape[i];
} else if (shape[i] === -1) {
if (implicitIdx !== -1) {
throw Error(
`Shapes can only have 1 implicit size. ` +
`Found -1 at dim ${implicitIdx} and dim ${i}`);
}
implicitIdx = i;
} else if (shape[i] < 0) {
throw Error(`Shapes can not be < 0. Found ${shape[i]} at dim ${i}`);
}
}
if (implicitIdx === -1) {
if (size > 0 && size !== shapeProd) {
throw Error(`Size(${size}) must match the product of shape ${shape}`);
}
return shape;
}
if (shapeProd === 0) {
throw Error(
`Cannot infer the missing size in [${shape}] when ` +
`there are 0 elements`);
}
if (size % shapeProd !== 0) {
throw Error(
`The implicit shape can't be a fractional number. ` +
`Got ${size} / ${shapeProd}`);
}
const newShape = shape.slice();
newShape[implicitIdx] = size / shapeProd;
return newShape;
}
export function parseAxisParam(
axis: number|number[], shape: number[]): number[] {
const rank = shape.length;
// Normalize input
axis = axis == null ? shape.map((s, i) => i) : [].concat(axis);
// Check for valid range
assert(
axis.every(ax => ax >= -rank && ax < rank),
() =>
`All values in axis param must be in range [-${rank}, ${rank}) but ` +
`got axis ${axis}`);
// Check for only integers
assert(
axis.every(ax => isInt(ax)),
() => `All values in axis param must be integers but ` +
`got axis ${axis}`);
// Handle negative axis.
return axis.map(a => a < 0 ? rank + a : a);
}
/** Reduces the shape by removing all dimensions of shape 1. */
export function squeezeShape(shape: number[], axis?: number[]):
{newShape: number[], keptDims: number[]} {
const newShape: number[] = [];
const keptDims: number[] = [];
const isEmptyArray = axis != null && Array.isArray(axis) && axis.length === 0;
const axes = (axis == null || isEmptyArray) ?
null :
parseAxisParam(axis, shape).sort();
let j = 0;
for (let i = 0; i < shape.length; ++i) {
if (axes != null) {
if (axes[j] === i && shape[i] !== 1) {
throw new Error(
`Can't squeeze axis ${i} since its dim '${shape[i]}' is not 1`);
}
if ((axes[j] == null || axes[j] > i) && shape[i] === 1) {
newShape.push(shape[i]);
keptDims.push(i);
}
if (axes[j] <= i) {
j++;
}
}
if (shape[i] !== 1) {
newShape.push(shape[i]);
keptDims.push(i);
}
}
return {newShape, keptDims};
}
export function getTypedArrayFromDType<D extends NumericDataType>(
dtype: D, size: number): DataTypeMap[D] {
return getArrayFromDType<D>(dtype, size);
}
export function getArrayFromDType<D extends DataType>(
dtype: D, size: number): DataTypeMap[D] {
let values = null;
if (dtype == null || dtype === 'float32') {
values = new Float32Array(size);
} else if (dtype === 'int32') {
values = new Int32Array(size);
} else if (dtype === 'bool') {
values = new Uint8Array(size);
} else if (dtype === 'string') {
values = new Array<string>(size);
} else {
throw new Error(`Unknown data type ${dtype}`);
}
return values as DataTypeMap[D];
}
export function checkConversionForErrors<D extends DataType>(
vals: DataTypeMap[D]|number[], dtype: D): void {
for (let i = 0; i < vals.length; i++) {
const num = vals[i] as number;
if (isNaN(num) || !isFinite(num)) {
throw Error(`A tensor of type ${dtype} being uploaded contains ${num}.`);
}
}
}
/** Returns true if the dtype is valid. */
export function isValidDtype(dtype: DataType): boolean {
return dtype === 'bool' || dtype === 'complex64' || dtype === 'float32' ||
dtype === 'int32' || dtype === 'string';
}
/**
* Returns true if the new type can't encode the old type without loss of
* precision.
*/
export function hasEncodingLoss(oldType: DataType, newType: DataType): boolean {
if (newType === 'complex64') {
return false;
}
if (newType === 'float32' && oldType !== 'complex64') {
return false;
}
if (newType === 'int32' && oldType !== 'float32' && oldType !== 'complex64') {
return false;
}
if (newType === 'bool' && oldType === 'bool') {
return false;
}
return true;
}
export function bytesPerElement(dtype: DataType): number {
if (dtype === 'float32' || dtype === 'int32') {
return 4;
} else if (dtype === 'complex64') {
return 8;
} else if (dtype === 'bool') {
return 1;
} else {
throw new Error(`Unknown dtype ${dtype}`);
}
}
/**
* Returns the approximate number of bytes allocated in the string array - 2
* bytes per character. Computing the exact bytes for a native string in JS
* is not possible since it depends on the encoding of the html page that
* serves the website.
*/
export function bytesFromStringArray(arr: Uint8Array[]): number {
if (arr == null) {
return 0;
}
let bytes = 0;
arr.forEach(x => bytes += x.length);
return bytes;
}
/** Returns true if the value is a string. */
export function isString(value: {}): value is string {
return typeof value === 'string' || value instanceof String;
}
export function isBoolean(value: {}): boolean {
return typeof value === 'boolean';
}
export function isNumber(value: {}): boolean {
return typeof value === 'number';
}
export function inferDtype(values: TensorLike|WebGLData|WebGPUData): DataType {
if (Array.isArray(values)) {
return inferDtype(values[0]);
}
if (values instanceof Float32Array) {
return 'float32';
} else if (
values instanceof Int32Array || values instanceof Uint8Array ||
values instanceof Uint8ClampedArray) {
return 'int32';
} else if (isNumber(values)) {
return 'float32';
} else if (isString(values)) {
return 'string';
} else if (isBoolean(values)) {
return 'bool';
}
return 'float32';
}
export function isFunction(f: Function) {
return !!(f && f.constructor && f.call && f.apply);
}
export function nearestDivisor(size: number, start: number): number {
for (let i = start; i < size; ++i) {
if (size % i === 0) {
return i;
}
}
return size;
}
export function computeStrides(shape: number[]): number[] {
const rank = shape.length;
if (rank < 2) {
return [];
}
// Last dimension has implicit stride of 1, thus having D-1 (instead of D)
// strides.
const strides = new Array(rank - 1);
strides[rank - 2] = shape[rank - 1];
for (let i = rank - 3; i >= 0; --i) {
strides[i] = strides[i + 1] * shape[i + 1];
}
return strides;
}
function createNestedArray(
offset: number, shape: number[], a: TypedArray, isComplex = false) {
const ret = new Array();
if (shape.length === 1) {
const d = shape[0] * (isComplex ? 2 : 1);
for (let i = 0; i < d; i++) {
ret[i] = a[offset + i];
}
} else {
const d = shape[0];
const rest = shape.slice(1);
const len = rest.reduce((acc, c) => acc * c) * (isComplex ? 2 : 1);
for (let i = 0; i < d; i++) {
ret[i] = createNestedArray(offset + i * len, rest, a, isComplex);
}
}
return ret;
}
// Provide a nested array of TypedArray in given shape.
export function toNestedArray(
shape: number[], a: TypedArray, isComplex = false) {
if (shape.length === 0) {
// Scalar type should return a single number.
return a[0];
}
const size = shape.reduce((acc, c) => acc * c) * (isComplex ? 2 : 1);
if (size === 0) {
// A tensor with shape zero should be turned into empty list.
return [];
}
if (size !== a.length) {
throw new Error(`[${shape}] does not match the input size ${a.length}${
isComplex ? ' for a complex tensor' : ''}.`);
}
return createNestedArray(0, shape, a, isComplex);
}
export function convertBackendValuesAndArrayBuffer(
data: BackendValues|ArrayBuffer, dtype: DataType) {
// If is type Uint8Array[], return it directly.
if (Array.isArray(data)) {
return data;
}
if (dtype === 'float32') {
return data instanceof Float32Array ? data : new Float32Array(data);
} else if (dtype === 'int32') {
return data instanceof Int32Array ? data : new Int32Array(data);
} else if (dtype === 'bool' || dtype === 'string') {
return Uint8Array.from(new Int32Array(data));
} else {
throw new Error(`Unknown dtype ${dtype}`);
}
}
export function makeOnesTypedArray<D extends DataType>(
size: number, dtype: D): DataTypeMap[D] {
const array = makeZerosTypedArray(size, dtype);
for (let i = 0; i < array.length; i++) {
array[i] = 1;
}
return array;
}
export function makeZerosTypedArray<D extends DataType>(
size: number, dtype: D): DataTypeMap[D] {
if (dtype == null || dtype === 'float32' || dtype === 'complex64') {
return new Float32Array(size) as DataTypeMap[D];
} else if (dtype === 'int32') {
return new Int32Array(size) as DataTypeMap[D];
} else if (dtype === 'bool') {
return new Uint8Array(size) as DataTypeMap[D];
} else {
throw new Error(`Unknown data type ${dtype}`);
}
}
/**
* Make nested `TypedArray` filled with zeros.
* @param shape The shape information for the nested array.
* @param dtype dtype of the array element.
*/
export function makeZerosNestedTypedArray<D extends DataType>(
shape: number[], dtype: D) {
const size = shape.reduce((prev, curr) => prev * curr, 1);
if (dtype == null || dtype === 'float32') {
return toNestedArray(shape, new Float32Array(size));
} else if (dtype === 'int32') {
return toNestedArray(shape, new Int32Array(size));
} else if (dtype === 'bool') {
return toNestedArray(shape, new Uint8Array(size));
} else {
throw new Error(`Unknown data type ${dtype}`);
}
}
export function assertNonNegativeIntegerDimensions(shape: number[]) {
shape.forEach(dimSize => {
assert(
Number.isInteger(dimSize) && dimSize >= 0,
() =>
`Tensor must have a shape comprised of positive integers but got ` +
`shape [${shape}].`);
});
}
/**
* Computes flat index for a given location (multidimentionsal index) in a
* Tensor/multidimensional array.
*
* @param locs Location in the tensor.
* @param rank Rank of the tensor.
* @param strides Tensor strides.
*/
export function locToIndex(
locs: number[], rank: number, strides: number[]): number {
if (rank === 0) {
return 0;
} else if (rank === 1) {
return locs[0];
}
let index = locs[locs.length - 1];
for (let i = 0; i < locs.length - 1; ++i) {
index += strides[i] * locs[i];
}
return index;
}
/**
* Computes the location (multidimensional index) in a
* tensor/multidimentional array for a given flat index.
*
* @param index Index in flat array.
* @param rank Rank of tensor.
* @param strides Strides of tensor.
*/
export function indexToLoc(
index: number, rank: number, strides: number[]): number[] {
if (rank === 0) {
return [];
} else if (rank === 1) {
return [index];
}
const locs: number[] = new Array(rank);
for (let i = 0; i < locs.length - 1; ++i) {
locs[i] = Math.floor(index / strides[i]);
index -= locs[i] * strides[i];
}
locs[locs.length - 1] = index;
return locs;
}
/**
* This method asserts whether an object is a Promise instance.
* @param object
*/
// tslint:disable-next-line: no-any
export function isPromise(object: any): object is Promise<unknown> {
// We chose to not use 'obj instanceOf Promise' for two reasons:
// 1. It only reliably works for es6 Promise, not other Promise
// implementations.
// 2. It doesn't work with framework that uses zone.js. zone.js monkey
// patch the async calls, so it is possible the obj (patched) is
// comparing to a pre-patched Promise.
return object && object.then && typeof object.then === 'function';
}