{"record":{"id":"a6fa34c4710e5779","repo":"sgl-project/sglang","slug":"a-b-must-be-contiguous-in-the-last-dim","errorCode":null,"errorMessage":"`a`/`b` must be contiguous in the last dim.","messagePattern":"`a`/`b` must be contiguous in the last dim\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/fla/fused_recurrent.py","lineNumber":291,"sourceCode":"    dt_bias: torch.Tensor,\n    scale: float,\n    initial_state: torch.Tensor,\n    out: torch.Tensor,\n    ssm_state_indices: torch.Tensor,\n    use_qk_l2norm_in_kernel: bool = False,\n) -> tuple[torch.Tensor, torch.Tensor]:\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            f\"`ssm_state_indices` must be 1D for packed decode (got ndim={ssm_state_indices.ndim}).\"\n        )\n    if not out.is_contiguous():\n        raise ValueError(\"`out` must be contiguous.\")\n\n    dev = mixed_qkv.device\n    if any(\n        t.device != dev\n        for t in (a, b, A_log, dt_bias, initial_state, out, ssm_state_indices)\n    ):\n        raise ValueError(\"All inputs must be on the same device.\")\n","sourceCodeStart":273,"sourceCodeEnd":309,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/fla/fused_recurrent.py#L273-L309","documentation":"The packed decode kernel requires unit stride in the last dimension of both gate tensors a and b. The wrapper checks a.stride(-1) != 1 or b.stride(-1) != 1 and raises when either is a non-contiguous view along the feature dim.","triggerScenarios":"Creating a and b via stacking/chunking that leaves a stride > 1, e.g. a, b = gates[..., 0], gates[..., 1] on a (T, HV, 2) tensor (stride 2 in the last dim after transpose), or transposed views.","commonSituations":"Interleaved gate layouts where a and b alternate per element; using .unbind(-1) on a tensor whose last dim after permutation is not compact; caching gate tensors in a transposed buffer.","solutions":["Make both contiguous: a = a.contiguous(); b = b.contiguous()","Produce a and b with torch.chunk(2, dim=-1) on a (T, 2*HV) contiguous projection so both children inherit unit stride"],"exampleFix":"# before\na, b = g[..., 0], g[..., 1]  # stride-2 views\n# after\na, b = g.unbind(dim=-1)\na = a.contiguous(); b = b.contiguous()","handlingStrategy":"validation","validationCode":"if a.stride(-1) != 1: a = a.contiguous()\nif b.stride(-1) != 1: b = b.contiguous()","typeGuard":"def gates_contiguous(a: torch.Tensor, b: torch.Tensor) -> bool:\n    return a.stride(-1) == 1 and b.stride(-1) == 1","tryCatchPattern":null,"preventionTips":["Avoid interleaved (…, 2) gate layouts; use chunk(2, dim=-1) on a (T, 2*HV) buffer","Materialize .contiguous() copies once when gates are produced, not per step"],"tags":["fla","fused-recurrent","contiguity","stride-check"],"backgroundTag":"tensor-not-contiguous","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}