Skip to content

[ExecuTorch][WebGPU] Add Qwen3 K16 attention and fp16 KV cache#21136

Open
JCNTH wants to merge 6 commits into
gh/JCNTH/112/basefrom
gh/JCNTH/112/head
Open

[ExecuTorch][WebGPU] Add Qwen3 K16 attention and fp16 KV cache#21136
JCNTH wants to merge 6 commits into
gh/JCNTH/112/basefrom
gh/JCNTH/112/head

Conversation

@JCNTH

@JCNTH JCNTH commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

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_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.
    @exported-using-ghexport

Differential Revision: D113171744

Differential Revision: D113171744

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jul 22, 2026

Copy link
Copy Markdown

🔗 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 SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ No Failures

As of commit 2cfbf64 with merge base 86c3470 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 22, 2026
[ghstack-poisoned]
JCNTH added a commit that referenced this pull request Jul 22, 2026
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/)
[ghstack-poisoned]
JCNTH added a commit that referenced this pull request Jul 22, 2026
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/)
[ghstack-poisoned]
JCNTH added a commit that referenced this pull request Jul 22, 2026
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/)
[ghstack-poisoned]
JCNTH added a commit that referenced this pull request Jul 22, 2026
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/)
[ghstack-poisoned]
JCNTH added a commit that referenced this pull request Jul 22, 2026
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/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant