[ExecuTorch][WebGPU] Add Qwen3 K16 attention and fp16 KV cache#21136
[ExecuTorch][WebGPU] Add Qwen3 K16 attention and fp16 KV cache#21136JCNTH wants to merge 6 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/21136
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ✅ No FailuresAs of commit 2cfbf64 with merge base 86c3470 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
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: 405473339 @exported-using-ghexport Differential Revision: [D113171744](https://our.internmc.facebook.com/intern/diff/D113171744/)
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: 405484520 @exported-using-ghexport Differential Revision: [D113171744](https://our.internmc.facebook.com/intern/diff/D113171744/)
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: 405709851 @exported-using-ghexport Differential Revision: [D113171744](https://our.internmc.facebook.com/intern/diff/D113171744/)
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: 405717908 @exported-using-ghexport Differential Revision: [D113171744](https://our.internmc.facebook.com/intern/diff/D113171744/)
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: 405727519 @exported-using-ghexport Differential Revision: [D113171744](https://our.internmc.facebook.com/intern/diff/D113171744/)
Stack from ghstack (oldest at bottom):
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_tileruntime 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:
streaming_attention_qwen3_q32_k16_causal_bound.wgsl (+ generated headers): the
Q16 and Q32 online-softmax Qwen3 kernels.
default route selection, and the fp32-host to fp16-device KV-cache conversion.
sdpa_query_tileruntime spec and threadit into graph build so the Q32 tile can be requested without a rebuild.
headers with a regression test.
@exported-using-ghexport
Differential Revision: D113171744
Differential Revision: D113171744