mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-06-25 02:50:42 +00:00
### Description <!-- Describe your changes. --> [ORT 1.17.0 Release] Cherry pick 1st round PR authors please take a look, and let me know if there are any questions about the changes or approve accordingly. ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> --------- Co-authored-by: wejoncy <wejoncy@163.com> Co-authored-by: Xavier Dupré <xadupre@users.noreply.github.com> Co-authored-by: Yulong Wang <7679871+fs-eire@users.noreply.github.com> Co-authored-by: Hector Li <hecli@microsoft.com> Co-authored-by: luoyu-intel <yu.luo@intel.com> Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com> Co-authored-by: Chi Lo <54722500+chilo-ms@users.noreply.github.com> Co-authored-by: Ye Wang <52801275+wangyems@users.noreply.github.com> Co-authored-by: Adrian Lizarraga <adlizarraga@microsoft.com> Co-authored-by: snadampal <87143774+snadampal@users.noreply.github.com> Co-authored-by: Tianlei Wu <tlwu@microsoft.com> Co-authored-by: Heflin Stephen Raj <heflinstephen03@gmail.com> Co-authored-by: Yifan Li <109183385+yf711@users.noreply.github.com> Co-authored-by: Yufeng Li <liyufeng1987@gmail.com> Co-authored-by: Changming Sun <chasun@microsoft.com>
74 lines
2.9 KiB
TypeScript
74 lines
2.9 KiB
TypeScript
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT License.
|
|
|
|
import {cpus} from 'node:os';
|
|
import {Backend, env, InferenceSession, InferenceSessionHandler} from 'onnxruntime-common';
|
|
|
|
import {initializeOrtEp, initializeWebAssemblyAndOrtRuntime} from './wasm/proxy-wrapper';
|
|
import {OnnxruntimeWebAssemblySessionHandler} from './wasm/session-handler-inference';
|
|
|
|
/**
|
|
* This function initializes all flags for WebAssembly.
|
|
*
|
|
* Those flags are accessible from `ort.env.wasm`. Users are allow to set those flags before the first inference session
|
|
* being created, to override default value.
|
|
*/
|
|
export const initializeFlags = (): void => {
|
|
if (typeof env.wasm.initTimeout !== 'number' || env.wasm.initTimeout < 0) {
|
|
env.wasm.initTimeout = 0;
|
|
}
|
|
|
|
if (typeof env.wasm.simd !== 'boolean') {
|
|
env.wasm.simd = true;
|
|
}
|
|
|
|
if (typeof env.wasm.proxy !== 'boolean') {
|
|
env.wasm.proxy = false;
|
|
}
|
|
|
|
if (typeof env.wasm.trace !== 'boolean') {
|
|
env.wasm.trace = false;
|
|
}
|
|
|
|
if (typeof env.wasm.numThreads !== 'number' || !Number.isInteger(env.wasm.numThreads) || env.wasm.numThreads <= 0) {
|
|
// Web: when crossOriginIsolated is false, SharedArrayBuffer is not available so WebAssembly threads will not work.
|
|
// Node.js: onnxruntime-web does not support multi-threads in Node.js.
|
|
if ((typeof self !== 'undefined' && !self.crossOriginIsolated) ||
|
|
(typeof process !== 'undefined' && process.versions && process.versions.node)) {
|
|
env.wasm.numThreads = 1;
|
|
}
|
|
const numCpuLogicalCores = typeof navigator === 'undefined' ? cpus().length : navigator.hardwareConcurrency;
|
|
env.wasm.numThreads = Math.min(4, Math.ceil((numCpuLogicalCores || 1) / 2));
|
|
}
|
|
};
|
|
|
|
export class OnnxruntimeWebAssemblyBackend implements Backend {
|
|
/**
|
|
* This function initializes the WebAssembly backend.
|
|
*
|
|
* This function will be called only once for each backend name. It will be called the first time when
|
|
* `ort.InferenceSession.create()` is called with a registered backend name.
|
|
*
|
|
* @param backendName - the registered backend name.
|
|
*/
|
|
async init(backendName: string): Promise<void> {
|
|
// populate wasm flags
|
|
initializeFlags();
|
|
|
|
// init wasm
|
|
await initializeWebAssemblyAndOrtRuntime();
|
|
|
|
// performe EP specific initialization
|
|
await initializeOrtEp(backendName);
|
|
}
|
|
createInferenceSessionHandler(path: string, options?: InferenceSession.SessionOptions):
|
|
Promise<InferenceSessionHandler>;
|
|
createInferenceSessionHandler(buffer: Uint8Array, options?: InferenceSession.SessionOptions):
|
|
Promise<InferenceSessionHandler>;
|
|
async createInferenceSessionHandler(pathOrBuffer: string|Uint8Array, options?: InferenceSession.SessionOptions):
|
|
Promise<InferenceSessionHandler> {
|
|
const handler = new OnnxruntimeWebAssemblySessionHandler();
|
|
await handler.loadModel(pathOrBuffer, options);
|
|
return Promise.resolve(handler);
|
|
}
|
|
}
|