Skip to content

Commit

Permalink
[js/web] show warning when numThreads is set but threads is not suppo…
Browse files Browse the repository at this point in the history
…rted (#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.
  • Loading branch information
fs-eire authored Jan 17, 2024
1 parent 146ebaf commit f87e698
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
6 changes: 6 additions & 0 deletions js/web/lib/backend-wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
33 changes: 27 additions & 6 deletions js/web/lib/wasm/wasm-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f87e698

Please sign in to comment.