{"record":{"id":"c1a109e49c133fee","repo":"sgl-project/sglang","slug":"unexpected-a-shape-for-varlen-a-shape","errorCode":null,"errorMessage":"Unexpected a shape for varlen: {a.shape}","messagePattern":"Unexpected a shape for varlen: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/cutedsl_kda.py","lineNumber":1373,"sourceCode":"        )\n    return dt_bias.reshape(HV, K).contiguous()\n\n\ndef _normalize_kda_a(a, *, is_varlen_decode, N, HV, K):\n    \"\"\"Normalize `a` to match the compile-time shape expected by the kernel.\n\n    varlen kernel compiled shape: (N, HV, K)  -- 3D\n    dense kernel compiled shape:  (N, 1, HV, K) -- 4D\n    \"\"\"\n    if is_varlen_decode:\n        # Target: (N, HV, K) -- 3D\n        if a.dim() == 2 and a.shape == (N, HV * K):\n            return a.view(N, HV, K)\n        if a.dim() == 3 and a.shape == (N, HV, K):\n            return a  # already correct\n        if a.dim() == 4 and a.shape == (1, N, HV, K):\n            return a.squeeze(0)  # remove leading dim\n        raise ValueError(f\"Unexpected a shape for varlen: {a.shape}\")\n    else:\n        # Target: (N, 1, HV, K) -- 4D\n        if a.dim() == 2 and a.shape == (N, HV * K):\n            return a.view(N, 1, HV, K)\n        if a.dim() == 3 and a.shape == (N, HV, K):\n            return a.unsqueeze(1)\n        if a.dim() == 4 and a.shape == (N, 1, HV, K):\n            return a\n        raise ValueError(f\"Unexpected a shape for dense: {a.shape}\")\n\n\ndef cutedsl_fused_sigmoid_gating_kda_update(\n    A_log: torch.Tensor,\n    dt_bias: torch.Tensor,\n    q: torch.Tensor,\n    k: torch.Tensor,\n    v: torch.Tensor,\n    a: torch.Tensor,","sourceCodeStart":1355,"sourceCodeEnd":1391,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/cutedsl_kda.py#L1355-L1391","documentation":"For variable-length (varlen) decode, _normalize_kda_a accepts the gating tensor `a` only as (N, HV*K) 2D, (N, HV, K) 3D, or (1, N, HV, K) 4D. Any other shape raises ValueError('Unexpected a shape for varlen: ...').","triggerScenarios":"Calling the KDA fused update with is_varlen_decode=True and an `a` tensor that is (N, 1, HV, K) (the dense layout, missing squeeze of dim 1), or a per-request 3D tensor without the token dim N, or wrong N due to a cu_seqlen mismatch.","commonSituations":"Switching the same caller code between dense and varlen decode paths without reshaping `a`; ragged-batch schedulers producing `a` with token counts that disagree with the N argument computed elsewhere.","solutions":["Reshape to (N, HV, K): if dense layout (N,1,HV,K), call a.squeeze(1)","Ensure N equals a.shape[0] (token count) exactly; recompute N from the same source as the input tensors","Add a pre-call assert a.dim() in (2,3) and a.shape[0] == N"],"exampleFix":"# before (varlen): a shape (N, 1, HV, K) -> ValueError\n# after\na = a.squeeze(1) if a.dim() == 4 else a  # -> (N, HV, K)\nupdate = cutedsl_fused_sigmoid_gating_kda_update(..., a=a, ...)","handlingStrategy":"validation","validationCode":"assert a.dim() in (2, 3) or (a.dim() == 4 and a.shape[0] == 1), f'a shape {a.shape}'\nassert a.shape[0] == N, f'a token dim {a.shape[0]} != N {N}'","typeGuard":"def is_valid_varlen_a(a, N: int, HV: int, K: int) -> bool:\n    return a.shape in [(N, HV*K), (N, HV, K), (1, N, HV, K)]","tryCatchPattern":null,"preventionTips":["Use the same normalization helper before both dense and varlen paths","Compute N from the actual tensor shapes, not from a separately tracked counter"],"tags":["kda","varlen","shape-validation","cutedsl"],"backgroundTag":"tensor-rank-validation","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}