{"record":{"id":"43b1bc996af97dd2","repo":"sgl-project/sglang","slug":"mixed-qkv-must-be-contiguous-in-the-last-dim","errorCode":null,"errorMessage":"`mixed_qkv` must be contiguous in the last dim.","messagePattern":"`mixed_qkv` 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":285,"sourceCode":"\ndef fused_recurrent_gated_delta_rule_packed_decode(\n    mixed_qkv: torch.Tensor,\n    a: torch.Tensor,\n    b: torch.Tensor,\n    A_log: torch.Tensor,\n    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","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/fla/fused_recurrent.py#L267-L303","documentation":"The packed decode Triton kernel indexes mixed_qkv assuming unit stride in the last dimension. The Python wrapper checks mixed_qkv.stride(-1) != 1 and raises when the qkv rows are not contiguous in the feature dimension (e.g. a sliced or transposed view).","triggerScenarios":"Passing mixed_qkv created via transpose, narrow/slicing of the last dim, or expand; e.g. mixed_qkv = proj[..., ::2] or a (T, D) slice of a wider buffer with row stride > D.","commonSituations":"Slicing a fused qkv-plus-gates projection buffer and passing a non-compacted view; reusing a cached projection tensor after a .transpose(0, 1); tensors produced by torch.chunk on dim 1 followed by no .contiguous() where strides leak.","solutions":["Call .contiguous() on mixed_qkv before the kernel (or ensure it comes directly from a contiguous Linear output)","Restructure upstream slicing to produce compact rows: mixed_qkv = buf[:, :qkv_dim].contiguous()"],"exampleFix":"# before\nmixed_qkv = mixed_qkv_proj[:, :qkv_dim]  # row stride > qkv_dim\n# after\nmixed_qkv = mixed_qkv_proj[:, :qkv_dim].contiguous()","handlingStrategy":"validation","validationCode":"mixed_qkv = mixed_qkv.contiguous() if mixed_qkv.stride(-1) != 1 else mixed_qkv","typeGuard":"def last_dim_contiguous(t: torch.Tensor) -> bool:\n    return t.stride(-1) == 1","tryCatchPattern":null,"preventionTips":["Call .contiguous() defensively after any slice/transpose of projection outputs","Prefer torch.chunk along the last dim (inherits unit stride) over strided indexing"],"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"}