Reduce KV memory for local-attention layers

Open
#1,016 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Active
Tech stack
cpp

Research direction

Start by locating the runtime-aware cache constructor, the default Flash attention path, and the tiled attention backends described in the issue. Trace how local and global layers allocate and read BF16 K/V buffers, then verify that resizing, snapshots, cache reuse, shared-KV layers, and backend selection preserve history and existing attention behavior.

Written by the indexing model from the issue text.

Description

Problem

On dev (1658f88), the default flash attention path reserves full-context BF16 KV/K/V storage for local and global layers alike. Local layers only attend to their sliding window, so retaining capacity for the entire context wastes memory as the configured context grows.

The runtime-aware cache constructor also allocates compact tiled buffers that the default Flash path does not use. The tiled attention backends already have compact local rings; the default Flash path still reads the legacy transposed K/V buffers.

Fix direction

  • Allocate separate BF16 buffers per owning layer for the default Flash path.
  • Size local rings for the attention window plus the full prefill batch and trailing SIMD padding, capped by the configured context. The extra rows prevent batch writes and padding from overwriting history needed by early queries.
  • Keep global layers at the configured context capacity, with the logical context limit separate from physical alignment padding.
  • Reuse the source layer's buffers for shared-KV layers and retain the largest window required by their consumers.
  • Preserve live history when runtime batches require larger rings, keep snapshots independent, and clear buffers safely when reusing a cache.
  • Allocate only the buffers used by the selected attention backend and preserve the existing BF16 attention arithmetic and model-specific cache layouts.
Dominant language
C++
Stars
7k
Forks
660
Avg merge
20h 43m
Merged PRs (30d)
33

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from google/gemma.cpp

All issues in google/gemma.cpp

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.