Skip to content

Commit 0eb9bc0

Browse files
authored
Enable WebGPU for Safari 26 and above (#1700)
* Enable WebGPU for Safari 26 and above * Disable asyncify for Safari below 26 when WebGPU is not available
1 parent bc9cf74 commit 0eb9bc0

2 files changed

Lines changed: 27 additions & 25 deletions

File tree

packages/transformers/src/backends/onnx.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,16 @@ if (ONNX_ENV) {
349349
) {
350350
const wasmPathPrefix = `https://cdn.jsdelivr.net/npm/onnxruntime-web@${ONNX_ENV.versions.web}/dist/`;
351351

352-
ONNX_ENV.wasm.wasmPaths = apis.IS_SAFARI
353-
? {
354-
mjs: `${wasmPathPrefix}ort-wasm-simd-threaded.mjs`,
355-
wasm: `${wasmPathPrefix}ort-wasm-simd-threaded.wasm`,
356-
}
357-
: {
358-
mjs: `${wasmPathPrefix}ort-wasm-simd-threaded.asyncify.mjs`,
359-
wasm: `${wasmPathPrefix}ort-wasm-simd-threaded.asyncify.wasm`,
360-
};
352+
let wasmPathSuffix = '.asyncify'; // Default to asyncify WASM build
353+
if (apis.IS_SAFARI_BELOW_26 && !apis.IS_WEBGPU_AVAILABLE) {
354+
// Disable asyncify for Safari below 26 when WebGPU is not available
355+
wasmPathSuffix = '';
356+
}
357+
358+
ONNX_ENV.wasm.wasmPaths = {
359+
mjs: `${wasmPathPrefix}ort-wasm-simd-threaded${wasmPathSuffix}.mjs`,
360+
wasm: `${wasmPathPrefix}ort-wasm-simd-threaded${wasmPathSuffix}.wasm`,
361+
};
361362
}
362363

363364
// Users may wish to proxy the WASM backend to prevent the UI from freezing,

packages/transformers/src/env.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,33 @@ const IS_SERVICE_WORKER_ENV =
6565
typeof ServiceWorkerGlobalScope !== 'undefined' && HAS_SELF && self instanceof ServiceWorkerGlobalScope;
6666

6767
/**
68-
* Check if the current environment is Safari browser.
69-
* Works in both browser and web worker contexts.
70-
* @returns {boolean} Whether the current environment is Safari.
68+
* Check whether the current environment is a Safari browser older than version 26 (Safari >= 26 enables WebGPU by default).
69+
* @returns {boolean} Whether the current environment is Safari older than version 26.
7170
*/
72-
const isSafari = () => {
73-
// Check if we're in a browser environment
71+
const isSafariBelow26 = () => {
7472
if (typeof navigator === 'undefined') {
7573
return false;
7674
}
7775

7876
const userAgent = navigator.userAgent;
79-
const vendor = navigator.vendor || '';
8077

81-
// Safari has "Apple" in vendor string
82-
const isAppleVendor = vendor.indexOf('Apple') > -1;
83-
84-
// Exclude Chrome on iOS (CriOS), Firefox on iOS (FxiOS),
85-
// Edge on iOS (EdgiOS), and other browsers
86-
const notOtherBrowser =
78+
// Safari has "Apple" in its vendor string. Exclude Chrome on iOS (CriOS),
79+
// Firefox on iOS (FxiOS), Edge on iOS (EdgiOS), and other browsers.
80+
const isSafari =
81+
(navigator.vendor || '').indexOf('Apple') > -1 &&
8782
!userAgent.match(/CriOS|FxiOS|EdgiOS|OPiOS|mercury|brave/i) &&
8883
!userAgent.includes('Chrome') &&
8984
!userAgent.includes('Android');
85+
if (!isSafari) {
86+
return false;
87+
}
9088

91-
return isAppleVendor && notOtherBrowser;
89+
// Safari reports its version via the "Version/<major>.<minor>" token in the user agent.
90+
// If the version is missing/unparseable, assume a modern Safari (i.e. not below 26).
91+
const match = userAgent.match(/Version\/(\d+)/);
92+
return match ? parseInt(match[1], 10) < 26 : false;
9293
};
93-
const IS_SAFARI = isSafari();
94+
const IS_SAFARI_BELOW_26 = isSafariBelow26();
9495

9596
/**
9697
* A read-only object containing information about the APIs available in the current environment.
@@ -120,8 +121,8 @@ export const apis = Object.freeze({
120121
/** Whether the WebNN API is available */
121122
IS_WEBNN_AVAILABLE,
122123

123-
/** Whether we are running in a Safari browser */
124-
IS_SAFARI,
124+
/** Whether we are running in a Safari browser older than version 26. */
125+
IS_SAFARI_BELOW_26,
125126

126127
/** Whether the Node.js process API is available */
127128
IS_PROCESS_AVAILABLE,

0 commit comments

Comments
 (0)