sgl-project/sglang · error · ValueError

`out` must be contiguous.

Error message

`out` must be contiguous.

What it means

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.

Source

Thrown at python/sglang/kernels/ops/attention/fla/fused_recurrent_linear_replayssm.py:488

    and the (flush-only) checkpoint write (ht), in place.

    Allocates nothing persistent: the caller owns the ring tensors and is
    responsible for advancing / resetting ``write_pos`` (e.g. ``(write_pos+1) %
    L`` after each step).  This is a STANDALONE kernel; the memory-pool / cache
    integration is a later phase.
    """
    if mixed_qkv.ndim != 2:
        raise ValueError(f"`mixed_qkv` must be 2D (got ndim={mixed_qkv.ndim}).")
    if mixed_qkv.stride(-1) != 1:
        raise ValueError("`mixed_qkv` must be contiguous in the last dim.")
    if b.ndim != 2:
        raise ValueError(f"`b` must be 2D (got b.ndim={b.ndim}).")
    if A_log.ndim != 1:
        raise ValueError("`A_log` must be a 1D tensor.")
    if initial_state.ndim != 4:
        raise ValueError(f"`initial_state` must be 4D (got ndim={initial_state.ndim}).")
    if not out.is_contiguous():
        raise ValueError("`out` must be contiguous.")
    if write_pos.ndim != 1 or write_pos.dtype != torch.int32:
        raise ValueError("`write_pos` must be a 1D int32 tensor.")
    if force_flush is not None and (
        force_flush.ndim != 1 or force_flush.dtype != torch.int32
    ):
        raise ValueError("`force_flush` must be a 1D int32 tensor or None.")

    B = mixed_qkv.shape[0]
    num_state_slots, HV, V, K = initial_state.shape
    qkv_dim = mixed_qkv.shape[1]
    q_dim = (qkv_dim - HV * V) // 2
    if q_dim <= 0 or q_dim % K != 0:
        raise ValueError(
            f"Invalid packed `mixed_qkv` last dim={qkv_dim} for HV={HV}, V={V}, K={K}."
        )
    H = q_dim // K
    if H <= 0 or HV % H != 0:
        raise ValueError(

View on GitHub (pinned to 0132848349)

Solutions

  1. Allocate out with torch.empty(num_tokens, dim) (contiguous) per step, or copy the strided view into a contiguous tensor
  2. If a persistent buffer is required for graph capture, store outputs in a contiguous slab and slice rows, not columns
  3. Check out.is_contiguous() before the call in debug builds

Example fix

// before
out = ring_buf[:, step, :]  # non-contiguous stride
// after
out = torch.empty(num_tokens, dim, device=dev, dtype=dt)
fused_recurrent_linear_replayssm_decode(..., out=out, ...)
ring_buf[:, step, :] = out
Defensive patterns

Strategy: validation

Validate before calling

if not out.is_contiguous():
    out = out.contiguous()

Type guard

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

Prevention

When it happens

Trigger: Passing out as a slice of a ring buffer (out = ring[:, :, step]), a transposed view, or a tensor with padding between rows.

Common situations: CUDA-graph replay setups where out aliases a strided region of a persistent buffer; preallocating outputs inside a larger padded workspace.

Related errors


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