Coder Social home page Coder Social logo

webmachinelearning / webnn-polyfill Goto Github PK

View Code? Open in Web Editor NEW
100.0 10.0 18.0 612.04 MB

🧠⚙️ Web Neural Network API polyfill based on TensorFlow.js

Home Page: https://www.npmjs.com/package/@webmachinelearning/webnn-polyfill

License: Apache License 2.0

JavaScript 77.95% TypeScript 11.41% HTML 0.81% Shell 0.03% Python 9.80%

webnn-polyfill's Introduction

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.

webnn-polyfill's People

Contributors

anssiko avatar brucedai avatar dependabot[bot] avatar honry avatar huningxin avatar reillyeon avatar wangli69087 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webnn-polyfill's Issues

CI failed to execute "npm run build-production --if-present"

Detail error message:

ERROR in /home/runner/work/webnn-polyfill/webnn-polyfill/src/nn/utils.ts
./src/nn/utils.ts 2:30-71
[tsl] ERROR in /home/runner/work/webnn-polyfill/webnn-polyfill/src/nn/utils.ts(2,31)
      TS2307: Cannot find module '@tensorflow/tfjs-core/src/ops/conv_util' or its corresponding type declarations.

ERROR in /home/runner/work/webnn-polyfill/webnn-polyfill/src/nn/ops/conv2d.ts
[tsl] ERROR in /home/runner/work/webnn-polyfill/webnn-polyfill/src/nn/ops/conv2d.ts(2,31)
      TS2307: Cannot find module '@tensorflow/tfjs-core/src/ops/conv_util' or its corresponding type declarations.

ERROR in /home/runner/work/webnn-polyfill/webnn-polyfill/src/nn/ops/pool2d.ts
[tsl] ERROR in /home/runner/work/webnn-polyfill/webnn-polyfill/src/nn/ops/pool2d.ts(2,31)
      TS2307: Cannot find module '@tensorflow/tfjs-core/src/ops/conv_util' or its corresponding type declarations.

[Test] Add tests for 'conv2d' with 'dilations' option

While developing DeepLab V3 MobileNet V2 example, I captured a bug when using 'conv2d' with 'dilations' option and identified it is a tfjs-webgl-backend issue, see details at tensorflow/tfjs#5293.

So we may need to add relevant tests to cover the gap, besides above checkpoint, tests from https://github.com/tensorflow/tfjs/blob/master/tfjs-core/src/ops/depthwise_conv2d_test.ts maybe good examples for making good test coverage.

Where I capture the issue?

I covert the frozen graph from http://download.tensorflow.org/models/deeplabv3_mnv2_pascal_trainval_2018_01_29.tar.gz to a tflite model, and found the issue at node of MobilenetV2/expanded_conv_14/depthwise/depthwise.

Code snippet to reproduce

    const input = tf.fill([1, 65, 65, 960], 0, 'float32');
    const weights = tf.fill([3, 3, 960, 1], 1, 'float32');
    const result = tf.depthwiseConv2d(input, weights, 1, 'same', 'NHWC', 4);

[test] Failed to test mobilenetv2 batchnorm nchw with test_data_set_0.

Base on MobileNet v2-1.0 model and test data of mobilenet/README.md, we're adding mobilenetv2 batchnorm model tests of pr #49, with input and output data of test_data_set_0, we got this AssertionError error:

AssertionError: expected false to be true
    at Module.checkValue (utils.js:37:12)

, the delta of expected data of ouput[97] 0.015684127807617188, with actual data output[97] 0.015800103545188904 is 0.00011597573757171631, it isn't satisfied with the criteria by atol 1e-5 as and rtol as 1e-3.

delta <= atol + rtol * expected

For CI pass, I skipped to test mobilenetv2 batchnorm model tests with this test_data_set_0.

@huningxin Any suggestion, thanks.

The implement of 'same-upper' autoPad isn't align with the definition in Spec.

According to the definition in Spec

With the "same-upper" option, the padding values are automatically computed such that the additional ending padding
of the spatial input dimensions would allow all of the input values in the corresponding dimension to be filtered.

the implement code as below isn't align with the definition in Spec,

          // https://github.com/webmachinelearning/webnn-polyfill/blob/master/src/nn/ops/conv2d.ts#L136
          // Calculate the explicit paddings for 'same-upper'
          if (this.autoPad_ === MLAutoPad['same-upper']) {
            padding = [[0, 0], [0, 0], [0, 0], [0, 0]];
            const outputSizes = [0, 0];
            for (let i = 0; i < 2; ++i) {
              outputSizes[i] = Math.ceil(input.shape[1 + i] / this.strides_[i]);
            }
            const totalPadding: [number, number] = [0, 0];
            for (let i = 0; i < 2; ++i) {
              totalPadding[i] = this.strides_[i] * (outputSizes[i] - 1) +
                  this.outputPadding_[i] +
                  ((filter.shape[i] - 1) * this.dilations_[i] + 1) -
                  input.shape[1 + i];
            }
            for (let i = 0; i < 2; ++i) {
              padding[i + 1][0] =
                  totalPadding[i] - Math.floor(totalPadding[i] / 2);
              padding[i + 1][1] = Math.floor(totalPadding[i] / 2);
            }

while the above code actually matches 'same-lower' definition :

The "same-lower" option is similar but padding is applied to the beginning padding of the spatial input dimensions instead
of the ending one.

Gemm only support 2D tensor

The third optional operand must be 2D tensor.

Maybe we should remove gemm scalar bias , gemm single vector bias test case, and remove 0.0 for both gemm aTranspose and gemm bTranspose.

Build fails on Linux

The root cause is that some file names have capital characters but the import statements assume lowercase. This is fine to build on Windows. But it fails on Linux.

Implement the changes/additions of ops for style transfer models

Implement the spec change of webmachinelearning/webnn#123.

The changes include:

  • Extend the conv2d operation to support transposed convolution, an essential upsample tool for encoder-decoder models.
  • Add instanceNormalization in addition to batch-normalization. Instance-normalization is a fused operation for a normalization subgraph that computes the mean and variance values per-feature instance on the fly.
  • Replace the sqrt unary operation with a more generic pow binary operation used by the normalization process.
  • Add pad operation that supports all 4 padding modes found in various frameworks.
  • Add resample operation to support both upsampling and downsampling of feature instances. This operation is used in the ONNX version of the style-transfer models.

Implement 'same-lower' autoPad option for conv2d op.

There's implement on https://github.com/webmachinelearning/webnn-polyfill/blob/master/src/nn/ops/conv2d.ts#L136

          // Calculate the explicit paddings for 'same-lower'
          if (this.autoPad_ === MLAutoPad['same-lower']) {
            padding = [[0, 0], [0, 0], [0, 0], [0, 0]];
            const outputSizes = [0, 0];
            for (let i = 0; i < 2; ++i) {
              outputSizes[i] = Math.ceil(input.shape[1 + i] / this.strides_[i]);
            }
            const totalPadding: [number, number] = [0, 0];
            for (let i = 0; i < 2; ++i) {
              totalPadding[i] = this.strides_[i] * (outputSizes[i] - 1) +
                  this.outputPadding_[i] +
                  ((filter.shape[i] - 1) * this.dilations_[i] + 1) -
                  input.shape[1 + i];
            }
            for (let i = 0; i < 2; ++i) {
              padding[i + 1][0] =
                  totalPadding[i] - Math.floor(totalPadding[i] / 2);
              padding[i + 1][1] = Math.floor(totalPadding[i] / 2);
            }
          }

while below two blocks don't implement to support 'same-lower':

https://github.com/webmachinelearning/webnn-polyfill/blob/master/src/nn/ops/conv2d.ts#L171

          if (this.autoPad_ === MLAutoPad['same-upper']) {
            padding = 'same';
          } else {
            throw new Error(
                'tf.depthwiseConv2d only supports the same-upper auto pad.');
          }

https://github.com/webmachinelearning/webnn-polyfill/blob/master/src/nn/ops/conv2d.ts#L201

        if (this.autoPad_ === MLAutoPad['same-upper']) {
          padding = 'same';
          this.outputSizes_ = [
            input.shape[1] * this.strides_[0],
            input.shape[2] * this.strides_[1],
          ];
        } else {
          throw new Error(
              'tf.conv2dTranspose only supports the same-upper auto pad.');
        }

I think these throw error blocks also need to implement 'same-lower' likes https://github.com/webmachinelearning/webnn-polyfill/blob/master/src/nn/ops/conv2d.ts#L136.

@huningxin What's your opinion, thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.