tensorflow / tfjs

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

isWebGPUSupported() crashes under Node #8066

Closed reillyeon closed 1 year ago

reillyeon commented 1 year ago

System information

Describe the current behavior Under Node.js the globalThis object does not have a navigator property, since isWebGPUSupported() tries to access a property on globalThis.navigator an exception is thrown. I believe that this regressed in #8001.

Describe the expected behavior isWebGPUSupported() should return false in non-browser contexts like Node (unless a Node WebGPU is available as suggested in #7595).

Standalone code to reproduce the issue

Create an empty npm project, install @tensorflow/tjfs-backend-webgpu and then try to invoke import('@tensorflow/tjfs-backend-webgpu') on the Node REPL.

Other info / logs

$ node
Welcome to Node.js v18.17.1.
Type ".help" for more information.
> await import('@tensorflow/tfjs-backend-webgpu')
Uncaught TypeError: Cannot read properties of undefined (reading 'gpu')
    at isWebGPUSupported (/home/reillyg/tmp/node_modules/@tensorflow/tfjs-backend-webgpu/dist/tf-backend-webgpu.node.js:1208:35)
    at Object.<anonymous> (/home/reillyg/tmp/node_modules/@tensorflow/tfjs-backend-webgpu/dist/tf-backend-webgpu.node.js:2117:5)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
axinging commented 1 year ago

Running tfjs webgpu on node requires webgpu on node, however there isn't any official webgpu on node support, as described here: https://bugs.chromium.org/p/dawn/issues/detail?id=1762.

A local workaround is here: https://github.com/axinging/webgpu-node-demo/blob/main/tfjs-on-external-node/README.md

qjia7 commented 1 year ago

Please check that whether !!globalThis.navigator?.gpu instead of !!globalThis.navigator.gpu in isWebGPUSupported() can fix your issue. And feel free to submit a PR for it. Thanks.

gaikwadrahul8 commented 1 year ago

Hi, @reillyeon

Apologize for the delayed response and I was trying to replicate the same issue from my end and I'm also getting the same error which you mentioned above in your issue template, please refer above comments from @axinging and @qjia7 and see is it resolving your issue or not ? Thank you

For reference I have added error log below:

(base) gaikwadrahul-macbookpro:test-8066 gaikwadrahul$ node
Welcome to Node.js v18.17.0.
Type ".help" for more information.
> await import('@tensorflow/tfjs-backend-webgpu')
Uncaught TypeError: Cannot read properties of undefined (reading 'gpu')
    at isWebGPUSupported (/Users/gaikwadrahul/Desktop/TFJS/test-8066/node_modules/@tensorflow/tfjs-backend-webgpu/dist/tf-backend-webgpu.node.js:1208:35)
    at Object.<anonymous> (/Users/gaikwadrahul/Desktop/TFJS/test-8066/node_modules/@tensorflow/tfjs-backend-webgpu/dist/tf-backend-webgpu.node.js:2117:5)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
> 
qjia7 commented 1 year ago

@reillyeon It seems that @mattsoulanille just fixed this issue 2 hours ago by #8070. Thanks for your contribution.

google-ml-butler[bot] commented 1 year ago

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