sgl-project/sglang · error · ValueError

`out` must be contiguous.

Error message

`out` must be contiguous.

What it means

The output tensor must be fully contiguous because the Triton kernel writes it with flat offsets. A non-contiguous out (overlapping or strided storage) would produce incorrect results, so it is rejected before launch.

Source

Thrown at python/sglang/kernels/ops/attention/helion/kda_decode.py:250

    if mixed_qkv.stride(-1) != 1:
        raise ValueError("`mixed_qkv` must be contiguous in the last dim.")
    if a.ndim != 2 or b.ndim != 2:
        raise ValueError(
            f"`a` and `b` must be 2D tensors (got a.ndim={a.ndim}, b.ndim={b.ndim})."
        )
    if a.stride(-1) != 1 or b.stride(-1) != 1:
        raise ValueError("`a`/`b` must be contiguous in the last dim.")
    if A_log.ndim != 1 or dt_bias.ndim != 1:
        raise ValueError("`A_log`/`dt_bias` must be 1D tensors.")
    if A_log.stride(0) != 1 or dt_bias.stride(0) != 1:
        raise ValueError("`A_log`/`dt_bias` must be contiguous.")
    if ssm_state_indices.ndim != 1:
        raise ValueError(
            "`ssm_state_indices` must be 1D for packed decode "
            f"(got ndim={ssm_state_indices.ndim})."
        )
    if not out.is_contiguous():
        raise ValueError("`out` must be contiguous.")

    device = mixed_qkv.device
    if any(
        tensor.device != device
        for tensor in (
            a,
            b,
            A_log,
            dt_bias,
            initial_state,
            out,
            ssm_state_indices,
        )
    ):
        raise ValueError("All inputs must be on the same device.")

    B = mixed_qkv.shape[0]
    if a.shape[0] != B or b.shape[0] != B:

View on GitHub (pinned to 0132848349)

Solutions

  1. Allocate out with torch.empty_like/empty of the exact shape (contiguous)
  2. Or call out = out.contiguous() before the call (note: writes won't propagate back to the original view)

Example fix

# before
out = buf[:, 0]  # strided view
decode(..., out=out, ...)
# after
out = torch.empty(B, out_dim, device=qkv.device, dtype=qkv.dtype)
decode(..., out=out, ...)
Defensive patterns

Strategy: validation

Validate before calling

if not out.is_contiguous():
    out = torch.empty_like(out, memory_format=torch.contiguous_format)

Type guard

def safe_out(o: torch.Tensor) -> torch.Tensor:
    return o if o.is_contiguous() else o.contiguous().clone()

Prevention

When it happens

Trigger: Passing an out tensor that is a transposed/sliced view or has non-standard strides.

Common situations: Preallocating out as a view of a larger buffer; passing a tensor sliced from an output cache.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/269635c31b0e313f. Report an issue: GitHub.