Skip to content

llama: fix k-pool scatter data race on shared sequences - #29994

Merged
ServeurpersoCom merged 3 commits into
ggml-org:masterfrom
ServeurpersoCom:kpool-shared-rep-tsan
Oct 6, 2026
Merged

ServeurpersoCom merged 3 commits into
ggml-org:masterfrom
ServeurpersoCom:kpool-shared-rep-tsan

Conversation

@ServeurpersoCom

Copy link
Copy Markdown
Contributor

Overview

The k-pool models write their pooled keys back even when sequences share cells after a seq_cp, so a shared pool was written twice from different threads, which TSan flags in test_shared_seq_reserve.

First commit re-pools each shared pool once, second commit asserts that seq_cp copies whole sequences, since a partial copy cannot work with shared pools.

TSan is clean on glm5-next and qwen4exp.

Additional information

Follow-up #29958

Requirements

With shared cells every pool is re-pooled, and since the pooled keys
are always scattered, the pools a seq_cp shares between sequences
wrote the same rep row from several scatter entries, a data race on
the CPU backend. Mark each rep once: the sharing sequences read the
same row through pool_cells.
The recurrent state is always copied whole whatever the range, and a
k-pool cell shared by a partial copy could carry two pool groupings
with a single pooled row. Every caller copies whole sequences, so
reject partial ranges instead of supporting them.

@am17an am17an left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we should try and remove this cache_safe logic. It's just not intuitive in any way.

@ServeurpersoCom

Copy link
Copy Markdown
Contributor Author

we should try and remove this cache_safe logic. It's just not intuitive in any way.

I'm going it in a 3rd commit. Look like the assertion in the second commit make cache_safe obsolete!

With whole-sequence seq_cp, sequences sharing cells share their pools
too, so the pooled row of a shared rep is valid for all of them. Mark
each rep once in every ubatch instead of re-pooling everything while
cells are shared, which removes the sharing scan and the stale-all
workarounds in seq_rm, state_read and state_drop. seq_cp now only
stales the destination.
@ServeurpersoCom

Copy link
Copy Markdown
Contributor Author

Pushed: cache_safe, the sharing scan and the stale-all workarounds in seq_rm, state_read and state_drop are gone, each rep is pooled once per ubatch and seq_cp only stales the destination. Shared vs unshared decode match after seq_cp (CPU and CUDA), TSan clean on glm5-next and qwen4exp.

@ServeurpersoCom
ServeurpersoCom requested a review from am17an October 5, 2026 18:53
@github-actions github-actions Bot added the model Model specific label Oct 5, 2026
@ServeurpersoCom
ServeurpersoCom merged commit 43fe9c6 into ggml-org:master Oct 6, 2026
17 of 19 checks passed
gagallo7 pushed a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Oct 14, 2026
* llama: re-pool each shared k-pool rep once

With shared cells every pool is re-pooled, and since the pooled keys
are always scattered, the pools a seq_cp shares between sequences
wrote the same rep row from several scatter entries, a data race on
the CPU backend. Mark each rep once: the sharing sequences read the
same row through pool_cells.

* llama: assert whole-sequence seq_cp in the hybrid idx memory

The recurrent state is always copied whole whatever the range, and a
k-pool cell shared by a partial copy could carry two pool groupings
with a single pooled row. Every caller copies whole sequences, so
reject partial ranges instead of supporting them.

* llama: drop the k-pool cache_safe mode

With whole-sequence seq_cp, sequences sharing cells share their pools
too, so the pooled row of a shared rep is valid for all of them. Mark
each rep once in every ubatch instead of re-pooling everything while
cells are shared, which removes the sharing scan and the stale-all
workarounds in seq_rm, state_read and state_drop. seq_cp now only
stales the destination.

(cherry picked from commit 43fe9c6)

[ dropped qwen4exp.cpp, comment-only. ]

Assisted-by: Claude Opus 5.5
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 24, 2026
auRose94 pushed a commit to auRose94/llama.cpp that referenced this pull request Oct 28, 2026
* llama: re-pool each shared k-pool rep once

With shared cells every pool is re-pooled, and since the pooled keys
are always scattered, the pools a seq_cp shares between sequences
wrote the same rep row from several scatter entries, a data race on
the CPU backend. Mark each rep once: the sharing sequences read the
same row through pool_cells.

* llama: assert whole-sequence seq_cp in the hybrid idx memory

The recurrent state is always copied whole whatever the range, and a
k-pool cell shared by a partial copy could carry two pool groupings
with a single pooled row. Every caller copies whole sequences, so
reject partial ranges instead of supporting them.

* llama: drop the k-pool cache_safe mode

With whole-sequence seq_cp, sequences sharing cells share their pools
too, so the pooled row of a shared rep is valid for all of them. Mark
each rep once in every ubatch instead of re-pooling everything while
cells are shared, which removes the sharing scan and the stale-all
workarounds in seq_rm, state_read and state_drop. seq_cp now only
stales the destination.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model Model specific

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants