Repository navigation
Give a KV layout a second head shape and compile Gemma 4's attention - #823
Open
kevinzhao-dev wants to merge 1 commit into
Open
kevinzhao-dev wants to merge 1 commit into
kevinzhao-dev wants to merge 1 commit into
Conversation
Gemma 4 31B has two kinds of attention layers. Its 50 sliding layers attend the 1,023 keys before each row with 16 KV heads of 256; its 10 global layers attend the whole history with 4 KV heads of 512, use each key head as its value head and RMS-normalize the values without a weight. Both score with a scale of 1. One page layout now holds both shapes and the kernels compile both geometries; no target uses them yet, and every existing kernel compiles as before. - kv::Layout gains windowKvHeads and windowHeadDimension, a second head shape for its windowed regions, whose bytes follow the whole-history regions in each extent. layerLayout(layer) gives a layer its own shape and kvBytesBeforeRegion(count) the bytes of a page's first regions, which PageStorage, the page sizes and the extent alignment use. Without a second shape each is what it was. - kCompiledAttention gains bf16Only, wholeHistory and sharedKv, and its commitSuffix becomes optional for pages that never leave their extent. Its new rows are _kv4_g8_d512 (shared K/V, normalized values) and _kv16_g2_w1023 (the window alone), both BF16. No compiled geometry matches a layout with a second shape: a target selects each layer's by layerLayout(layer). - The QKV prepare takes NormalizeValue and SharedKv and the attention tile UnitScale, each defaulting to what the existing geometries compile. - attention-variants runs both geometries in one sequence against FP64 across a Page32 boundary and a Slim relocation, and checks every row's stored K and V against FP64 from the raw projection, so the shared K/V and the value norm are checked too; q8-paged-kv checks the two page shapes in both formats; engine-cache checks that a 1,024-key window trims a page only once every row has passed it and keeps the global layers' history. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kevinzhao-dev
force-pushed
the
exp/gemma4-kv-attention
branch
from
November 25, 2026 09:24
a011362 to
b4f007c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #8.
Gemma 4 31B has two kinds of attention layers, and a Splash KV layout holds one head shape. This PR lets a layout hold a second shape and compiles Gemma 4's two attention geometries. No target uses them yet. The engine target is the next PR.
From
google/gemma-4-31B-it'sconfig.json:sliding_window: 1024, sowindowTokens = 1023).attention_k_eq_v), and the values are RMS-normalized without a weight.26B-A4B has the same layer pattern, window and K-as-V, with half the heads: 16 query heads, 8 × 256 on sliding layers and 2 × 512 on global ones. It can use this layout and these template switches, but needs two geometry rows of its own.
What changes
kv::LayoutgainswindowKvHeadsandwindowHeadDimension, a second head shape for the windowed regions. In each extent, their bytes follow the whole-history regions.layerLayout(layer)gives each layer its own shape.kvBytesBeforeRegion(count)gives the bytes of a page's firstcountregions.PageStorage, the page sizes and the extent alignment now use it._kv4_g8_d512for the global layers: shared K/V and normalized values._kv16_g2with a 1,023-key window for the sliding layers.NormalizeValueandSharedKv, and the attention tile gainsUnitScale. Each defaults to what the existing geometries compile.kCompiledAttentiongainsbf16Only,wholeHistoryandsharedKv, andcommitSuffixbecomes optional.What changes for existing users
Nothing.
storesLayoutrefuses a layout with a second shape, so no existing model can select the new rows.Testing
MacBook Pro, M5 Pro (18-core CPU, 20-core GPU), 64 GB, macOS 26.6.2, on main at d40c250.
attention-variants, underMTL_SHADER_VALIDATION=1. One sequence runs 1,064 tokens through both layers, which share a page table. The run crosses a Page32 boundary and a Slim relocation.NormalizeValueoff, and when the global layer reads its values from a V slot instead of its keys. I checked both by breaking the kernel locally.q8-paged-kv: both page shapes, in both formats.engine-cache: a 1,024-key window trims a page only after every row has passed it, and the global layers keep their history.make check-sourceandmake check-native-enginepass: the architecture check, the CPU and Metal suites (Metal underMTL_SHADER_VALIDATION=1), and the Metal suites again under ThreadSanitizer.python3 dev/tools/kernel_identity.py MAIN.metallib build/splash.metallib, withMAIN.metallibbuilt from d40c250, reports 2,309 of 2,317 kernels identical. The other 8 exist only in this PR.Known limitations
What comes next
The whole port runs on my fork (kevinzhao-dev#1). It sits on this PR's previous version, which was on 1.7.2. On the same GGUF, greedy output matches llama.cpp on three prompts, and one of them goes past the window. The numbers are in #8. To reproduce them, the fork has
dev/tools/gemma4_parity.pyanddev/benchmarks/gemma4_benchmark.mm.Next I'd send the engine target: Gemma 4 as a layout that the Qwen target runs, as it runs Flint. DFlash v1 in the runtime would come after that. If you'd rather this took a different shape, I'm happy to change it.
🤖 Generated with Claude Code