{"record":{"id":"bcf9f96344626abb","repo":"sgl-project/sglang","slug":"unsupported-d-qk-d-qk-expected-dsv4-d-qk-de","errorCode":null,"errorMessage":"Unsupported d_qk: {d_qk}. Expected {DSV4_D_QK} (DeepSeek V4)","messagePattern":"Unsupported d_qk: (.+?)\\. Expected (.+?) \\(DeepSeek V4\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/nsa_triton_decode/triton_mla_kernels_decode_optimized.py","lineNumber":62,"sourceCode":"            return total_tokens <= 32\n        else:\n            return total_tokens <= 128\n    return True\n\n\ndef triton_sparse_attn_decode(\n    q: torch.Tensor,\n    kv_scope,\n    extra_kv_scope,\n    sm_scale: float,\n    d_v: int = 512,\n    attn_sink: Optional[torch.Tensor] = None,\n) -> Tuple[torch.Tensor, torch.Tensor]:\n    \"\"\"Optimized sparse attention decode for DeepSeek V4 (d_qk=512).\"\"\"\n    d_qk = q.shape[-1]\n\n    if d_qk != DSV4_D_QK:\n        raise ValueError(\n            f\"Unsupported d_qk: {d_qk}. Expected {DSV4_D_QK} (DeepSeek V4)\"\n        )\n\n    return _triton_sparse_attn_decode_dsv4(\n        q, kv_scope, extra_kv_scope, sm_scale, d_v, attn_sink\n    )\n\n\ndef _triton_sparse_attn_decode_dsv4(\n    q: torch.Tensor,\n    kv_scope,\n    extra_kv_scope,\n    sm_scale: float,\n    d_v: int,\n    attn_sink: Optional[torch.Tensor],\n) -> Tuple[torch.Tensor, torch.Tensor]:\n    \"\"\"Sparse attention decode for DeepSeek V4 (d_qk=512).\n","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/nsa_triton_decode/triton_mla_kernels_decode_optimized.py#L44-L80","documentation":"triton_sparse_attn_decode is a DeepSeek V4-specialized kernel that hard-requires the DSV4 query/key head dimension (DSV4_D_QK = 512). It reads d_qk = q.shape[-1] and rejects anything else, forwarding only the DSV4 layout to _triton_sparse_attn_decode_dsv4.","triggerScenarios":"Calling triton_sparse_attn_decode (directly or via triton_fp8_attention_fwd) with a q tensor whose last dimension is not 512 — e.g. DSV3's 576 or a standard 128-dim head.","commonSituations":"Running a non-DSV4 model (DeepSeek V3/R1, or a generic MLA model) while the attention backend selects the DSV4-optimized decode kernel; model-architecture dispatch falling through to the wrong kernel.","solutions":["Route the model to its correct decode kernel (DSV3 or generic sparse attention path)","If you intend DSV4, check that q was projected to the 512-dim qk layout before this call"],"exampleFix":"# before\nout = triton_sparse_attn_decode(q, kv_scope, ...)  # q.shape[-1] == 576\n# after\nif q.shape[-1] != DSV4_D_QK:\n    out = dsv3_sparse_attn_decode(q, kv, ...)\nelse:\n    out = triton_sparse_attn_decode(q, kv_scope, ...)","handlingStrategy":"type-guard","validationCode":"d_qk = q.shape[-1]\nfn = triton_sparse_attn_decode if d_qk == DSV4_D_QK else generic_sparse_decode\nout = fn(q, kv_scope, ...)","typeGuard":"def is_dsv4_q(q: torch.Tensor) -> bool:\n    return q.ndim == 3 and q.shape[-1] == 512  # DSV4_D_QK","tryCatchPattern":null,"preventionTips":["Dispatch decode kernels by model architecture + head dim","Assert head dim in model-load-time config validation"],"tags":["nsa","triton","deepseek","head-dim","model-mismatch"],"backgroundTag":"unsupported-head-dimension","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}