onnxruntime/js/README.md
harshithapv 8c0c25c768
cherry picked commits for rel-1.8.1 (#8076)
* Cache initializers and avoid device check ot end of forward (#7905)

* ATenOp Enhancement (#7725)

* config parser, default argument values

* ut

* win build

* maxpool2d

* fix win build

* fix build

* unfold atenop

* Update CMakeLists.txt for openvino EP (#7980)

* Add SoftmaxCrossEntropyLossInternal to Support Dynamic ignore_index Input (#7899)

* add SoftmaxCrossEntropyLossInternal

* bugfix and ut

* fix ut

* fix ut

* support torch1.8.1

* function body for nll_loss_internal

* Override ORTModule named_modules to support extra arg (#7954)

* add missing provider_options.h in packages (#7995)

* consolidate copy binary script for gpu/trt tarball package

* add provider_options.h

* add provider_options.h

* Add cuda provides files (#8002)

* Save module output for backward if needed (#8010)

* Save module output for backward if needed

* Make logic in InsertCastTransformer around forcing a node to fp32 more precise. (#8018)

* Address #7981

Reworked the logic around forcing a node to run on fp32 even if it was supported on fp16.

The github issue had multiple factors. In ORT 1.8 we remove Identity nodes that produce graph outputs as they're not needed. That resulted in a Loop node no longer having output nodes (it produces graph outputs instead), which meant the check in IsSingleInputNodeFloat16Node returned true as there was no longer a downstream Identity node processing fp16 data.

We shouldn't only force a node to fp32 in very specific circumstances, and the changes hopefully check for those more precisely.

* Fix Memory Leak from DlpackToOrtValue (#8029)

* Update DirectML EP changes from DmlDev as of 2021-06-07 (#7987)

* Merged PR 6093117: Fix test_DynamicQuantizedLinear_max_adjusted_expanded by allowing Identity operator to run on non-float inputs

Motivation:
As part of the OnnxConformance Backend tests, DynamicQuantizedLinear_max_adjusted_expanded is failing.

Root Cause:
- The test model has `Identity` operator as one of the node. The input of this node is of non-float data type.
- In DML, `Identity` operator is registered as operator which requires floating input.
- As per `DirectMLSchema.h`, support for non-float input has been added for `Identity` operator in DML but the same has not been reflected in the `OperatorRegistration.cpp`.

Changes:
- Removed all traces of the requiresFloatFormatsForGraph flag from it's definition and usage. This flag was only used for Identity and it's related operator.
- Added null check for the graphOutput nodeArg in GraphDescBuilder.cpp to stop the crash of the test.

Related work items: #33076298

* Merged PR 6103324: Remove usage of non-generic error code (FWP_E_NULL_POINTER)

Motivation:
Addressing Dwayne comment on the previous PR. [Ref: [6093117](https://dev.azure.com/microsoft/WindowsAI/_git/onnxruntime/pullrequest/6093117?discussionId=44292162&path=%2Fonnxruntime%2Fcore%2Fproviders%2Fdml%2FDmlExecutionProvider%2Fsrc%2FGraphPartitioner.cpp)]

Changes:
Inside the DML EP, we should not use some other platform specific error codes. Instead we should a appropriate generic error code.

Related work items: #33076298

Co-authored-by: Sumit Agarwal <sumitagarwal@microsoft.com>

* [js/react_native] Use a mobile ORT instead of a full ORT (#8042)

* Change full ort to mobile ort

* Update Android example to load mobile ort

* Change the format of test models to ort

* update ios to use mobile ort

* revise README

* use onnxruntime-mobile-c CocoaPods in a npm package

* fix PATH addition in windows

should set PATH, not add to the tail the copy of PATH

* Reduce Kernel Optimization (#8067)

* reduce optimization

* bug fix

* add a check

* add ut

* refactor

* add ut cases for keepdims=true

Co-authored-by: baijumeswani <bmeswani@microsoft.com>
Co-authored-by: Vincent Wang <wangwchpku@outlook.com>
Co-authored-by: Changming Sun <chasun@microsoft.com>
Co-authored-by: George Wu <jywu@microsoft.com>
Co-authored-by: Ryan Hill <38674843+RyanUnderhill@users.noreply.github.com>
Co-authored-by: Sherlock <baihan.huang@gmail.com>
Co-authored-by: Scott McKay <skottmckay@gmail.com>
Co-authored-by: sumitsays <sumitagarwal330@gmail.com>
Co-authored-by: Sumit Agarwal <sumitagarwal@microsoft.com>
Co-authored-by: Sunghoon <35605090+hanbitmyths@users.noreply.github.com>
Co-authored-by: iperov <lepersorium@gmail.com>
2021-06-18 07:44:55 -07:00

15 KiB

ONNX Runtime JavaScript API

This directory contains multiple NPM projects:

Development

This folder contains a .vscode folder for Visual Studio Code workspace configs. Using VSCode to open this folder will allow code-formatting and linting features on typescript and C/C++ source code inside this folder. Following files are used for code-formatting and linting features for developers:

  • .vscode/**
  • package.json
  • packages-lock.json
  • .eslintrc.js
  • .clang-format

Please follow the steps described below to setup development environment.

Prerequisites

Setup TypeScript development environment

In <ORT_ROOT>/js, run:

npm ci

This will install Clang-format and ESLint for code-formatting and linting features. This is a one-time setup unless a git clean is performed or folder <ORT_ROOT>/js/node_modules is removed manually.

Using VSCode:

Use VSCode to open folder <ORT_ROOT>/js.

Make sure to open the correct folder to allow VSCode to load workspace configuration. Otherwise typescript and code formatter may not work as expected.

To populate typescript type declarations, in each project folder, run npm ci.

Run code formatter and linter manually

In <ORT_ROOT>/js, use npm run lint to run ESLint , and use npm run format to run clang-format.

onnxruntime-common

language: typescript

dependency:

folder: <ORT_ROOT>/js/common

This project is designed to include all "common" code, which are pure javascript that can run in both Node.js and browsers.

Requirements

Node.js v12+ (recommended v14+)

Build

Use following command in folder <ORT_ROOT>/js/common to install NPM packages, build typescript files and generate bundles:

npm ci

Distribution

It should be able to consumed by both from projects that uses NPM packages (through a Node.js folder structure of node_modules folder that generated by npm install onnxruntime-common) and from a CDN service that serves a .min.js bundle file.

Features

Following features are included in onnxruntime-common:

  • InferenceSession interfaces
  • Tensor/OnnxValue interfaces, implementation and a set of utility functions
  • Backend interfaces and a set of functions for backend registration

onnxruntime-node

language: typescript/C++

dependency: onnxruntime-common, ONNXRuntime.dll

folder: <ORT_ROOT>/js/node

This project is designed to be used as a NPM package to enable Node.js users to consume ONNX Runtime via Node.js binding, in Node.js or any Node.js compatible environment.

Requirements

Node.js v12+ (recommended v14+)

Build

Build ONNX Runtime and Node.js binding

Follow instructions for building ONNX Runtime Node.js binding

Build Node.js binding only

Use following command in folder <ORT_ROOT>/js/node to install NPM packages and build typescript files:

npm ci

This will download the latest pre-built ONNX Runtime binaries for the current platform.

Distribution

It should be able to consumed by from projects that uses NPM packages (through a Node.js folder structure of node_modules folder that generated by npm install onnxruntime-node).

onnxruntime-web

language: typescript

dependency: onnxruntime-common, ONNXRuntime WebAssembly

folder: <ORT_ROOT>/js/web

This project is a library for running ONNX models on browsers. It is the successor of ONNX.js.

Build

  1. Install NPM packages

    1. in <ORT_ROOT>/js/, run npm ci.
    2. in <ORT_ROOT>/js/common/, run npm ci.
    3. in <ORT_ROOT>/js/web/, run npm ci.
  2. Follow instructions for building ONNX Runtime WebAssembly. (TODO: document is not ready. we are working on it. Please see steps described as below.)

    in <ORT_ROOT>/, run one of the following commands to build WebAssembly:

    # In windows, use 'build' to replace './build.sh'
    
    # The following command build debug.
    ./build.sh --build_wasm
    
    # The following command build debug with debug info.
    ./build.sh --build_wasm --skip_tests --enable_wasm_debug_info
    
    # The following command build release.
    ./build.sh --config Release --build_wasm --skip_tests --disable_wasm_exception_catching --disable_rtti
    

    To build with multi-thread support, append flag --enable_wasm_threads to the command. Make sure to build both single-thread and multi-thread before next step.

    NOTE: You can also find latest build artifacts on Windows WebAssembly CI Pipeline. Choose any build for master branch, download artifacts "Release_ort-wasm" and "Release_ort-wasm-threaded" and unzip.

  3. Copy following files from build output folder to <ORT_ROOT>/js/web/dist/:

    • ort-wasm.wasm
    • ort-wasm-threaded.wasm (build with flag '--enable_wasm_threads')
  4. Copy following files from build output folder to <ORT_ROOT>/js/web/lib/wasm/binding/:

    • ort-wasm.js
    • ort-wasm-threaded.js (build with flag '--enable_wasm_threads')
    • ort-wasm-threaded.worker.js (build with flag '--enable_wasm_threads')
  5. Use following command in folder <ORT_ROOT>/js/web to build:

    npm run build
    

Test

We use command npm test (test runner) and npm run test:e2e (E2E test) for tests in ONNXRuntime Web.

test runner

In folder <ORT_ROOT>/js/web,

  • Run npm test -- --help for a full CLI instruction.
  • Run npm test -- <your-args> --debug to run one or more test cases.

There are multiple levels of tests for ONNXRuntime Web:

  • unit test: tests for individual components written in TypeScript. Launch unit test by:

    npm test -- unittest
    
  • model test: run a single model. The model folder should contains one .onnx model file and one or more folders for test cases, each folder contains several input**.pb and output**.pb as test data. Launch model test by:

    npm test -- model <model_folder>
    
  • op test: test a single operator. An op test is described in a .jsonc file which specify the operator type, its attributes and one or more test case(s), each includes a list of expected input tensor(s) and output tensor(s). The .jsonc file is located at <ORT_ROOT>/js/web/test/data/ops. Launch op test by:

    npm test -- op <file_name>
    
  • suite test: suite test includes unit test, a list of model tests and op tests. Launch suite test by:

    npm test
    

E2E test

E2E test is for testing end-to-end package consuming. In this test, NPM packages for onnxruntime-common and onnxruntime-web are generated and a clean folder is used for installing packages. Then a simple mocha test is performed to make sure package can be consumed correctly.

To launch E2E test:

npm run test:e2e

Debugging

Debugging TypeScript on Desktop/Chrome

To debug the code from test-runner on Chrome:

  • Launch npm test -- <your_args> --debug. It opens an instance of Chrome browser.
  • In the open Chrome browser, click the DEBUG button on the top-right of the page.
  • In VSCode, click [side bar]->Run and Debug->select [Attach to Chrome]->click [Start Debugging] to attach.
  • put breakpoints in source code, and Refresh the page to reload.

Debugging TypeScript on iOS/Safari

To debug on an Apple iOS device, please refer to the following steps:

  • install RemoteDebug iOS WebKit Adapter by following its instructions.
  • launch the adapter in commandline: remotedebug_ios_webkit_adapter --port=9000.
  • in VSCode, select debug configuration Remote Browser via Webkit Adaptor.
  • follow the steps above to debug.

Debugging TypeScript on Android/Chrome

To debug on an Android device, please refer to the following steps:

  • Install Android SDK Platform Tools and make sure adb is ready to use.
  • Follow instructions in Remote Debugging on Android to launch adb. Make sure to use port 9000 so that the existing debug configuration works.
  • in VSCode, select debug configuration Remote Browser via Webkit Adaptor.
  • follow the steps above to debug.

Debugging C/C++ for ONNX Runtime WebAssembly

To debug C/C++ code for ONNX Runtime WebAssembly, you need to build ONNX Runtime with debug info (see Build).

Currently debugging C/C++ code in WebAssembly is not supported in VSCode yet. Please follow this instruction to debug in browser devtool using extension C/C++ DevTools Support (DWARF).

Generating Document

Use command npm run build:doc to generate the latest documents.

Distribution

It should be able to consumed by both from projects that uses NPM packages (through a Node.js folder structure of node_modules folder that generated by npm install onnxruntime-web) and from a CDN service that serves a ort.min.js file and one or multiple .wasm file(s).

onnxruntime-react-native

language: typescript, java, objective-c

dependency: onnxruntime-common

folder: <ORT_ROOT>/js/react_native

This project provides an ONNX Runtime React Native JavaScript library to run ONNX models on React Native Android and iOS app.

Requirements

  • Yarn
  • Android SDK and NDK, which can be installed via Android Studio or sdkmanager command line tool
  • A Mac computer with the latest macOS
  • Xcode
  • CMake
  • Python 3

Models with ORT format

By default, ONNX Runtime React Native leverages ONNX Runtime Mobile package with ORT format. Follow the instruciton to covert ONNX model to ORT format.

Build

  1. Install NPM packages for ONNX Runtime common JavaScript library and required React Native JavaScript libraries

    • in <ORT_ROOT>/js/, run npm ci.
    • in <ORT_ROOT>/js/common/, run npm ci.
    • in <ORT_ROOT>/js/react_native/, run yarn.
  2. Build Android ONNX Runtime package

    1. To use a published Android ONNX Runtime Mobile package from Maven, go to step 5.

    2. Set up an Android build environment referring to instruction

    3. In <ORT_ROOT>, run this python script to build ONNX Runtime Android archive file. In windows, this requires an admin account to build. To build a model specific package with reduced size, refer to instruction.

      python tools/ci_build/github/android/build_aar_package.py tools/ci_build/github/android/default_mobile_aar_build_settings.json --config MinSizeRel --android_sdk_path <ANDROID_SDK_PATH> --android_ndk_path <ANDROID_NDK_PATH> --build_dir <BUILD_DIRECTORY> --include_ops_by_config tools/ci_build/github/android/mobile_package.required_operators.config
      
    4. Copy <BUILD_DIRECTORY>/aar_out/MinSizeRel/com/microsoft/onnxruntime/onnxruntime-mobile/<version>/onnxruntime-mobile-<version>.aar into <ORT_ROOT>/js/react_native/android/libs directory.

    5. Modify Onnxruntime_mobileVersion property in <ORT_ROOT>/js/react_native/android/build.properties to consume a locally built package or a newly published package from Maven.

    6. To verify, open Android Emulator and run this command from <ORT_ROOT>/js/react_native/android

      adb shell am instrument -w ai.onnxruntime.react_native.test/androidx.test.runner.AndroidJUnitRunner
      
  3. Build iOS ONNX Runtime package

    1. To use a published c/c++ ONNX Runtime Mobile package from CocoaPods, skip all steps below.

    2. Set up iOS build environment referring to instruction.

    3. Build a fat ONNX Runtime Mobile Framework for iOS and iOS simulator from <ORT_ROOT> using this command,

      python tools/ci_build/github/apple/build_ios_framework.py tools/ci_build/github/apple/default_mobile_ios_framework_build_settings.json --config MinSizeRel --include_ops_by_config tools/ci_build/github/android/mobile_package.required_operators.config
      

      It creates onnxruntime.framework in build/iOS_framework/framework_out directory. Create an archive file of onnxruntime.framework directory as follows and copy to <ORT_ROOT>/js/react_native/local_pods directory.

      zip -r onnxruntime-mobile-c.zip onnxruntime.framework
      
    4. To verify, open iOS Simulator and run this command from <ORT_ROOT>/js/react_native/ios. Change a destination to specify a running iOS Simulator.

      pod install
      xcodebuild test -workspace OnnxruntimeModule.xcworkspace -scheme OnnxruntimeModuleTest -destination 'platform=iOS Simulator,name=iPhone 11,OS=14.5'
      
  4. Test an example for Android and iOS. In Windows, open Android Emulator first. From <ORT_ROOT>/js/react_native

    yarn bootstrap
    yarn example ios
    yarn example android
    

NPM Packaging

  1. Update a version using npm verison <version> from <ORT_ROOT>/js/react_native folder. If it's for a dev, use npm version <version>-dev.<subversion>

  2. Modify Onnxruntime_mobileVersion property in <ORT_ROOT>/js/react_native/android/build.properties to update ONNX Runtime Android package version.

  3. Run yarn prepack to change onnxruntime-common to point to a published npm package

  4. Run npm pack and verify NPM package contents

  5. Run npm publish <tgz> --dry-run to see how it's going to be published

  6. Run npm publish <tgz> to publish to npmjs. If it's for a dev, add flag --tag dev.

Distribution

It should be able to consumed by React Native projects that uses Yarn packages through yarn add onnxruntime-react-native.