Skip to content

webgpu : fix CI by keeping scratch alloc sizes monotonic in batch length - #31422

Merged
ggerganov merged 3 commits into
masterfrom
pascal/webgpu-monotonic-alloc-size
Nov 2, 2026
Merged

ggerganov merged 3 commits into
masterfrom
pascal/webgpu-monotonic-alloc-size

Conversation

@ServeurpersoCom

Copy link
Copy Markdown
Contributor

Overview

Since #31300, the self-hosted WebGPU CI fails in test-llama-context: the comp_padding case on the DeepSeek V4 models aborts with "unexpected graph reallocation", on both the Apple and NVIDIA runners.

The cause is in how WebGPU sizes some tensors. For small batches, flash attention (under 20 query rows) and MMVQ (up to 4 rows) take a faster path that needs extra scratch space right after the output, and get_alloc_size only counted that space when the current batch took the small path.

So the size did not grow with the batch. The graph reserved at n_ubatch takes the large path and gets no scratch, the one reserved at one token gets scratch for a single row, and a 5-token batch ends up needing more than either: 95072 bytes for a FLASH_ATTN_EXT output reserved at 65536.

This PR always reserves the scratch of the largest batch the small path can serve, so a reservation made at n_ubatch covers every smaller batch.

Additional information

This follows what Metal already does for its flash attention temp buffer.

Tested on a rootless container with an RTX PRO 6000, with the CI Dawn release and GGML_SCHED_NO_REALLOC=ON. comp_padding now passes on both models, the main ctest label of the job is 17/17, and test-backend-ops on WebGPU passes for FLASH_ATTN_EXT and MUL_MAT.

Fixes #31346 (the WebGPU part that remained after #31344).

Requirements

The flash attention vec path and the MMVQ path append a scratch area
after dst, but get_alloc_size only accounted for it when the current
batch takes that path. The size was therefore not monotonic in the
batch length: a graph reserved at n_ubatch (tiled path, no scratch)
or at one token did not cover batches of 2 to 19 query rows, and
GGML_SCHED_NO_REALLOC builds aborted on an unexpected reallocation.

Pass the row count to both path predicates and size the scratch with
the row count clamped to the longest batch the short path serves.
@ServeurpersoCom
ServeurpersoCom requested a review from a team as a code owner October 30, 2026 13:08
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning WebGPU labels Oct 30, 2026

@yomaytk yomaytk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Yes, reserving the scratch for the largest small-path batch looks fine, and the changes looks good to me.

@yomaytk yomaytk added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Nov 2, 2026
@ggerganov
ggerganov merged commit 1c5ed70 into master Nov 2, 2026
22 of 25 checks passed
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Nov 6, 2026
…gth (ggml-org#31422)

* webgpu: keep scratch alloc sizes monotonic in the batch length

The flash attention vec path and the MMVQ path append a scratch area
after dst, but get_alloc_size only accounted for it when the current
batch takes that path. The size was therefore not monotonic in the
batch length: a graph reserved at n_ubatch (tiled path, no scratch)
or at one token did not cover batches of 2 to 19 query rows, and
GGML_SCHED_NO_REALLOC builds aborted on an unexpected reallocation.

Pass the row count to both path predicates and size the scratch with
the row count clamped to the longest batch the short path serves.

* nit: align the mmvq pipeline key with clang-format 22

* webgpu: address review @yomaytk, name the mat-vec row threshold

(cherry picked from commit 1c5ed70)
gagallo7 pushed a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Nov 10, 2026
…gth (ggml-org#31422)

* webgpu: keep scratch alloc sizes monotonic in the batch length

The flash attention vec path and the MMVQ path append a scratch area
after dst, but get_alloc_size only accounted for it when the current
batch takes that path. The size was therefore not monotonic in the
batch length: a graph reserved at n_ubatch (tiled path, no scratch)
or at one token did not cover batches of 2 to 19 query rows, and
GGML_SCHED_NO_REALLOC builds aborted on an unexpected reallocation.

Pass the row count to both path predicates and size the scratch with
the row count clamped to the longest batch the short path serves.

* nit: align the mmvq pipeline key with clang-format 22

* webgpu: address review @yomaytk, name the mat-vec row threshold

Assisted-by: Claude Code
(cherry picked from commit 1c5ed70)
iancris pushed a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Nov 19, 2026
…gth (ggml-org#31422)

* webgpu: keep scratch alloc sizes monotonic in the batch length

The flash attention vec path and the MMVQ path append a scratch area
after dst, but get_alloc_size only accounted for it when the current
batch takes that path. The size was therefore not monotonic in the
batch length: a graph reserved at n_ubatch (tiled path, no scratch)
or at one token did not cover batches of 2 to 19 query rows, and
GGML_SCHED_NO_REALLOC builds aborted on an unexpected reallocation.

Pass the row count to both path predicates and size the scratch with
the row count clamped to the longest batch the short path serves.

* nit: align the mmvq pipeline key with clang-format 22

* webgpu: address review @yomaytk, name the mat-vec row threshold

(cherry picked from commit 1c5ed70)

Backport: ggml_webgpu_can_use_mmvq keeps fabric's dst argument, n_rows goes after it.

Assisted-by: Claude Opus 5.5
gianni-cor pushed a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Nov 19, 2026
…gth (ggml-org#31422) (#559)

* webgpu: keep scratch alloc sizes monotonic in the batch length

The flash attention vec path and the MMVQ path append a scratch area
after dst, but get_alloc_size only accounted for it when the current
batch takes that path. The size was therefore not monotonic in the
batch length: a graph reserved at n_ubatch (tiled path, no scratch)
or at one token did not cover batches of 2 to 19 query rows, and
GGML_SCHED_NO_REALLOC builds aborted on an unexpected reallocation.

Pass the row count to both path predicates and size the scratch with
the row count clamped to the longest batch the short path serves.

* nit: align the mmvq pipeline key with clang-format 22

* webgpu: address review @yomaytk, name the mat-vec row threshold

(cherry picked from commit 1c5ed70)

Backport: ggml_webgpu_can_use_mmvq keeps fabric's dst argument, n_rows goes after it.

Assisted-by: Claude Opus 5.5

Co-authored-by: Pascal <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. WebGPU

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misc. bug: test-llama-context crashing on deepseek41-moe.gguf

3 participants