npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@webmachinelearning/webnn-polyfill

v0.1.10

Published

WebNN API polyfill

Downloads

13

Readme

build and test deploy

WebNN Polyfill

A JavaScript implementation of the Web Neural Network API.

Backends

The implementation of this webnn-polyfill is based on TensorFlow.js that supports the following 3 backends:

Notes

  • 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.

Usage

Import the packages

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>

Set backend

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.

Samples

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.

Build and Test

Setup

> git clone --recurse-submodules https://github.com/webmachinelearning/webnn-polyfill
> cd webnn-polyfill & npm install

Build

Development build

> npm run build

Production build

> npm run build-production

Test

Run tests in node.js.

> npm test

Run tests in web browser.

> 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

Run only CTS tests in node.js.

> npm run test-cts

Run only CTS tests in web browser.

> npm start

Open the web browser and navigate to http://localhost:8080/test/cts.html

Other scripts

Build docs

> npm run build-docs

Lint

> npm run lint

Format

> npm run format

Start dev server

> npm run dev

Watch files

> npm run watch

License

This project is licensed under the Apache License Version 2.0.