diff --git a/src/data/dataset_test.ts b/src/data/dataset_test.ts index 151ea60fd1..a59b5b6061 100644 --- a/src/data/dataset_test.ts +++ b/src/data/dataset_test.ts @@ -55,16 +55,15 @@ describe('Dataset', () => { test_util.expectArraysClose( new Float32Array([0, 0, 0, 1, .25, .75]), - normalizedInputs[0].getValues(), 1e-5); + normalizedInputs[0].getValues()); test_util.expectArraysClose( new Float32Array([1 / 3, 1 / 3, 1 / 3, 2 / 3, .75, .5]), - normalizedInputs[1].getValues(), 1e-5); + normalizedInputs[1].getValues()); test_util.expectArraysClose( new Float32Array([2 / 3, 2 / 3, 2 / 3, 1 / 3, 0, 0]), - normalizedInputs[2].getValues(), 1e-5); + normalizedInputs[2].getValues()); test_util.expectArraysClose( - new Float32Array([1, 1, 1, 0, 1, 1]), normalizedInputs[3].getValues(), - 1e-5); + new Float32Array([1, 1, 1, 0, 1, 1]), normalizedInputs[3].getValues()); dataset.normalizeWithinBounds(dataIndex, -1, 1); @@ -72,16 +71,15 @@ describe('Dataset', () => { test_util.expectArraysClose( new Float32Array([-1, -1, -1, 1, -.5, .5]), - normalizedInputs[0].getValues(), 1e-5); + normalizedInputs[0].getValues()); test_util.expectArraysClose( new Float32Array([-1 / 3, -1 / 3, -1 / 3, 1 / 3, .5, .0]), - normalizedInputs[1].getValues(), 1e-5); + normalizedInputs[1].getValues()); test_util.expectArraysClose( new Float32Array([1 / 3, 1 / 3, 1 / 3, -1 / 3, -1, -1]), - normalizedInputs[2].getValues(), 1e-5); + normalizedInputs[2].getValues()); test_util.expectArraysClose( - new Float32Array([1, 1, 1, -1, 1, 1]), normalizedInputs[3].getValues(), - 1e-5); + new Float32Array([1, 1, 1, -1, 1, 1]), normalizedInputs[3].getValues()); dataset.removeNormalization(dataIndex); @@ -89,15 +87,15 @@ describe('Dataset', () => { test_util.expectArraysClose( new Float32Array([1, 2, 10, -1, -2, .75]), - normalizedInputs[0].getValues(), 1e-5); + normalizedInputs[0].getValues()); test_util.expectArraysClose( new Float32Array([2, 3, 20, -2, 2, .5]), - normalizedInputs[1].getValues(), 1e-5); + normalizedInputs[1].getValues()); test_util.expectArraysClose( new Float32Array([3, 4, 30, -3, -4, 0]), - normalizedInputs[2].getValues(), 1e-5); + normalizedInputs[2].getValues()); test_util.expectArraysClose( - new Float32Array([4, 5, 40, -4, 4, 1]), normalizedInputs[3].getValues(), - 1e-5); + new Float32Array([4, 5, 40, -4, 4, 1]), + normalizedInputs[3].getValues()); }); }); diff --git a/src/graph/ops/max_pool_test.ts b/src/graph/ops/max_pool_test.ts index 86aed27134..2a3ac656eb 100644 --- a/src/graph/ops/max_pool_test.ts +++ b/src/graph/ops/max_pool_test.ts @@ -105,8 +105,7 @@ describe('Max pool', () => { const y = activations.get(yTensor); const expectedResult = Array3D.new([2, 2, 2], [6, 66, 8, 88, 14, 140, 16, 160]); - test_util.expectArraysClose( - y.getValues(), expectedResult.getValues(), 1e-6); + test_util.expectArraysClose(y.getValues(), expectedResult.getValues()); }); it('MaxPool depth = 2, with some negative numbers', () => { @@ -133,8 +132,7 @@ describe('Max pool', () => { const expectedResult = Array3D.new([2, 2, 2], [6, 55, 8, 88, 14, 140, 16, 150]); - test_util.expectArraysClose( - y.getValues(), expectedResult.getValues(), 1e-6); + test_util.expectArraysClose(y.getValues(), expectedResult.getValues()); }); it('MaxPool downsampling depth is preserved', () => { diff --git a/src/graph/session_test.ts b/src/graph/session_test.ts index c2b666e896..bcfe931835 100644 --- a/src/graph/session_test.ts +++ b/src/graph/session_test.ts @@ -19,14 +19,14 @@ import {InputProvider} from '../data/input_provider'; import {NDArrayMathCPU} from '../math/math_cpu'; import {NDArrayMathGPU} from '../math/math_gpu'; import {Array1D, NDArray, Scalar} from '../math/ndarray'; +import * as test_util from '../test_util'; import {Graph, Tensor} from './graph'; import {AdagradOptimizer} from './optimizers/adagrad_optimizer'; import {MomentumOptimizer} from './optimizers/momentum_optimizer'; import {RMSPropOptimizer} from './optimizers/rmsprop_optimizer'; -import {FeedDictionary, FeedEntry, Session} from './session'; import {SGDOptimizer} from './optimizers/sgd_optimizer'; -import * as test_util from '../test_util'; +import {FeedDictionary, FeedEntry, Session} from './session'; describe('FeedDictionary', () => { @@ -95,7 +95,7 @@ describe('Session', () => { const session = new Session(g, new NDArrayMathCPU()); const yVal = session.eval(y, [{tensor: x, data: Array1D.new([5, 4])}]); const expected = new Float32Array([28, 19]); - test_util.expectArraysClose(yVal.getValues(), expected, 1e-5); + test_util.expectArraysClose(yVal.getValues(), expected); }); it('y=x^2 + 3: GPU', () => { @@ -107,7 +107,7 @@ describe('Session', () => { math.scope(() => { const yVal = session.eval(y, [{tensor: x, data: Array1D.new([5, 4])}]); const expected = new Float32Array([28, 19]); - test_util.expectArraysClose(yVal.getValues(), expected, 1e-5); + test_util.expectArraysClose(yVal.getValues(), expected); }); }); @@ -122,7 +122,7 @@ describe('Session', () => { const yVal = session.eval(y, [{tensor: xSquared, data: Array1D.new([25, 16])}]); const expected = new Float32Array([28, 19]); - test_util.expectArraysClose(yVal.getValues(), expected, 1e-5); + test_util.expectArraysClose(yVal.getValues(), expected); }); }); @@ -139,8 +139,8 @@ describe('Session', () => { session.evalAll([y, z], [{tensor: x, data: Array1D.new([5, 4])}]); const expectedY = new Float32Array([28, 19]); const expectedZ = new Float32Array([27, 18]); - test_util.expectArraysClose(result[0].getValues(), expectedY, 1e-5); - test_util.expectArraysClose(result[1].getValues(), expectedZ, 1e-5); + test_util.expectArraysClose(result[0].getValues(), expectedY); + test_util.expectArraysClose(result[1].getValues(), expectedZ); }); }); @@ -155,11 +155,11 @@ describe('Session', () => { math.scope(() => { const result1 = session.eval(y, [{tensor: x, data: Array1D.new([5, 4])}]); const expectedY = new Float32Array([30, 20]); - test_util.expectArraysClose(result1.getValues(), expectedY, 1e-5); + test_util.expectArraysClose(result1.getValues(), expectedY); const result2 = session.eval(z, [{tensor: x, data: Array1D.new([5, 4])}]); const expectedZ = new Float32Array([31, 21]); - test_util.expectArraysClose(result2.getValues(), expectedZ, 1e-5); + test_util.expectArraysClose(result2.getValues(), expectedZ); }); }); @@ -214,7 +214,7 @@ describe('Session', () => { // dw/dx = [2*x_1 + 1, 2*x_2 + 1] session.train(w, [{tensor: x, data: inputProvider}], 1, optimizer); const dwdx = session.gradientArrayMap.get(x).getValues(); - test_util.expectArraysClose(dwdx, new Float32Array([5, 9]), 1e-5); + test_util.expectArraysClose(dwdx, new Float32Array([5, 9])); }); it('Specify which variables to update (var_list)', () => { @@ -248,8 +248,8 @@ describe('Session', () => { const b0After1 = session.activationArrayMap.get(b0).getValues(); const b1After1 = session.activationArrayMap.get(b1).getValues(); - test_util.expectArraysClose(b0After1, new Float32Array([-0.8, -1.6]), 1e-5); - test_util.expectArraysClose(b1After1, new Float32Array([0, 0]), 1e-5); + test_util.expectArraysClose(b0After1, new Float32Array([-0.8, -1.6])); + test_util.expectArraysClose(b1After1, new Float32Array([0, 0])); // Update both b0 and b1 const optimizerAll = new SGDOptimizer(0.1); @@ -283,7 +283,7 @@ describe('Session', () => { math.scope(() => { const yVal = session.eval(y, [{tensor: x, data: Array1D.new([5, 4])}]); const expected = new Float32Array([25, 16]); - test_util.expectArraysClose(yVal.getValues(), expected, 1e-5); + test_util.expectArraysClose(yVal.getValues(), expected); }); }); @@ -309,7 +309,7 @@ describe('Session', () => { // dw/dx = [2*x_1 + 1, 2*x_2 + 1] session.train(w, [{tensor: x, data: inputProvider}], 1, optimizer); const dwdx = session.gradientArrayMap.get(x).getValues(); - test_util.expectArraysClose(dwdx, new Float32Array([5, 9]), 1e-5); + test_util.expectArraysClose(dwdx, new Float32Array([5, 9])); }); }); @@ -337,13 +337,13 @@ describe('Session', () => { // w = [ w_old - lr*vel_w1, w_old - lr*vel_w2] = [-0.2, -0.4] session.train(y, [{tensor: x, data: inputProvider}], 1, optimizer); const dydw = session.activationArrayMap.get(w).getValues(); - test_util.expectArraysClose(dydw, new Float32Array([-.2, -0.4]), 1e-5); + test_util.expectArraysClose(dydw, new Float32Array([-.2, -0.4])); // velocity_w = [momentum* old_vel_w1 + x_1, // momentum* old_vel_w2 + x_2] = [3,6] // w = [ w_old - lr*vel_w1, w_old - lr*vel_w2] = [-0.5, -1.0] session.train(y, [{tensor: x, data: inputProvider}], 1, optimizer); const dydw2 = session.activationArrayMap.get(w).getValues(); - test_util.expectArraysClose(dydw2, new Float32Array([-.5, -1.0]), 2e-5); + test_util.expectArraysClose(dydw2, new Float32Array([-.5, -1.0])); }); }); @@ -374,7 +374,7 @@ describe('Session', () => { // = [-0.1, -0.1] session.train(y, [{tensor: x, data: inputProvider}], 1, optimizer); const dydw = session.activationArrayMap.get(w).getValues(); - test_util.expectArraysClose(dydw, new Float32Array([-.1, -0.1]), 1e-5); + test_util.expectArraysClose(dydw, new Float32Array([-.1, -0.1])); // cache = [old_cache_w1 + grad_w1**2, // old_cache_w2 + grad_w2**2] = [4,16] // w = [ w1_old - lr*grad_w1/sqrt(cahce_w2 + eps), @@ -382,8 +382,7 @@ describe('Session', () => { // = [-0.1707, -0.1707] session.train(y, [{tensor: x, data: inputProvider}], 1, optimizer); const dydw2 = session.activationArrayMap.get(w).getValues(); - test_util.expectArraysClose( - dydw2, new Float32Array([-.1707, -.1707]), 2e-5); + test_util.expectArraysClose(dydw2, new Float32Array([-.1707, -.1707])); }); }); @@ -413,8 +412,7 @@ describe('Session', () => { // = [-0.2236, -0.2236] session.train(y, [{tensor: x, data: inputProvider}], 1, optimizer); const dydw = session.activationArrayMap.get(w).getValues(); - test_util.expectArraysClose( - dydw, new Float32Array([-.2236, -0.2236]), 1e-5); + test_util.expectArraysClose(dydw, new Float32Array([-.2236, -0.2236])); // cache = [gamma*old_cache_w1 + (1-gamma)*grad_w1**2, // gamma*old_cache_w2 + (1-gamma)*grad_w2**2] // = [1.44, 5.76] @@ -423,8 +421,7 @@ describe('Session', () => { // = [-.39027, -.39027] session.train(y, [{tensor: x, data: inputProvider}], 1, optimizer); const dydw2 = session.activationArrayMap.get(w).getValues(); - test_util.expectArraysClose( - dydw2, new Float32Array([-.39027, -.39027]), 2e-5); + test_util.expectArraysClose(dydw2, new Float32Array([-.39027, -.39027])); }); }); diff --git a/src/math/math_cpu_test.ts b/src/math/math_cpu_test.ts index 898c33ceef..8d884ed3eb 100644 --- a/src/math/math_cpu_test.ts +++ b/src/math/math_cpu_test.ts @@ -84,7 +84,7 @@ describe('NDArrayMathCPU slice2D', () => { const aValues = a.getValues(); const expected = new Float32Array([aValues[0], aValues[1], aValues[10], aValues[11]]); - test_util.expectArraysClose(b.getValues(), expected, 0); + test_util.expectArraysClose(b.getValues(), expected); }); it('returns the rectangle specified', () => { @@ -863,9 +863,9 @@ describe('NDArrayMathCPU argmin/max, argmaxequals, min/max', () => { it('topk', () => { const topk = math.topK(Array1D.new([1, -1, 100, -5, -10.6, 3.3, 5]), 3); test_util.expectArraysClose( - topk.values.getValues(), new Float32Array([100, 5, 3.3]), 1e-6); + topk.values.getValues(), new Float32Array([100, 5, 3.3])); test_util.expectArraysClose( - topk.indices.getValues(), new Float32Array([2, 6, 5]), 1e-6); + topk.indices.getValues(), new Float32Array([2, 6, 5])); }); it('Arg min', () => { @@ -1560,7 +1560,7 @@ describe('NDArrayMathCPU resizeBilinear', () => { test_util.expectArraysClose( output.getValues(), - new Float32Array([2, 2, 2, 10 / 3, 10 / 3, 10 / 3, 4, 4, 4]), 1e-4); + new Float32Array([2, 2, 2, 10 / 3, 10 / 3, 10 / 3, 4, 4, 4])); }); it('simple alignCorners=true', () => { @@ -1568,8 +1568,7 @@ describe('NDArrayMathCPU resizeBilinear', () => { const output = math.resizeBilinear3D(input, [3, 3], true); test_util.expectArraysClose( - output.getValues(), new Float32Array([2, 2, 2, 3, 3, 3, 4, 4, 4]), - 1e-4); + output.getValues(), new Float32Array([2, 2, 2, 3, 3, 3, 4, 4, 4])); }); it('matches tensorflow w/ random numbers alignCorners=false', () => { @@ -1589,8 +1588,7 @@ describe('NDArrayMathCPU resizeBilinear', () => { 0.69152176, 0.44905344, 1.07186723, 0.03823943, 1.19864893, 0.6183514, 3.49600649, 1.50272655, 1.73724651, 1.68149579, 0.69152176, 0.44905344, 1.07186723, 0.03823943, 1.19864893 - ]), - 1e-4); + ])); }); it('matches tensorflow w/ random numbers alignCorners=true', () => { @@ -1610,8 +1608,7 @@ describe('NDArrayMathCPU resizeBilinear', () => { 1.70539713, 1.3923912, 1.68282723, 1.54382229, 1.66025746, 1.62451875, 1.83673346, 1.38198328, 1.92833281, 1.13944793, 2.01993227, 1.57932377, 2.34758639, 2.01919961, 2.67524052 - ]), - 1e-4); + ])); }); }); @@ -1640,8 +1637,7 @@ describe('NDArrayMathCPU batchNorm', () => { Math.sqrt(variance.get(0) + varianceEpsilon), (x.get(1, 0, 1) - mean.get(1)) * 1 / Math.sqrt(variance.get(1) + varianceEpsilon) - ]), - 1e-6); + ])); }); it('simple batchnorm, no offset, 2x1x2', () => { @@ -1664,8 +1660,7 @@ describe('NDArrayMathCPU batchNorm', () => { Math.sqrt(variance.get(0) + varianceEpsilon), (x.get(1, 0, 1) - mean.get(1)) * scale.get(1) / Math.sqrt(variance.get(1) + varianceEpsilon) - ]), - 1e-6); + ])); }); it('simple batchnorm, no scale, 2x1x2', () => { @@ -1693,8 +1688,7 @@ describe('NDArrayMathCPU batchNorm', () => { offset.get(1) + (x.get(1, 0, 1) - mean.get(1)) * 1 / Math.sqrt(variance.get(1) + varianceEpsilon) - ]), - 1e-6); + ])); }); it('simple batchnorm, 2x1x2', () => { @@ -1723,8 +1717,7 @@ describe('NDArrayMathCPU batchNorm', () => { offset.get(1) + (x.get(1, 0, 1) - mean.get(1)) * scale.get(1) / Math.sqrt(variance.get(1) + varianceEpsilon) - ]), - 1e-6); + ])); }); it('batchnorm matches tensorflow, 2x3x3', () => { @@ -1750,7 +1743,6 @@ describe('NDArrayMathCPU batchNorm', () => { 1.52106473, -0.07704776, 0.26144429, 1.28010017, -1.14422404, -1.15776136, 1.15425493, 1.82644104, -0.52249442, 1.04803919, 0.74932291, 0.40568101, 1.2844412 - ]), - 1e-5); + ])); }); }); diff --git a/src/math/math_gpu_test.ts b/src/math/math_gpu_test.ts index 2a3dd34180..0da3b2b45b 100644 --- a/src/math/math_gpu_test.ts +++ b/src/math/math_gpu_test.ts @@ -244,7 +244,7 @@ describe('NDArrayMathGPU slice2D', () => { const aValues = a.getValues(); const expected = new Float32Array([aValues[0], aValues[1], aValues[10], aValues[11]]); - test_util.expectArraysClose(b.getValues(), expected, 0); + test_util.expectArraysClose(b.getValues(), expected); a.dispose(); }); @@ -1172,7 +1172,7 @@ describe('NDArrayMathGPU unary ops', () => { for (let i = 0; i < a.size; i++) { expected[i] = 1 / (1 + Math.exp(-values[i])); } - test_util.expectArraysClose(result.getValues(), expected, 1e-6); + test_util.expectArraysClose(result.getValues(), expected); a.dispose(); }); @@ -1181,7 +1181,7 @@ describe('NDArrayMathGPU unary ops', () => { const a = Array1D.new([3, NaN]); const res = math.sigmoid(a).getValues(); test_util.expectArraysClose( - res, new Float32Array([1 / (1 + Math.exp(-3)), NaN]), 1e-5); + res, new Float32Array([1 / (1 + Math.exp(-3)), NaN])); a.dispose(); }); @@ -1193,7 +1193,7 @@ describe('NDArrayMathGPU unary ops', () => { for (let i = 0; i < a.size; i++) { expected[i] = Math.sin(values[i]); } - test_util.expectArraysClose(result.getValues(), expected, 1e-3); + test_util.expectArraysClose(result.getValues(), expected); a.dispose(); }); @@ -1202,7 +1202,7 @@ describe('NDArrayMathGPU unary ops', () => { const a = Array1D.new([4, NaN, 0]); const res = math.sin(a).getValues(); const expected = [Math.sin(4), NaN, Math.sin(0)]; - test_util.expectArraysClose(res, new Float32Array(expected), 1e-4); + test_util.expectArraysClose(res, new Float32Array(expected)); a.dispose(); }); @@ -1214,7 +1214,7 @@ describe('NDArrayMathGPU unary ops', () => { for (let i = 0; i < a.size; i++) { expected[i] = Math.cos(values[i]); } - test_util.expectArraysClose(result.getValues(), expected, 1e-3); + test_util.expectArraysClose(result.getValues(), expected); a.dispose(); }); @@ -1223,7 +1223,7 @@ describe('NDArrayMathGPU unary ops', () => { const a = Array1D.new([4, NaN, 0]); const res = math.cos(a).getValues(); const expected = [Math.cos(4), NaN, Math.cos(0)]; - test_util.expectArraysClose(res, new Float32Array(expected), 1e-4); + test_util.expectArraysClose(res, new Float32Array(expected)); a.dispose(); }); @@ -1244,7 +1244,7 @@ describe('NDArrayMathGPU unary ops', () => { const a = Array1D.new([4, NaN, 0]); const res = math.tan(a).getValues(); const expected = [Math.tan(4), NaN, Math.tan(0)]; - test_util.expectArraysClose(res, new Float32Array(expected), 1e-4); + test_util.expectArraysClose(res, new Float32Array(expected)); a.dispose(); }); @@ -1265,7 +1265,7 @@ describe('NDArrayMathGPU unary ops', () => { const a = Array1D.new([4, NaN, 0]); const res = math.asin(a).getValues(); const expected = [Math.asin(4), NaN, Math.asin(0)]; - test_util.expectArraysClose(res, new Float32Array(expected), 1e-4); + test_util.expectArraysClose(res, new Float32Array(expected)); a.dispose(); }); @@ -1286,7 +1286,7 @@ describe('NDArrayMathGPU unary ops', () => { const a = Array1D.new([4, NaN, 0]); const res = math.acos(a).getValues(); const expected = [Math.acos(4), NaN, Math.acos(0)]; - test_util.expectArraysClose(res, new Float32Array(expected), 1e-4); + test_util.expectArraysClose(res, new Float32Array(expected)); a.dispose(); }); @@ -1307,7 +1307,7 @@ describe('NDArrayMathGPU unary ops', () => { const a = Array1D.new([4, NaN, 0]); const res = math.atan(a).getValues(); const expected = [Math.atan(4), NaN, Math.atan(0)]; - test_util.expectArraysClose(res, new Float32Array(expected), 1e-4); + test_util.expectArraysClose(res, new Float32Array(expected)); a.dispose(); }); @@ -1328,7 +1328,7 @@ describe('NDArrayMathGPU unary ops', () => { const a = Array1D.new([4, NaN, 0]); const res = math.sinh(a).getValues(); const expected = [Math.sinh(4), NaN, Math.sinh(0)]; - test_util.expectArraysClose(res, new Float32Array(expected), 1e-5); + test_util.expectArraysClose(res, new Float32Array(expected)); a.dispose(); }); @@ -1349,7 +1349,7 @@ describe('NDArrayMathGPU unary ops', () => { const a = Array1D.new([4, NaN, 0]); const res = math.cosh(a).getValues(); const expected = [Math.cosh(4), NaN, Math.cosh(0)]; - test_util.expectArraysClose(res, new Float32Array(expected), 1e-5); + test_util.expectArraysClose(res, new Float32Array(expected)); a.dispose(); }); @@ -1361,7 +1361,7 @@ describe('NDArrayMathGPU unary ops', () => { for (let i = 0; i < a.size; i++) { expected[i] = util.tanh(values[i]); } - test_util.expectArraysClose(result.getValues(), expected, 1e-6); + test_util.expectArraysClose(result.getValues(), expected); a.dispose(); }); @@ -1370,7 +1370,7 @@ describe('NDArrayMathGPU unary ops', () => { const a = Array1D.new([4, NaN, 0]); const res = math.tanh(a).getValues(); const expected = [util.tanh(4), NaN, util.tanh(0)]; - test_util.expectArraysClose(res, new Float32Array(expected), 1e-5); + test_util.expectArraysClose(res, new Float32Array(expected)); a.dispose(); }); }); @@ -2313,7 +2313,7 @@ describe('NDArrayMathGPU resizeBilinear', () => { test_util.expectArraysClose( output.getValues(), - new Float32Array([2, 2, 2, 10 / 3, 10 / 3, 10 / 3, 4, 4, 4]), 1e-4); + new Float32Array([2, 2, 2, 10 / 3, 10 / 3, 10 / 3, 4, 4, 4])); input.dispose(); }); @@ -2322,8 +2322,7 @@ describe('NDArrayMathGPU resizeBilinear', () => { const output = math.resizeBilinear3D(input, [3, 3], true); test_util.expectArraysClose( - output.getValues(), new Float32Array([2, 2, 2, 3, 3, 3, 4, 4, 4]), - 1e-4); + output.getValues(), new Float32Array([2, 2, 2, 3, 3, 3, 4, 4, 4])); input.dispose(); }); @@ -2344,8 +2343,7 @@ describe('NDArrayMathGPU resizeBilinear', () => { 0.69152176, 0.44905344, 1.07186723, 0.03823943, 1.19864893, 0.6183514, 3.49600649, 1.50272655, 1.73724651, 1.68149579, 0.69152176, 0.44905344, 1.07186723, 0.03823943, 1.19864893 - ]), - 1e-4); + ])); input.dispose(); }); @@ -2366,8 +2364,7 @@ describe('NDArrayMathGPU resizeBilinear', () => { 1.70539713, 1.3923912, 1.68282723, 1.54382229, 1.66025746, 1.62451875, 1.83673346, 1.38198328, 1.92833281, 1.13944793, 2.01993227, 1.57932377, 2.34758639, 2.01919961, 2.67524052 - ]), - 1e-4); + ])); input.dispose(); }); @@ -2404,8 +2401,7 @@ describe('NDArrayMathGPU batchNorm', () => { Math.sqrt(variance.get(0) + varianceEpsilon), (x.get(1, 0, 1) - mean.get(1)) * 1 / Math.sqrt(variance.get(1) + varianceEpsilon) - ]), - 1e-4); + ])); x.dispose(); mean.dispose(); variance.dispose(); @@ -2431,8 +2427,7 @@ describe('NDArrayMathGPU batchNorm', () => { Math.sqrt(variance.get(0) + varianceEpsilon), (x.get(1, 0, 1) - mean.get(1)) * scale.get(1) / Math.sqrt(variance.get(1) + varianceEpsilon) - ]), - 1e-4); + ])); x.dispose(); mean.dispose(); variance.dispose(); @@ -2464,8 +2459,7 @@ describe('NDArrayMathGPU batchNorm', () => { offset.get(1) + (x.get(1, 0, 1) - mean.get(1)) * 1 / Math.sqrt(variance.get(1) + varianceEpsilon) - ]), - 1e-4); + ])); x.dispose(); mean.dispose(); variance.dispose(); @@ -2498,8 +2492,7 @@ describe('NDArrayMathGPU batchNorm', () => { offset.get(1) + (x.get(1, 0, 1) - mean.get(1)) * scale.get(1) / Math.sqrt(variance.get(1) + varianceEpsilon) - ]), - 1e-4); + ])); x.dispose(); mean.dispose(); variance.dispose(); @@ -2530,8 +2523,7 @@ describe('NDArrayMathGPU batchNorm', () => { 1.52106473, -0.07704776, 0.26144429, 1.28010017, -1.14422404, -1.15776136, 1.15425493, 1.82644104, -0.52249442, 1.04803919, 0.74932291, 0.40568101, 1.2844412 - ]), - 1e-4); + ])); x.dispose(); mean.dispose(); variance.dispose(); @@ -2668,14 +2660,12 @@ describe('LSTMCell', () => { const output = math.multiRNNCell([lstm1, lstm2], onehot, c, h); test_util.expectArraysClose( - output[0][0].getValues(), new Float32Array([-0.7440074682235718]), - 1e-4); + output[0][0].getValues(), new Float32Array([-0.7440074682235718])); test_util.expectArraysClose( - output[0][1].getValues(), new Float32Array([0.7460772395133972]), 1e-4); + output[0][1].getValues(), new Float32Array([0.7460772395133972])); test_util.expectArraysClose( - output[1][0].getValues(), new Float32Array([-0.5802832245826721]), - 1e-4); + output[1][0].getValues(), new Float32Array([-0.5802832245826721])); test_util.expectArraysClose( - output[1][1].getValues(), new Float32Array([0.5745711922645569]), 1e-4); + output[1][1].getValues(), new Float32Array([0.5745711922645569])); }); }); diff --git a/src/math/webgl/abs_gpu_test.ts b/src/math/webgl/abs_gpu_test.ts index a6abf99af3..164012eaab 100644 --- a/src/math/webgl/abs_gpu_test.ts +++ b/src/math/webgl/abs_gpu_test.ts @@ -35,7 +35,7 @@ describe('abs_gpu', () => { for (let i = 0; i < a.length; ++i) { expected[i] = Math.abs(a[i]); } - test_util.expectArraysClose(result, expected, 0.0001); + test_util.expectArraysClose(result, expected); }); }); diff --git a/src/math/webgl/addscaledmat_gpu_test.ts b/src/math/webgl/addscaledmat_gpu_test.ts index 95c8de0127..743249ac19 100644 --- a/src/math/webgl/addscaledmat_gpu_test.ts +++ b/src/math/webgl/addscaledmat_gpu_test.ts @@ -46,7 +46,7 @@ describe('addscaledmat_gpu', () => { const b = Array1D.new([0.1, 0.2, 0.3, 0.4, 0.5, 0.6]); const result = uploadAddScaledMatDownload(a, b, 1, 1); test_util.expectArraysClose( - result, new Float32Array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6]), 0.0001); + result, new Float32Array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6])); }); it('returns A * aScalar when B and bScalar are 0', () => { @@ -54,7 +54,7 @@ describe('addscaledmat_gpu', () => { const b = Array1D.zerosLike(a); const result = uploadAddScaledMatDownload(a, b, 1.1, 0); test_util.expectArraysClose( - result, new Float32Array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6]), 0.0001); + result, new Float32Array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6])); }); it('returns B * bScalar when A and aScalar are 0', () => { @@ -62,7 +62,7 @@ describe('addscaledmat_gpu', () => { const a = Array1D.zerosLike(b); const result = uploadAddScaledMatDownload(a, b, 0, 1.1); test_util.expectArraysClose( - result, new Float32Array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6]), 0.0001); + result, new Float32Array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6])); }); it('returns (A * aScalar) + (B * bScalar)', () => { @@ -75,7 +75,7 @@ describe('addscaledmat_gpu', () => { const c2 = 0.25; const result = uploadAddScaledMatDownload(a, b, c1, c2); test_util.expectArraysClose( - result, cpuAddScaledMatrices(aVals, c1, bVals, c2), 0.001); + result, cpuAddScaledMatrices(aVals, c1, bVals, c2)); }); }); diff --git a/src/math/webgl/avg_pool_gpu_test.ts b/src/math/webgl/avg_pool_gpu_test.ts index a03fc148bd..92e8c85d82 100644 --- a/src/math/webgl/avg_pool_gpu_test.ts +++ b/src/math/webgl/avg_pool_gpu_test.ts @@ -39,7 +39,7 @@ describe('avg_pool_gpu', () => { const yGPU = uploadAvgPoolDownload(x.getValues(), x.shape, fSize, stride, pad); - test_util.expectArraysClose(yGPU, yCPU.getValues(), 1e-5); + test_util.expectArraysClose(yGPU, yCPU.getValues()); } it('matches CPU on random input, d1=1,d2=1,f=2,s=1,p=0', () => { diff --git a/src/math/webgl/batchnorm_gpu_test.ts b/src/math/webgl/batchnorm_gpu_test.ts index 2eb6366913..a535c7d985 100644 --- a/src/math/webgl/batchnorm_gpu_test.ts +++ b/src/math/webgl/batchnorm_gpu_test.ts @@ -41,7 +41,7 @@ describe('batchnorm gpu test', () => { (x[2] - mean[0]) * 1 / Math.sqrt(variance[0] + varianceEpsilon), (x[3] - mean[1]) * 1 / Math.sqrt(variance[1] + varianceEpsilon) ]); - test_util.expectArraysClose(result, expectedResult, 1e-5); + test_util.expectArraysClose(result, expectedResult); }); it('simple batchnorm, no offset, 2x1x2', () => { @@ -61,7 +61,7 @@ describe('batchnorm gpu test', () => { (x[2] - mean[0]) * scale[0] / Math.sqrt(variance[0] + varianceEpsilon), (x[3] - mean[1]) * scale[1] / Math.sqrt(variance[1] + varianceEpsilon) ]); - test_util.expectArraysClose(result, expectedResult, 1e-5); + test_util.expectArraysClose(result, expectedResult); }); it('simple batchnorm, no scale, 2x1x2', () => { @@ -85,7 +85,7 @@ describe('batchnorm gpu test', () => { offset[1] + (x[3] - mean[1]) * 1 / Math.sqrt(variance[1] + varianceEpsilon) ]); - test_util.expectArraysClose(result, expectedResult, 1e-5); + test_util.expectArraysClose(result, expectedResult); }); it('simple batchnorm, 2x1x2', () => { @@ -113,7 +113,7 @@ describe('batchnorm gpu test', () => { offset[1] + (x[3] - mean[1]) * scale[1] / Math.sqrt(variance[1] + varianceEpsilon) ]); - test_util.expectArraysClose(result, expectedResult, 1e-5); + test_util.expectArraysClose(result, expectedResult); }); it('batchnorm matches tensorflow, 2x3x3', () => { @@ -137,7 +137,7 @@ describe('batchnorm gpu test', () => { -0.07704776, 0.26144429, 1.28010017, -1.14422404, -1.15776136, 1.15425493, 1.82644104, -0.52249442, 1.04803919, 0.74932291, 0.40568101, 1.2844412 ]); - test_util.expectArraysClose(result, expectedResult, 1e-5); + test_util.expectArraysClose(result, expectedResult); }); }); diff --git a/src/math/webgl/binaryop_gpu_test.ts b/src/math/webgl/binaryop_gpu_test.ts index e91be95e83..320f9c6183 100644 --- a/src/math/webgl/binaryop_gpu_test.ts +++ b/src/math/webgl/binaryop_gpu_test.ts @@ -37,15 +37,14 @@ describe('binaryop_gpu Add', () => { const c = Scalar.new(0); const a = Array1D.new([1, 2, 3]); const result = uploadBinaryOpDownload(c, a, binaryop_gpu.ADD); - test_util.expectArraysClose(result, new Float32Array([1, 2, 3]), 0); + test_util.expectArraysClose(result, new Float32Array([1, 2, 3])); }); it('adds the scalar to every element in the matrix', () => { const a = Array1D.new([1, 2, 3, 4]); const c = Scalar.new(0.5); const result = uploadBinaryOpDownload(c, a, binaryop_gpu.ADD); - test_util.expectArraysClose( - result, new Float32Array([1.5, 2.5, 3.5, 4.5]), 0.0001); + test_util.expectArraysClose(result, new Float32Array([1.5, 2.5, 3.5, 4.5])); }); }); @@ -61,23 +60,21 @@ describe('binaryop_gpu Sub', () => { const a = Array1D.new([1, 2, 3]); const c = Scalar.new(0); const result = uploadBinaryOpDownload(a, c, binaryop_gpu.SUB); - test_util.expectArraysClose(result, new Float32Array([1, 2, 3]), 0); + test_util.expectArraysClose(result, new Float32Array([1, 2, 3])); }); it('subtracts the scalar from every element in the matrix', () => { const a = Array1D.new([1, 2, 3, 4]); const c = Scalar.new(0.5); const result = uploadBinaryOpDownload(a, c, binaryop_gpu.SUB); - test_util.expectArraysClose( - result, new Float32Array([0.5, 1.5, 2.5, 3.5]), 0.0001); + test_util.expectArraysClose(result, new Float32Array([0.5, 1.5, 2.5, 3.5])); }); it('2D - 1D broadcasting', () => { const a = Array2D.new([3, 2], [[1, 2], [3, 4], [5, 6]]); const b = Array1D.new([1, 3]); const result = uploadBinaryOpDownload(a, b, binaryop_gpu.SUB); - test_util.expectArraysClose( - result, new Float32Array([0, -1, 2, 1, 4, 3]), 1e-4); + test_util.expectArraysClose(result, new Float32Array([0, -1, 2, 1, 4, 3])); }); it('2D - 1D invalid shapes for broadcasting', () => { @@ -94,7 +91,7 @@ describe('binaryop_gpu Sub', () => { // shape [3, 2] is not compatible with shape [3]. const res = uploadBinaryOpDownload(a, b, binaryop_gpu.SUB); test_util.expectArraysClose( - res, new Float32Array([0, 0, 0, -1, 4, 4, 4, 3]), 1e-4); + res, new Float32Array([0, 0, 0, -1, 4, 4, 4, 3])); }); }); @@ -119,14 +116,14 @@ describe('binaryop_gpu Mul', () => { const a = Array1D.new([1, 2, 3]); const c = Scalar.new(0); const result = uploadBinaryOpDownload(c, a, binaryop_gpu.MUL); - test_util.expectArraysClose(result, new Float32Array([0, 0, 0]), 0); + test_util.expectArraysClose(result, new Float32Array([0, 0, 0])); }); it('triples the matrix when the scalar is 3', () => { const a = Array1D.new([1, 2, 3]); const c = Scalar.new(3); const result = uploadBinaryOpDownload(c, a, binaryop_gpu.MUL); - test_util.expectArraysClose(result, new Float32Array([3, 6, 9]), 0); + test_util.expectArraysClose(result, new Float32Array([3, 6, 9])); }); it('sets all result entries to 0 if A is 0', () => { @@ -153,7 +150,7 @@ describe('binaryop_gpu Mul', () => { const b = Array1D.zeros([16]); b.fill(1.0); const result = uploadBinaryOpDownload(a, b, binaryop_gpu.MUL); - test_util.expectArraysClose(result, expected, 0.0001); + test_util.expectArraysClose(result, expected); }); it('writes the element-wise product of A and B to result', () => { @@ -161,7 +158,7 @@ describe('binaryop_gpu Mul', () => { const b = Array1D.new(test_util.randomArrayInRange(64, -10, 10)); const expected = cpuMultiply(a.getValues(), b.getValues()); const result = uploadBinaryOpDownload(a, b, binaryop_gpu.MUL); - test_util.expectArraysClose(result, expected, 0.0001); + test_util.expectArraysClose(result, expected); }); }); diff --git a/src/math/webgl/concat_gpu_test.ts b/src/math/webgl/concat_gpu_test.ts index d1c647bf93..e0a7e17f9e 100644 --- a/src/math/webgl/concat_gpu_test.ts +++ b/src/math/webgl/concat_gpu_test.ts @@ -234,8 +234,7 @@ describe('concat3d_gpu', () => { test_util.expectArraysClose( result, new Float32Array([ 1, 11, 111, 2, 22, 222, 5, 55, 555, 6, 66, 666, 7, 77, 777, 8, 88, 888 - ]), - 1e-6); + ])); }); @@ -248,8 +247,7 @@ describe('concat3d_gpu', () => { test_util.expectArraysClose( result, new Float32Array([ 1, 11, 111, 5, 55, 555, 6, 66, 666, 3, 33, 333, 7, 77, 777, 8, 88, 888 - ]), - 1e-6); + ])); }); it('concat axis=2', () => { @@ -258,12 +256,10 @@ describe('concat3d_gpu', () => { new Float32Array([5, 55, 555, 6, 66, 666, 7, 77, 777, 8, 88, 888]); const result = uploadConcat3dDownload(x1, x2, [2, 2, 2], [2, 2, 3], 2); - test_util.expectArraysClose( - result, new Float32Array([ - 1, 11, 5, 55, 555, 2, 22, 6, 66, 666, - 3, 33, 7, 77, 777, 4, 44, 8, 88, 888 - ]), - 1e-6); + test_util.expectArraysClose(result, new Float32Array([ + 1, 11, 5, 55, 555, 2, 22, 6, 66, 666, + 3, 33, 7, 77, 777, 4, 44, 8, 88, 888 + ])); }); }); @@ -303,8 +299,7 @@ describe('concat4d_gpu', () => { test_util.expectArraysClose( result, new Float32Array([ 1, 11, 111, 2, 22, 222, 5, 55, 555, 6, 66, 666, 7, 77, 777, 8, 88, 888 - ]), - 1e-6); + ])); }); @@ -317,8 +312,7 @@ describe('concat4d_gpu', () => { test_util.expectArraysClose( result, new Float32Array([ 1, 11, 111, 5, 55, 555, 6, 66, 666, 3, 33, 333, 7, 77, 777, 8, 88, 888 - ]), - 1e-6); + ])); }); it('concat axis=2', () => { @@ -327,12 +321,10 @@ describe('concat4d_gpu', () => { [2, 2, 3, 1], [5, 55, 555, 6, 66, 666, 7, 77, 777, 8, 88, 888]); const result = doConcat4D(x1, x2, 2); - test_util.expectArraysClose( - result, new Float32Array([ - 1, 11, 5, 55, 555, 2, 22, 6, 66, 666, - 3, 33, 7, 77, 777, 4, 44, 8, 88, 888 - ]), - 1e-6); + test_util.expectArraysClose(result, new Float32Array([ + 1, 11, 5, 55, 555, 2, 22, 6, 66, 666, + 3, 33, 7, 77, 777, 4, 44, 8, 88, 888 + ])); }); function doConcat4D(a: Array4D, b: Array4D, axis: number): Float32Array { diff --git a/src/math/webgl/conv_backprop_gpu_derbias_test.ts b/src/math/webgl/conv_backprop_gpu_derbias_test.ts index 25a001e666..31d29eba05 100644 --- a/src/math/webgl/conv_backprop_gpu_derbias_test.ts +++ b/src/math/webgl/conv_backprop_gpu_derbias_test.ts @@ -51,7 +51,7 @@ describe('conv_gpu derBias', () => { const dBiasCPU = mathCPU.conv2dDerBias(dy); const dBiasGPU = uploadDerBiasDownload(dy); - test_util.expectArraysClose(dBiasGPU, dBiasCPU.getValues(), 1e-5); + test_util.expectArraysClose(dBiasGPU, dBiasCPU.getValues()); } it('matches CPU on random input. dy shape [3, 3, 2]', () => { diff --git a/src/math/webgl/conv_backprop_gpu_derweights_test.ts b/src/math/webgl/conv_backprop_gpu_derweights_test.ts index 3969983dea..f3433c1600 100644 --- a/src/math/webgl/conv_backprop_gpu_derweights_test.ts +++ b/src/math/webgl/conv_backprop_gpu_derweights_test.ts @@ -66,7 +66,7 @@ describe('conv_gpu derWeights', () => { x, dy, [fSize, fSize, inDepth, outDepth], stride, zeroPad); const dwGPU = uploadDerWeightsDownload(x, dy, fSize, stride, zeroPad); - test_util.expectArraysClose(dwGPU, dwCPU.getValues(), 1e-5); + test_util.expectArraysClose(dwGPU, dwCPU.getValues()); } it('matches CPU on random input, d1=3,d2=4,f=2,s=1,p=0', () => { diff --git a/src/math/webgl/conv_backprop_transpose_gpu_test.ts b/src/math/webgl/conv_backprop_transpose_gpu_test.ts index a6fe372b29..aca50270f6 100644 --- a/src/math/webgl/conv_backprop_transpose_gpu_test.ts +++ b/src/math/webgl/conv_backprop_transpose_gpu_test.ts @@ -72,7 +72,7 @@ describe('conv_gpu transpose', () => { x, weights, origInputShape, origStride, origPad); const yGPU = uploadConvTransposeDownload( x, weights, origInputShape, fSize, origStride, origPad); - test_util.expectArraysClose(yGPU, yCPU.getValues(), 1e-5); + test_util.expectArraysClose(yGPU, yCPU.getValues()); } it('matches CPU on random input, d1=1,d2=1,f=2,s=1,p=0', () => { diff --git a/src/math/webgl/conv_gpu_test.ts b/src/math/webgl/conv_gpu_test.ts index 3feaac18da..4ef90d14c7 100644 --- a/src/math/webgl/conv_gpu_test.ts +++ b/src/math/webgl/conv_gpu_test.ts @@ -79,7 +79,7 @@ describe('conv_gpu', () => { x.getValues(), xShape, weights.getValues(), biases.getValues(), resultDepth, fSize, stride, pad); - test_util.expectArraysClose(yGPU, yCPU.getValues(), 1e-5); + test_util.expectArraysClose(yGPU, yCPU.getValues()); } it('1x1x1 in, 1d out, 1x1 filter, 1 stride: [0] => [0]', () => { @@ -347,8 +347,7 @@ describe('conv_gpu', () => { test_util.expectArraysClose( result, new Float32Array( - [7, -8, 8, -2, 7, -2, 5, 5, 4, 6, 1, 2, -1, 3, 7, -2, 1, 4]), - 0.00001); + [7, -8, 8, -2, 7, -2, 5, 5, 4, 6, 1, 2, -1, 3, 7, -2, 1, 4])); }); it('matches CPU on random input, d1=1,d2=1,f=2,s=1,p=0', () => { diff --git a/src/math/webgl/copy_gpu_test.ts b/src/math/webgl/copy_gpu_test.ts index 7187a83c8e..115c376450 100644 --- a/src/math/webgl/copy_gpu_test.ts +++ b/src/math/webgl/copy_gpu_test.ts @@ -140,7 +140,7 @@ describe('copy_gpu', () => { const result = uploadCopyDownload( source, [10, 10], [0, 3], [10, 1], [0, 0], [1, 10], dest, [1, 10]); test_util.expectArraysClose( - result, new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 0); + result, new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])); }); it('doesn\'t touch destination pixels outside of the source box', () => { @@ -184,6 +184,6 @@ describe('copy_gpu', () => { gpgpu.deleteProgram(binary.webGLProgram); gpgpu.dispose(); - test_util.expectArraysClose(res, sourceVals, 0); + test_util.expectArraysClose(res, sourceVals); }); }); diff --git a/src/math/webgl/exp_gpu_test.ts b/src/math/webgl/exp_gpu_test.ts index d66cfd16dc..1bcc87ed97 100644 --- a/src/math/webgl/exp_gpu_test.ts +++ b/src/math/webgl/exp_gpu_test.ts @@ -39,7 +39,7 @@ describe('exp_gpu', () => { const result = uploadExpDownload(a, 1, a.length); const expected = new Float32Array(a.length); expected.fill(Math.E); - test_util.expectArraysClose(result, expected, 0.0001); + test_util.expectArraysClose(result, expected); }); it('calculates f(x)=e^x for every value in the matrix', () => { @@ -49,7 +49,7 @@ describe('exp_gpu', () => { for (let i = 0; i < a.length; ++i) { expected[i] = Math.exp(a[i]); } - test_util.expectArraysClose(result, expected, 0.0001); + test_util.expectArraysClose(result, expected); }); }); diff --git a/src/math/webgl/gpgpu_context_test.ts b/src/math/webgl/gpgpu_context_test.ts index ea9004bbc5..a6b42f1763 100644 --- a/src/math/webgl/gpgpu_context_test.ts +++ b/src/math/webgl/gpgpu_context_test.ts @@ -377,7 +377,7 @@ describe('GPGPUContext setOutputMatrixWriteRegion', () => { const result = gpgpu.downloadMatrixFromTexture(output, 4, 4); const expected = new Float32Array(4 * 4); expected.fill(2); - test_util.expectArraysClose(result, expected, 0); + test_util.expectArraysClose(result, expected); }); it('sets the scissor box to the requested parameters', () => { @@ -395,7 +395,7 @@ describe('GPGPUContext setOutputMatrixWriteRegion', () => { const result = gpgpu.downloadMatrixFromTexture(output, 4, 4); const expected = new Float32Array([0, 0, 0, 0, 0, 2, 2, 0, 0, 2, 2, 0, 0, 0, 0, 0]); - test_util.expectArraysClose(result, expected, 0); + test_util.expectArraysClose(result, expected); }); it('preserves data from previous writes outside of write region', () => { @@ -406,7 +406,7 @@ describe('GPGPUContext setOutputMatrixWriteRegion', () => { const result = gpgpu.downloadMatrixFromTexture(output, 4, 4); const expected = new Float32Array([2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2]); - test_util.expectArraysClose(result, expected, 0); + test_util.expectArraysClose(result, expected); }); it('writes adjacent cells across multiple calls', () => { @@ -419,7 +419,7 @@ describe('GPGPUContext setOutputMatrixWriteRegion', () => { const result = gpgpu.downloadMatrixFromTexture(output, 4, 4); const expected = new Float32Array(4 * 4); expected.fill(2); - test_util.expectArraysClose(result, expected, 0); + test_util.expectArraysClose(result, expected); }); }); diff --git a/src/math/webgl/log_gpu_test.ts b/src/math/webgl/log_gpu_test.ts index 68448aaf38..88c8822f75 100644 --- a/src/math/webgl/log_gpu_test.ts +++ b/src/math/webgl/log_gpu_test.ts @@ -40,7 +40,7 @@ describe('log_gpu', () => { const result = uploadLogDownload(a, 1, a.length); const expected = new Float32Array(a.length); expected.fill(1.0); - test_util.expectArraysClose(result, expected, 0.0001); + test_util.expectArraysClose(result, expected); }); it('calculates f(x)=ln x for every value in the matrix', () => { @@ -50,7 +50,7 @@ describe('log_gpu', () => { for (let i = 0; i < a.length; ++i) { expected[i] = Math.log(a[i]); } - test_util.expectArraysClose(result, expected, 0.0001); + test_util.expectArraysClose(result, expected); }); }); diff --git a/src/math/webgl/max_pool_backprop_gpu_test.ts b/src/math/webgl/max_pool_backprop_gpu_test.ts index ac2212dc24..34d075708b 100644 --- a/src/math/webgl/max_pool_backprop_gpu_test.ts +++ b/src/math/webgl/max_pool_backprop_gpu_test.ts @@ -68,7 +68,7 @@ describe('max_pool_backprop_gpu', () => { const dxCPU = mathCPU.maxPoolBackprop(dy, x, fSize, origStride, origPad); const dxGPU = uploadMaxPoolBackpropDownload(dy, x, fSize, origStride, origPad); - test_util.expectArraysClose(dxGPU, dxCPU.getValues(), 1e-5); + test_util.expectArraysClose(dxGPU, dxCPU.getValues()); } it('matches CPU on random input, d1=1,d2=1,f=2,s=1,p=0', () => { diff --git a/src/math/webgl/max_pool_gpu_test.ts b/src/math/webgl/max_pool_gpu_test.ts index f410212277..afa9408ab8 100644 --- a/src/math/webgl/max_pool_gpu_test.ts +++ b/src/math/webgl/max_pool_gpu_test.ts @@ -39,7 +39,7 @@ describe('max_pool_gpu', () => { const yGPU = uploadMaxPoolDownload(x.getValues(), x.shape, fSize, stride, pad); - test_util.expectArraysClose(yGPU, yCPU.getValues(), 1e-5); + test_util.expectArraysClose(yGPU, yCPU.getValues()); } it('matches CPU on random input, d1=1,d2=1,f=2,s=1,p=0', () => { diff --git a/src/math/webgl/max_pool_positions_gpu_test.ts b/src/math/webgl/max_pool_positions_gpu_test.ts index 1f80ff16e2..2e5f2ac107 100644 --- a/src/math/webgl/max_pool_positions_gpu_test.ts +++ b/src/math/webgl/max_pool_positions_gpu_test.ts @@ -62,7 +62,7 @@ describe('max_pool_position', () => { const yCPU = mathCPU.maxPoolPositions(x, convInfo); const yGPU = uploadMaxPoolPositionDownload( x.getValues(), x.shape, fSize, stride, pad); - test_util.expectArraysClose(yGPU, yCPU.getValues(), 1e-5); + test_util.expectArraysClose(yGPU, yCPU.getValues()); } it('matches CPU on random input, d1=1,d2=1,f=2,s=1,p=0', () => { diff --git a/src/math/webgl/min_pool_gpu_test.ts b/src/math/webgl/min_pool_gpu_test.ts index 452b7046cf..e163866f34 100644 --- a/src/math/webgl/min_pool_gpu_test.ts +++ b/src/math/webgl/min_pool_gpu_test.ts @@ -38,7 +38,7 @@ describe('min_pool_gpu', () => { const yGPU = uploadMinPoolDownload(x.getValues(), x.shape, fSize, stride, pad); - test_util.expectArraysClose(yGPU, yCPU.getValues(), 1e-5); + test_util.expectArraysClose(yGPU, yCPU.getValues()); } it('matches CPU on random input, d1=1,d2=1,f=2,s=1,p=0', () => { diff --git a/src/math/webgl/mulmat_gpu_test.ts b/src/math/webgl/mulmat_gpu_test.ts index 9cd51cfd84..a7dcc58a4d 100644 --- a/src/math/webgl/mulmat_gpu_test.ts +++ b/src/math/webgl/mulmat_gpu_test.ts @@ -220,7 +220,7 @@ describe('mulmat_gpu (different shapes)', () => { const b = new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); const result = uploadMultiplyMatrixDownload(a, 2, 4, b, 4, 3); const expected = test_util.cpuMultiplyMatrix(a, 2, 4, b, 4, 3); - test_util.expectArraysClose(result, expected, 0.00001); + test_util.expectArraysClose(result, expected); }); }); @@ -237,7 +237,7 @@ describe('mulmat_gpu (large matrices)', () => { const b = test_util.randomArrayInRange(128 * 128, -1, 1); const result = uploadMultiplyMatrixDownload(a, 128, 128, b, 128, 128); const expected = test_util.cpuMultiplyMatrix(a, 128, 128, b, 128, 128); - test_util.expectArraysClose(result, expected, 0.001); + test_util.expectArraysClose(result, expected); }); }); @@ -288,7 +288,7 @@ describe('mulmat_gpu (multiple matrices)', () => { const expected = test_util.cpuMultiplyMatrix( test_util.cpuMultiplyMatrix(aData, 4, 2, bData, 2, 12), 4, 12, cData, 12, 1); - test_util.expectArraysClose(result, expected, 0.0001); + test_util.expectArraysClose(result, expected); gpgpu.deleteMatrixTexture(a); gpgpu.deleteMatrixTexture(b); @@ -343,7 +343,7 @@ describe('mulmat_gpu huge matrix', () => { a, 1, sharedDim, matrix, sharedDim, outDim); const cpuResult = test_util.cpuMultiplyMatrix(a, 1, sharedDim, matrix, sharedDim, outDim); - test_util.expectArraysClose(result, cpuResult, 1e-4); + test_util.expectArraysClose(result, cpuResult); }); }); diff --git a/src/math/webgl/mulmat_packed_gpu_test.ts b/src/math/webgl/mulmat_packed_gpu_test.ts index bea644cfb9..ca302b050d 100644 --- a/src/math/webgl/mulmat_packed_gpu_test.ts +++ b/src/math/webgl/mulmat_packed_gpu_test.ts @@ -224,7 +224,7 @@ describe('mulmat_packed_gpu (different shapes)', () => { const result = mulmat_packed_gpu.uploadMultiplyMatrixPackedDownload( a, [4, 2], b, [2, 2]); const expected = test_util.cpuMultiplyMatrix(a, 4, 2, b, 2, 2); - test_util.expectArraysClose(result, expected, 0); + test_util.expectArraysClose(result, expected); }); it('multiplies a 4x1 by a non-identity 4x4', () => { @@ -250,7 +250,7 @@ describe('mulmat_packed_gpu (different shapes)', () => { const result = mulmat_packed_gpu.uploadMultiplyMatrixPackedDownload( a, [2, 4], b, [4, 3]); const expected = test_util.cpuMultiplyMatrix(a, 2, 4, b, 4, 3); - test_util.expectArraysClose(result, expected, 0.00001); + test_util.expectArraysClose(result, expected); }); }); @@ -269,7 +269,7 @@ describe('mulmat_packed_gpu (large matrices)', () => { const result = mulmat_packed_gpu.uploadMultiplyMatrixPackedDownload( a, [128, 128], b, [128, 128]); const expected = test_util.cpuMultiplyMatrix(a, 128, 128, b, 128, 128); - test_util.expectArraysClose(result, expected, 0.001); + test_util.expectArraysClose(result, expected); }); }); @@ -312,7 +312,7 @@ describe('mulmat_packed_gpu (multiple matrices)', () => { const expected = test_util.cpuMultiplyMatrix( test_util.cpuMultiplyMatrix(aData, 4, 2, bData, 2, 12), 4, 12, cData, 12, 1); - test_util.expectArraysClose(result, expected, 0.0001); + test_util.expectArraysClose(result, expected); gpgpu.deleteMatrixTexture(a); gpgpu.deleteMatrixTexture(b); @@ -344,7 +344,7 @@ describe('mulmat_packed_gpu A * B^t', () => { const bt = new Float32Array([b[0], b[2], b[1], b[3]]); const expected = test_util.cpuMultiplyMatrix(a, 2, 2, bt, 2, 2); - test_util.expectArraysClose(result, expected, 0); + test_util.expectArraysClose(result, expected); }); it('2x4 * 4x2', () => { @@ -369,7 +369,7 @@ describe('mulmat_packed_gpu A * B^t', () => { const bt = new Float32Array([b[0], b[4], b[1], b[5], b[2], b[6], b[3], b[7]]); const expected = test_util.cpuMultiplyMatrix(a, 2, 4, bt, 4, 2); - test_util.expectArraysClose(result, expected, 0); + test_util.expectArraysClose(result, expected); }); }); diff --git a/src/math/webgl/neg_gpu_test.ts b/src/math/webgl/neg_gpu_test.ts index 59ad24b377..7927dd828c 100644 --- a/src/math/webgl/neg_gpu_test.ts +++ b/src/math/webgl/neg_gpu_test.ts @@ -50,7 +50,7 @@ describe('neg_gpu', () => { const a = new Float32Array([0.5, 0, -2.3, 4, -12, -Math.E]); const result = uploadNegDownload(a, 2, 3); const expected = new Float32Array([-0.5, 0, 2.3, -4, 12, Math.E]); - test_util.expectArraysClose(result, expected, 0.0001); + test_util.expectArraysClose(result, expected); }); }); diff --git a/src/math/webgl/relu_gpu_test.ts b/src/math/webgl/relu_gpu_test.ts index b76c39d15c..f177fbcc3e 100644 --- a/src/math/webgl/relu_gpu_test.ts +++ b/src/math/webgl/relu_gpu_test.ts @@ -50,14 +50,14 @@ describe('relu_gpu', () => { const a = Array2D.new([3, 3], [[-1, 2, -3], [4, -5, 6], [-7, 8, -9]]); const result = uploadReluDownload(a); test_util.expectArraysClose( - result, new Float32Array([0, 2, 0, 4, 0, 6, 0, 8, 0]), 0.0001); + result, new Float32Array([0, 2, 0, 4, 0, 6, 0, 8, 0])); }); it('propagates NaNs', () => { const a = Array3D.new([2, 2, 2], [-1, NaN, -3, 4, 6, 0, -3, 1]); const result = uploadReluDownload(a); test_util.expectArraysClose( - result, new Float32Array([0, NaN, 0, 4, 6, 0, 0, 1]), 0.0001); + result, new Float32Array([0, NaN, 0, 4, 6, 0, 0, 1])); }); }); diff --git a/src/math/webgl/resize_bilinear_gpu_test.ts b/src/math/webgl/resize_bilinear_gpu_test.ts index 688fe261dd..28a89faa49 100644 --- a/src/math/webgl/resize_bilinear_gpu_test.ts +++ b/src/math/webgl/resize_bilinear_gpu_test.ts @@ -30,8 +30,7 @@ describe('resize bilinear', () => { const result = uploadResizeBilinearDownload(a, [2, 2, 1], [3, 3], false); test_util.expectArraysClose( - result, new Float32Array([2, 2, 2, 10 / 3, 10 / 3, 10 / 3, 4, 4, 4]), - 1e-4); + result, new Float32Array([2, 2, 2, 10 / 3, 10 / 3, 10 / 3, 4, 4, 4])); }); it('simple alignCorners=true', () => { @@ -40,7 +39,7 @@ describe('resize bilinear', () => { const result = uploadResizeBilinearDownload(a, [2, 2, 1], [3, 3], true); test_util.expectArraysClose( - result, new Float32Array([2, 2, 2, 3, 3, 3, 4, 4, 4]), 1e-4); + result, new Float32Array([2, 2, 2, 3, 3, 3, 4, 4, 4])); }); it('matches tensorflow w/ random numbers alignCorners=false', () => { @@ -61,8 +60,7 @@ describe('resize bilinear', () => { 0.69152176, 0.44905344, 1.07186723, 0.03823943, 1.19864893, 0.6183514, 3.49600649, 1.50272655, 1.73724651, 1.68149579, 0.69152176, 0.44905344, 1.07186723, 0.03823943, 1.19864893 - ]), - 1e-4); + ])); }); it('matches tensorflow w/ random numbers alignCorners=true', () => { @@ -83,8 +81,7 @@ describe('resize bilinear', () => { 1.70539713, 1.3923912, 1.68282723, 1.54382229, 1.66025746, 1.62451875, 1.83673346, 1.38198328, 1.92833281, 1.13944793, 2.01993227, 1.57932377, 2.34758639, 2.01919961, 2.67524052 - ]), - 1e-4); + ])); }); }); diff --git a/src/math/webgl/sigmoid_gpu_test.ts b/src/math/webgl/sigmoid_gpu_test.ts index e9a23358aa..76880c60d3 100644 --- a/src/math/webgl/sigmoid_gpu_test.ts +++ b/src/math/webgl/sigmoid_gpu_test.ts @@ -37,7 +37,7 @@ describe('sigmoid_gpu', () => { } const result = uploadSigmoidDownload(a, 1, size); - test_util.expectArraysClose(result, expectedResult, 1e-6); + test_util.expectArraysClose(result, expectedResult); }); }); diff --git a/src/math/webgl/step_gpu_test.ts b/src/math/webgl/step_gpu_test.ts index cfc7715a7c..3e7ab1e461 100644 --- a/src/math/webgl/step_gpu_test.ts +++ b/src/math/webgl/step_gpu_test.ts @@ -59,14 +59,14 @@ describe('step_gpu', () => { const result = uploadStepDownload(aArr); const expected = new Float32Array(a.length); expected.fill(1); - test_util.expectArraysClose(result, expected, 0); + test_util.expectArraysClose(result, expected); }); it('operates on a heterogeneous matrix', () => { const aArr = Array3D.new([1, 2, 2], [-1, 0, 100, -0.001]); const result = uploadStepDownload(aArr); const expected = new Float32Array([0, 0, 1, 0]); - test_util.expectArraysClose(result, expected, 0); + test_util.expectArraysClose(result, expected); }); }); diff --git a/src/math/webgl/tex_util_test.ts b/src/math/webgl/tex_util_test.ts index 786f4608c6..0408493e6a 100644 --- a/src/math/webgl/tex_util_test.ts +++ b/src/math/webgl/tex_util_test.ts @@ -90,16 +90,14 @@ describe('tex_util encodeMatrixToUnpackedArray, channels = 4', () => { const matrix = new Float32Array([1]); const unpackedRGBA = new Float32Array([0, 0, 0, 0]); tex_util.encodeMatrixToUnpackedArray(matrix, unpackedRGBA, 4); - test_util.expectArraysClose( - unpackedRGBA, new Float32Array([1, 0, 0, 0]), 0); + test_util.expectArraysClose(unpackedRGBA, new Float32Array([1, 0, 0, 0])); }); it('1x1 can upload texels with values greater than 1', () => { const matrix = new Float32Array([100]); const unpackedRGBA = new Float32Array([0, 0, 0, 0]); tex_util.encodeMatrixToUnpackedArray(matrix, unpackedRGBA, 4); - test_util.expectArraysClose( - unpackedRGBA, new Float32Array([100, 0, 0, 0]), 0); + test_util.expectArraysClose(unpackedRGBA, new Float32Array([100, 0, 0, 0])); }); it('1x4 each texel has 4 elements with matrix value in R channel', () => { @@ -108,7 +106,7 @@ describe('tex_util encodeMatrixToUnpackedArray, channels = 4', () => { tex_util.encodeMatrixToUnpackedArray(matrix, unpackedRGBA, 4); test_util.expectArraysClose( unpackedRGBA, - new Float32Array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]), 0); + new Float32Array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0])); }); }); @@ -117,22 +115,21 @@ describe('tex_util encodeMatrixToUnpackedArray, channels = 1', () => { const matrix = new Float32Array([1]); const unpackedRGBA = new Float32Array([0]); tex_util.encodeMatrixToUnpackedArray(matrix, unpackedRGBA, 1); - test_util.expectArraysClose(unpackedRGBA, new Float32Array([1]), 0); + test_util.expectArraysClose(unpackedRGBA, new Float32Array([1])); }); it('1x1 can upload texels with values greater than 1', () => { const matrix = new Float32Array([100]); const unpackedRGBA = new Float32Array([0]); tex_util.encodeMatrixToUnpackedArray(matrix, unpackedRGBA, 1); - test_util.expectArraysClose(unpackedRGBA, new Float32Array([100]), 0); + test_util.expectArraysClose(unpackedRGBA, new Float32Array([100])); }); it('1x4 each texel has 4 elements with matrix value in R channel', () => { const matrix = new Float32Array([1, 2, 3, 4]); const unpackedRGBA = new Float32Array(4); tex_util.encodeMatrixToUnpackedArray(matrix, unpackedRGBA, 1); - test_util.expectArraysClose( - unpackedRGBA, new Float32Array([1, 2, 3, 4]), 0); + test_util.expectArraysClose(unpackedRGBA, new Float32Array([1, 2, 3, 4])); }); }); @@ -150,7 +147,7 @@ describe('tex_util decodeMatrixFromUnpackedArray', () => { const matrix = new Float32Array(2); tex_util.decodeMatrixFromUnpackedArray(unpackedRGBA, matrix, 4); expect(matrix.length).toEqual(2); - test_util.expectArraysClose(matrix, new Float32Array([1, 2]), 0); + test_util.expectArraysClose(matrix, new Float32Array([1, 2])); }); }); @@ -159,28 +156,28 @@ describe('tex_util encodeMatrixToPackedRGBA', () => { const matrix = new Float32Array([1]); const packedRGBA = new Float32Array(4); tex_util.encodeMatrixToPackedRGBA(matrix, 1, 1, packedRGBA); - test_util.expectArraysClose(packedRGBA, new Float32Array([1, 0, 0, 0]), 0); + test_util.expectArraysClose(packedRGBA, new Float32Array([1, 0, 0, 0])); }); it('1x2 loads the second element into G and 0\'s into BA', () => { const matrix = new Float32Array([1, 2]); const packedRGBA = new Float32Array(4); tex_util.encodeMatrixToPackedRGBA(matrix, 1, 2, packedRGBA); - test_util.expectArraysClose(packedRGBA, new Float32Array([1, 2, 0, 0]), 0); + test_util.expectArraysClose(packedRGBA, new Float32Array([1, 2, 0, 0])); }); it('2x1 loads the second element into G and 0\'s into BA', () => { const matrix = new Float32Array([1, 2]); const packedRGBA = new Float32Array(4); tex_util.encodeMatrixToPackedRGBA(matrix, 2, 1, packedRGBA); - test_util.expectArraysClose(packedRGBA, new Float32Array([1, 0, 2, 0]), 0); + test_util.expectArraysClose(packedRGBA, new Float32Array([1, 0, 2, 0])); }); it('2x2 exactly fills one texel', () => { const matrix = new Float32Array([1, 2, 3, 4]); const packedRGBA = new Float32Array(4); tex_util.encodeMatrixToPackedRGBA(matrix, 2, 2, packedRGBA); - test_util.expectArraysClose(packedRGBA, new Float32Array([1, 2, 3, 4]), 0); + test_util.expectArraysClose(packedRGBA, new Float32Array([1, 2, 3, 4])); }); it('4x3 pads the final column G and A channels with 0', () => { @@ -195,8 +192,7 @@ describe('tex_util encodeMatrixToPackedRGBA', () => { tex_util.encodeMatrixToPackedRGBA(matrix, 4, 3, packedRGBA); test_util.expectArraysClose( packedRGBA, - new Float32Array([1, 2, 4, 5, 3, 0, 6, 0, 7, 8, 10, 11, 9, 0, 12, 0]), - 0); + new Float32Array([1, 2, 4, 5, 3, 0, 6, 0, 7, 8, 10, 11, 9, 0, 12, 0])); }); it('3x4 pads the final row B and A channels with 0', () => { @@ -210,8 +206,7 @@ describe('tex_util encodeMatrixToPackedRGBA', () => { tex_util.encodeMatrixToPackedRGBA(matrix, 3, 4, packedRGBA); test_util.expectArraysClose( packedRGBA, - new Float32Array([1, 2, 5, 6, 3, 4, 7, 8, 9, 10, 0, 0, 11, 12, 0, 0]), - 0); + new Float32Array([1, 2, 5, 6, 3, 4, 7, 8, 9, 10, 0, 0, 11, 12, 0, 0])); }); it('3x3 bottom-right texel is R000', () => { @@ -225,7 +220,7 @@ describe('tex_util encodeMatrixToPackedRGBA', () => { tex_util.encodeMatrixToPackedRGBA(matrix, 3, 3, packedRGBA); test_util.expectArraysClose( packedRGBA, - new Float32Array([1, 2, 4, 5, 3, 0, 6, 0, 7, 8, 0, 0, 9, 0, 0, 0]), 0); + new Float32Array([1, 2, 4, 5, 3, 0, 6, 0, 7, 8, 0, 0, 9, 0, 0, 0])); }); }); @@ -241,21 +236,21 @@ describe('tex_util decodeMatrixFromPackedRGBA', () => { const packedRGBA = new Float32Array([1, 2, 0, 0]); const matrix = new Float32Array(2); tex_util.decodeMatrixFromPackedRGBA(packedRGBA, 1, 2, matrix); - test_util.expectArraysClose(matrix, new Float32Array([1, 2]), 0); + test_util.expectArraysClose(matrix, new Float32Array([1, 2])); }); it('2x1 matrix loads RB from only texel', () => { const packedRGBA = new Float32Array([1, 0, 2, 0]); const matrix = new Float32Array(2); tex_util.decodeMatrixFromPackedRGBA(packedRGBA, 2, 1, matrix); - test_util.expectArraysClose(matrix, new Float32Array([1, 2]), 0); + test_util.expectArraysClose(matrix, new Float32Array([1, 2])); }); it('2x2 matrix loads RGBA from only texel', () => { const packedRGBA = new Float32Array([1, 2, 3, 4]); const matrix = new Float32Array(4); tex_util.decodeMatrixFromPackedRGBA(packedRGBA, 2, 2, matrix); - test_util.expectArraysClose(matrix, new Float32Array([1, 2, 3, 4]), 0); + test_util.expectArraysClose(matrix, new Float32Array([1, 2, 3, 4])); }); it('4x3 final column only reads RB from edge texels', () => { @@ -270,7 +265,7 @@ describe('tex_util decodeMatrixFromPackedRGBA', () => { const matrix = new Float32Array(12); tex_util.decodeMatrixFromPackedRGBA(packedRGBA, 4, 3, matrix); test_util.expectArraysClose( - matrix, new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), 0); + matrix, new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])); }); it('3x4 final row only reads RG from edge texels', () => { @@ -284,7 +279,7 @@ describe('tex_util decodeMatrixFromPackedRGBA', () => { const matrix = new Float32Array(12); tex_util.decodeMatrixFromPackedRGBA(packedRGBA, 3, 4, matrix); test_util.expectArraysClose( - matrix, new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), 0); + matrix, new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])); }); it('3x3 bottom-right only reads R from corner texel', () => { @@ -298,6 +293,6 @@ describe('tex_util decodeMatrixFromPackedRGBA', () => { const matrix = new Float32Array(9); tex_util.decodeMatrixFromPackedRGBA(packedRGBA, 3, 3, matrix); test_util.expectArraysClose( - matrix, new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9]), 0); + matrix, new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9])); }); }); diff --git a/src/math/webgl/trig_gpu_test.ts b/src/math/webgl/trig_gpu_test.ts index 1315ea219f..a703cff7b4 100644 --- a/src/math/webgl/trig_gpu_test.ts +++ b/src/math/webgl/trig_gpu_test.ts @@ -210,7 +210,7 @@ describe('tanh_gpu', () => { } const aArr = Array2D.new([2, 5], a); const result = unaryop_gpu_test.uploadUnaryDownload(aArr, unaryop_gpu.TANH); - test_util.expectArraysClose(result, expectedResult, 1e-6); + test_util.expectArraysClose(result, expectedResult); }); it('overflow', () => { diff --git a/src/test_util.ts b/src/test_util.ts index a4307cd47b..d308b2255e 100644 --- a/src/test_util.ts +++ b/src/test_util.ts @@ -15,8 +15,11 @@ * ============================================================================= */ +/** Accuracy for tests. */ +const EPSILON = 1e-4; + export function expectArraysClose( - actual: Float32Array, expected: Float32Array, epsilon = 1e-4) { + actual: Float32Array, expected: Float32Array, epsilon = EPSILON) { if (actual.length !== expected.length) { throw new Error( 'Matrices have different lengths (' + actual.length + ' vs ' +