Commit e53fa18
committed
[ExecuTorch][WebGPU] Add Qwen3 K16 attention and fp16 KV cache
Pull Request resolved: #21136
Qwen3's attention geometry differs from Llama's, and its KV cache is produced in
fp32 on the host but must be consumed in fp16 on the device. This adds guarded
Qwen3 K16 streaming online-softmax attention schedules — a Q16 schedule that is
the automatic default whenever the exact geometry and capability guards pass,
plus a Q32 candidate that is opt-in through the `sdpa_query_tile` runtime spec
(BackendOption) for future autotuning — together with the exact fp32-host to
fp16-device KV-cache boundary conversion. Selection requires the exact Qwen3
geometry, fp16 KV storage, adapter limits, a valid workgroup count, and an exact
2:1 byte ratio; the established Llama, materialized, and FlashDecoding routes
remain fallbacks. This builds on the HuggingFace rotate-half
RoPE operator. No Vulkan analogue (WebGPU-specific online-softmax attention;
Vulkan has only a materialized attention). It also makes the long generated WGSL
provenance and constant declarations format-stable and covers them with a
generator regression test.
Key changes:
- runtime/ops/sdpa/streaming_attention_qwen3_k16_causal_bound.wgsl and
streaming_attention_qwen3_q32_k16_causal_bound.wgsl (+ generated headers): the
Q16 and Q32 online-softmax Qwen3 kernels.
- Sdpa.cpp, WebGPUGraph.{cpp,h}: exact Qwen3 geometry and limit guards, Q16
default route selection, and the fp32-host to fp16-device KV-cache conversion.
- WebGPUBackend.cpp: read the optional `sdpa_query_tile` runtime spec and thread
it into graph build so the Q32 tile can be requested without a rebuild.
- scripts/gen_wgsl_headers.py (+ test_wgsl_codegen.py): format-stable generated
headers with a regression test.
ghstack-source-id: 411961459
@exported-using-ghexport
Differential Revision: [D113171744](https://our.internmc.facebook.com/intern/diff/D113171744/)1 parent 2246c4c commit e53fa18
19 files changed
Lines changed: 2488 additions & 122 deletions
File tree
- backends/webgpu
- runtime
- ops/sdpa
- scripts
- test
- native
- ops
- dynamic_shape
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
| 93 | + | |
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
104 | 115 | | |
105 | 116 | | |
106 | 117 | | |
| |||
125 | 136 | | |
126 | 137 | | |
127 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
128 | 145 | | |
129 | 146 | | |
130 | 147 | | |
131 | | - | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
132 | 153 | | |
133 | 154 | | |
134 | 155 | | |
| |||
163 | 184 | | |
164 | 185 | | |
165 | 186 | | |
| 187 | + | |
| 188 | + | |
166 | 189 | | |
167 | | - | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
168 | 194 | | |
169 | 195 | | |
170 | 196 | | |
| |||
205 | 231 | | |
206 | 232 | | |
207 | 233 | | |
208 | | - | |
| 234 | + | |
209 | 235 | | |
210 | 236 | | |
211 | 237 | | |
212 | 238 | | |
213 | | - | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
214 | 243 | | |
215 | 244 | | |
216 | 245 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
| 68 | + | |
| 69 | + | |
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
73 | 74 | | |
74 | | - | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
75 | 81 | | |
76 | 82 | | |
77 | 83 | | |
| |||
91 | 97 | | |
92 | 98 | | |
93 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
94 | 107 | | |
95 | 108 | | |
96 | 109 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| |||
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
21 | | - | |
| 23 | + | |
| 24 | + | |
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
| |||
0 commit comments