Skip to content

webgpu : fix race condition in memset - #31162

Merged
yomaytk merged 1 commit into
ggml-org:masterfrom
eapache:ehuus/fix-race-webgpu-memset
Oct 26, 2026
Merged

yomaytk merged 1 commit into
ggml-org:masterfrom
eapache:ehuus/fix-race-webgpu-memset

Conversation

@eapache

@eapache eapache commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Overview

#31157 enabled automatic parallel test workers. This exposed an existing race in the WebGPU backend in CI, e.g. https://github.com/ggml-org/llama.cpp/actions/runs/40218104124/job/121243543710

Concurrent calls to ggml_backend_webgpu_buffer_memset can race between setting and using the shared memset_params_buf. Fix by guarding with the existing mutex.

This showed up in CI consistently with the Grok fragmented KV-cache test. I didn't check this part, but apparently it showed up here specifically because "Grok runs without flash attention, so its transposed f16 value-cache writes can require small, unaligned updates".

A few quick perf checks did not reveal any regressions from using the lock here.

Requirements

@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning WebGPU labels Oct 25, 2026
@eapache eapache changed the title ci : fix race in webgpu memset webgpu : fix race condition in memset Oct 25, 2026
@eapache
eapache marked this pull request as ready for review October 25, 2026 21:15
@eapache
eapache requested a review from a team as a code owner October 25, 2026 21:15

@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.

LGTM, thanks! CI passes.

@yomaytk
yomaytk merged commit 9125a2e into ggml-org:master Oct 26, 2026
23 checks passed
@ggerganov

Copy link
Copy Markdown
Member

@yomaytk Such a global mutex seems a bit strange - is it really necessary to have it? Which part of WebGPU makes it necessary?

@yomaytk

yomaytk commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

@yomaytk Such a global mutex seems a bit strange - is it really necessary to have it? Which part of WebGPU makes it necessary?

Thanks, good catch. After this PR is merged, this global mutex is necessary for memset and get_tensor to avoid conflicts when accessing the intermediate buffers defined in the global context struct. But we can define those buffers and locks in the buffer context struct, which holds the actual buffer of ggml_backend_buffer_t, and it would be better than having a global lock.
On the other hand, it seems that Vulkan uses a mutex defined in the device struct in functions such as get_tensor, so there might be a reason for this design.

If it looks good, I'll open a follow-up PR to address this and check it with the tests.

@ggerganov

Copy link
Copy Markdown
Member

If we can move the buffers to the context and avoid the mutex it would be cleaner IMO.

Not sure about the Vulkan mutex. It's used quite extensively already, so likely it's necessary for some reason.

@yomaytk

yomaytk commented Oct 26, 2026

Copy link
Copy Markdown
Member

Got it, I'll try that design and open a follow-up PR once it's ready.

@eapache
eapache deleted the ehuus/fix-race-webgpu-memset branch October 26, 2026 13:09
auRose94 pushed a commit to auRose94/llama.cpp that referenced this pull request Oct 28, 2026
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 30, 2026
huaxel pushed a commit to huaxel/strix-llama.cpp that referenced this pull request Nov 1, 2026
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 WebGPU

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants