sgl-project/sglang · error · ValueError

kv-canary: lut_len must be 0 when has_swa_lut is False

Error message

kv-canary: lut_len must be 0 when has_swa_lut is False

What it means

When there is no SWA LUT the kernel specialization expects lut_len to be exactly 0 so it can skip LUT addressing entirely; a nonzero lut_len with has_swa_lut=False indicates inconsistent arguments.

Source

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

        )
    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")
    if not has_swa_lut and lut_len != 0:
        raise ValueError("kv-canary: lut_len must be 0 when has_swa_lut is False")

    _require_len(req_pool_indices, "req_pool_indices", bs)
    _require_len(prefix_lens, "prefix_lens", bs)
    _require_len(extend_seq_lens, "extend_seq_lens", bs)
    _require_2d(req_to_token, "req_to_token")
    _require_min_len(lut_tensor, "lut_tensor", max(lut_len, 1))
    _require_min_len(
        out_verify_offsets_scratch,
        "out_verify_offsets_scratch",
        _PLAN_BS_BLOCK_SIZE + 1,
    )
    _require_len(out_write_offsets, "out_write_offsets", write_offsets_len)
    _require_len(
        out_write_seed_slot_indices,
        "out_write_seed_slot_indices",
        write_req_capacity,
    )
    _require_len(out_verify_num_valid, "out_verify_num_valid", 1)

View on GitHub (pinned to 0132848349)

Solutions

  1. Derive both from the same source: has_swa_lut = lut is not None; lut_len = int(lut.shape[0]) if has_swa_lut else 0
  2. Use the module's prepare-LUT helper which returns a consistent (lut, len, flag) triple

Example fix

// before
launch_plan_offsets_kernel(..., lut_tensor=lut, lut_len=int(lut.shape[0]), has_swa_lut=False)
// after
has_swa = lut is not None
launch_plan_offsets_kernel(..., lut_tensor=lut if has_swa else zeros1, lut_len=int(lut.shape[0]) if has_swa else 0, has_swa_lut=has_swa)
Defensive patterns

Strategy: validation

Validate before calling

has_swa_lut = lut is not None
lut_len = int(lut.shape[0]) if has_swa_lut else 0

Prevention

When it happens

Trigger: Calling launch_plan_offsets_kernel with has_swa_lut=False but lut_len > 0, e.g. passing a placeholder LUT tensor while disabling the flag.

Common situations: Inconsistent derivation of the pair — the flag computed from one source (window size) and the length from another (a non-None tensor).

Related errors


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