Repository navigation
Description
Please make sure that this is a bug. As per our
GitHub Policy,
we only address code/doc bugs, performance issues, feature requests and
build/installation issues on GitHub. tag:bug_template
System information
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow.js):yes
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 20.04.2 LTS
- Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
- TensorFlow.js installed from (npm or script link): script link
- TensorFlow.js version (use command below): 3.11.0
- Browser version: Google Chrome 93.0.4577.63
- Tensorflow.js Converter Version:
Describe the current behavior
tf.isNaN() has different results when running on different backends. For example, for the following code:
const y = tf.tensor1d([Infinity, .5, 4, -38.8, NaN]);
y.isNaN().print();
In webgl backend, the output is
Tensor
[false, false, false, false, false]
In cpu backend, the output is
Tensor
[false, false, false, false, true]
In wasm backend, get the error is
Uncaught (in promise) Error: Kernel 'IsNan' not registered for backend 'wasm'
These results indicate that tf.isNaN()cannot correctly recognize NaN in webgl backend.
Describe the expected behavior
According to the official documentation,tf.isNaN(x) RReturns which elements of x are NaN.
Therefore, the correct result should be
Tensor
[false, false, false, false, true]
Standalone code to reproduce the issue
Provide a reproducible test case that is the bare minimum necessary to generate
the problem. If possible, please share a link to Colab/CodePen/any notebook.
the code to reproduce the problem is
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"> </script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-webgl"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm/dist/tf-backend-wasm.js"></script>
</head>
<body>
<script>
async function nan_test() {
const y = tf.tensor1d([Infinity, .5, 4, -38.8, NaN]);
tf.setBackend("webgl");
await tf.ready();
console.log("webgl backend result:");
y.isNaN().print();
tf.setBackend("cpu");
await tf.ready();
console.log("cpu backend result:");
y.isNaN().print();
tf.setBackend("wasm");
await tf.ready();
console.log("wasm backend result:");
y.isNaN().print();
}
nan_test();
</script>
</body>
</html>
Other info / logs Include any logs or source code that would be helpful to
diagnose the problem. If including tracebacks, please include the full
traceback. Large logs and files should be attached
the code to reproduce the problem
nan_test.zip
.
Activity
rthadur commentedon Nov 1, 2021
I tried in lates chrome browser(MAC OS) and was not able to see above behavior

liliquan0118 commentedon Nov 2, 2021
@rthadur Thank you for your prompt reply. I did the test again, and still got incorrect results on Ubuntu (ThinkPad and Dell). I also tested it on Mac OS and Windows, and I really can't see this behavior. This is strange, what is the reason?


The output of chrome on Ubuntu is as follows:
The same error appears on Tensorflow.js API web page (https://js.tensorflow.org/api/latest/?hl=zh-cn#isNaN)
rthadur commentedon Nov 2, 2021
Can you try run this https://js.tensorflow.org/debug/ and provide screenshot ?
liliquan0118 commentedon Nov 2, 2021
This is screenshot:

rthadur commentedon Nov 2, 2021
can you please upgrade to Chrome latest and try ?
liliquan0118 commentedon Nov 2, 2021
I have updated Chrome to version 95.0.4638.69 and the error still exists. In addition, there is the same error on firefox.

result on chrome:
result on Firefox:

The debug information is as follows

liliquan0118 commentedon Nov 23, 2021
Thank you for fixing this issue, but I have some doubts. First , I feel that the current changes cannot explain the incorrect results under Linux. In your fix, delete
return (val > 0.0 || val < 0.0) ? false : val != 0.0;and addreturn val != val;. However, in Linux, whenval=NaN, the result of(val > 0.0 || val < 0.0) ? false : val != 0.0a is TRUE, as expected . So is this issue caused by the insufficient webgl support under Linux? as stated by ahmedsabie “Modify the implementation to also support UHD Graphics on Linux” . Second, I would like to ask if this fix will be updated to the future version? Thanks again.butanoid commentedon Nov 29, 2021
The issue is caused by WebGL being optimized and supported slightly differently across different GPUs so behavior isn't always consistent. The issue is more of an Intel UHD Graphics issue rather than a Linux one. Yes the fix will be added to the next release once merged!
shaoboyan commentedon Jan 26, 2022
@ahmedsabie @liliquan0118
I have a PR to implement isnan in webgpu backend. The implementation follows IEEE 754-1985 which should be platform-independent and strict. So
Also cc @qjia7 @gyagp
Update isnan implementation in WebGL backend to follow IEEE 754-1985
Update isnan implementation in WebGL backend to follow IEEE 754-1985 (#…
rthadur commentedon Mar 10, 2022
Related PR has been merged , closing this request. Thank you
google-ml-butler commentedon Mar 10, 2022
Are you satisfied with the resolution of your issue?
Yes
No