sgl-project/sglang · error · ValueError

bad compress_ratio {compress_ratio}

Error message

bad compress_ratio {compress_ratio}

What it means

build_prefill_indices only supports compress_ratio values 0 (no compression), 128 (C128 full-context pages), and 4 (C4 sparse pages). Any other ratio hits the else branch and raises ValueError.

Source

Thrown at python/sglang/kernels/ops/attention/dsv4/unified_kv_kernels/runtime.py:420

    c128_page_indices: Optional[torch.Tensor],
    c4_sparse_page_indices: Optional[torch.Tensor],
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
    """Build ragged prefill indices: prefix (SWA ring + swa_pages + compressed) into unified_kv + extend into current-chunk kv; returns (prefix_indices, prefix_indptr, extend_indices, extend_indptr)."""
    device = state_slot.device
    T = state_slot.shape[0]
    assert positions.is_contiguous() and chunk_start.is_contiguous()
    assert cu_q.is_contiguous() and state_slot.is_contiguous()

    if compress_ratio == 0:
        page_idx = None
    elif compress_ratio == 128:
        assert c128_page_indices is not None
        page_idx = c128_page_indices[:T]
    elif compress_ratio == 4:
        assert c4_sparse_page_indices is not None
        page_idx = c4_sparse_page_indices[:T]
    else:
        raise ValueError(f"bad compress_ratio {compress_ratio}")

    has_compress = page_idx is not None
    if has_compress:
        assert page_idx.is_contiguous()
    Wc = page_idx.shape[1] if has_compress else 0

    block = min(1024, triton.next_power_of_2(max(win, Wc, 1)))
    prefix_len = torch.empty(T, dtype=torch.int32, device=device)
    extend_len = torch.empty(T, dtype=torch.int32, device=device)
    _prefill_lengths_kernel[(T,)](
        positions,
        chunk_start,
        page_idx if has_compress else positions,  # dummy ptr when no compress
        prefix_len,
        extend_len,
        win=win,
        Wc=Wc if has_compress else 1,
        HAS_COMPRESS=has_compress,

View on GitHub (pinned to 0132848349)

Solutions

  1. Set compress_ratio to one of the supported values: 0, 128, or 4
  2. If you need a new ratio, extend the if/elif chain in build_prefill_indices and provide the matching page-indices tensor
  3. Trace where compress_ratio is computed (scheduler/config) and validate it against the allowed set early

Example fix

// before
idx = build_prefill_indices(..., compress_ratio=64)
// after
idx = build_prefill_indices(..., compress_ratio=128)  # or 0 / 4
Defensive patterns

Strategy: validation

Validate before calling

assert compress_ratio in (0, 4, 128), f"unsupported compress_ratio {compress_ratio}"

Type guard

def compress_ratio_ok(r: int) -> bool:
    return r in (0, 4, 128)

Try / catch

try:
    idx = build_prefill_indices(..., compress_ratio=cr)
except ValueError:
    cr = 0  # fall back to no compression
    idx = build_prefill_indices(..., compress_ratio=cr)

Prevention

When it happens

Trigger: Calling build_prefill_indices with compress_ratio not in {0, 128, 4} — e.g. a config exposing 16, 32, or 64, or an uninitialized/None value coerced to an unexpected int.

Common situations: Adding a new compression granularity to the DSV4 unified-KV runtime without extending this dispatcher; a typo in config plumbing; a default value that was never updated when the API changed from a boolean to a ratio.

Related errors


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