{"record":{"id":"4c83c60bb2a16eef","repo":"sgl-project/sglang","slug":"out-must-be-contiguous","errorCode":null,"errorMessage":"`out` must be contiguous.","messagePattern":"`out` must be contiguous\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/fla/fused_recurrent.py","lineNumber":301,"sourceCode":"        )\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\n    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]}, 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] (got {tuple(ssm_state_indices.shape)}; expected ({B},)).\"\n        )","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/fla/fused_recurrent.py#L283-L319","documentation":"The output tensor out must be contiguous because the Triton kernel writes results with compact indexing. The wrapper checks out.is_contiguous() and raises when a preallocated output buffer is a strided view (e.g. a slice of a larger circular cache or a transposed buffer).","triggerScenarios":"Passing out = out_cache[locs] views, out = buf[:, :, t, :] style slices, or outputs created with as_strided/slice of paged buffers.","commonSituations":"Zero-copy output into a paged/ring output cache; DP/TP code reusing a padded output buffer; test harnesses allocating out with unusual strides via torch.empty followed by slicing.","solutions":["Allocate out with torch.empty((B, 1, HV, V), dtype=..., device=...) (naturally contiguous) and copy into the cache afterwards, or pass a .contiguous() view","If writing into a cache is required, do out_cache[slots] = out after the kernel"],"exampleFix":"# before\nout = out_buf[:, :, t]  # strided view\n# after\nout = torch.empty((B, 1, HV, V), dtype=x.dtype, device=x.device)\nout, state = fused_recurrent_gated_delta_rule_packed_decode(..., out=out, ...)\nout_buf[:, :, t] = out","handlingStrategy":"validation","validationCode":"if not out.is_contiguous():\n    out = torch.empty_like(out, memory_format=torch.contiguous_format)","typeGuard":"def contiguous_out(o: torch.Tensor) -> bool:\n    return o.is_contiguous()","tryCatchPattern":null,"preventionTips":["Always allocate out fresh with torch.empty((B, 1, HV, V), …); scatter into caches afterwards","Avoid passing views of ring/paged output buffers into kernels"],"tags":["fla","fused-recurrent","contiguity","output-buffer"],"backgroundTag":"tensor-not-contiguous","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}