2021-04-27 07:04:25 +00:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
|
|
2023-10-06 20:37:37 +00:00
|
|
|
import {cpus} from 'node:os';
|
2023-09-30 02:05:10 +00:00
|
|
|
import {Backend, env, InferenceSession, InferenceSessionHandler} from 'onnxruntime-common';
|
2021-04-27 07:04:25 +00:00
|
|
|
|
[js/web] revise backend registration (#18715)
### Description
This PR revises the backend registration.
The following describes the expected behavior after this change:
(**bolded are changed behavior**)
- (ort.min.js - built without webgpu support)
- loading: do not register 'webgpu' backend
- creating session without EP list: use default EP list ['webnn', 'cpu',
'wasm']
- creating session with ['webgpu'] as EP list: should fail with backend
not available
- (ort.webgpu.min.js - built with webgpu support)
- loading: **always register 'webgpu' backend**
( previous behavior: only register 'webgpu' backend when `navigator.gpu`
is available)
- creating session without EP list: use default EP list ['webgpu',
'webnn', 'cpu', 'wasm']
- when WebGPU is available (win): use WebGPU backend
- when WebGPU is unavailable (android): **should fail backend init,**
and try to use next backend in the list, 'webnn'
(previous behavior: does not fail backend init, but fail in JSEP init,
which was too late to switch to next backend)
- creating session with ['webgpu'] as EP list
- when WebGPU is available (win): use WebGPU backend
- when WebGPU is unavailable (android): **should fail backend init, and
because no more EP listed, fail.
related PRs: #18190 #18144
2023-12-20 22:45:55 +00:00
|
|
|
import {initializeOrtEp, initializeWebAssemblyAndOrtRuntime} from './wasm/proxy-wrapper';
|
2023-11-02 15:32:50 +00:00
|
|
|
import {OnnxruntimeWebAssemblySessionHandler} from './wasm/session-handler-inference';
|
2021-05-07 19:12:37 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 06:24:27 +00:00
|
|
|
if (typeof env.wasm.simd !== 'boolean') {
|
|
|
|
|
env.wasm.simd = true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 17:23:42 +00:00
|
|
|
if (typeof env.wasm.proxy !== 'boolean') {
|
|
|
|
|
env.wasm.proxy = false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:13:17 +00:00
|
|
|
if (typeof env.wasm.trace !== 'boolean') {
|
|
|
|
|
env.wasm.trace = false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 22:46:50 +00:00
|
|
|
if (typeof env.wasm.numThreads !== 'number' || !Number.isInteger(env.wasm.numThreads) || env.wasm.numThreads <= 0) {
|
2021-05-15 01:15:38 +00:00
|
|
|
const numCpuLogicalCores = typeof navigator === 'undefined' ? cpus().length : navigator.hardwareConcurrency;
|
2021-05-26 22:46:50 +00:00
|
|
|
env.wasm.numThreads = Math.min(4, Math.ceil((numCpuLogicalCores || 1) / 2));
|
2021-05-07 19:12:37 +00:00
|
|
|
}
|
|
|
|
|
};
|
2021-04-27 07:04:25 +00:00
|
|
|
|
2023-10-12 18:16:56 +00:00
|
|
|
export class OnnxruntimeWebAssemblyBackend implements Backend {
|
[js/web] revise backend registration (#18715)
### Description
This PR revises the backend registration.
The following describes the expected behavior after this change:
(**bolded are changed behavior**)
- (ort.min.js - built without webgpu support)
- loading: do not register 'webgpu' backend
- creating session without EP list: use default EP list ['webnn', 'cpu',
'wasm']
- creating session with ['webgpu'] as EP list: should fail with backend
not available
- (ort.webgpu.min.js - built with webgpu support)
- loading: **always register 'webgpu' backend**
( previous behavior: only register 'webgpu' backend when `navigator.gpu`
is available)
- creating session without EP list: use default EP list ['webgpu',
'webnn', 'cpu', 'wasm']
- when WebGPU is available (win): use WebGPU backend
- when WebGPU is unavailable (android): **should fail backend init,**
and try to use next backend in the list, 'webnn'
(previous behavior: does not fail backend init, but fail in JSEP init,
which was too late to switch to next backend)
- creating session with ['webgpu'] as EP list
- when WebGPU is available (win): use WebGPU backend
- when WebGPU is unavailable (android): **should fail backend init, and
because no more EP listed, fail.
related PRs: #18190 #18144
2023-12-20 22:45:55 +00:00
|
|
|
/**
|
|
|
|
|
* 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> {
|
2021-05-07 19:12:37 +00:00
|
|
|
// populate wasm flags
|
|
|
|
|
initializeFlags();
|
|
|
|
|
|
|
|
|
|
// init wasm
|
[js/web] revise backend registration (#18715)
### Description
This PR revises the backend registration.
The following describes the expected behavior after this change:
(**bolded are changed behavior**)
- (ort.min.js - built without webgpu support)
- loading: do not register 'webgpu' backend
- creating session without EP list: use default EP list ['webnn', 'cpu',
'wasm']
- creating session with ['webgpu'] as EP list: should fail with backend
not available
- (ort.webgpu.min.js - built with webgpu support)
- loading: **always register 'webgpu' backend**
( previous behavior: only register 'webgpu' backend when `navigator.gpu`
is available)
- creating session without EP list: use default EP list ['webgpu',
'webnn', 'cpu', 'wasm']
- when WebGPU is available (win): use WebGPU backend
- when WebGPU is unavailable (android): **should fail backend init,**
and try to use next backend in the list, 'webnn'
(previous behavior: does not fail backend init, but fail in JSEP init,
which was too late to switch to next backend)
- creating session with ['webgpu'] as EP list
- when WebGPU is available (win): use WebGPU backend
- when WebGPU is unavailable (android): **should fail backend init, and
because no more EP listed, fail.
related PRs: #18190 #18144
2023-12-20 22:45:55 +00:00
|
|
|
await initializeWebAssemblyAndOrtRuntime();
|
|
|
|
|
|
|
|
|
|
// performe EP specific initialization
|
|
|
|
|
await initializeOrtEp(backendName);
|
2021-04-27 07:04:25 +00:00
|
|
|
}
|
2023-09-30 02:05:10 +00:00
|
|
|
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> {
|
2021-04-27 07:04:25 +00:00
|
|
|
const handler = new OnnxruntimeWebAssemblySessionHandler();
|
2022-11-14 20:18:02 +00:00
|
|
|
await handler.loadModel(pathOrBuffer, options);
|
2021-04-27 07:04:25 +00:00
|
|
|
return Promise.resolve(handler);
|
|
|
|
|
}
|
|
|
|
|
}
|