tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.51k stars 1.94k forks source link

The output of the tf.matMul operation on the CPU backend is inconsistent with the output on the WebGL, Wasm, and TensorFlow backends #8400

Open liliquan0118 opened 1 month ago

liliquan0118 commented 1 month ago

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

Describe the current behavior

Given the "a":[[[[692093061.0947313,797688219.5022278]]],[[[-1668064714.535003,1419422926.3580394]]]],"b":[[-318504117.74568224,888231962.4587994],[-1840707687.672637,1053049392.4807925]], tf.matMul(a,b) return different results across all backends.

Output:

image

Describe the expected behavior The output should be consistent across all backends.

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.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>bug00</title>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.1.0/dist/tf.js"> </script>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm@4.1.0/dist/tf-backend-wasm.js"></script>
</head>
<body>

<script>
    async function matMul(backend){
        await tf.setBackend(backend);
        await tf.ready() 
        var a = [[[[692093061.0947313,797688219.5022278]]],[[[-1668064714.535003,1419422926.3580394]]]]
        var b = [[-318504117.74568224,888231962.4587994],[-1840707687.672637,1053049392.4807925]]
       var result = await tf.matMul(a, b);
        await console.log("the result of ", tf.getBackend(), "is:\n" );
        await result.print();
    }
    async function test() {
        await matMul("cpu");
        await matMul("webgl");
        await matMul("wasm");
    }

    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.