{"record":{"id":"7fcc838de7c105ba","repo":"sgl-project/sglang","slug":"initial-state-must-be-contiguous-in-the-last-dim-7fcc83","errorCode":null,"errorMessage":"`initial_state` must be contiguous in the last dim.","messagePattern":"`initial_state` must be contiguous in the last dim\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/helion/kda_decode.py","lineNumber":285,"sourceCode":"    B = mixed_qkv.shape[0]\n    if a.shape[0] != B or b.shape[0] != B:\n        raise ValueError(\n            \"Mismatched batch sizes: \"\n            f\"mixed_qkv.shape[0]={B}, a.shape[0]={a.shape[0]}, \"\n            f\"b.shape[0]={b.shape[0]}.\"\n        )\n    if ssm_state_indices.shape[0] != B:\n        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:","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/helion/kda_decode.py#L267-L303","documentation":"The Helion KDA decode kernel indexes the last dimension of initial_state with unit stride, so the tensor must be contiguous in that dim. validate_packed_decode_inputs checks initial_state.stride(-1) == 1 and raises when the state was sliced, transposed, or otherwise made non-contiguous in its innermost axis.","triggerScenarios":"Passing a state pool that was created via torch.transpose, narrow/slicing along the last dim, or expand of a smaller tensor into helion_fused_recurrent_kda_packed_decode / helion_fused_recurrent_kda_replayssm_decode.","commonSituations":"Reusing a state cache view created from a larger pool with a non-unit last-dim stride; converting weights from a checkpoint with a transposed layout; passing a broadcasted/expanded dummy state in tests.","solutions":["Call initial_state = initial_state.contiguous() before the decode call","If the state comes from a pool view, rebuild the pool allocation so the last dim is contiguous","Avoid .t()/.transpose() on the state; store the pool in [.., V, K] layout directly"],"exampleFix":"// before\nstate = state_pool[indices].transpose(-1, -2)\nout = helion_fused_recurrent_kda_packed_decode(..., state, ...)\n// after\nstate = state_pool[indices].transpose(-1, -2).contiguous()\nout = helion_fused_recurrent_kda_packed_decode(..., state, ...)","handlingStrategy":"validation","validationCode":"if initial_state.stride(-1) != 1:\n    initial_state = initial_state.contiguous()","typeGuard":"def last_dim_contiguous(t: torch.Tensor) -> bool:\n    return t.stride(-1) == 1","tryCatchPattern":null,"preventionTips":["Apply .contiguous() after any transpose/slice of state tensors","Allocate state pools fresh rather than deriving strided views"],"tags":["kda","helion","contiguity","tensor-stride"],"backgroundTag":"tensor-not-contiguous","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}