Skip to content

Commit

Permalink
Ensuring that 'Exposed ORT' implements necessary functions
Browse files Browse the repository at this point in the history
- Adding checkings for 'Tensor' and 'InferenceSession' members of the
exposed custom ort.
  • Loading branch information
kallebysantos committed Dec 8, 2024
1 parent 7b7175e commit 32dc19d
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/backends/onnx.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,24 @@ const supportedDevices = [];
let defaultDevices;
let ONNX;

// If the JS runtime exposes their own ONNX runtime, use it
if (apis.IS_EXPOSED_RUNTIME_ENV) {
// If the JS runtime exposes their own ONNX runtime, use it
ONNX = globalThis[apis.EXPOSED_RUNTIME_SYMBOL];
const onnxruntime = globalThis[apis.EXPOSED_RUNTIME_SYMBOL];

// ensure that the runtime implements the necessary functions
// consider use array map if need to check more required members.
if (!Object.hasOwn(onnxruntime, 'Tensor')) {
throw new Error(`Invalid "globalThis[${String(apis.EXPOSED_RUNTIME_SYMBOL)}]" definition. Missing required exported member "Tensor".`)
}

if (!Object.hasOwn(onnxruntime, 'InferenceSession')) {
throw new Error(`Invalid "globalThis[${String(apis.EXPOSED_RUNTIME_SYMBOL)}]" definition. Missing required exported member "InferenceSession".`)
}
if(!Object.hasOwn(onnxruntime?.InferenceSession, 'create')) {
throw new Error(`Invalid "globalThis[${String(apis.EXPOSED_RUNTIME_SYMBOL)}].InferenceSession" definition. Missing required exported member "InferenceSession.create".`)
}

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

Expand Down

0 comments on commit 32dc19d

Please sign in to comment.