{"record":{"id":"251fc113947db9ce","repo":"sgl-project/sglang","slug":"out-must-be-contiguous-251fc1","errorCode":null,"errorMessage":"`out` must be contiguous.","messagePattern":"`out` must be contiguous\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/fla/fused_recurrent_linear_replayssm.py","lineNumber":488,"sourceCode":"    and the (flush-only) checkpoint write (ht), in place.\n\n    Allocates nothing persistent: the caller owns the ring tensors and is\n    responsible for advancing / resetting ``write_pos`` (e.g. ``(write_pos+1) %\n    L`` after each step).  This is a STANDALONE kernel; the memory-pool / cache\n    integration is a later phase.\n    \"\"\"\n    if mixed_qkv.ndim != 2:\n        raise ValueError(f\"`mixed_qkv` must be 2D (got ndim={mixed_qkv.ndim}).\")\n    if mixed_qkv.stride(-1) != 1:\n        raise ValueError(\"`mixed_qkv` must be contiguous in the last dim.\")\n    if b.ndim != 2:\n        raise ValueError(f\"`b` must be 2D (got b.ndim={b.ndim}).\")\n    if A_log.ndim != 1:\n        raise ValueError(\"`A_log` must be a 1D tensor.\")\n    if initial_state.ndim != 4:\n        raise ValueError(f\"`initial_state` must be 4D (got ndim={initial_state.ndim}).\")\n    if not out.is_contiguous():\n        raise ValueError(\"`out` must be contiguous.\")\n    if write_pos.ndim != 1 or write_pos.dtype != torch.int32:\n        raise ValueError(\"`write_pos` must be a 1D int32 tensor.\")\n    if force_flush is not None and (\n        force_flush.ndim != 1 or force_flush.dtype != torch.int32\n    ):\n        raise ValueError(\"`force_flush` must be a 1D int32 tensor or None.\")\n\n    B = mixed_qkv.shape[0]\n    num_state_slots, HV, V, K = initial_state.shape\n    qkv_dim = mixed_qkv.shape[1]\n    q_dim = (qkv_dim - HV * V) // 2\n    if q_dim <= 0 or q_dim % K != 0:\n        raise ValueError(\n            f\"Invalid packed `mixed_qkv` last dim={qkv_dim} for HV={HV}, V={V}, K={K}.\"\n        )\n    H = q_dim // K\n    if H <= 0 or HV % H != 0:\n        raise ValueError(","sourceCodeStart":470,"sourceCodeEnd":506,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/fla/fused_recurrent_linear_replayssm.py#L470-L506","documentation":"The output buffer for replaySSM decode must be contiguous so the kernel can write each token's results densely. A non-contiguous out (strided view, slice of a larger tensor with gaps) is rejected before launch.","triggerScenarios":"Passing out as a slice of a ring buffer (out = ring[:, :, step]), a transposed view, or a tensor with padding between rows.","commonSituations":"CUDA-graph replay setups where out aliases a strided region of a persistent buffer; preallocating outputs inside a larger padded workspace.","solutions":["Allocate out with torch.empty(num_tokens, dim) (contiguous) per step, or copy the strided view into a contiguous tensor","If a persistent buffer is required for graph capture, store outputs in a contiguous slab and slice rows, not columns","Check out.is_contiguous() before the call in debug builds"],"exampleFix":"// before\nout = ring_buf[:, step, :]  # non-contiguous stride\n// after\nout = torch.empty(num_tokens, dim, device=dev, dtype=dt)\nfused_recurrent_linear_replayssm_decode(..., out=out, ...)\nring_buf[:, step, :] = out","handlingStrategy":"validation","validationCode":"if not out.is_contiguous():\n    out = out.contiguous()","typeGuard":"def contiguous_out(o: torch.Tensor) -> torch.Tensor:\n    return o if o.is_contiguous() else o.contiguous()","tryCatchPattern":null,"preventionTips":["For CUDA graphs, allocate a contiguous slab and write results back after the kernel","Assert is_contiguous on all kernel outputs in debug builds"],"tags":["pytorch","contiguity","replayssm","output-buffer"],"backgroundTag":"non-contiguous-tensor","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}