sgl-project/sglang · error · ValueError

Invalid stream index: {idx}

Error message

Invalid stream index: {idx}

What it means

set_current_stream_idx switches the process-global current stream group used for PD multiplexing; the index must address STREAM_GROUPS. Out-of-range indices indicate bookkeeping bugs (e.g. using sm_group_num instead of group index, off-by-one).

Source

Thrown at python/sglang/srt/multiplex/pdmux_context.py:147

    STREAM_GROUPS.append(
        (torch.cuda.Stream(gpu_id), torch.cuda.Stream(gpu_id))
    )  # Normal stream for prefill
    for prefill_sm, decode_sm in divisions:
        STREAM_GROUPS.append(
            (spatial.create_greenctx_stream_by_value(prefill_sm, decode_sm, gpu_id))
        )
    STREAM_GROUPS.append(
        (torch.cuda.Stream(gpu_id), torch.cuda.Stream(gpu_id))
    )  # Normal stream for decode

    CURRENT_STREAM_IDX = 0
    CURRENT_STREAM_GROUP = STREAM_GROUPS[CURRENT_STREAM_IDX]


def set_current_stream_idx(idx: int):
    global CURRENT_STREAM_IDX, CURRENT_STREAM_GROUP
    if idx < 0 or idx >= len(STREAM_GROUPS):
        raise ValueError(f"Invalid stream index: {idx}")
    CURRENT_STREAM_IDX = idx
    CURRENT_STREAM_GROUP = STREAM_GROUPS[CURRENT_STREAM_IDX]


def get_stream_groups() -> list[tuple[torch.cuda.Stream, torch.cuda.Stream]]:
    """Get the stream groups."""
    return STREAM_GROUPS


def get_sm_counts() -> list[tuple[int, int]]:
    """Get the SM counts."""
    return SM_COUNTS


def get_current_stream_idx() -> int:
    """Get the current stream index."""
    return CURRENT_STREAM_IDX

View on GitHub (pinned to 0132848349)

Solutions

  1. Use idx in range [0, len(STREAM_GROUPS)-1]; for the last group use len(STREAM_GROUPS)-1
  2. Validate idx against get_stream_groups() length before setting
  3. Re-check whether you meant to pass an index vs a count

Example fix

# before
set_current_stream_idx(sm_group_num)  # off-by-one
# after
set_current_stream_idx(len(get_stream_groups()) - 1)
Defensive patterns

Strategy: validation

Validate before calling

groups = get_stream_groups()
assert 0 <= idx < len(groups), f'idx must be in [0, {len(groups)-1}]'
set_current_stream_idx(idx)

Prevention

When it happens

Trigger: Calling set_current_stream_idx(idx) with idx < 0 or idx >= len(STREAM_GROUPS), e.g. passing a group count or a rank number instead of an index.

Common situations: adjust_stream_groups logic passing sm_group_num as the last index (should be sm_group_num-1); index computed from a differently-sized list after config change.

Related errors


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