sgl-project/sglang · error · ValueError

kv-canary: write_req_capacity must be non-negative, got {wri

Error message

kv-canary: write_req_capacity must be non-negative, got {write_req_capacity}

What it means

The write-request capacity must be non-negative; a negative value would make buffer sizing and bounds checks in the offsets kernel meaningless and indicates corrupted capacity plumbing.

Source

Thrown at python/sglang/kernels/ops/kv_canary/plan/offsets_kernel.py:161

    )
    _require_dtype(out_write_offsets, "out_write_offsets", torch.int64)
    _require_dtype(
        out_write_seed_slot_indices, "out_write_seed_slot_indices", torch.int64
    )
    _require_dtype(out_verify_num_valid, "out_verify_num_valid", torch.int32)
    _require_dtype(out_verify_enable, "out_verify_enable", torch.int32)
    _require_dtype(out_write_num_valid_reqs, "out_write_num_valid_reqs", torch.int32)

    if bs < 0 or bs > _PLAN_BS_BLOCK_SIZE:
        raise ValueError(
            f"kv-canary: offsets kernel bs must be in [0, {_PLAN_BS_BLOCK_SIZE}], got {bs}"
        )
    if write_offsets_len <= 0:
        raise ValueError(
            f"kv-canary: write_offsets_len must be positive, got {write_offsets_len}"
        )
    if write_req_capacity < 0:
        raise ValueError(
            f"kv-canary: write_req_capacity must be non-negative, got {write_req_capacity}"
        )
    if verify_capacity < 0:
        raise ValueError(
            f"kv-canary: verify_capacity must be non-negative, got {verify_capacity}"
        )
    if req_to_token_stride0 <= 0:
        raise ValueError(
            f"kv-canary: req_to_token_stride0 must be positive, got {req_to_token_stride0}"
        )
    if lut_len < 0:
        raise ValueError(f"kv-canary: lut_len must be non-negative, got {lut_len}")
    if not isinstance(has_swa_lut, bool):
        raise ValueError(
            f"kv-canary: has_swa_lut must be bool, got {type(has_swa_lut).__name__}"
        )
    if has_swa_lut and lut_len <= 0:
        raise ValueError("kv-canary: lut_len must be positive when has_swa_lut is True")

View on GitHub (pinned to 0132848349)

Solutions

  1. Compute write_req_capacity from the actual write pool size, clamped at 0
  2. Replace -1 sentinel values with the real capacity or 0

Example fix

// before
write_req_capacity = pool_size - num_running  # can go negative
// after
write_req_capacity = max(0, pool_size - num_running)
Defensive patterns

Strategy: validation

Validate before calling

write_req_capacity = max(0, int(write_req_capacity))

Prevention

When it happens

Trigger: Calling launch_plan_offsets_kernel with write_req_capacity < 0, e.g. from subtracting free slots and underflowing, or from an uninitialized -1 sentinel.

Common situations: Capacity computed as (free - pending) underflowing when more requests are in flight than slots; passing a 'not set' sentinel like -1 instead of a real capacity.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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