sgl-project/sglang · error · ValueError

spt must be a bool when provided

Error message

spt must be a bool when provided

What it means

Raised when the spt field of BlockSparseTensorsTorch is provided but is not a Python bool. spt toggles a special kernel mode and is a flag, not a tensor.

Source

Thrown at python/sglang/kernels/ops/attention/flash_attn/cute/block_sparsity.py:492

    dq_write_order = _check_and_expand_metadata_tensor(
        "dq_write_order",
        tensors.dq_write_order,
        tuple(mask_idx.shape),
        context,
        hint,
        mask_cnt.device,
    )
    dq_write_order_full = _check_and_expand_metadata_tensor(
        "dq_write_order_full",
        tensors.dq_write_order_full,
        tuple(full_idx.shape) if full_idx is not None else expected_index_shape,
        context,
        hint,
        mask_cnt.device,
    )
    spt = tensors.spt
    if spt is not None and not isinstance(spt, bool):
        raise ValueError("spt must be a bool when provided")
    if spt is not None and dq_write_order is None:
        raise ValueError("spt requires dq_write_order to be provided")

    return BlockSparseTensorsTorch(
        mask_block_cnt=mask_cnt,
        mask_block_idx=mask_idx,
        full_block_cnt=full_cnt,
        full_block_idx=full_idx,
        cu_total_m_blocks=tensors.cu_total_m_blocks,
        cu_block_idx_offsets=tensors.cu_block_idx_offsets,
        block_size=tensors.block_size,
        dq_write_order=dq_write_order,
        dq_write_order_full=dq_write_order_full,
        spt=spt,
    )


def is_block_sparsity_enabled(tensors: BlockSparseTensorsTorch) -> bool:

View on GitHub (pinned to 0132848349)

Solutions

  1. Coerce to bool when loading config: spt=bool(cfg['spt'])
  2. Assign True/False directly

Example fix

// before
tensors.spt = cfg['spt']  # 'true' (str)
// after
tensors.spt = bool(cfg['spt'])
Defensive patterns

Strategy: type-guard

Validate before calling

assert tensors.spt is None or isinstance(tensors.spt, bool)

Type guard

def valid_spt(s): return s is None or isinstance(s, bool)

Prevention

When it happens

Trigger: Setting tensors.spt = 1, 'true', or a torch tensor instead of True/False before normalize_block_sparse_tensors.

Common situations: Loading config from JSON/YAML where spt arrives as a string/int and is assigned verbatim.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


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