Skip to content

When in webgl backend, tf.isNaN() produces incorrect results #5800

Closed

Description

@liliquan0118

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

  1. added
    type:bug
    on Nov 1, 2021
  2. rthadur commented on Nov 1, 2021

    @rthadur
    Contributor

    I tried in lates chrome browser(MAC OS) and was not able to see above behavior
    image

  3. self-assigned this
    on Nov 1, 2021
  4. liliquan0118 commented on Nov 2, 2021

    @liliquan0118
    Author

    @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:
    image
    The same error appears on Tensorflow.js API web page (https://js.tensorflow.org/api/latest/?hl=zh-cn#isNaN)
    image

  5. rthadur commented on Nov 2, 2021

    @rthadur
    Contributor
  6. liliquan0118 commented on Nov 2, 2021

    @liliquan0118
    Author

    This is screenshot:
    image

  7. rthadur commented on Nov 2, 2021

    @rthadur
    Contributor

    can you please upgrade to Chrome latest and try ?

  8. liliquan0118 commented on Nov 2, 2021

    @liliquan0118
    Author

    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:
    image

    result on Firefox:
    image

    The debug information is as follows
    Selection_139

  9. assigned and unassigned on Nov 2, 2021
  10. liliquan0118 commented on Nov 23, 2021

    @liliquan0118
    Author

    image
    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 add return val != val; . However, in Linux, when val=NaN, the result of (val > 0.0 || val < 0.0) ? false : val != 0.0 a 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.

  11. butanoid commented on Nov 29, 2021

    @butanoid
    Contributor

    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!

  12. shaoboyan commented on Jan 26, 2022

    @shaoboyan
    Contributor

    @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

    • Is this bit-level check isnan implementaion suitable for tfjs?
    • Do you think is it possible to have similar implementation in webgl backend?

    Also cc @qjia7 @gyagp

  13. rthadur commented on Mar 10, 2022

    @rthadur
    Contributor

    Related PR has been merged , closing this request. Thank you

  14. google-ml-butler commented on Mar 10, 2022

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions