The WebNN Polyfill has been published to NPM.
It is a JavaScript implementation of the WebNN API, based on TensorFlow.js that supports multiple backends for both Web browsers and Node.js.
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.
Usage
Import the package via either NPM or a script tag.
- Via NPM
import '@webmachinelearning/webnn-polyfill';
- Via a script tag
<script src="https://cdn.jsdelivr.net/npm/@webmachinelearning/webnn-polyfill/dist/webnn-polyfill.js"></script>
Before using WebNN API, you should set backend to enable TensorFlow.js as follows.
Currently WebNN Polyfill supports 3 backends: CPU
, WebGL
and WASM
. CPU
backend
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();
Samples
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.
Vision
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.