Set device: ‘webgpu’ when loading a model in Transformers.js, or import onnxruntime-web/webgpu directly in ONNX Runtime Web. WebGPU needs a secure context (HTTPS) and a browser that supports it; it falls back to WASM/CPU otherwise.
At a glance
| Fact | Value | Source |
|---|---|---|
| Transformers.js WebGPU setup | device: ‘webgpu’ | huggingface.co |
| ONNX Runtime Web WebGPU import | onnxruntime-web/webgpu | onnxruntime.ai |
| Default browser backend without WebGPU | CPU via WASM | huggingface.co |
For Transformers.js, load the model with device: 'webgpu' – the library uses ONNX Runtime under the hood and this one option switches the execution provider. For ONNX Runtime Web directly, import the WebGPU build instead of the default: import * as ort from 'onnxruntime-web/webgpu'. Both are described as experimental on the ONNX Runtime Web docs.
If you build your web game with an AI coding agent, the Playgama MCP server lets the agent upload builds, fill in game forms, and publish directly to a public sandbox link without moderation.
import { pipeline } from '@huggingface/transformers';
const pipe = await pipeline('text-generation', 'model-id', { device: 'webgpu' });
WebGPU is accessible only to secure contexts, so the game page must be served over HTTPS (or localhost) – this matters for games iframed into portals, since the portal’s own page also needs to be HTTPS, which is standard for web game hosting today. Firefox and Safari may need flags enabled on some versions. Without WebGPU, Transformers.js and ONNX Runtime Web quietly run on CPU via WASM, which is slower but still works – test both paths rather than assuming WebGPU is present.
What to check before shipping
- Model file size and first-load time: large ONNX models benefit from caching in IndexedDB so the page does not re-download on every refresh.
- Run model loading and inference off the main thread (a Worker) so the UI does not block.
- Test on the actual browsers your portal traffic uses, not just your dev Chrome.
Sources
- Running models on WebGPU – Transformers.js
- Get started with ONNX Runtime Web
- Deploying ONNX Runtime Web
- Transformers.js documentation
- Playgama Bridge SDK docs
Related questions
What happens if WebGPU isn’t supported?
Transformers.js and ONNX Runtime Web fall back to running on CPU via WASM by default – slower, but functional, so always test the non-WebGPU path too.
Does WebGPU work inside an iframe on a game portal?
It needs a secure context (HTTPS), which is standard on portals; check the specific portal’s embed setup if ads or the model fail to load.
Last updated: 01 October 2026