Add StaticCache: fixed-capacity in-place KV cache for WebGPU - #1745
Open
kylo5aby wants to merge 2 commits into
Open
Add StaticCache: fixed-capacity in-place KV cache for WebGPU#1745kylo5aby wants to merge 2 commits into
kylo5aby wants to merge 2 commits into
Conversation
Signed-off-by: Zhenwei Jin <zhenwei.jin@intel.com>
Signed-off-by: Zhenwei Jin <zhenwei.jin@intel.com>
kylo5aby
marked this pull request as draft
August 13, 2026 09:35
kylo5aby
marked this pull request as ready for review
August 14, 2026 05:37
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds an opt-in
StaticCachethat eliminates per-step KV cache reallocation on WebGPU.Fixes #1741
With
DynamicCache(the default), every decode step replaces every KV tensor with a slightly larger one. On WebGPU each replacement destroys and re-creates a zero-filled GPU buffer, so the per-step cost grows with context length and jumps permanently at the buffer-pool bucket boundaries, producing a staircase-shaped latency curve.StaticCacheallocates each cache entry once at a fixedmax_cache_len, and binds the model'spresent.*outputs in place onto the same GPU buffers viasession.run(feeds, fetches). Decoding performs zero cache (re-)allocations per step.Usage
Performance
Decode throughput, q4f16 on WebGPU (TPS), 256 new tokens:
The gain grows with context length:
DynamicCachethroughput degrades as buffers get larger whileStaticCachestays nearly flat. Qwen3.5 (hybrid attention) benefits less because only its full-attention layers have growing KV entries. In our measurementsStaticCachematches a hand-written ORT static-KV baseline within 2%.Correctness
Token-identical output vs
DynamicCache(greedy, 256 new tokens, crossing the 2048/4096 bucket boundaries), on Phi-4-mini-instructand Qwen3.5-4B, Both paths are self-deterministic.Constraints
Enforced with explicit errors,
DynamicCacheremains the default everywhere:past_present_share_buffersemantics — true for GQA-based exports, which current WebGPU decoder exports use). This is why the feature is opt-in.