Skip to content

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

@liliquan0118

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
.

Metadata

Metadata

Assignees

Labels

type:bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions