Default flash attention retains redundant BF16 KV projection history
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 68/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- cpp
- Domain
- machine-learning, performance
Research direction
Read gemma/attention.cc lines 208-322 and gemma/kv_cache.cc lines 275-297 first to trace the projection, transpose, and cache allocations. Reproduce the default-flash Gemma 3 workload with the stated prompt and sequence settings, then inspect cache extents and peak RSS. Done means the redundant KV history is not retained while context length, BF16 rounding, and model behavior remain unchanged.
Written by the indexing model from the issue text.
Description
Summary
Default flash attention retains two BF16 representations of KV history.
The sequence-major projection buffer remains allocated and populated after
its keys and values have been transposed into the buffers attention reads.
This increases resident memory as context length grows.
Affected path
- Backend: default
--attention_impl flash. - Confirmed with Gemma 3 270M, 1B, and 4B text inference.
- Measured baseline:
ffc1abc05abdf11d875d25d36c8859553ddf2641. - The retained-history path is also present on current
dev(b68def1). - T5 and DeepSeek use their legacy buffers differently.
What happens
ComputeQKVwrites BF16 projections into sequence-majorkv_cache.- It applies normalization and positional encoding with BF16 rounding.
- It transposes the results into
k_cacheandv_cache. FlashAttentionconsumes those transposed buffers.- The original projection buffer still retains every layer and position.
References:
Reproduction and observed cost
Run default-flash Gemma 3 270M with a 32,736-token text prompt,
sequence capacity 32,768, prefill batch 4,096, and 16 decode tokens.
Inspect the allocated cache extents and peak process RSS during inference.
The projection buffer occupies 578 MiB including row padding.
The transposed K/V buffers occupy another 576 MiB.
Measured peak process RSS is 1,778.59 MiB on this workload.
This is active retained data, not merely unused virtual address space.
Environment: Linux, Intel i5-12400F, six pinned threads,
Release AVX2/Haswell build, no oneDNN, and approximately 15.5 GiB RAM.
Expected behavior
Projection intermediates should not retain a second complete KV history
after attention's persistent representation has been produced.
Required context, existing BF16 rounding, and model behavior must be preserved.
- Dominant language
- C++
- Stars
- 7k
- Forks
- 660
- Avg merge
- 20h 43m
- Merged PRs (30d)
- 33
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from google/gemma.cpp
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
Difficulty 4/5 3-5 days Newbie friendliness 64/100
All issues in google/gemma.cpp
Similar issues
-
enhancement
Difficulty 1/5 Under an hour Newbie friendliness 88/100
QuantStack/git2cpp#187 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
mlcommons/mobile_app_open#1182 ·
-
Needs-Triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
microsoft/winget-cli#6547 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
AXERA-TECH/ax-llm#77 ·