From b51886644c3f59d1cf5ffc1555566dfa36d14a3b Mon Sep 17 00:00:00 2001 From: Kalleby Santos <105971119+kallebysantos@users.noreply.github.com> Date: Mon, 30 Sep 2024 17:28:42 +0100 Subject: [PATCH] Support to load ONNX APIs based on JS runtime (#947) * Add support for global `onnxruntime` - Allows custom JS runtimes to expose their own ONNX API. * Update onnx.js --------- Co-authored-by: Joshua Lochner --- src/backends/onnx.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/backends/onnx.js b/src/backends/onnx.js index 26d771b4a..dd3c15168 100644 --- a/src/backends/onnx.js +++ b/src/backends/onnx.js @@ -59,7 +59,13 @@ const supportedDevices = []; /** @type {ONNXExecutionProviders[]} */ let defaultDevices; let ONNX; -if (apis.IS_NODE_ENV) { +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]; + +} else if (apis.IS_NODE_ENV) { ONNX = ONNX_NODE.default ?? ONNX_NODE; // Updated as of ONNX Runtime 1.18.0