Skip to content

Commit aa484a6

Browse files
dydavidkimLinchennqjia7mattsoulanillepyu10055
authored
Updating to tfjs master: latest (#41)
* Update jasmine_util.ts (tensorflow#6872) FIX * webgl: Fix NaN issue (tensorflow#6828) Fix tensorflow#6822 Problem 1: On some GPUs, even if a and b are both non-NaN, the value of isNaN in vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0)); are still larger than 0., which misleads all values become NAN. 2: After resolving NAN issue, the result is still incorrect. It seems that the isnan_custom is not well supported on the problem GPU. After switching back to builtin isnan, everything works well. Solution: Use the bool type bvec4 instead of float type vec4 to calculate isNaN to avoid the the float precision issue when comparing with zero. Meanwhile, add an env flag WEBGL2_ISNAN_CUSTOM to allow user to specify which isnan to use. * Upgrade nodejs to 18.7.0 (tensorflow#6863) * Upgrade nodejs to 18.7.0 * Fix hash table test string not passed as base64 * fixed prelu fusing code that pre-maturely neg the const on multiply (tensorflow#6876) Co-authored-by: RajeshT <[email protected]> Co-authored-by: Linchenn <[email protected]> Co-authored-by: Jiajia Qin <[email protected]> Co-authored-by: Matthew Soulanille <[email protected]> Co-authored-by: Ping Yu <[email protected]> Co-authored-by: RajeshT <[email protected]>
1 parent 6cb3a93 commit aa484a6

File tree

17 files changed

+131
-182
lines changed

17 files changed

+131
-182
lines changed

WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")
5050

5151
nodejs_register_toolchains(
5252
name = "nodejs",
53-
node_version = "16.13.2",
53+
node_version = "18.7.0",
5454
)
5555

5656
# Install the yarn tool

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"rollup": "^2.46.0",
5959
"rollup-plugin-sourcemaps": "^0.6.3",
6060
"rollup-plugin-terser": "^7.0.2",
61-
"rollup-plugin-visualizer": "~3.3.2",
61+
"rollup-plugin-visualizer": "~5.8.2",
6262
"seedrandom": "^3.0.5",
6363
"shelljs": "~0.8.5",
6464
"string_decoder": "^1.3.0",

tfjs-backend-webgl/src/binaryop_packed_gpu.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import {GPGPUProgram, useShapeUniforms} from './gpgpu_math';
2121
import {getChannels} from './packing_util';
2222
import {getCoordsDataType} from './shader_compiler';
2323

24-
export const CHECK_NAN_SNIPPET = `
25-
result.r = isNaN.r > 0. ? NAN : result.r;
26-
result.g = isNaN.g > 0. ? NAN : result.g;
27-
result.b = isNaN.b > 0. ? NAN : result.b;
28-
result.a = isNaN.a > 0. ? NAN : result.a;
24+
export const CHECK_NAN_SNIPPET_PACKED = `
25+
result.r = isNaN.r ? NAN : result.r;
26+
result.g = isNaN.g ? NAN : result.g;
27+
result.b = isNaN.b ? NAN : result.b;
28+
result.a = isNaN.a ? NAN : result.a;
2929
`;
3030

3131
export const ELU_DER = `

tfjs-backend-webgl/src/flags_webgl.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,9 @@ ENV.registerFlag('WEBGL_MAX_SIZE_FOR_NARROW_TEXTURE', () => Infinity);
268268
* problem: https://github.com/tensorflow/tfjs/issues/6775.
269269
*/
270270
ENV.registerFlag('WEBGL_AUTO_SQUARIFY_NARROW_TEXTURE_SHAPE', () => false);
271+
272+
/**
273+
* Whether to use the customized isnan. It's only useful for webgl2 since webgl1
274+
* doesn't have the builtin isnan.
275+
*/
276+
ENV.registerFlag('WEBGL2_ISNAN_CUSTOM', () => false);

tfjs-backend-webgl/src/glsl_version.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function getGlslDifferences(): GLSL {
6262
// - fraction = anything except all 0 bits (since all 0 bits represents
6363
// infinity).
6464
// https://en.wikipedia.org/wiki/IEEE_754-1985#Representation_of_non-numbers
65-
defineSpecialNaN = `
65+
defineSpecialNaN = env().getBool('WEBGL2_ISNAN_CUSTOM') ? `
6666
bool isnan_custom(float val) {
6767
uint floatToUint = floatBitsToUint(val);
6868
return (floatToUint & 0x7fffffffu) > 0x7f800000u;
@@ -74,7 +74,8 @@ export function getGlslDifferences(): GLSL {
7474
}
7575
7676
#define isnan(value) isnan_custom(value)
77-
`;
77+
` :
78+
'';
7879
// In webgl 2 we do not need to specify a custom isinf so there is no
7980
// need for a special INFINITY constant.
8081
defineSpecialInf = ``;

tfjs-backend-webgl/src/kernel_utils/kernel_funcs_utils.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@ import {SimpleBinaryKernelImplCPU, SimpleUnaryKernelImplCPU} from './shared';
3232

3333
export const CHECK_NAN_SNIPPET_UNARY = `if (isnan(x)) return x;`;
3434

35-
export const CHECK_NAN_SNIPPET_BINARY = `
36-
if (isnan(a)) return a;
37-
if (isnan(b)) return b;
38-
`;
39-
40-
export const CHECK_NAN_SNIPPET_BINARY_PACKED = `
41-
result.r = isNaN.r > 0. ? NAN : result.r;
42-
result.g = isNaN.g > 0. ? NAN : result.g;
43-
result.b = isNaN.b > 0. ? NAN : result.b;
44-
result.a = isNaN.a > 0. ? NAN : result.a;
45-
`;
46-
4735
type UnaryKernelFuncConfig = {
4836
opSnippet: string,
4937
packedOpSnippet?: string,

tfjs-backend-webgl/src/kernels/Atan2.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@
1717

1818
import {Atan2} from '@tensorflow/tfjs-core';
1919
import {KernelConfig} from '@tensorflow/tfjs-core';
20+
import {CHECK_NAN_SNIPPET} from '../binaryop_gpu';
21+
import {CHECK_NAN_SNIPPET_PACKED} from '../binaryop_packed_gpu';
22+
import {binaryKernelFunc} from '../kernel_utils/kernel_funcs_utils';
2023

21-
import {binaryKernelFunc, CHECK_NAN_SNIPPET_BINARY, CHECK_NAN_SNIPPET_BINARY_PACKED} from '../kernel_utils/kernel_funcs_utils';
22-
23-
const ATAN2 = CHECK_NAN_SNIPPET_BINARY + `
24+
const ATAN2 = CHECK_NAN_SNIPPET + `
2425
return atan(a, b);
2526
`;
2627

2728
const ATAN2_PACKED = `
2829
vec4 result = atan(a, b);
29-
vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));
30+
bvec4 isNaNA = isnan(a);
31+
bvec4 isNaNB = isnan(b);
32+
bvec4 isNaN = bvec4(isNaNA.x || isNaNB.x, isNaNA.y || isNaNB.y, isNaNA.z || isNaNB.z, isNaNA.w || isNaNB.w);
3033
` +
31-
CHECK_NAN_SNIPPET_BINARY_PACKED + `
34+
CHECK_NAN_SNIPPET_PACKED + `
3235
return result;
3336
`;
3437

tfjs-backend-webgl/src/kernels/Maximum.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import {KernelConfig, KernelFunc, Maximum} from '@tensorflow/tfjs-core';
1919

2020
import {CHECK_NAN_SNIPPET} from '../binaryop_gpu';
21-
import {CHECK_NAN_SNIPPET as CHECK_NAN_SNIPPET_PACKED} from '../binaryop_packed_gpu';
21+
import {CHECK_NAN_SNIPPET_PACKED} from '../binaryop_packed_gpu';
2222
import {binaryKernelFunc} from '../kernel_utils/kernel_funcs_utils';
2323
import {maximumImplCPU} from '../kernel_utils/shared';
2424

@@ -28,7 +28,9 @@ const MAXIMUM = CHECK_NAN_SNIPPET + `
2828

2929
const MAXIMUM_PACKED = `
3030
vec4 result = vec4(max(a, b));
31-
vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));
31+
bvec4 isNaNA = isnan(a);
32+
bvec4 isNaNB = isnan(b);
33+
bvec4 isNaN = bvec4(isNaNA.x || isNaNB.x, isNaNA.y || isNaNB.y, isNaNA.z || isNaNB.z, isNaNA.w || isNaNB.w);
3234
` +
3335
CHECK_NAN_SNIPPET_PACKED + `
3436
return result;

tfjs-backend-webgl/src/kernels/Minimum.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import {KernelConfig, KernelFunc, Minimum} from '@tensorflow/tfjs-core';
1919

2020
import {CHECK_NAN_SNIPPET} from '../binaryop_gpu';
21-
import {CHECK_NAN_SNIPPET as CHECK_NAN_SNIPPET_PACKED} from '../binaryop_packed_gpu';
21+
import {CHECK_NAN_SNIPPET_PACKED} from '../binaryop_packed_gpu';
2222
import {binaryKernelFunc} from '../kernel_utils/kernel_funcs_utils';
2323
import {minimumImplCPU} from '../kernel_utils/shared';
2424

@@ -28,7 +28,9 @@ const MINIMUM = CHECK_NAN_SNIPPET + `
2828

2929
const MINIMUM_PACKED = `
3030
vec4 result = vec4(min(a, b));
31-
vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));
31+
bvec4 isNaNA = isnan(a);
32+
bvec4 isNaNB = isnan(b);
33+
bvec4 isNaN = bvec4(isNaNA.x || isNaNB.x, isNaNA.y || isNaNB.y, isNaNA.z || isNaNB.z, isNaNA.w || isNaNB.w);
3234
` +
3335
CHECK_NAN_SNIPPET_PACKED + `
3436
return result;

tfjs-backend-webgl/src/kernels/Mod.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717

1818
import {KernelConfig, KernelFunc, Mod} from '@tensorflow/tfjs-core';
1919

20-
import {CHECK_NAN_SNIPPET} from '../binaryop_packed_gpu';
20+
import {CHECK_NAN_SNIPPET_PACKED} from '../binaryop_packed_gpu';
2121
import {binaryKernelFunc} from '../kernel_utils/kernel_funcs_utils';
2222

2323
const MOD = `if (b == 0.0) return NAN;
2424
return mod(a, b);`;
2525

2626
const MOD_PACKED = `
2727
vec4 result = mod(a, b);
28-
vec4 isNaN = vec4(equal(b, vec4(0.0)));
28+
bvec4 isNaN = equal(b, vec4(0.0));
2929
` +
30-
CHECK_NAN_SNIPPET + `
30+
CHECK_NAN_SNIPPET_PACKED + `
3131
return result;
3232
`;
3333

0 commit comments

Comments
 (0)