Grouped-query attention

Repeat value heads

×

Copy each value head to every query head in its group.

VG[h]=CV[0:T][⌊h/4⌋]{\htmlData{light=grouped-values v-repeat}{{\htmlClass{vl-stream-v}{V_{\mathrm G}}}}}[h]={\htmlData{light=kv-cache}{C_V}}[0:T]\big[\lfloor h/4\rfloor\big]
in
CVC_{V} [B batch, 8 kv heads, T positions, 128 d_h]
out
VGV_{\mathrm G} [B batch, 32 q heads, T positions, 128 d_h]

Reads rows 0 … T − 1 of the cache, after this call's write.

Explanation

Heads are counted from zero. The repeat runs after the cache is written and read (model.py 165–177), so the cache holds 8 heads.

KV head 0Q 0Q 1Q 2Q 3
One of 8 groups: query heads 0–3 share the same key/value pair. The association repeats across 32 query heads.
Sources

In coderepeat_kv(values, self.n_rep) · 175

Open pinned code ↗

Direct evidence

repeat_kv copies each KV head n_rep times (model.py 78–87).

model.py 78–87 ↗ · meta-llama/llama3 @ a0940f9cf706

Excerpt
def repeat_kv(x: torch.Tensor, n_rep: int) -> torch.Tensor:
    """torch.repeat_interleave(x, dim=2, repeats=n_rep)"""
    bs, slen, n_kv_heads, head_dim = x.shape
    if n_rep == 1:
        return x
    return (
        x[:, :, :, None, :]
        .expand(bs, slen, n_kv_heads, n_rep, head_dim)
        .reshape(bs, slen, n_kv_heads * n_rep, head_dim)
    )

The cache is allocated at n_kv heads (129–144), written and read at that size, and the repeat follows the read (165–177).

model.py 129–177 ↗ · meta-llama/llama3 @ a0940f9cf706

Excerpt
        self.cache_k = torch.zeros(
            (
                args.max_batch_size,
                args.max_seq_len,
                self.n_local_kv_heads,
                self.head_dim,
            )
        ).cuda()
        self.cache_v = torch.zeros(
            (
                args.max_batch_size,
                args.max_seq_len,
                self.n_local_kv_heads,
                self.head_dim,
            )
        ).cuda()

… lines 145–164 omitted …

        self.cache_k[:bsz, start_pos : start_pos + seqlen] = xk
        self.cache_v[:bsz, start_pos : start_pos + seqlen] = xv

        keys = self.cache_k[:bsz, : start_pos + seqlen]
        values = self.cache_v[:bsz, : start_pos + seqlen]

        # repeat k/v heads if n_kv_heads < n_heads
        keys = repeat_kv(
            keys, self.n_rep
        )  # (bs, cache_len + seqlen, n_local_heads, head_dim)
        values = repeat_kv(
            values, self.n_rep
        )  # (bs, cache_len + seqlen, n_local_heads, head_dim)

Meta's Llama 3.1 8B base catalogue entry supplies dimensions, head counts, the vocabulary (LLAMA3_VOCAB_SIZE, line 19), MLP rounding, epsilon, rope_theta and use_scaled_rope: True.

sku_list.py 19–248 ↗ · meta-llama/llama-models @ 0e0b8c519242

Excerpt
LLAMA3_VOCAB_SIZE = 128256

… lines 20–230 omitted …

        Model(
            core_model_id=CoreModelId.llama3_1_8b,
            description="Llama 3.1 8b model",
            huggingface_repo="meta-llama/Llama-3.1-8B",
            arch_args={
                "dim": 4096,
                "n_layers": 32,
                "n_heads": 32,
                "n_kv_heads": 8,
                "vocab_size": LLAMA3_VOCAB_SIZE,
                "ffn_dim_multiplier": 1.3,
                "multiple_of": 1024,
                "norm_eps": 1e-05,
                "rope_theta": 500000.0,
                "use_scaled_rope": True,
            },
            pth_file_count=1,
        ),