forked from tensorflow/tfjs-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.ts
322 lines (267 loc) · 9.24 KB
/
util_test.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
/**
* @license
* Copyright 2017 Google Inc. 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 * as tf from './index';
import {Tensor} from './tensor';
import {CPU_ENVS} from './test_util';
import {describeWithFlags} from './jasmine_util';
import {NamedTensorMap} from './types';
import * as util from './util';
describe('Util', () => {
it('Flatten arrays', () => {
expect(util.flatten([[1, 2, 3], [4, 5, 6]])).toEqual([1, 2, 3, 4, 5, 6]);
expect(util.flatten([[[1, 2], [3, 4], [5, 6], [7, 8]]])).toEqual([
1, 2, 3, 4, 5, 6, 7, 8
]);
expect(util.flatten([1, 2, 3, 4, 5, 6])).toEqual([1, 2, 3, 4, 5, 6]);
});
it('Correctly gets size from shape', () => {
expect(util.sizeFromShape([1, 2, 3, 4])).toEqual(24);
});
it('Correctly identifies scalars', () => {
expect(util.isScalarShape([])).toBe(true);
expect(util.isScalarShape([1, 2])).toBe(false);
expect(util.isScalarShape([1])).toBe(false);
});
it('Number arrays equal', () => {
expect(util.arraysEqual([1, 2, 3, 6], [1, 2, 3, 6])).toBe(true);
expect(util.arraysEqual([1, 2], [1, 2, 3])).toBe(false);
expect(util.arraysEqual([1, 2, 5], [1, 2])).toBe(false);
});
it('Is integer', () => {
expect(util.isInt(0.5)).toBe(false);
expect(util.isInt(1)).toBe(true);
});
it('Size to squarish shape (perfect square)', () => {
expect(util.sizeToSquarishShape(9)).toEqual([3, 3]);
});
it('Size to squarish shape (prime number)', () => {
expect(util.sizeToSquarishShape(11)).toEqual([1, 11]);
});
it('Size to squarish shape (almost square)', () => {
expect(util.sizeToSquarishShape(35)).toEqual([5, 7]);
});
it('Size of 1 to squarish shape', () => {
expect(util.sizeToSquarishShape(1)).toEqual([1, 1]);
});
it('infer shape single number', () => {
expect(util.inferShape(4)).toEqual([]);
});
it('infer shape 1d array', () => {
expect(util.inferShape([1, 2, 5])).toEqual([3]);
});
it('infer shape 2d array', () => {
expect(util.inferShape([[1, 2, 5], [5, 4, 1]])).toEqual([2, 3]);
});
it('infer shape 3d array', () => {
const a = [[[1, 2], [2, 3], [5, 6]], [[5, 6], [4, 5], [1, 2]]];
expect(util.inferShape(a)).toEqual([2, 3, 2]);
});
it('infer shape 4d array', () => {
const a = [
[[[1], [2]], [[2], [3]], [[5], [6]]],
[[[5], [6]], [[4], [5]], [[1], [2]]]
];
expect(util.inferShape(a)).toEqual([2, 3, 2, 1]);
});
it('infer shape of typed array', () => {
const a = new Float32Array([1, 2, 3, 4, 5]);
expect(util.inferShape(a)).toEqual([5]);
});
});
describe('util.repeatedTry', () => {
it('resolves', (doneFn) => {
let counter = 0;
const checkFn = () => {
counter++;
if (counter === 2) {
return true;
}
return false;
};
util.repeatedTry(checkFn).then(doneFn).catch(() => {
throw new Error('Rejected backoff.');
});
});
it('rejects', (doneFn) => {
const checkFn = () => false;
util.repeatedTry(checkFn, () => 0, 5)
.then(() => {
throw new Error('Backoff resolved');
})
.catch(doneFn);
});
});
describe('util.getQueryParams', () => {
it('basic', () => {
expect(util.getQueryParams('?a=1&b=hi&f=animal'))
.toEqual({'a': '1', 'b': 'hi', 'f': 'animal'});
});
});
describe('util.inferFromImplicitShape', () => {
it('empty shape', () => {
const result = util.inferFromImplicitShape([], 0);
expect(result).toEqual([]);
});
it('[2, 3, 4] -> [2, 3, 4]', () => {
const result = util.inferFromImplicitShape([2, 3, 4], 24);
expect(result).toEqual([2, 3, 4]);
});
it('[2, -1, 4] -> [2, 3, 4], size=24', () => {
const result = util.inferFromImplicitShape([2, -1, 4], 24);
expect(result).toEqual([2, 3, 4]);
});
it('[-1, 3, 4] -> [2, 3, 4], size=24', () => {
const result = util.inferFromImplicitShape([-1, 3, 4], 24);
expect(result).toEqual([2, 3, 4]);
});
it('[2, 3, -1] -> [2, 3, 4], size=24', () => {
const result = util.inferFromImplicitShape([2, 3, -1], 24);
expect(result).toEqual([2, 3, 4]);
});
it('[2, -1, -1] throws error', () => {
expect(() => util.inferFromImplicitShape([2, -1, -1], 24)).toThrowError();
});
it('[2, 3, -1] size=13 throws error', () => {
expect(() => util.inferFromImplicitShape([2, 3, -1], 13)).toThrowError();
});
it('[2, 3, 4] size=25 (should be 24) throws error', () => {
expect(() => util.inferFromImplicitShape([2, 3, 4], 25)).toThrowError();
});
});
describe('util.squeezeShape', () => {
it('scalar', () => {
const {newShape, keptDims} = util.squeezeShape([]);
expect(newShape).toEqual([]);
expect(keptDims).toEqual([]);
});
it('1x1 reduced to scalar', () => {
const {newShape, keptDims} = util.squeezeShape([1, 1]);
expect(newShape).toEqual([]);
expect(keptDims).toEqual([]);
});
it('1x3x1 reduced to [3]', () => {
const {newShape, keptDims} = util.squeezeShape([1, 3, 1]);
expect(newShape).toEqual([3]);
expect(keptDims).toEqual([1]);
});
it('1x1x4 reduced to [4]', () => {
const {newShape, keptDims} = util.squeezeShape([1, 1, 4]);
expect(newShape).toEqual([4]);
expect(keptDims).toEqual([2]);
});
it('2x3x4 not reduction', () => {
const {newShape, keptDims} = util.squeezeShape([2, 3, 4]);
expect(newShape).toEqual([2, 3, 4]);
expect(keptDims).toEqual([0, 1, 2]);
});
describe('with axis', () => {
it('should only reduce dimensions specified by axis', () => {
const {newShape, keptDims} = util.squeezeShape([1, 1, 1, 1, 4], [1, 2]);
expect(newShape).toEqual([1, 1, 4]);
expect(keptDims).toEqual([0, 3, 4]);
});
it('throws error when specified axis is not squeezable', () => {
expect(() => util.squeezeShape([1, 1, 2, 1, 4], [1, 2])).toThrowError();
});
});
});
describe('util.isTensorInList', () => {
it('not in list', () => {
const a = tf.scalar(1);
const list: Tensor[] = [tf.scalar(1), tf.tensor1d([1, 2, 3])];
expect(util.isTensorInList(a, list)).toBe(false);
});
it('in list', () => {
const a = tf.scalar(1);
const list: Tensor[] = [tf.scalar(2), tf.tensor1d([1, 2, 3]), a];
expect(util.isTensorInList(a, list)).toBe(true);
});
});
describe('util.checkForNaN', () => {
it('Float32Array has NaN', () => {
expect(
() => util.checkForNaN(
new Float32Array([1, 2, 3, NaN, 4, 255]), 'float32', ''))
.toThrowError();
});
it('Float32Array no NaN', () => {
// Int32 and Bool NaNs should not trigger an error.
expect(
() => util.checkForNaN(
new Float32Array([1, 2, 3, 4, -1, 255]), 'float32', ''))
.not.toThrowError();
});
});
describe('util.flattenNameArrayMap', () => {
it('basic', () => {
const a = tf.scalar(1);
const b = tf.scalar(3);
const c = tf.tensor1d([1, 2, 3]);
const map: NamedTensorMap = {a, b, c};
expect(util.flattenNameArrayMap(map, Object.keys(map))).toEqual([a, b, c]);
});
});
describe('util.unflattenToNameArrayMap', () => {
it('basic', () => {
const a = tf.scalar(1);
const b = tf.scalar(3);
const c = tf.tensor1d([1, 2, 3]);
expect(util.unflattenToNameArrayMap(['a', 'b', 'c'], [
a, b, c
])).toEqual({a, b, c});
});
});
describe('util.hasEncodingLoss', () => {
it('any to float32', () => {
expect(util.hasEncodingLoss('bool', 'float32')).toBe(false);
expect(util.hasEncodingLoss('int32', 'float32')).toBe(false);
expect(util.hasEncodingLoss('float32', 'float32')).toBe(false);
});
it('float32 to any', () => {
expect(util.hasEncodingLoss('float32', 'float32')).toBe(false);
expect(util.hasEncodingLoss('float32', 'int32')).toBe(true);
expect(util.hasEncodingLoss('float32', 'bool')).toBe(true);
});
it('int32 to lower', () => {
expect(util.hasEncodingLoss('int32', 'int32')).toBe(false);
expect(util.hasEncodingLoss('int32', 'bool')).toBe(true);
});
it('lower to int32', () => {
expect(util.hasEncodingLoss('bool', 'int32')).toBe(false);
});
it('bool to bool', () => {
expect(util.hasEncodingLoss('bool', 'bool')).toBe(false);
});
});
describeWithFlags('extractTensorsFromAny', CPU_ENVS, () => {
it('null input returns empty tensor', () => {
const results = util.extractTensorsFromAny(null);
expect(results).toEqual([]);
});
it('tensor input returns one element tensor', () => {
const x = tf.scalar(1);
const results = util.extractTensorsFromAny(x);
expect(results).toEqual([x]);
});
it('name tensor map returns flattened tensor', () => {
const x1 = tf.scalar(1);
const x2 = tf.scalar(3);
const x3 = tf.scalar(4);
const results = util.extractTensorsFromAny({x1, x2, x3});
expect(results).toEqual([x1, x2, x3]);
});
});