[js/web] show warning when numThreads is set but threads is not supported (#19179)

### Description
show warning when numThreads is set but threads is not supported.
Resolves #19148, #18933

for web: when crossOriginIsolated is false.
for node: always disable.
This commit is contained in:
Yulong Wang 2024-01-17 15:04:22 -08:00 committed by GitHub
parent 146ebaf91e
commit f87e69801f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 7 deletions

View file

@ -31,6 +31,12 @@ export const initializeFlags = (): void => {
}
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));
}

View file

@ -28,13 +28,34 @@ let initialized = false;
let initializing = false;
let aborted = false;
const isMultiThreadSupported = (): boolean => {
try {
// If 'SharedArrayBuffer' is not available, WebAssembly threads will not work.
if (typeof SharedArrayBuffer === 'undefined') {
return false;
}
const isMultiThreadSupported = (numThreads: number): boolean => {
// WebAssembly threads are set to 1 (single thread).
if (numThreads === 1) {
return false;
}
// If 'SharedArrayBuffer' is not available, WebAssembly threads will not work.
if (typeof SharedArrayBuffer === 'undefined') {
if (typeof self !== 'undefined' && !self.crossOriginIsolated) {
// eslint-disable-next-line no-console
console.warn(
'env.wasm.numThreads is set to ' + numThreads +
', but this will not work unless you enable crossOriginIsolated mode. ' +
'See https://web.dev/cross-origin-isolation-guide/ for more info.');
}
return false;
}
// onnxruntime-web does not support multi-threads in Node.js.
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
// eslint-disable-next-line no-console
console.warn(
'env.wasm.numThreads is set to ' + numThreads +
', however, currently onnxruntime-web does not support multi-threads in Node.js. ' +
'Please consider using onnxruntime-node for performance critical scenarios.');
}
try {
// Test for transferability of SABs (for browsers. needed for Firefox)
// https://groups.google.com/forum/#!msg/mozilla.dev.platform/IHkBZlHETpA/dwsMNchWEQAJ
if (typeof MessageChannel !== 'undefined') {
@ -106,7 +127,7 @@ export const initializeWebAssembly = async(flags: Env.WebAssemblyFlags): Promise
const numThreads = flags.numThreads!;
const simd = flags.simd!;
const useThreads = numThreads > 1 && isMultiThreadSupported();
const useThreads = isMultiThreadSupported(numThreads);
const useSimd = simd && isSimdSupported();
const wasmPaths = flags.wasmPaths;