sgl-project/sglang · error · ValueError

{name} must start with 0 and contain at least one sequence

Error message

{name} must start with 0 and contain at least one sequence

What it means

After materializing boundary values, _packed_boundaries requires at least two entries (one real sequence) and that the first entry is 0. cu_seqlens is a cumulative sum convention: [0, len0, len0+len1, ...]; a tensor not starting at 0 or with fewer than 2 elements is malformed.

Source

Thrown at python/sglang/multimodal_gen/runtime/layers/attention/backends/ascend_fa.py:42

) -> tuple[int, ...]:
    if cu_seqlens is None:
        raise ValueError(f"{name} is required for NPU packed attention")
    if cu_seqlens.ndim != 1 or cu_seqlens.dtype not in (
        torch.int32,
        torch.int64,
    ):
        raise ValueError(f"{name} must be a 1D int32 or int64 tensor")
    if cu_seqlens_host is not None and len(cu_seqlens_host) != cu_seqlens.numel():
        raise ValueError(f"{name} and its host copy must have the same length")

    boundaries = tuple(
        int(value)
        for value in (
            cu_seqlens.tolist() if cu_seqlens_host is None else cu_seqlens_host
        )
    )
    if len(boundaries) < 2 or boundaries[0] != 0:
        raise ValueError(f"{name} must start with 0 and contain at least one sequence")
    if boundaries[-1] != total_tokens:
        raise ValueError(
            f"{name} must end at the packed token count {total_tokens}, "
            f"got {boundaries[-1]}"
        )
    if any(stop < start for start, stop in zip(boundaries[:-1], boundaries[1:])):
        raise ValueError(f"{name} must be non-decreasing")
    return boundaries


def fused_infer_attention_varlen(
    q: torch.Tensor,
    k: torch.Tensor,
    v: torch.Tensor,
    cu_seqlens_q: torch.Tensor,
    cu_seqlens_k: torch.Tensor,
    *,
    cu_seqlens_q_host: Sequence[int] | None = None,

View on GitHub (pinned to 0132848349)

Solutions

  1. Rebase to start at 0: cu = cu - cu[0] when slicing a larger packed tensor
  2. Use the standard construction: cu = torch.tensor([0] + list(torch.tensor(lens).cumsum(0)))
  3. Skip the kernel call entirely for empty batches instead of passing [0]

Example fix

# before
cu = full_cu[start_idx:]  # starts at nonzero offset
# after
cu = full_cu[start_idx:] - full_cu[start_idx]  # rebase to 0
Defensive patterns

Strategy: validation

Validate before calling

assert len(boundaries) >= 2 and boundaries[0] == 0, "cu_seqlens must start at 0 with >=2 entries"

Prevention

When it happens

Trigger: Passing cu_seqlens that starts at a nonzero value (e.g. [3, 8, 12] from slicing without rebasing), or a degenerate tensor like [0] or [] (len(boundaries) < 2).

Common situations: Rebasing/slicing a packed batch and forgetting to subtract boundaries[0]; passing cumsum without the leading zero pad; an empty batch shortcut that still calls the kernel with a 1-element tensor.

Related errors


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