{"record":{"id":"879fbc74a92da609","repo":"sgl-project/sglang","slug":"mixed-qkv-must-be-a-2d-tensor-got-ndim-mixed-q-879fbc","errorCode":null,"errorMessage":"`mixed_qkv` must be a 2D tensor (got ndim={mixed_qkv.ndim}).","messagePattern":"`mixed_qkv` must be a 2D tensor \\(got ndim=(.+?)\\)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/helion/kda_decode.py","lineNumber":229,"sourceCode":"\n\ndef _is_power_of_two(value: int) -> bool:\n    return value > 0 and value & (value - 1) == 0\n\n\ndef validate_packed_decode_inputs(\n    mixed_qkv: torch.Tensor,\n    a: torch.Tensor,\n    b: torch.Tensor,\n    A_log: torch.Tensor,\n    dt_bias: torch.Tensor,\n    initial_state: torch.Tensor,\n    out: torch.Tensor,\n    ssm_state_indices: torch.Tensor,\n) -> tuple[int, int, int, int, int]:\n    \"\"\"Apply the shape and layout checks from SGLang's packed wrapper.\"\"\"\n    if mixed_qkv.ndim != 2:\n        raise ValueError(\n            f\"`mixed_qkv` must be a 2D tensor (got ndim={mixed_qkv.ndim}).\"\n        )\n    if mixed_qkv.stride(-1) != 1:\n        raise ValueError(\"`mixed_qkv` must be contiguous in the last dim.\")\n    if a.ndim != 2 or b.ndim != 2:\n        raise ValueError(\n            f\"`a` and `b` must be 2D tensors (got a.ndim={a.ndim}, b.ndim={b.ndim}).\"\n        )\n    if a.stride(-1) != 1 or b.stride(-1) != 1:\n        raise ValueError(\"`a`/`b` must be contiguous in the last dim.\")\n    if A_log.ndim != 1 or dt_bias.ndim != 1:\n        raise ValueError(\"`A_log`/`dt_bias` must be 1D tensors.\")\n    if A_log.stride(0) != 1 or dt_bias.stride(0) != 1:\n        raise ValueError(\"`A_log`/`dt_bias` must be contiguous.\")\n    if ssm_state_indices.ndim != 1:\n        raise ValueError(\n            \"`ssm_state_indices` must be 1D for packed decode \"\n            f\"(got ndim={ssm_state_indices.ndim}).\"","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/helion/kda_decode.py#L211-L247","documentation":"The packed KDA (Kimi Delta Attention) decode validator requires mixed_qkv as a 2D [B, qkv_len] tensor because the Triton/Helion kernel iterates over a flattened batch of packed projections. Any other ndim is rejected before launching.","triggerScenarios":"Calling helion_fused_recurrent_kda_packed_decode (or replayssm variant) with a 3D/4D qkv tensor (e.g. [B, seq, D] or [B, H, D, ...]).","commonSituations":"Passing a decode-shaped tensor that still has a sequence dim; upstream reorganize_qkv not flattening before decode; reusing prefill-style tensors.","solutions":["Flatten mixed_qkv to 2D: mixed_qkv.view(-1, mixed_qkv.shape[-1]) or ensure decode path packs [B, total_qkv_dim]","Check the caller's qkv packing step matches the kernel's expected layout"],"exampleFix":"# before\nout = helion_fused_recurrent_kda_packed_decode(qkv_3d, ...)  # [B, seq, D]\n# after\nqkv = qkv_3d.reshape(qkv_3d.shape[0], -1)\nout = helion_fused_recurrent_kda_packed_decode(qkv, ...)","handlingStrategy":"validation","validationCode":"assert mixed_qkv.ndim == 2, f'mixed_qkv must be 2D, got {mixed_qkv.ndim}'\nmixed_qkv = mixed_qkv.view(mixed_qkv.shape[0], -1) if mixed_qkv.ndim > 2 else mixed_qkv","typeGuard":"def is_packed_qkv(t: torch.Tensor) -> bool:\n    return t.ndim == 2 and t.stride(-1) == 1","tryCatchPattern":null,"preventionTips":["Flatten packed projections once at schedule time","Keep decode-path tensors 2D by convention"],"tags":["kda","mamba","tensor-shape","helion","triton"],"backgroundTag":"invalid-tensor-shape","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}