Index group
×An MoE layer that runs its own lightning indexer, then 3 MoE layers that attend to the positions it selected.
Why
The indexer’s cost. Sparse attention takes the main attention from O(L²) to O(Lk), but the lightning indexer that chooses the k positions still scores every earlier position, in every layer: O(NL²) over N layers. The authors of IndexCache report that its share of the latency rises sharply with the context, in prefill above all. IndexCache §1, §2.1
Selections repeat. The positions the indexer chooses are much the same from one layer to the next: the authors measure that adjacent layers share 70–100% of them. So most layers can take the choice of the nearest earlier layer that kept its indexer, which costs inference one conditional branch. IndexCache §1
One in four. Kept by a fixed interleave, as here (21 of 78 layers), sharing loses quality in a model trained without it. Trained with a loss that teaches each kept indexer the attention of every layer it serves, the authors report that keeping one indexer in four stays within 0.4% of the original on their 30B model. IndexCache §3.1.1, §4.4
Keeping a quarter of the indexers, they report prefill 1.82× and decoding 1.48× faster at 200K tokens on that model, and at least 1.3× on GLM-5 beyond 100K. IndexCache §4.2
All 14 steps
- in
- [B batch, T positions, 6144 d] [T positions, 32 pairs] [T queries, T keys]
- out
- [B batch, T positions, 6144 d]
Parameters39,481,502,976 × 18 groups
39,481,502,976 parameters per index group, across its learned components
| Component | Calculation | Parameters |
|---|---|---|
| Indexing MoE layer | 9,877,404,672 | |
| Sharing MoE layer | 9,868,032,768 × 3 | 29,604,098,304 |
| Per-instance total | 39,481,502,976 | |
One of 18 independent index groups.
Explanation
18 such groups: layers 7–10, layers 11–14, …, layers 75–78.
The configuration lists the indexer type of every layer: 21 full, 57 shared (config.json 26–105). A shared layer builds no indexer (modeling_glm_moe_dsa.py 348–349) and attends to the indices of the last full layer before it (404–417).
Sources
In codeself.layers
Direct evidence
A layer with its own indexer selects its top-k positions; a "shared" layer takes the top-k indices of the previous layer that ran one, and fails without them. Both build the attention mask from those indices and the causal mask, and every layer returns the indices it used, which the model hands to the next layer.
modeling_glm_moe_dsa.py 404–722 ↗ · huggingface/transformers @ c4c4605f9618
Excerpt
# DSA: select this layer's top-k tokens, or reuse the previous full layer's on `"shared"` layers.
if self.indexer is not None:
topk_indices = self.indexer(
hidden_states,
q_resid,
position_embeddings,
attention_mask[:, 0, :, :],
position_ids, # Kept for BC
past_key_values=past_key_values,
) # [B, S, topk]
else:
if prev_topk_indices is None:
raise ValueError("Shared DSA layers require top-k indices from a previous full indexer layer.")
topk_indices = prev_topk_indices
sparse_indices = None
if self.config._attn_implementation in ("eager", "sdpa"):
index_mask = (
topk_indices.new_ones((batch_size, seq_length, key_states.shape[2]), dtype=torch.bool)
.scatter(-1, topk_indices.long(), False)
.unsqueeze(1)
)
if attention_mask.dtype == torch.bool:
attention_mask = attention_mask & ~index_mask
else:
attention_mask = attention_mask.masked_fill(index_mask, torch.finfo(hidden_states.dtype).min)
else:… lines 432–599 omitted …
hidden_states, _, topk_indices = self.self_attn(
hidden_states=hidden_states,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_values=past_key_values,
use_cache=use_cache,
position_embeddings=position_embeddings,
prev_topk_indices=prev_topk_indices, # MAIN DIFF with DSV3.2
**kwargs,
)
hidden_states = residual + hidden_states
residual = hidden_states
hidden_states = self.post_attention_layernorm(hidden_states)
hidden_states = self.mlp(hidden_states)
hidden_states = residual + hidden_states
return hidden_states, topk_indices… lines 617–710 omitted …
topk_indices = None # MAIN DIFF with DSV3.2
for i, decoder_layer in enumerate(self.layers[: self.config.num_hidden_layers]):
hidden_states, topk_indices = decoder_layer(
hidden_states,
attention_mask=causal_mask_mapping["indexed_attention"],
position_embeddings=position_embeddings,
position_ids=position_ids,
past_key_values=past_key_values,
use_cache=use_cache,
prev_topk_indices=topk_indices, # MAIN DIFF with DSV3.2
**kwargs,
)Each layer’s indexer type: layers 0, 1 and 2 and every fourth layer from 6 to 74 are "full"; the other 57 are "shared". The configuration also names the schedule that gives this list, index_topk_freq 4 with index_skip_topk_offset 3.
config.json 21–105 ↗ · zai-org/GLM-5.3 @ ba32f8c19c87
Excerpt
"index_skip_topk_offset": 3,
"index_topk": 2048,
"index_topk_freq": 4,
"index_topk_pattern": null,
"indexer_rope_interleave": true,
"indexer_types": [
"full",
"full",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared",
"full",
"shared",
"shared",
"shared"
],