onnxruntime/js/common
Yulong Wang 35697d2421
[js/webnn] update API of session options for WebNN (#20816)
### Description

This PR is an API-only change to address the requirements being
discussed in #20729.

There are multiple ways that users may create an ORT session by
specifying the session options differently.

All the code snippet below will use the variable `webnnOptions` as this:
```js
const myWebnnSession = await ort.InferenceSession.create('./model.onnx', {
   executionProviders: [
     webnnOptions
   ]
});
```

### The old way (backward-compatibility)

```js
// all-default, name only
const webnnOptions_0 = 'webnn';

// all-default, properties omitted
const webnnOptions_1 = { name: 'webnn' };

// partial
const webnnOptions_2 = {
  name: 'webnn',
  deviceType: 'cpu'
};

// full
const webnnOptions_3 = {
  name: 'webnn',
  deviceType: 'gpu',
  numThreads: 1,
  powerPreference: 'high-performance'
};
```

### The new way (specify with MLContext)

```js
// options to create MLcontext
const options = {
  deviceType: 'gpu',
  powerPreference: 'high-performance'
};

const myMlContext = await navigator.ml.createContext(options);

// options for session options
const webnnOptions = {
  name: 'webnn',
  context: myMlContext,
  ...options
};
```

This should throw (because no deviceType is specified):
```js
const myMlContext = await navigator.ml.createContext({ ... });
const webnnOptions = {
  name: 'webnn',
  context: myMlContext
};
```

### Interop with WebGPU
```js
// get WebGPU device
const adaptor = await navigator.gpu.requestAdapter({ ... });
const device = await adaptor.requestDevice({ ... });

// set WebGPU adaptor and device
ort.env.webgpu.adaptor = adaptor;
ort.env.webgpu.device = device;

const myMlContext = await navigator.ml.createContext(device);
const webnnOptions = {
  name: 'webnn',
  context: myMlContext,
  gpuDevice: device
};
```

This should throw (because cannot specify both gpu device and MLContext
option at the same time):
```js
const webnnOptions = {
  name: 'webnn',
  context: myMlContext,
  gpuDevice: device,
  deviceType: 'gpu'
};
```
2024-05-31 03:25:14 -07:00
..
lib [js/webnn] update API of session options for WebNN (#20816) 2024-05-31 03:25:14 -07:00
test [js/common] upgrade tsc in common from 4.9.5 to 5.2.2 (#19317) 2024-02-20 17:33:37 -08:00
.gitignore [js/common] a few fixes/revises to onnxruntime-common (#16853) 2023-08-01 11:17:39 -07:00
.npmignore [js/common] a few fixes/revises to onnxruntime-common (#16853) 2023-08-01 11:17:39 -07:00
build.js [js] upgrade JS shared dev dependencies (#17831) 2023-10-10 17:44:39 -07:00
package-lock.json Bump up version in main from 1.18.0 to 1.19.0 (#20489) 2024-04-29 20:21:41 -07:00
package.json Bump up version in main from 1.18.0 to 1.19.0 (#20489) 2024-04-29 20:21:41 -07:00
README.md Replace 'master' branch ref to 'main' in the code (#12547) 2022-08-22 10:48:12 -07:00
tsconfig.json [js/web] fix typescript type check (#18343) 2023-11-10 16:03:38 -08:00
typedoc.json [js] enable formatter for more file types (#16888) 2023-07-28 15:46:58 -07:00
webpack.config.js [js/common] allow import onnxruntime-common as ESM and CJS (#15772) 2023-06-12 12:05:11 -07:00

ONNX Runtime JavaScript API

ONNX Runtime JavaScript API is a unified API for all JavaScript usages. It's dependency of the following NPM packages:

  • onnxruntime-node
  • onnxruntime-web
  • onnxruntime-react-native

This package (onnxruntime-common) is not designed for using directly. Please consider to install one of the NPM packages above according to target platform.

License

License information can be found here.