onnxruntime
101 строка · 3.9 Кб
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4import { Backend, env, InferenceSession, InferenceSessionHandler } from 'onnxruntime-common';
5
6import { initializeOrtEp, initializeWebAssemblyAndOrtRuntime } from './wasm/proxy-wrapper';
7import { OnnxruntimeWebAssemblySessionHandler } from './wasm/session-handler-inference';
8import { scriptSrc } from './wasm/wasm-utils-import';
9
10/**
11* This function initializes all flags for WebAssembly.
12*
13* Those flags are accessible from `ort.env.wasm`. Users are allow to set those flags before the first inference session
14* being created, to override default value.
15*/
16export const initializeFlags = (): void => {
17if (typeof env.wasm.initTimeout !== 'number' || env.wasm.initTimeout < 0) {
18env.wasm.initTimeout = 0;
19}
20
21if (env.wasm.simd === false) {
22// eslint-disable-next-line no-console
23console.warn(
24'Deprecated property "env.wasm.simd" is set to false. ' +
25'non-SIMD build is no longer provided, and this setting will be ignored.',
26);
27}
28
29if (typeof env.wasm.proxy !== 'boolean') {
30env.wasm.proxy = false;
31}
32
33if (typeof env.wasm.trace !== 'boolean') {
34env.wasm.trace = false;
35}
36
37if (typeof env.wasm.numThreads !== 'number' || !Number.isInteger(env.wasm.numThreads) || env.wasm.numThreads <= 0) {
38// The following logic only applies when `ort.env.wasm.numThreads` is not set by user. We will always honor user's
39// setting if it is provided.
40
41// Browser: when crossOriginIsolated is false, SharedArrayBuffer is not available so WebAssembly threads will not
42// work. In this case, we will set numThreads to 1.
43//
44// There is an exception: when the browser is configured to force-enable SharedArrayBuffer (e.g. Chromuim with
45// --enable-features=SharedArrayBuffer), it is possible that `self.crossOriginIsolated` is false and
46// SharedArrayBuffer is available at the same time. This is usually for testing. In this case, we will still set
47// numThreads to 1 here. If we want to enable multi-threading in test, we should set `ort.env.wasm.numThreads` to a
48// value greater than 1.
49if (typeof self !== 'undefined' && !self.crossOriginIsolated) {
50env.wasm.numThreads = 1;
51} else {
52const numCpuLogicalCores =
53typeof navigator === 'undefined' ? require('node:os').cpus().length : navigator.hardwareConcurrency;
54env.wasm.numThreads = Math.min(4, Math.ceil((numCpuLogicalCores || 1) / 2));
55}
56}
57
58if (!BUILD_DEFS.DISABLE_DYNAMIC_IMPORT) {
59// overwrite wasm paths override if not set
60if (env.wasm.wasmPaths === undefined && scriptSrc && scriptSrc.indexOf('blob:') !== 0) {
61env.wasm.wasmPaths = scriptSrc.substring(0, scriptSrc.lastIndexOf('/') + 1);
62}
63}
64};
65
66export class OnnxruntimeWebAssemblyBackend implements Backend {
67/**
68* This function initializes the WebAssembly backend.
69*
70* This function will be called only once for each backend name. It will be called the first time when
71* `ort.InferenceSession.create()` is called with a registered backend name.
72*
73* @param backendName - the registered backend name.
74*/
75async init(backendName: string): Promise<void> {
76// populate wasm flags
77initializeFlags();
78
79// init wasm
80await initializeWebAssemblyAndOrtRuntime();
81
82// performe EP specific initialization
83await initializeOrtEp(backendName);
84}
85createInferenceSessionHandler(
86path: string,
87options?: InferenceSession.SessionOptions,
88): Promise<InferenceSessionHandler>;
89createInferenceSessionHandler(
90buffer: Uint8Array,
91options?: InferenceSession.SessionOptions,
92): Promise<InferenceSessionHandler>;
93async createInferenceSessionHandler(
94pathOrBuffer: string | Uint8Array,
95options?: InferenceSession.SessionOptions,
96): Promise<InferenceSessionHandler> {
97const handler = new OnnxruntimeWebAssemblySessionHandler();
98await handler.loadModel(pathOrBuffer, options);
99return Promise.resolve(handler);
100}
101}
102