Skip to content

Commit 1cfcdf8

Browse files
authored
docs: add WebGPU backend documentation (pytorch#22360)
### Summary [PLEASE REMOVE] See [CONTRIBUTING.md's Pull Requests](https://github.com/pytorch/executorch/blob/main/CONTRIBUTING.md#pull-requests) for ExecuTorch PR guidelines. [PLEASE REMOVE] If this PR closes an issue, please add a `Fixes #<issue-id>` line. [PLEASE REMOVE] If this PR introduces a fix or feature that should be the upcoming release notes, please add a "Release notes: <area>" label. For a list of available release notes labels, check out [CONTRIBUTING.md's Pull Requests](https://github.com/pytorch/executorch/blob/main/CONTRIBUTING.md#pull-requests). ### Test plan [PLEASE REMOVE] How did you test this PR? Please write down any manual commands you used and note down tests that you have written if applicable.
1 parent 89e4dd4 commit 1cfcdf8

5 files changed

Lines changed: 218 additions & 0 deletions

File tree

‎backends/webgpu/README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ Key design choices:
130130
- **Shader code generation:** `gen_wgsl_headers.py` generates embedded
131131
`*_wgsl.h` headers; source/header drift fails validation.
132132

133+
Check committed shader headers without regenerating them:
134+
135+
```bash
136+
python backends/webgpu/scripts/gen_wgsl_headers.py --check
137+
```
138+
133139
## Linux Native Quick Start
134140

135141
The `test_build_webgpu.sh` flow sources

‎docs/source/backends-overview.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Backends are the bridge between your exported model and the hardware it runs on.
2525
| [Core ML](backends/coreml/coreml-overview.md) | iOS, macOS | NPU/GPU/CPU | Apple devices, high performance |
2626
| [MLX](/backends/mlx/mlx-overview.md) | macOS | GPU | Apple Silicon GPU (MLX) |
2727
| [Vulkan](backends/vulkan/vulkan-overview.md) | Android, Linux, Windows | GPU | Android devices (mature); Desktops (experimental) |
28+
| [WebGPU](backends/webgpu/webgpu-overview.md) | Browser, Linux, macOS | GPU | Cross-platform and browser GPU execution (experimental) |
2829
| [Qualcomm](backends-qualcomm) | Android | NPU | Qualcomm SoCs |
2930
| [MediaTek](backends-mediatek) | Android | NPU | MediaTek SoCs |
3031
| [Arm Ethos-U](backends/arm-ethos-u/arm-ethos-u-overview.md) | Embedded | NPU | Arm MCUs |
@@ -57,6 +58,7 @@ backends/cuda/cuda-overview
5758
backends/coreml/coreml-overview
5859
backends/mlx/mlx-overview
5960
backends/vulkan/vulkan-overview
61+
backends/webgpu/webgpu-overview
6062
backends-qualcomm
6163
backends-mediatek
6264
backends/arm-ethos-u/arm-ethos-u-overview
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Operator Support
2+
3+
The current source of truth for WebGPU operator support is the runtime registry under
4+
[`backends/webgpu/runtime/ops`](https://github.com/pytorch/executorch/tree/main/backends/webgpu/runtime/ops).
5+
Operator tests are under
6+
[`backends/webgpu/test/ops`](https://github.com/pytorch/executorch/tree/main/backends/webgpu/test/ops),
7+
and the generated native test catalog is defined in
8+
[`backends/webgpu/test/op_tests/cases.py`](https://github.com/pytorch/executorch/blob/main/backends/webgpu/test/op_tests/cases.py).
9+
10+
## Compatibility Considerations
11+
12+
- `WebGPUPartitioner` is a thin wrapper over `VulkanPartitioner`; it preserves
13+
Vulkan partitioning behavior and does not add WebGPU capability policy.
14+
- Support can depend on tensor rank, shape, layout, and data type in addition
15+
to the operator name.
16+
- FP16 shader paths require a WebGPU adapter exposing `shader-f16`.
17+
- Dynamic shapes allocate buffers at the exported maximum shape. Operators
18+
must also implement runtime resize propagation.
19+
- Quantized and LLM-specific operators generally require the corresponding
20+
Vulkan custom-op export pattern.
21+
22+
Use the native operator-test framework to generate fixtures for specific
23+
operators. For example, generate the verified `add` cases with:
24+
25+
```bash
26+
python -m executorch.backends.webgpu.test.op_tests.generate_op_tests \
27+
--output /tmp/webgpu_op_tests --ops add
28+
```
29+
30+
The full build-and-run flow is available through:
31+
32+
```bash
33+
bash backends/webgpu/test/test_build_webgpu.sh
34+
```
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# WebGPU Backend
2+
3+
The ExecuTorch WebGPU backend runs delegated model graphs on GPUs through the
4+
[WebGPU API](https://www.w3.org/TR/webgpu/). It uses
5+
[Dawn](https://dawn.googlesource.com/dawn) and Tint for native execution and
6+
Emscripten's `emdawnwebgpu` port for browser builds. Operators are implemented
7+
as in-tree WGSL compute shaders.
8+
9+
::::{note}
10+
The WebGPU backend is experimental and under active development.
11+
::::
12+
13+
## Features
14+
15+
- Native GPU execution on macOS and Linux.
16+
- Browser execution through Emscripten and `emdawnwebgpu`.
17+
- Dynamic tensor shapes backed by maximum-size GPU buffer allocations.
18+
- FP32 execution and device-dependent FP16 paths.
19+
- Quantized linear, embedding, and convolution kernels, including 4-bit
20+
weight-only and dynamic 8-bit activation paths.
21+
- LLM-focused kernels for SDPA, KV-cache updates, rotary embeddings, and
22+
fused SwiGLU and QKV projections.
23+
- GPU timestamp-query profiling on devices that expose the feature.
24+
25+
The backend contains more than 100 registered operators.
26+
See [Operator Support](webgpu-op-support.md) for details.
27+
28+
## Architecture
29+
30+
WebGPU currently reuses the Vulkan backend's export and serialization path:
31+
32+
```text
33+
PyTorch model
34+
│ torch.export
35+
▼
36+
ExportedProgram
37+
│ WebGPUPartitioner (thin wrapper over VulkanPartitioner)
38+
▼
39+
Vulkan FlatBuffer delegate payload (VH00/VK00) inside a .pte file
40+
│
41+
├── Native: Dawn/Tint → Metal or Vulkan
42+
└── Browser: Emscripten + emdawnwebgpu → browser WebGPU implementation
43+
```
44+
45+
`WebGPUPartitioner` is a thin wrapper over `VulkanPartitioner`. It forwards
46+
partitioning unchanged and preserves Vulkan serialization and the
47+
`VulkanBackend` delegate identifier. It does not independently validate the
48+
narrower WebGPU runtime capability set. The runtime ignores Vulkan
49+
texture-storage annotations and executes the graph with WebGPU buffers and
50+
WGSL compute pipelines.
51+
52+
::::{important}
53+
The WebGPU runtime currently registers with ExecuTorch as `VulkanBackend` so it
54+
can consume Vulkan delegate payloads. Do not link the Vulkan and WebGPU runtime
55+
backends into the same application. They provide the same backend identifier.
56+
::::
57+
58+
## Target Requirements
59+
60+
| Target | Requirements |
61+
|---|---|
62+
| macOS native | A Metal-capable GPU and a Dawn installation built for macOS |
63+
| Linux native | A Vulkan-capable GPU and driver, plus Dawn |
64+
| Browser | A browser with WebGPU enabled and an Emscripten build using `emdawnwebgpu` |
65+
66+
The exact shader features available, including FP16 and timestamp queries,
67+
depend on the selected WebGPU adapter.
68+
69+
## Development Requirements
70+
71+
- CMake 3.19 or later.
72+
- A Python environment with ExecuTorch installed for model export.
73+
- Dawn's CMake package for native builds.
74+
- Emscripten for browser builds.
75+
76+
On Linux, the end-to-end validation script installs pinned Dawn and
77+
SwiftShader dependencies automatically:
78+
79+
```bash
80+
bash backends/webgpu/test/test_build_webgpu.sh
81+
```
82+
83+
On macOS, provide a configured Dawn installation and set `Dawn_DIR` to its
84+
CMake package directory.
85+
86+
## Exporting a Model
87+
88+
Use `WebGPUPartitioner` to produce a `.pte` file that the WebGPU runtime can
89+
consume. This example delegates a supported `aten.add.Tensor` operation:
90+
91+
```python
92+
import torch
93+
94+
from executorch.backends.webgpu.partitioner import WebGPUPartitioner
95+
from executorch.exir import to_edge_transform_and_lower
96+
97+
98+
class AddOne(torch.nn.Module):
99+
def forward(self, x: torch.Tensor) -> torch.Tensor:
100+
return x + 1.0
101+
102+
103+
example_inputs = (torch.randn(4, 4),)
104+
exported_program = torch.export.export(AddOne(), example_inputs)
105+
106+
et_program = to_edge_transform_and_lower(
107+
exported_program,
108+
partitioner=[WebGPUPartitioner()],
109+
).to_executorch()
110+
111+
with open("add_webgpu.pte", "wb") as file:
112+
file.write(et_program.buffer)
113+
```
114+
115+
Because `WebGPUPartitioner` delegates unchanged to `VulkanPartitioner`, it can
116+
select operators that do not yet have a WebGPU implementation. Validate the
117+
delegated graph against the WebGPU registry and operator tests. A
118+
Vulkan-supported but WebGPU-unsupported operator can fail when the WebGPU
119+
runtime builds the graph instead of falling back automatically.
120+
121+
## Runtime Integration
122+
123+
Configure a native source build with the WebGPU backend and Dawn:
124+
125+
```bash
126+
# Dawn_DIR must point to Dawn's CMake package directory.
127+
cmake -B cmake-out-webgpu \
128+
-DCMAKE_BUILD_TYPE=Release \
129+
-DEXECUTORCH_BUILD_WEBGPU=ON \
130+
-DDawn_DIR="${Dawn_DIR}"
131+
cmake --build cmake-out-webgpu --target webgpu_backend
132+
```
133+
134+
Link the `webgpu_backend` target into the application. The target exports the
135+
link options needed to retain its static backend and operator registrations.
136+
An exported Vulkan delegate payload will then be handled by WebGPU at runtime.
137+
138+
When building with Emscripten, the backend automatically adds
139+
`--use-port=emdawnwebgpu` to its compile and link options instead of finding a
140+
native Dawn package.
141+
142+
## Profiling
143+
144+
Build with timestamp-query support and enable it at runtime:
145+
146+
```bash
147+
# Dawn_DIR must point to Dawn's CMake package directory.
148+
cmake -B cmake-out-webgpu \
149+
-DEXECUTORCH_BUILD_WEBGPU=ON \
150+
-DEXECUTORCH_BUILD_WEBGPU_PROFILING=ON \
151+
-DDawn_DIR="${Dawn_DIR}"
152+
cmake --build cmake-out-webgpu
153+
154+
export WEBGPU_TIMESTAMP_QUERY=1
155+
```
156+
157+
Run the WebGPU-enabled application normally after exporting the environment
158+
variable. Timestamp queries require adapter support and cannot be used with
159+
chunked execution.
160+
161+
## Reference
162+
163+
**→{doc}`/backends/webgpu/webgpu-op-support` — Operator coverage and limitations.**
164+
165+
```{toctree}
166+
:maxdepth: 2
167+
:hidden:
168+
:caption: WebGPU Backend
169+
170+
webgpu-op-support
171+
```

‎docs/source/desktop-backends.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ Available hardware acceleration backends for desktop platforms.
77

88
- {doc}`desktop-xnnpack` — XNNPACK (CPU acceleration)
99
- {doc}`desktop-openvino` — OpenVINO (Intel hardware optimization)
10+
- {doc}`backends/webgpu/webgpu-overview` — WebGPU acceleration (experimental)
1011

1112
## macOS Backends
1213

1314
- {doc}`backends/coreml/coreml-overview` — CoreML (recommended for Apple Silicon)
1415
- {doc}`backends/mlx/mlx-overview` — MLX (Apple Silicon GPU)
16+
- {doc}`backends/webgpu/webgpu-overview` — WebGPU acceleration (experimental)
1517
- {doc}`desktop-xnnpack` — XNNPACK (CPU acceleration)
1618

1719
## Windows Backends
@@ -24,3 +26,6 @@ Available hardware acceleration backends for desktop platforms.
2426
desktop-xnnpack
2527
desktop-openvino
2628
backends/coreml/coreml-overview
29+
backends/mlx/mlx-overview
30+
backends/webgpu/webgpu-overview
31+
```

0 commit comments

Comments
 (0)