google / google/gemma.cpp

Reduce KV memory for local-attention layers

Offen
#1,016 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
C++
Sterne
7k
Forks
660
Ø Merge
20 Std. 43 Min.
Gemergte PRs (30 T.)
33

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne damit, den runtime-bewussten Cache-Konstruktor, den standardmäßigen Flash-Attention-Pfad und die im Issue beschriebenen Tiled-Attention-Backends zu lokalisieren. Verfolge, wie lokale und globale Layer BF16-K/V-Puffer allokieren und lesen, und überprüfe anschließend, dass Resizing, Snapshots, die Wiederverwendung des Caches, Layer mit gemeinsamem K/V und die Backend-Auswahl den bisherigen Verlauf und das bestehende Attention-Verhalten bewahren.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
cpp
Bereich
machine-learning, performance
Issue-Typ
Refactoring
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Aktiv
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.