Skip to content

Commit

Permalink
Add 'Exposed Runtime' to api mappings
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
kallebysantos committed Dec 3, 2024
1 parent 3d9b9e6 commit 9994c75
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/backends/onnx.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ const supportedDevices = [];
/** @type {ONNXExecutionProviders[]} */
let defaultDevices;
let ONNX;
const ORT_SYMBOL = Symbol.for('onnxruntime');

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

} else if (apis.IS_NODE_ENV) {
ONNX = ONNX_NODE.default ?? ONNX_NODE;
Expand Down
9 changes: 9 additions & 0 deletions src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const IS_WEB_CACHE_AVAILABLE = IS_BROWSER_ENV && 'caches' in self;
const IS_WEBGPU_AVAILABLE = typeof navigator !== 'undefined' && 'gpu' in navigator;
const IS_WEBNN_AVAILABLE = typeof navigator !== 'undefined' && 'ml' in navigator;

const EXPOSED_RUNTIME_SYMBOL = Symbol.for('onnxruntime');
const IS_EXPOSED_RUNTIME_ENV = EXPOSED_RUNTIME_SYMBOL in globalThis;

const IS_PROCESS_AVAILABLE = typeof process !== 'undefined';
const IS_NODE_ENV = IS_PROCESS_AVAILABLE && process?.release?.name === 'node';
const IS_FS_AVAILABLE = !isEmpty(fs);
Expand All @@ -59,6 +62,12 @@ export const apis = Object.freeze({
/** Whether the WebNN API is available */
IS_WEBNN_AVAILABLE,

/** Symbol from JS environment that exposes their own ONNX runtime */
EXPOSED_RUNTIME_SYMBOL,

/** Whether we are running in a JS environment that exposes their own ONNX runtime */
IS_EXPOSED_RUNTIME_ENV,

/** Whether the Node.js process API is available */
IS_PROCESS_AVAILABLE,

Expand Down
5 changes: 4 additions & 1 deletion src/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {

// If the device is not specified, we use the default (supported) execution providers.
const selectedDevice = /** @type {import("./utils/devices.js").DeviceType} */(
device ?? (apis.IS_NODE_ENV ? 'cpu' : 'wasm')
device ?? (
apis.IS_EXPOSED_RUNTIME_ENV ? 'auto' : (
apis.IS_NODE_ENV ? 'cpu' : 'wasm'
))
);
const executionProviders = deviceToExecutionProviders(selectedDevice);

Expand Down

0 comments on commit 9994c75

Please sign in to comment.