The WebNN Polyfill has been published to NPM.
With this polyfill, Web developers are able to experience the WebNN API early before the native implementations are shipped. Meanwhile, it can be treated as an independent implementation to help validate the feasibility and stability of the WebNN specification.
Import the package via either NPM or a script tag.
- Via NPM
- Via a script tag
Before using WebNN API, you should set backend to enable TensorFlow.js as follows.
Currently WebNN Polyfill supports 3 backends:
has higher numerical precision, while
WebGL backend provides better performance.
A new backend
WebGPU is work in progress.
const backend = 'webgl'; // or 'cpu', 'wasm' const context = await navigator.ml.createContext(); const tf = context.tf; await tf.setBackend(backend); await tf.ready();
Try out the live version of WebNN samples which fall back to the WebNN Polyfill in the browser without native WebNN API support. These samples showcase various popular use cases for neural networks powered by WebNN API.
We will continuously develop the WebNN Polyfill to keep it aligned with the WebNN API spec. At the same time, WebNN API development in Chromium is in full swing, we hope it won’t take too long to bring hardware acceleration access to Web developers through WebNN API.