Skip to content

WebGPU: Optimize GraphCacheManager by removing pending_buffers - #27453

Merged
qjia7 merged 1 commit into
mainfrom
jiajiaqin/webgpu-optimize-graph-cache-manager
Feb 26, 2026
Merged

WebGPU: Optimize GraphCacheManager by removing pending_buffers#27453
qjia7 merged 1 commit into
mainfrom
jiajiaqin/webgpu-optimize-graph-cache-manager

Conversation

@qjia7

@qjia7 qjia7 commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

This pull request refactors the buffer management logic in the GraphCacheManager class to simplify buffer handling and improve initialization. The main changes focus on removing the use of a pending buffer queue and ensuring buckets are properly initialized.

Benefits:
✅ Simpler, more efficient code
✅ Immediate buffer reuse - no waiting for OnRefresh()
✅ Less memory overhead

@qjia7
qjia7 merged commit c6491e0 into main Feb 26, 2026
90 of 91 checks passed
@qjia7
qjia7 deleted the jiajiaqin/webgpu-optimize-graph-cache-manager branch February 26, 2026 00:18
feich-ms added a commit that referenced this pull request Jun 11, 2026
Remove pending_buffers_ intermediate queue. Instead, track the current
GraphCaptureState in a member and branch directly in ReleaseBuffer:
Default mode does immediate bucket reuse (preserving PR #27453's
optimization), Capturing/Replaying quarantines to captured_buffers_.

Co-Authored-By: Claude Opus 4 (1M context) <noreply@anthropic.com>
feich-ms added a commit that referenced this pull request Jun 11, 2026
Three changes required for graph capture to work on models with
KV-shared layers (e.g. Gemma4), independent of how the model was built:

1. ConstantOfShape WebGPU kernel — KV-shared layers use ConstantOfShape
   to create zero-filled tensors. Without a WebGPU kernel, these fall
   back to CPU, flooding the captured graph with MemcpyFromHost.
   Registered opset-by-opset (9, 20, 21, 23, 24) to match ONNX spec
   changes and KernelRegistry::VerifyVersion semantics.

2. GraphCacheManager buffer quarantine — during graph capture/replay,
   released buffers must not be immediately reused because captured
   bind_groups still reference them. Tracks GraphCaptureState in a
   member; Default mode keeps PR #27453 immediate-reuse optimization,
   Capturing/Replaying quarantines to captured_buffers_.

3. PrepareIndirectDispatch for kv_empty layers — KV-shared layers skip
   CopyKVCache (no new K/V to append), which normally populates the
   indirect dispatch buffer. Under graph capture, step 2+ crashes with
   Invalid dispatch group size (0, 1, 1). A single-thread shader now
   fills the buffer from seqlen_k[0]+1 when CopyKVCache is skipped.

Co-Authored-By: Claude Opus 4 (1M context) <noreply@anthropic.com>
feich-ms added a commit that referenced this pull request Jun 11, 2026
Three changes required for graph capture to work on models with
KV-shared layers (e.g. Gemma4), independent of how the model was built:

1. ConstantOfShape WebGPU kernel — KV-shared layers use ConstantOfShape
   to create zero-filled tensors. Without a WebGPU kernel, these fall
   back to CPU, flooding the captured graph with MemcpyFromHost.
   Registered opset-by-opset (9, 20, 21, 23, 24) to match ONNX spec
   changes and KernelRegistry::VerifyVersion semantics.

2. GraphCacheManager buffer quarantine — during graph capture/replay,
   released buffers must not be immediately reused because captured
   bind_groups still reference them. Tracks GraphCaptureState in a
   member; Default mode keeps PR #27453 immediate-reuse optimization,
   Capturing/Replaying quarantines to captured_buffers_.

3. PrepareIndirectDispatch for kv_empty layers — KV-shared layers skip
   CopyKVCache (no new K/V to append), which normally populates the
   indirect dispatch buffer. Under graph capture, step 2+ crashes with
   Invalid dispatch group size (0, 1, 1). A single-thread shader now
   fills the buffer from seqlen_k[0]+1 when CopyKVCache is skipped.

Co-Authored-By: Claude Opus 4 (1M context) <noreply@anthropic.com>
feich-ms added a commit that referenced this pull request Jun 11, 2026
Three changes required for graph capture to work on models with
KV-shared layers (e.g. Gemma4), independent of how the model was built:

1. ConstantOfShape WebGPU kernel — KV-shared layers use ConstantOfShape
   to create zero-filled tensors. Without a WebGPU kernel, these fall
   back to CPU, flooding the captured graph with MemcpyFromHost.
   Registered opset-by-opset (9, 20, 21, 23, 24) to match ONNX spec
   changes and KernelRegistry::VerifyVersion semantics.

2. GraphCacheManager buffer quarantine — during graph capture/replay,
   released buffers must not be immediately reused because captured
   bind_groups still reference them. Tracks GraphCaptureState in a
   member; Default mode keeps PR #27453 immediate-reuse optimization,
   Capturing/Replaying quarantines to captured_buffers_.

3. PrepareIndirectDispatch for kv_empty layers — KV-shared layers skip
   CopyKVCache (no new K/V to append), which normally populates the
   indirect dispatch buffer. Under graph capture, step 2+ crashes with
   Invalid dispatch group size (0, 1, 1). A single-thread shader now
   fills the buffer from seqlen_k[0]+1 when CopyKVCache is skipped.

Co-Authored-By: Claude Opus 4 (1M context) <noreply@anthropic.com>
feich-ms added a commit that referenced this pull request Jun 11, 2026
Tested end-to-end: graph capture produces correct output ("2+2 is 4")
with PR #27453's immediate buffer reuse. The quarantine was incorrectly
attributed as a fix during the original GC investigation. Phi-4 graph
capture also works without it, confirming immediate reuse is safe.

Co-Authored-By: Claude Opus 4 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants