Skip to content

🎥 Make videos programmatically with React

@remotion/whisper-webgpu: Probe Node.js WebGPU support

JonnyBurger8a6b5730d946
diff --git a/bun.lock b/bun.lock
index 9c2d4ae6ec..b930ad9f8c 100644
--- a/bun.lock
+++ b/bun.lock
@@ -2897,6 +2897,7 @@
"version": "4.0.526",
"dependencies": {
"@remotion/captions": "workspace:*",
+ "onnxruntime-node": "catalog:",
},
"devDependencies": {
"@huggingface/transformers": "catalog:",
@@ -2984,6 +2985,7 @@
"eslint": "9.19.0",
"mediabunny": "1.56.1",
"next": "16.3.3",
+ "onnxruntime-node": "1.30.0",
"openai": "4.67.1",
"playwright": "1.55.1",
"prettier": "3.8.1",
diff --git a/package.json b/package.json
index 516d2e3b48..b7269e38b4 100644
--- a/package.json
+++ b/package.json
@@ -113,6 +113,7 @@
],
"catalog": {
"@huggingface/transformers": "4.3.0",
+ "onnxruntime-node": "1.30.0",
"@aws-sdk/s3-request-presigner": "3.986.0",
"@aws-sdk/credential-provider-ini": "3.972.5",
"@aws-sdk/client-s3": "3.986.0",
diff --git a/packages/docs/docs/whisper-webgpu/can-use-whisper-webgpu.mdx b/packages/docs/docs/whisper-webgpu/can-use-whisper-webgpu.mdx
index 1902c23e8f..fe00413740 100644
--- a/packages/docs/docs/whisper-webgpu/can-use-whisper-webgpu.mdx
+++ b/packages/docs/docs/whisper-webgpu/can-use-whisper-webgpu.mdx
@@ -8,7 +8,7 @@ crumb: '@remotion/whisper-webgpu'
Asynchronously checks whether WebGPU transcription is supported in the current environment.
-In a browser, WebGPU, a usable adapter, and a secure context (or localhost) are required. Node.js support is available from <AvailableFrom v="4.0.527" inline />. In Node.js, the function checks whether the current operating system and architecture are supported by the native WebGPU backend.
+In a browser, WebGPU, a usable adapter, and a secure context (or localhost) are required. Node.js support is available from <AvailableFrom v="4.0.527" inline />. In Node.js, the function creates and disposes a tiny ONNX Runtime session using the WebGPU execution provider. This checks whether ONNX Runtime can acquire a WebGPU adapter without downloading a Whisper model or running inference.
```ts twoslash title="support.ts"
import {canUseWhisperWebGpu} from '@remotion/whisper-webgpu';
diff --git a/packages/docs/docs/whisper-webgpu/node.mdx b/packages/docs/docs/whisper-webgpu/node.mdx
index 822cfe5edd..f47f51d8d2 100644
--- a/packages/docs/docs/whisper-webgpu/node.mdx
+++ b/packages/docs/docs/whisper-webgpu/node.mdx
@@ -6,7 +6,9 @@ crumb: '@remotion/whisper-webgpu'
# Using @remotion/whisper-webgpu in Node.js<AvailableFrom v="4.0.527" />
-`@remotion/whisper-webgpu` can transcribe a 16kHz mono waveform in Node.js using the native WebGPU backend from [Transformers.js](https://huggingface.co/docs/transformers.js).
+`@remotion/whisper-webgpu` can transcribe a 16kHz mono waveform in Node.js using [Transformers.js](https://huggingface.co/docs/transformers.js) and [ONNX Runtime's WebGPU execution provider](https://onnxruntime.ai/docs/execution-providers/WebGPU-ExecutionProvider.html).
+
+Node.js does not expose the browser `navigator.gpu` API. Transformers.js uses the native `onnxruntime-node` addon instead, whose experimental WebGPU execution provider uses Dawn to access Metal, Vulkan, or Direct3D 12.
The following example uses [`@mediabunny/server`](https://mediabunny.dev/guide/extensions/server) to decode and resample a local audio or video file before transcribing it.
@@ -122,11 +124,11 @@ await writeFile('captions.json', JSON.stringify(captions, null, 2));
The model is downloaded on the first run and cached on the file system. `await using` releases it from memory when the script exits while keeping the downloaded files cached. The input to [`transcribe()`](/docs/whisper-webgpu/transcribe) must be a mono `Float32Array` sampled at 16kHz.
-Call [`canUseWhisperWebGpu()`](/docs/whisper-webgpu/can-use-whisper-webgpu) before loading the model if the script may run on different operating systems or CPU architectures.
+Call [`canUseWhisperWebGpu()`](/docs/whisper-webgpu/can-use-whisper-webgpu) before loading the model if the script may run on different hardware. It creates and disposes a tiny WebGPU-backed ONNX session, so it verifies that ONNX Runtime can acquire an adapter without downloading a Whisper model.
## Supported platforms
-The native WebGPU backend supports Node.js on macOS x64 and Arm64, Windows x64 and Arm64, and Linux x64.
+ONNX Runtime bundles its experimental WebGPU execution provider for Node.js on macOS x64 and Arm64, Windows x64 and Arm64, and Linux x64. A usable GPU adapter and compatible driver are still required.
## See also
diff --git a/packages/whisper-webgpu/bundle.ts b/packages/whisper-webgpu/bundle.ts
index a0a1a34b77..35cd9724b2 100644
--- a/packages/whisper-webgpu/bundle.ts
+++ b/packages/whisper-webgpu/bundle.ts
@@ -12,6 +12,7 @@ const output = await build({
external: [
'@huggingface/transformers',
'@remotion/captions',
+ 'onnxruntime-node',
'remotion',
'remotion/no-react',
'react',
diff --git a/packages/whisper-webgpu/package.json b/packages/whisper-webgpu/package.json
index cdb8764a0a..e629e127c7 100644
--- a/packages/whisper-webgpu/package.json
+++ b/packages/whisper-webgpu/package.json
@@ -24,7 +24,8 @@
"url": "https://github.com/remotion-dev/remotion/issues"
},
"dependencies": {
- "@remotion/captions": "workspace:*"
+ "@remotion/captions": "workspace:*",
+ "onnxruntime-node": "catalog:"
},
"peerDependencies": {
"@huggingface/transformers": "4.3.0"
diff --git a/packages/whisper-webgpu/src/can-use-whisper-webgpu.ts b/packages/whisper-webgpu/src/can-use-whisper-webgpu.ts
index f2c6c09b2d..f277e34835 100644
--- a/packages/whisper-webgpu/src/can-use-whisper-webgpu.ts
+++ b/packages/whisper-webgpu/src/can-use-whisper-webgpu.ts
@@ -18,22 +18,19 @@ export const canUseWhisperWebGpu =
async (): Promise<CanUseWhisperWebGpuResult> => {
if (typeof window === 'undefined') {
if (typeof process !== 'undefined' && process.release?.name === 'node') {
- const supportsNativeWebGpu =
- (process.platform === 'darwin' &&
- (process.arch === 'arm64' || process.arch === 'x64')) ||
- (process.platform === 'win32' &&
- (process.arch === 'arm64' || process.arch === 'x64')) ||
- (process.platform === 'linux' && process.arch === 'x64');
-
- if (supportsNativeWebGpu) {
+ try {
+ const {probeNodeWebGpu} = await import('./probe-node-webgpu');
+ await probeNodeWebGpu();
return {supported: true};
+ } catch (error) {
+ const message =
+ error instanceof Error ? error.message : String(error);
+ return {
+ supported: false,
+ reason: WhisperWebGpuUnsupportedReason.WebGpuUnavailable,
+ detailedReason: `ONNX Runtime could not initialize WebGPU: ${message}`,
+ };
}
-
- return {
- supported: false,
- reason: WhisperWebGpuUnsupportedReason.WebGpuUnavailable,
- detailedReason: `The native WebGPU backend is not available on ${process.platform} ${process.arch}.`,
- };
}
return {
diff --git a/packages/whisper-webgpu/src/probe-node-webgpu.ts b/packages/whisper-webgpu/src/probe-node-webgpu.ts
new file mode 100644
index 0000000000..02aaa2603b
--- /dev/null
+++ b/packages/whisper-webgpu/src/probe-node-webgpu.ts
@@ -0,0 +1,30 @@
+import type * as OnnxRuntimeNode from 'onnxruntime-node';
+
+// A 130-byte multiplication model from ONNX Runtime's test fixtures.
+// https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/test/testdata/mul_1.onnx
+// Creating a session is enough to make the WebGPU execution provider request an
+// adapter, without downloading a Whisper model or running inference.
+const webGpuProbeModel = new Uint8Array([
+ 0x08, 0x03, 0x12, 0x06, 0x63, 0x68, 0x65, 0x6e, 0x74, 0x61, 0x3a, 0x70, 0x0a,
+ 0x15, 0x0a, 0x01, 0x58, 0x0a, 0x01, 0x57, 0x12, 0x01, 0x59, 0x1a, 0x05, 0x6d,
+ 0x75, 0x6c, 0x5f, 0x31, 0x22, 0x03, 0x4d, 0x75, 0x6c, 0x12, 0x08, 0x6d, 0x75,
+ 0x6c, 0x20, 0x74, 0x65, 0x73, 0x74, 0x2a, 0x23, 0x08, 0x03, 0x08, 0x02, 0x10,
+ 0x01, 0x22, 0x18, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
+ 0x40, 0x40, 0x00, 0x00, 0x80, 0x40, 0x00, 0x00, 0xa0, 0x40, 0x00, 0x00, 0xc0,
+ 0x40, 0x42, 0x01, 0x57, 0x5a, 0x13, 0x0a, 0x01, 0x58, 0x12, 0x0e, 0x0a, 0x0c,
+ 0x08, 0x01, 0x12, 0x08, 0x0a, 0x02, 0x08, 0x03, 0x0a, 0x02, 0x08, 0x02, 0x62,
+ 0x13, 0x0a, 0x01, 0x59, 0x12, 0x0e, 0x0a, 0x0c, 0x08, 0x01, 0x12, 0x08, 0x0a,
+ 0x02, 0x08, 0x03, 0x0a, 0x02, 0x08, 0x02, 0x42, 0x04, 0x0a, 0x00, 0x10, 0x07,
+]);
+
+export const probeNodeWebGpu = async (): Promise<void> => {
+ // Keep the native Node.js addon out of browser bundles.
+ const packageName: string = 'onnxruntime-node';
+ const {InferenceSession} = (await import(
+ /* @vite-ignore */ packageName
+ )) as typeof OnnxRuntimeNode;
+ const session = await InferenceSession.create(webGpuProbeModel, {
+ executionProviders: ['webgpu'],
+ });
+ await session.release();
+};
diff --git a/packages/whisper-webgpu/src/test/transcribe.test.ts b/packages/whisper-webgpu/src/test/transcribe.test.ts
index dae095dd02..bd44c48d30 100644
--- a/packages/whisper-webgpu/src/test/transcribe.test.ts
+++ b/packages/whisper-webgpu/src/test/transcribe.test.ts
@@ -53,6 +53,14 @@ let cacheClear:
options: Record<string, unknown>;
}
| undefined;
+let webGpuProbe:
+ | {
+ model: Uint8Array;
+ options: Record<string, unknown>;
+ }
+ | undefined;
+let webGpuProbeError: Error | null = null;
+let webGpuProbeReleaseCalls = 0;
const originalTransformersEnvironment = {
remoteHost: 'https://huggingface.co/',
@@ -164,6 +172,24 @@ mock.module('@huggingface/transformers', () => ({
},
}));
+mock.module('onnxruntime-node', () => ({
+ InferenceSession: {
+ create: (model: Uint8Array, options: Record<string, unknown>) => {
+ webGpuProbe = {model, options};
+ if (webGpuProbeError) {
+ return Promise.reject(webGpuProbeError);
+ }
+
+ return Promise.resolve({
+ release: () => {
+ webGpuProbeReleaseCalls++;
+ return Promise.resolve();
+ },
+ });
+ },
+ },
+}));
+
test('removes legacy Hugging Face Whisper entries from the shared browser cache', async () => {
const originalCaches = Object.getOwnPropertyDescriptor(globalThis, 'caches');
const cacheEnvironment =
@@ -350,6 +376,21 @@ test('transcribes with word timestamps using WebGPU', async () => {
});
expect(transformersEnvironment).toEqual(originalTransformersEnvironment);
expect(await canUseWhisperWebGpu()).toEqual({supported: true});
+ expect(webGpuProbe?.model).toHaveLength(130);
+ expect(webGpuProbe?.options).toEqual({executionProviders: ['webgpu']});
+ expect(webGpuProbeReleaseCalls).toBe(1);
+
+ webGpuProbeError = new Error('Failed to get a WebGPU adapter');
+ try {
+ expect(await canUseWhisperWebGpu()).toEqual({
+ supported: false,
+ reason: WhisperWebGpuUnsupportedReason.WebGpuUnavailable,
+ detailedReason:
+ 'ONNX Runtime could not initialize WebGPU: Failed to get a WebGPU adapter',
+ });
+ } finally {
+ webGpuProbeError = null;
+ }
const originalWindow = Object.getOwnPropertyDescriptor(globalThis, 'window');
const originalNavigator = Object.getOwnPropertyDescriptor(
@@ -690,7 +731,7 @@ test('shares concurrent initialization and keeps the model host scoped until eve
expect(disposeCalls).toBe(disposalsBeforeLoading);
releaseInitialization();
- await expect(firstLoad).resolves.toEqual({alreadyLoaded: false});
+ await expect(firstLoad).resolves.toMatchObject({alreadyLoaded: false});
await expect(transcription).resolves.toMatchObject({model: 'base'});
expect(firstProgress.at(-1)).toBe(1);
expect(secondProgress).toEqual(firstProgress);