Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements on Exposed ORT support #976

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -159,7 +159,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