Skip to content

kv-cells: keep the used-cell set as a bitmap - #28245

Closed
ServeurpersoCom wants to merge 1 commit into
ggml-org:masterfrom
ServeurpersoCom:kv-cells-used-bitmap
Closed

kv-cells: keep the used-cell set as a bitmap#28245
ServeurpersoCom wants to merge 1 commit into
ggml-org:masterfrom
ServeurpersoCom:kv-cells-used-bitmap

Conversation

@ServeurpersoCom

@ServeurpersoCom ServeurpersoCom commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Overview

Reduce the generation slowdown as context grows. Split out of #27977 as requested, one PR per change.
Optimize the KV cache bookkeeping: track used cells in a bitmap instead of a std::set.

Additional information

llama_kv_cells tracked the used cells in a std::set<uint32_t>: one tree node per cell, no locality, for a set that is dense by nature (a large fraction of the cache).

This stores it as a bitmap, one 64-bit word per 64 cells. insert/erase become O(1), contains is a bit test, and first/last walk words with ctz/clz instead of chasing nodes; they run once per ubatch. Pure data structure change, no behavior change.

Requirements

used was a std::set<uint32_t>, one tree node per cell with no locality,
for a set that is dense by nature: a large fraction of the cache.

llama_kv_idx_set stores it as a bitmap instead, one word per 64 cells.
insert and erase become O(1), first and last scan the words and are
called once per ubatch.
@ServeurpersoCom

Copy link
Copy Markdown
Contributor Author

No more gain master. Closing.

kv-cells bitmap, 100k ctx, tg 1024:

            pp t/s    tg t/s
master      2692.3    56.68
+ bitmap    2671.7    56.54

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.

1 participant