PyTorch ExecuTorch WebGPU后端:浏览器内GPU加速推理
ExecuTorch WebGPU: PyTorch Models on the GPU, in the Browser
端侧部署者必看,原生支持浏览器GPU推理且无需格式转换,性能提升显著,直接解决隐私与算力痛点。
TL;DR
TL;DR(太长不看)
- PyTorch models now run in the web browser on the GPU through ExecuTorch, with no conversion to another framework’s format. You export with torch.export to an ExecuTorch .pte file, and the same file runs in any WebGPU browser via ExecuTorch’s new WebGPU backend.
- It works across a broad range of models: LLMs (Llama 3.2, Qwen3, Gemma 3), vision-language (SmolVLM), segmentation, depth, object detection, super-resolution and speech-to-text (Whisper). Everything runs locally in the tab, and no data leaves the device. Try it in the ExecuTorch WebGPU Playground.
- On the same machine, WebGPU is 4–7× faster than ExecuTorch’s CPU path (Wasm + XNNPACK) on prefill and 2–10× faster on decode.
- 通过 ExecuTorch,PyTorch 模型现在可以在 GPU 上直接在 Web 浏览器中运行,无需转换为其他框架的格式。你使用 torch.export 将模型导出为 ExecuTorch .pte 文件,该文件可通过 ExecuTorch 新的 WebGPU 后端在任何支持 WebGPU 的浏览器中运行。
- 它适用于广泛范围的模型:大语言模型(Llama 3.2、Qwen3、Gemma 3)、视觉-语言模型(SmolVLM)、分割、深度估计、目标检测、超分辨率以及语音转文本(Whisper)。所有内容均在浏览器标签页内本地运行,且数据不会离开设备。请在 ExecuTorch WebGPU Playground 中尝试。
- 在同一台机器上,WebGPU 在预填充(prefill)阶段比 ExecuTorch 的 CPU 路径(Wasm + XNNPACK)快 4–7 倍,在解码(decode)阶段快 2–10 倍。
Why the Browser?
为什么选择浏览器?
The web is the largest deployment surface with billions of users on Chrome, Firefox and Safari. Browser inference ensures privacy by keeping users’ prompts, model inputs, and outputs on the user’s device rather than the server. This reduces network latency, enables offline use-cases, and lowers server-side inference costs. With the ExecuTorch WebGPU backend, PyTorch models get a native path to GPU-accelerated inference in WebGPU-enabled browsers on desktop, laptop and mobile.
Web 是拥有数十亿 Chrome、Firefox 和 Safari 用户的最广泛部署平台。浏览器端推理通过将用户的提示词、模型输入和输出保留在用户设备上而非服务器上,从而确保隐私。这降低了网络延迟,支持离线用例,并减少了服务器端的推理成本。借助 ExecuTorch WebGPU 后端,PyTorch 模型可在桌面、笔记本和移动设备上支持 WebGPU 的浏览器中获得原生 GPU 加速推理路径。
WebGPU, the modern successor to WebGL, provides native compute shaders, shared memory, storage buffers, and barriers, making it viable for high-performance ML inference directly in the browser. Today, running a PyTorch model in the browser usually means first converting it to another framework’s format, such as GGUF, ONNX or TFLite. The ExecuTorch WebGPU backend lets PyTorch models reach the browser without leaving the PyTorch toolchain.
WebGPU 是 WebGL 的现代继任者,提供原生计算着色器、共享内存、存储缓冲区和屏障机制,使其能够直接在浏览器中进行高性能机器学习推理。目前,在浏览器中运行 PyTorch 模型通常意味着首先将其转换为其他框架的格式,如 GGUF、ONNX 或 TFLite。ExecuTorch WebGPU 后端使 PyTorch 模型无需脱离 PyTorch 工具链即可进入浏览器。
Backend Design
后端设计
[Figure 1: ExecuTorch WebGPU backend design: a PyTorch model is exported once to a .pte file and runs in any WebGPU browser through the ExecuTorch Wasm runtime]
[图 1:ExecuTorch WebGPU 后端设计:PyTorch 模型被导出一次到 .pte 文件,并通过 ExecuTorch Wasm 运行时在任何 WebGPU 浏览器中运行]
Three principles guided the WebGPU backend’s design:
WebGPU 后端的设计遵循三大原则:
- Architecture, export, and dynamic runtime. Models export through ExecuTorch’s torch.export and WebGPUPartitioner and run via WGSL compute shaders. At runtime, the WebGPU runtime allocates GPU buffers and dispatches compute shaders into command buffers. Dynamic shapes are handled via allocate-at-max buffers with per-dispatch workgroup recalculation to enable batched prefill, and the runtime routes shapes to specialized WGSL kernels at runtime.
- Same artifact across multiple devices and browsers. One model (.pte file) runs in WebGPU browsers such as Chrome and Safari, natively on Metal (macOS), Vulkan (Linux/Android) and D3D12 (Windows), translating WGSL to Metal Shading Language, SPIR-V or HLSL depending on the platform. For browser deployment, the dynamic C++ runtime compiles to WASM via Emscripten, and emdawnwebgpu bridges its WebGPU API calls to any browser’s WebGPU JavaScript API.
- Zero friction for the end user. With WebGPU, models run in the browser sandbox with no native installation, backend server, and no data leaving the device. For developers, deployment is a .pte file served over CDN on a web page. Pipeline compilation, memory management, and dispatch scheduling are all handled through the WebGPU backend, with all data staying in GPU device memory within the browser tab.
- 架构、导出与动态运行时。模型通过 ExecuTorch 的 torch.export 和 WebGPUPartitioner 进行导出,并通过 WGSL 计算着色器运行。在运行时,WebGPU 运行时分配 GPU 缓冲区并将计算着色器分发到命令缓冲区中。动态形状通过“最大尺寸分配”缓冲区处理,并在每次调度时重新计算工作组以支持批处理预填充;运行时还会在运行时将不同形状路由到专用的 WGSL 内核。
- 同一模型在多个设备和浏览器中通用。一个模型(.pte 文件)可在支持 WebGPU 的浏览器(如 Chrome 和 Safari)中运行,原生支持 Metal(macOS)、Vulkan(Linux/Android)和 D3D12(Windows),并根据平台将 WGSL 翻译为 Metal Shading Language、SPIR-V 或 HLSL。对于浏览器部署,动态 C++ 运行时通过 Emscripten 编译为 WASM,emdawnwebgpu 将其 WebGPU API 调用桥接到任意浏览器的 WebGPU JavaScript API。
- 终端用户零摩擦体验。借助 WebGPU,模型在浏览器沙箱中运行,无需本地安装、后端服务器,且数据不会离开设备。对于开发者,部署只需将一个 .pte 文件通过 CDN 在网页上提供服务。管道编译、内存管理和调度分发均由 WebGPU 后端处理,所有数据均保留在浏览器标签页内的 GPU 设备内存中。
Performance
性能
We benchmarked ExecuTorch WebGPU against ExecuTorch’s own Wasm CPU backend (XNNPACK, multi-threaded) on the same machine, using quantized Llama 3.2 1B and Qwen3-0.6B in Google Chrome on Apple M1 Pro. Each data point is the median of 5 runs after 3 warmups, across 128 to 8K tokens of context, with identical token IDs for both backends. Both are timed with wall-clock time, since GPU timestamps aren’t available for CPU execution. WebGPU uses 4-bit groupwise weights (group size 64) with fp32 activations; the CPU uses XNNPACK’s 4-bit weights with 8-bit dynamic activations.
我们在同一台机器上使用 Apple M1 Pro 上的 Google Chrome,对 ExecuTorch WebGPU 与 ExecuTorch 自身的 Wasm CPU 后端(XNNPACK,多线程)进行了基准测试,使用的模型为量化版的 Llama 3.2 1B 和 Qwen3-0.6B。每个数据点是经过 3 次预热后的 5 次运行的中位数,上下文长度从 128 到 8K token,两个后端的 token ID 完全相同。由于 CPU 执行无法获取 GPU 时间戳,两者均采用墙钟时间计时。WebGPU 使用 4-bit 组内权重(组大小 64)和 fp32 激活值;CPU 使用 XNNPACK 的 4-bit 权重和 8-bit 动态激活值。
[Figure 2: ExecuTorch WebGPU is 4–7× faster than ExecuTorch Wasm (XNNPACK) on prefill and 2–10× faster on decode, measured in Chrome on Apple M1 Pro]
[图 2:在 Apple M1 Pro 的 Chrome 中测得,ExecuTorch WebGPU 在预填充阶段比 ExecuTorch Wasm (XNNPACK) 快 4–7 倍,在解码阶段快 2–10 倍]
Models in Practice
实际模型应用
We’ve validated the WebGPU backend across models with very different architectures, demonstrating that the operator coverage generalizes beyond autoregressive text generation:
我们已在具有不同架构的模型上验证了 WebGPU 后端,证明了算子覆盖范围不仅限于自回归文本生成:
LLM Inference
大语言模型推理
- Llama 3.2 1B (4-bit quantized)
- Qwen2.5-0.5B (4-bit quantized)
- Qwen3-0.6B (4-bit quantized)
- Gemma 3 1B
- Llama 3.2 1B(4-bit 量化)
- Qwen2.5-0.5B(4-bit 量化)
- Qwen3-0.6B(4-bit 量化)
- Gemma 3 1B
Vision-Language Understanding
视觉-语言理解
- SmolVLM: image question answering and captioning
- SmolVLM:图像问答和图像描述生成
Vision
视觉
- Depth Anything V2: monocular depth estimation
- SAM 2 (SAM 2.1 Hiera Tiny): promptable image segmentation
- D-FINE: object detection
- Real-ESRGAN: super-resolution
- Background Removal (MODNet): foreground segmentation
- Depth Anything V2:单目深度估计
- SAM 2 (SAM 2.1 Hiera Tiny):可提示的图像分割
- D-FINE:目标检测
- Real-ESRGAN:超分辨率
- 背景移除 (MODNet):前景分割
Voice
语音
- Whisper (base): speech-to-text
- Phonon-31M-EN: text-to-speech
- Whisper (base):语音转文本
- Phonon-31M-EN:文本转语音
On-Device Training
端侧训练
- LoRA and DiReFT fine-tuning for Llama 3.2 1B, Qwen2.5 0.5B and Qwen3 0.6B, aimed at personalized assistants and domain adaptation on-device
- 针对 Llama 3.2 1B、Qwen2.5 0.5B 和 Qwen3 0.6B 进行 LoRA 和 DiReFT 微调,旨在实现设备端个性化助手和领域适配
[Figure 3: ExecuTorch WebGPU demonstration]
[图 3:ExecuTorch WebGPU 演示]
Try it: ExecuTorch WebGPU Playground
试一试:ExecuTorch WebGPU 游乐场
The ExecuTorch WebGPU Playground runs many of the models above, all on the same WebAssembly + WebGPU runtime. All the prompts, photos, camera frames and audio never leave your device. Each model’s Hugging Face repo includes the .pte files, a model card, and the exact export recipe.
ExecuTorch WebGPU 游乐场运行上述许多模型,全部基于相同的 WebAssembly + WebGPU 运行时。所有提示词、照片、相机帧和音频均不会离开您的设备。每个模型的 Hugging Face 仓库都包含 .pte 文件、模型卡片以及确切的导出配方。
What’s Next
下一步计划
The backend is still in early development. We’ll keep making the backend faster and more stable, and expand operator, data type, and model coverage.
后端仍处于早期开发阶段。我们将持续优化后端的速度与稳定性,并扩展对算子、数据类型和模型的支持范围。
Contributions / Feedback
贡献 / 反馈
ExecuTorch is open source, and we’d love your contributions to the WebGPU backend. If something breaks or a model you care about isn’t supported, open an issue or reach us on the ExecuTorch Discord. Contributions are welcome too, whether that’s new operators, new models or performance improvements.
ExecuTorch 是开源项目,我们非常欢迎您对 WebGPU 后端的贡献。如果遇到故障或您关注的模型不受支持,请提交 Issue 或通过 ExecuTorch Discord 联系我们。我们也欢迎各种形式的贡献,无论是新增算子、新模型还是性能优化。
Learn more at ExecuTorch Documentation | ExecuTorch repo | WebGPU Backend
更多信息请访问:ExecuTorch 文档 | ExecuTorch 仓库 | WebGPU 后端
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力