Skip to content

Commit 30b1ccf

Browse files
committed
Add 'Exposed Runtime' to api mappings
- Add global variable to check if `IS_EXPOSED_RUNTIME_ENV` -> true if Js exposes their own custom runtime. - Applying 'auto' device as default for exposed runtime environment.
1 parent c337c3b commit 30b1ccf

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/backends/onnx.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ const supportedDevices = [];
5959
/** @type {ONNXExecutionProviders[]} */
6060
let defaultDevices;
6161
let ONNX;
62-
const ORT_SYMBOL = Symbol.for('onnxruntime');
6362

64-
if (ORT_SYMBOL in globalThis) {
65-
// If the JS runtime exposes their own ONNX runtime, use it
66-
ONNX = globalThis[ORT_SYMBOL];
63+
if (apis.IS_EXPOSED_RUNTIME_ENV) {
64+
// If the JS runtime exposes their own ONNX runtime, use it
65+
ONNX = globalThis[apis.EXPOSED_RUNTIME_SYMBOL];
66+
defaultDevices = ['auto'];
6767

6868
} else if (apis.IS_NODE_ENV) {
6969
ONNX = ONNX_NODE.default ?? ONNX_NODE;

src/env.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ const IS_WEB_CACHE_AVAILABLE = IS_BROWSER_ENV && 'caches' in self;
3535
const IS_WEBGPU_AVAILABLE = typeof navigator !== 'undefined' && 'gpu' in navigator;
3636
const IS_WEBNN_AVAILABLE = typeof navigator !== 'undefined' && 'ml' in navigator;
3737

38+
const EXPOSED_RUNTIME_SYMBOL = Symbol.for('onnxruntime');
39+
const IS_EXPOSED_RUNTIME_ENV = EXPOSED_RUNTIME_SYMBOL in globalThis;
40+
3841
const IS_PROCESS_AVAILABLE = typeof process !== 'undefined';
3942
const IS_NODE_ENV = IS_PROCESS_AVAILABLE && process?.release?.name === 'node';
4043
const IS_FS_AVAILABLE = !isEmpty(fs);
@@ -59,6 +62,12 @@ export const apis = Object.freeze({
5962
/** Whether the WebNN API is available */
6063
IS_WEBNN_AVAILABLE,
6164

65+
/** Symbol from JS environment that exposes their own ONNX runtime */
66+
EXPOSED_RUNTIME_SYMBOL,
67+
68+
/** Whether we are running in a JS environment that exposes their own ONNX runtime */
69+
IS_EXPOSED_RUNTIME_ENV,
70+
6271
/** Whether the Node.js process API is available */
6372
IS_PROCESS_AVAILABLE,
6473

src/models.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
159159

160160
// If the device is not specified, we use the default (supported) execution providers.
161161
const selectedDevice = /** @type {import("./utils/devices.js").DeviceType} */(
162-
device ?? (apis.IS_NODE_ENV ? 'cpu' : 'wasm')
162+
device ?? (
163+
apis.IS_EXPOSED_RUNTIME_ENV ? 'auto' : (
164+
apis.IS_NODE_ENV ? 'cpu' : 'wasm'
165+
))
163166
);
164167
const executionProviders = deviceToExecutionProviders(selectedDevice);
165168

0 commit comments

Comments
 (0)