{"record":{"id":"0f381726e0cb23d2","repo":"sgl-project/sglang","slug":"layer-layer-id-layer-layer-id-is-not-in-the-k-on","errorCode":null,"errorMessage":"layer.layer_id={layer.layer_id} is not in the K-only sparse group. K-only layers: {list(self.index_k_layer_id_mapping.keys())}","messagePattern":"layer\\.layer_id=(.+?) is not in the K-only sparse group\\. K-only layers: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/mem_cache/memory_pool.py","lineNumber":5038,"sourceCode":"            layer,\n            loc,\n            cache_idx_k,\n            cache_idx_v,\n            k_scale,\n            v_scale,\n            layer_id_override=mapped_id,\n        )\n\n    def set_index_k_buffer(\n        self,\n        layer: RadixAttention,\n        loc: torch.Tensor,\n        cache_idx_k: torch.Tensor,\n        k_scale: Optional[float] = None,\n    ) -> None:\n        mapped_id = self.index_k_layer_id_mapping.get(layer.layer_id)\n        if mapped_id is None:\n            raise ValueError(\n                f\"layer.layer_id={layer.layer_id} is not in the K-only \"\n                f\"sparse group. K-only layers: \"\n                f\"{list(self.index_k_layer_id_mapping.keys())}\"\n            )\n        sub_pool = self.index_k_pool\n        if cache_idx_k.dtype != sub_pool.dtype:\n            if k_scale is not None:\n                cache_idx_k = cache_idx_k / k_scale\n        sub_pool.set_k_buffer(mapped_id, loc, cache_idx_k)\n\n    def _can_fuse_kv_index_store(\n        self,\n        index_pool: MHATokenToKVPool,\n        cache_k: torch.Tensor,\n        cache_idx_k: torch.Tensor,\n    ) -> bool:\n        \"\"\"Fast-path precondition: CUDA, no per-store quantization, and a uniform\n        head byte size shared by main and index caches.\"\"\"","sourceCodeStart":5020,"sourceCodeEnd":5056,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/mem_cache/memory_pool.py#L5020-L5056","documentation":"set_index_k_buffer on MiniMaxSparseKVPool writes only into the K-only sub-pool (index_k_pool) and requires the layer to be in index_k_layer_id_mapping. The supplied layer.layer_id is not in the K-only sparse group, so the write is rejected rather than silently writing to the wrong slot.","triggerScenarios":"Calling set_index_k_buffer(layer, loc, cache_idx_k, k_scale) where layer.layer_id maps to None in index_k_layer_id_mapping — e.g. an index-KV layer or a dense layer.","commonSituations":"Model code routing all sparse layers through the K-only setter; layer group misclassification in the model config; refactors that swapped the set_index_k_buffer / set_index_kv_buffer call sites.","solutions":["Use set_index_kv_buffer for layers in index_kv_layer_id_mapping and the main pool for dense layers","Check layer.layer_id in sparse_pool.index_k_layer_id_mapping before calling","Validate the K-only vs index-KV layer split used at pool construction matches the model"],"exampleFix":"# before\nsparse_pool.set_index_k_buffer(layer, loc, cache_k, k_scale)  # index-KV layer\n# after\nif layer.layer_id in sparse_pool.index_k_layer_id_mapping:\n    sparse_pool.set_index_k_buffer(layer, loc, cache_k, k_scale)\nelse:\n    sparse_pool.set_index_kv_buffer(layer, loc, cache_k, cache_v, k_scale, v_scale)","handlingStrategy":"validation","validationCode":"if layer.layer_id not in sparse_pool.index_k_layer_id_mapping:\n    raise ValueError(f'{layer.layer_id} not in K-only group; use set_index_kv_buffer')","typeGuard":"def is_k_only_layer(sparse_pool, layer) -> bool:\n    return sparse_pool.index_k_layer_id_mapping.get(layer.layer_id) is not None","tryCatchPattern":null,"preventionTips":["Verify layer group membership before each sparse-pool write","Centralise the K-only/index-KV/dense dispatch in one function shared by all layers"],"tags":["kv-cache","sparse-attention","layer-id","write-path","minimax","sglang"],"backgroundTag":"invalid-layer-id-mapping","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}