This document is relevant for: Trn2, Trn3

GLM Indexer Kernel API Reference#

GLM sparse-attention indexer: Q/K projections, paged KV-cache write, context-parallel gather, scoring, and top-k.

The row holds head_dim fp8 K values bitcast two-per-bf16 (head_dim // 2 containers), then one bf16 per-token dequant scale, then padding. Unlike the MLA cache there is no separate k_pe field: the indexer’s whole head_dim (RoPE included) is quantized, matching vLLM, so the cache stays one contiguous row per token. GLM’s head_dim=128 gives 64 + 1 = 65 -> 72 bf16 (144 B).

Background#

glm_indexer_proj is the GLM sparse-attention indexer: it projects the latent Q and K, writes the quantized K into the paged cache, gathers K across the context-parallel ranks, scores the query tiles against the context, and selects each query’s top-k positions.

API Reference#

Source code for this kernel API can be found at: glm_indexer.py

glm_indexer_proj#

nkilib.experimental.mla.glm.glm_indexer_proj(x: nl.NkiTensor, qr: nl.NkiTensor, wq: nl.NkiTensor, wk: nl.NkiTensor, k_norm_gamma: nl.NkiTensor, k_norm_beta: nl.NkiTensor, cos: nl.NkiTensor, sin: nl.NkiTensor, slot_mapping: nl.NkiTensor, weights: nl.NkiTensor, q_pos: nl.NkiTensor, end_pos: int = 0, cp_degree: int = 1, replica_groups: tuple = None, topk_nseg: int = 0, roll_score: int = 0, emit_score: bool = False, emit_proj: bool = False, k_ctx: nl.NkiTensor = None, k_cache: nl.NkiTensor = None, zero_topk: bool = False, score_chunk: int = 0, block_table: nl.NkiTensor = None, block_size: int = 0, cp_interleave: int = 0, index_topk: int = 2048, kv_fp8: bool = False, kv_cache_fp8_dtype=nl.float8_e4m3)#

The GLM indexer: Q/K projections, paged cache write, CP gather, score, top-k.

Parameters:
  • x (nl.NkiTensor) – [s_local, dim] bf16 hidden states.

  • qr (nl.NkiTensor) – [s_local, q_lora_rank] bf16 latent Q.

  • wq (nl.NkiTensor) – [q_lora_rank, n_heads * head_dim] bf16 Q weight.

  • wk (nl.NkiTensor) – [dim, head_dim] bf16 K weight.

  • k_norm_gamma (nl.NkiTensor) – [head_dim] fp32 LayerNorm scale for K.

  • k_norm_beta (nl.NkiTensor) – [head_dim] fp32 LayerNorm bias for K.

  • cos (nl.NkiTensor) – [s_local, rope_head_dim // 2] fp32 RoPE cosine table.

  • sin (nl.NkiTensor) – [s_local, rope_head_dim // 2] fp32 RoPE sine table.

  • slot_mapping (nl.NkiTensor) – [s_local] int32 flat cache slot per token, -1 for padding.

  • weights (nl.NkiTensor) – [s_local, n_heads] fp32 per-head score weights.

  • q_pos (nl.NkiTensor) – [s_local] fp32 global position of each local query; all-gathered to give the gathered K’s positions.

  • end_pos (int) – valid cache positions to score against, i.e. the filled prefix. 0 skips the score phase entirely ([Testing]).

  • cp_degree (int) – CP ranks, so s_global = cp_degree * s_local. 1 skips the collective.

  • replica_groups (tuple) – CP rank-lists for the in-kernel collectives, from the framework. None assumes a single group [0, cp_degree), which only holds for pure CP.

  • topk_nseg (int) – [Testing] force the top-k segment count; production derives it (0).

  • roll_score (int) – [Testing] force the position-block loop rolled (1) or unrolled (-1); production derives it (0).

  • emit_score (bool) – [Testing] also return the full [s_local, T] score. O(s_local * s_global), so it does not fit in HBM at long context.

  • k_ctx (nl.NkiTensor) – [Testing] [s_global, head_dim] bf16 context supplied by the caller instead of gathered from peers, so one rank can stand in for a whole CP group.

  • zero_topk (bool) – [Testing] return zeros for the top-k output instead of the picks.

  • block_table (nl.NkiTensor) – [n_blocks] int32 page ids, logical block i -> cache block block_table[i]. The score then reads K through this table rather than from the gathered K, which is what lets a cached prefix participate. Requires block_size.

  • block_size (int) – tokens per cache page. Must divide POS_BLOCK.

  • cp_interleave (int) – CP interleave granularity of the cache in tokens; 0 means contiguous.

  • score_chunk (int) – [Testing] force the score chunk width, or -1 to disable chunking; production derives it (0). Chunking is what bounds the score ring.

  • index_topk (int) – positions selected per query.

  • emit_proj (bool) – [Testing] also return the projected Q and the gathered K.

  • k_cache (nl.NkiTensor) – paged K cache, owned by the caller and written in place. Its flattened leading dims are slots; the minor dim is head_dim (bf16) or, with kv_fp8, the fused packed row width kv_row_bf16_width(head_dim) bf16 (fp8 K bitcast 2-per-bf16 + per-token scale). The block table addresses it on the read side.

  • kv_fp8 (bool) – store the paged K cache in fp8, quantized per token over head_dim (RoPE included, matching vLLM) and packed into a bf16-container row alongside the per-token dequant scale. Halves cache and CP-collective bandwidth. The packed rows are unpacked to dense bf16 (dequant_k_packed) before the score: after the gather on the CP path, or after linearize_pages on the block-table / paged-cache (APC) path. False keeps the bf16 cache path unchanged.

  • kv_cache_fp8_dtype – fp8 format for the K latent when kv_fp8 – nl.float8_e4m3 (legacy, max 240, default) or the OCP nl.float8_e4m3fn (max 448, NeuronCore-v4).

This document is relevant for: Trn2, Trn3