You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add an opt-in native f16 KV cache for WebGPU SDPA — −280 MiB device memory, decode-neutral, default-OFF (byte-identical when off).
Problem: The SDPA K/V cache is stored fp32, so at long context it dominates WebGPU device memory. On a device that negotiated the shader-f16 feature the cache can be stored as native f16 (half the bytes) while attention still accumulates in f32, at no measured decode-speed cost.
Solution: Behind a new opt-in build flag EXECUTORCH_WEBGPU_KV_F16 (defines WGPU_BACKEND_KV_F16, default OFF), and only when the device supports shader-f16 (fail-closed to f32 otherwise):
Before: the K/V caches are allocated fp32 (numel*4 bytes) and read by the f32 SDPA kernels.
After: the K/V caches are allocated on a dedicated f16 buffer (numel*2 bytes, zero-init); SDPA and FlashDecoding select _f16 kernel variants that bind the cache as f16 and widen to f32 on read — compute and accumulation are unchanged.
Implementation:
4 f16 kernel variants, each a storage-dtype delta of its landed f32 kernel (compute/dispatch/tiling unchanged): sdpa_compute_attn_weights_f16 / sdpa_compute_out_f16 bind array<vec4<f16>> and widen with vec4<f32>(...); sdpa_fd_split_f16 binds scalar array<f16> and widens with f32(...); update_cache_f16 writes f16(...).
WebGPUGraph::build: collect the sdpa K/V cache value ids (args[3], args[4]), allocate them as a dedicated half-size f16 buffer at the constant-allocation site, plus a defensive guard that throws if a non-sdpa op would misread an f16 cache.
Sdpa.cpp / SdpaFdDecode.cpp: select the _f16 shader when kv_f16() via a local const char* (mirrors the steel-f16 gate — no function-signature/ABI change).
Analogous to Vulkan's whole-graph force_fp16 fp32→fp16 storage downcast (backends/vulkan/serialization/vulkan_graph_builder.pyget_effective_dtype); here scoped to the K/V cache and gated on the negotiated shader-f16 feature rather than applied graph-wide.
Constraints: the default build (flag OFF) is byte-identical — all f16 code is #ifdef WGPU_BACKEND_KV_F16-gated with no ABI change and no KV-cache-layout migration on the default path; the opt-in build fail-closes to f32 on a non-shader-f16 device; f16 storage requires head_dim % 4 == 0 (already required and guarded).
meta-claBot
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 7, 2026
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"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
2 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack from ghstack (oldest at bottom):
Add an opt-in native f16 KV cache for WebGPU SDPA — −280 MiB device memory, decode-neutral, default-OFF (byte-identical when off).
Problem: The SDPA K/V cache is stored fp32, so at long context it dominates WebGPU device memory. On a device that negotiated the shader-f16 feature the cache can be stored as native f16 (half the bytes) while attention still accumulates in f32, at no measured decode-speed cost.
Solution: Behind a new opt-in build flag
EXECUTORCH_WEBGPU_KV_F16(definesWGPU_BACKEND_KV_F16, default OFF), and only when the device supports shader-f16 (fail-closed to f32 otherwise):numel*4bytes) and read by the f32 SDPA kernels.numel*2bytes, zero-init); SDPA and FlashDecoding select_f16kernel variants that bind the cache as f16 and widen to f32 on read — compute and accumulation are unchanged.Implementation:
sdpa_compute_attn_weights_f16/sdpa_compute_out_f16bindarray<vec4<f16>>and widen withvec4<f32>(...);sdpa_fd_split_f16binds scalararray<f16>and widens withf32(...);update_cache_f16writesf16(...).WebGPUGraph::build: collect the sdpa K/V cache value ids (args[3],args[4]), allocate them as a dedicated half-size f16 buffer at the constant-allocation site, plus a defensive guard that throws if a non-sdpa op would misread an f16 cache.Sdpa.cpp/SdpaFdDecode.cpp: select the_f16shader whenkv_f16()via a localconst char*(mirrors the steel-f16 gate — no function-signature/ABI change).force_fp16fp32→fp16 storage downcast (backends/vulkan/serialization/vulkan_graph_builder.pyget_effective_dtype); here scoped to the K/V cache and gated on the negotiated shader-f16 feature rather than applied graph-wide.Constraints: the default build (flag OFF) is byte-identical — all f16 code is
#ifdef WGPU_BACKEND_KV_F16-gated with no ABI change and no KV-cache-layout migration on the default path; the opt-in build fail-closes to f32 on a non-shader-f16 device; f16 storage requireshead_dim % 4 == 0(already required and guarded).Co-authored-with: Claude Code.
Differential Revision: D110919974