Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Working with longer context

IndexShare: could neighbouring layers reuse the search?

An indexer has already found candidate positions. Discover what another layer can reuse, what it must recompute, and when the selection needs refreshing.

A lightning indexer makes attention cheaper by choosing which earlier positions to read. But it still has to score the available history to make that choice. Repeat the search in every layer, and that preparation becomes a significant part of the work.

The next layer searches again

Each layer receives a changed representation, so its best positions can differ. Running another indexer gives that layer a fresh choice. It also repeats the scan over the history.

The diagrams use GLM-5.2 / GLM-5.3. Its MLA cache stores 512 latent coordinates plus 64 rotary-key coordinates per position, separately for each layer:

Follow the data through the mechanismEarlier context is dimmed; outlined columns are current.Current token computation.6,144Current token representationLayer 7Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.576Own MLA cacheLightning indexer32 heads · 128-wide keysTop 2,048 positionsSame selectionAttention+Layer 8Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.576Own MLA cacheLightning indexer32 heads · 128-wide keysTop 2,048 positionsSame selectionAttention+Earlier context is dimmed; outlined columns are current.Current token computation.6,144Updated token representation
Each layer runs an indexer even though it may choose many of the same positions. Each attention read still uses that layer’s own query and cached representations.

Both layers might decide to read many of the same positions. Could we save the second search while still letting the second layer ask its own question?

Reuse the addresses, read different contents

The reusable result is a list of selected positions. Pass that list to the next layer and let it gather its own cached representations at those addresses.

The next layer still computes its own query, attention scores and collected result. Its stored keys and values can be different, even at the same positions. Reusing the search therefore leaves useful work for that layer to do:

Follow the data through the mechanismEarlier context is dimmed; outlined columns are current.Current token computation.6,144Current token representationLayer 7Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.576Own MLA cacheLightning indexer32 heads · 128-wide keysTop 2,048 positionsSame selectionAttention+Layer 8Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.576Own MLA cacheReuse positionsTop 2,048 positionsSame selectionAttention+Earlier context is dimmed; outlined columns are current.Current token computation.6,144Updated token representation
Reuse only the positions. The next layer gathers its own cached representations at those positions and calculates new attention weights. No keys, values or attention results are copied from the previous layer.

This is different from sharing K/V caches across layers. Here, we share where to look. Each layer retains its own attention representations and decides how strongly to use the supplied candidates.

That distinction matters for the saving too. We remove an indexer scan from a reusing layer. We do not remove that layer’s attention calculation, its main cache or the work of fetching its selected entries.

Refresh the choice before it becomes stale

A selection made earlier cannot anticipate every later query. If it leaves out a useful position, changing attention weights among the retained candidates cannot bring that position back.

We could therefore train a short group of layers to use one choice, then run a fresh indexer for the next group. The compromise is between the cost of searching again and the freedom to choose different candidates.

The worked GLM-5.2 / GLM-5.3 model uses IndexShare. Its indexer has 32 heads with 128-coordinate keys, and chooses up to 2,048 positions. A typical four-layer group computes a selection in its first layer and reuses it in the next three:

Follow the data through the mechanismEarlier context is dimmed; outlined columns are current.Current token computation.6,144Current token representationLayer 7Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.576Own MLA cacheLightning indexer32 heads · 128-wide keysTop 2,048 positionsSame selectionAttention+Layer 8Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.576Own MLA cacheReuse positionsTop 2,048 positionsSame selectionAttention+Layer 9Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.576Own MLA cacheReuse positionsTop 2,048 positionsSame selectionAttention+Layer 10Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.576Own MLA cacheReuse positionsTop 2,048 positionsSame selectionAttention+Earlier context is dimmed; outlined columns are current.Current token computation.6,144Updated token representation
GLM IndexShare: this four-layer group runs one indexer instead of four. Across the 78-layer model, 21 layers compute a selection and 57 reuse one. Attention is still calculated in all layers.

Across the 78-layer model, 21 layers run an indexer and 57 reuse a selection. That is 21 full indexer scans rather than 78 for each token, while all 78 attention layers still perform their reads. The diagram shows layers 7–10, one group in that configured schedule; the opening dense layers have their own selections.

Sharing is part of the model’s design and training. Turning off indexers in an arbitrary existing model would change which information its layers can access.

The same question appears in CSA: choosing useful summaries has a cost of its own. Models can spend that cost at different depths. Streaming scores and choosing coarse blocks before individual tokens tackle other parts of the search; they do not need to be folded into this mechanism.

Sources and worked example

The picker is restricted to the GLM-5.2 / 5.3 profile. GLM-5 and GLM-5.3-Flash do not use this same sharing schedule. The drawn columns sample a much longer history; their highlighted positions are an illustrative selection. The per-layer MLA cache label counts 512 latent coordinates plus 64 rotary-key coordinates. Selected positions are shared within a token’s layer traversal, not fixed for the next generated token.

← Browse all subjectsBack to the beginning ↑