{"record":{"id":"f468c6d5dd414881","repo":"sgl-project/sglang","slug":"a-must-have-shape-b-hv-k-with-hv-hv-k-k-f468c6","errorCode":null,"errorMessage":"`a` must have shape [B, HV*K] with HV={HV}, K={K} (got a.shape={tuple(a.shape)}).","messagePattern":"`a` must have shape \\[B, HV\\*K\\] with HV=(.+?), K=(.+?) \\(got a\\.shape=(.+?)\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/helion/kda_decode.py","lineNumber":293,"sourceCode":"        raise ValueError(\n            f\"`ssm_state_indices` must have shape [B] \"\n            f\"(got {tuple(ssm_state_indices.shape)}; expected ({B},)).\"\n        )\n\n    if initial_state.ndim != 4:\n        raise ValueError(\n            f\"`initial_state` must be a 4D tensor (got ndim={initial_state.ndim}).\"\n        )\n    if initial_state.stride(-1) != 1:\n        raise ValueError(\"`initial_state` must be contiguous in the last dim.\")\n    HV, V, K = initial_state.shape[-3:]\n    if not _is_power_of_two(K) or not _is_power_of_two(V):\n        raise ValueError(\n            \"Helion KDA decode requires power-of-two key and value head \"\n            f\"dimensions (got K={K}, V={V}).\"\n        )\n    if a.shape[1] != HV * K:\n        raise ValueError(\n            f\"`a` must have shape [B, HV*K] with HV={HV}, K={K} \"\n            f\"(got a.shape={tuple(a.shape)}).\"\n        )\n    if b.shape[1] != HV:\n        raise ValueError(\n            f\"`b` must have shape [B, HV] with HV={HV} (got b.shape={tuple(b.shape)}).\"\n        )\n    if A_log.numel() != HV:\n        raise ValueError(f\"`A_log` must have {HV} elements (got {A_log.numel()}).\")\n    if dt_bias.numel() != HV * K:\n        raise ValueError(\n            f\"`dt_bias` must have {HV * K} elements (got {dt_bias.numel()}).\"\n        )\n    if out.shape != (B, 1, HV, V):\n        raise ValueError(\n            f\"`out` must have shape {(B, 1, HV, V)} (got out.shape={tuple(out.shape)}).\"\n        )\n","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/helion/kda_decode.py#L275-L311","documentation":"The KDA decay-input tensor `a` must be packed as [B, HV*K]: one row per batch item with all key-dim decays for every value head concatenated. validate_packed_decode_inputs derives HV and K from initial_state and checks a.shape[1] == HV*K, raising when the packed layout doesn't match.","triggerScenarios":"Calling helion_fused_recurrent_kda_packed_decode with `a` shaped [B, HV, K] (3D), [B, K] (single head), or with HV/K taken from a mismatched initial_state.","commonSituations":"Forgetting to .view(B, HV*K) / .reshape(B, -1) the conv/gate projection output before the fused decode; using a different num_v_heads in the projection than in the state pool; test fixtures built with the wrong packing.","solutions":["Flatten the head dims: a = a.reshape(B, -1) so shape[1] == HV*K","Verify HV and K in initial_state match the model's num_v_heads and head_k_dim used to produce `a`","Add a shape assert in your model forward before calling the fused kernel"],"exampleFix":"// before\na = a_proj(x)  # [B, HV, K]\nhelion_fused_recurrent_kda_packed_decode(qkv, a, b, ...)\n// after\na = a_proj(x).reshape(a.size(0), -1)  # [B, HV*K]\nhelion_fused_recurrent_kda_packed_decode(qkv, a, b, ...)","handlingStrategy":"validation","validationCode":"HV, V, K = initial_state.shape[-3:]\nassert a.reshape(a.size(0), -1).shape[1] == HV * K\na = a.reshape(a.size(0), -1)","typeGuard":"def valid_packed_a(a: torch.Tensor, hv: int, k: int) -> bool:\n    return a.ndim == 2 and a.shape[1] == hv * k","tryCatchPattern":null,"preventionTips":["Standardize on [B, HV*K] packing at the projection site","Derive HV/K from the same source (initial_state) everywhere"],"tags":["kda","helion","shape-validation","packed-layout"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}