Skip to content

Gemma-4 E2B / E4B: share the KV of KV-shared layers instead of copying it #882

Open

Description

@mkornreich

Follow-up from #59 (Gemma-4 E2B / E4B).

What

The last num_kv_shared_layers layers of E2B / E4B do not compute k or v: they attend over the k and v of the last earlier layer of their attention type. #59 hands those over within one forward, and each shared layer then stores them in its own KV slab. The shared layers could read the source layer's slab instead.

Why it matters

The copies are a large share of the KV cache, bf16, per token:

Release Full-attention layers shared SWA layers shared KV that is a copy
E2B 4 of 7 16 of 28 57%
E4B 3 of 7 15 of 35 43%

On an 8 GB card E4B leaves about 3.4 GiB for KV (--embed-weights host), about 84k tokens; without the copies the same memory would hold roughly 1.75x that.

What it needs

  • The KV pool or the attention backend maps a shared layer's KV reads to its source layer's slab, and skips the store for shared layers.
  • The pool sizing counts only the layers that own KV.
  • The SWA radix cache's per-layer bookkeeping stays consistent when several layers read one slab.

llama.cpp does the equivalent: shared layers read the cache of n_layer_kv_from_start - (is_swa ? 2 : 1) and write nothing.

Activity

  1. ischencheng commented on Nov 12, 2026

    @ischencheng
    Contributor

    Opened #898 for this. The shared layers now read their source layer's slab and store nothing, so on an A10 E2B fits 2.35x the KV tokens and E4B 1.8x, with the same greedy outputs as main (also with the 8-bit formats from #894).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions