Note
This repository is archived. This polyfill demonstrated early feasibility of the WebNN API. Now that the native implementations across multiple backends track the latest specification closely all web developers are advised to use the native implementations for their development and experimentation needs.
A JavaScript implementation of the Web Neural Network API.
The implementation of this webnn-polyfill is based on TensorFlow.js that supports the following 3 backends:
- TensorFlow.js CPU Backend, pure-JS backend for Node.js and the browser.
- TensorFlow.js WebGL Backend, WebGL backend for the browser.
- TensorFlow.js WASM Backend, WebAssembly backend for the browser.
- CPU backend is the only supported backend for Node.js.
- WASM backend does not support all the ops and some test failures are thus expected.
import '@webmachinelearning/webnn-polyfill';
<script src="https://cdn.jsdelivr.net/npm/@webmachinelearning/webnn-polyfill/dist/webnn-polyfill.js"></script>
WebNN Polyfill requires setting backend to enable TensorFlow.js.
- When running in Node.js, recommend using CPU backend for its higher numerical precision.
const backend = 'cpu';
const context = await navigator.ml.createContext();
const tf = context.tf;
await tf.setBackend(backend);
await tf.ready();
- When running in browsers, recommend using WebGL backend for better performance.
const backend = 'webgl'; // 'cpu' or 'wasm'
const context = await navigator.ml.createContext();
const tf = context.tf;
await tf.setBackend(backend);
await tf.ready();
- When running in browsers with WASM backend.
const backend = 'wasm';
const context = await navigator.ml.createContext();
const wasm = context.wasm;
// 1- Enforce use Wasm SIMD binary
wasm.setWasmPath(`${path}/tfjs-backend-wasm-simd.wasm`);
// 2- Use Wasm SIMD + Threads bianry if supported both SIMD and Threads
// 2.1- Configure by the path to the directory where the WASM binaries are located
// wasm.setWasmPaths(`https://unpkg.com/@tensorflow/tfjs-backend-wasm@${tf.version_core}/dist/`);
// or mapping from names of WASM binaries to custom full paths specifying the locations of those binaries
// wasm.setWasmPaths({
// 'tfjs-backend-wasm.wasm': 'renamed.wasm',
// 'tfjs-backend-wasm-simd.wasm': 'renamed-simd.wasm',
// 'tfjs-backend-wasm-threaded-simd.wasm': 'renamed-threaded-simd.wasm'
// });
wasm.setWasmPaths(${prefixOrFileMap});
// 2.2- Configure threads number manually, or it will use the number of logical CPU cores as the threads count by default
wasm.setThreadsCount(n); // n can be 1, 2, ...
const tf = context.tf;
await tf.setBackend(backend);
await tf.ready();
Please refer to the setPolyfillBackend()
usage in tests for concrete examples on how to best implement backend switching for your project.
Web Machine Learning Community Group provides various Samples (GitHub repo) that make use of the WebNN API. These samples fall back to the webnn-polyfill if the browser does not have a native implementation of the WebNN API available by default.
> git clone --recurse-submodules https://github.com/webmachinelearning/webnn-polyfill
> cd webnn-polyfill & npm install
> npm run build
> npm run build-production
> npm test
> npm start
Open the web browser and navigate to http://localhost:8080/test
Default backend is CPU backend, you could change to use WebGL backend by http://localhost:8080/test?backend=webgl
,
or use Wasm backend by http://localhost:8080/test?backend=wasm
> npm run test-cts
> npm start
Open the web browser and navigate to http://localhost:8080/test/cts.html
> npm run build-docs
> npm run lint
> npm run format
> npm run dev
> npm run watch
This project is licensed under the Apache License Version 2.0.