sgl-project/sglang · error · ValueError

raw_latent_shape must be (T, H, W) or (B, C, T, H, W) for SA

Error message

raw_latent_shape must be (T, H, W) or (B, C, T, H, W) for SAP attention

What it means

The SVG2 attention metadata builder derives (T, H, W) from raw_latent_shape. It accepts only a 3-tuple (T, H, W) or a 5-tuple (B, C, T, H, W); any other rank (e.g. 4-tuple (C, T, H, W)) raises this ValueError.

Source

Thrown at python/sglang/multimodal_gen/runtime/layers/attention/backends/sparse_video_gen_2_attn.py:151

        num_k_centroids: int,
        top_p_kmeans: float,
        min_kc_ratio: float,
        kmeans_iter_init: int,
        kmeans_iter_step: int,
        zero_step_kmeans_init: bool,
        first_layers_fp: float,
        first_times_fp: float,
        context_length: int = 0,
        prompt_length: int | None = None,
        **kwargs: dict[str, Any],
    ) -> SparseVideoGen2AttentionMetadata:
        raw_shape = tuple(raw_latent_shape)
        if len(raw_shape) == 5:
            t, h, w = raw_shape[2:5]
        elif len(raw_shape) == 3:
            t, h, w = raw_shape
        else:
            raise ValueError(
                "raw_latent_shape must be (T, H, W) or (B, C, T, H, W) for SAP attention"
            )
        pt, ph, pw = patch_size
        if t % pt != 0 or h % ph != 0 or w % pw != 0:
            raise ValueError(
                "raw_latent_shape must be divisible by patch_size for SAP attention"
            )

        num_frame = t // pt
        frame_size = (h // ph) * (w // pw)

        return SparseVideoGen2AttentionMetadata(
            current_timestep=current_timestep,
            num_q_centroids=num_q_centroids,
            num_k_centroids=num_k_centroids,
            top_p_kmeans=top_p_kmeans,
            min_kc_ratio=min_kc_ratio,
            kmeans_iter_init=kmeans_iter_init,

View on GitHub (pinned to 0132848349)

Solutions

  1. Pass a 3-tuple (T, H, W) of spatial-temporal dims only, or a full 5-tuple (B, C, T, H, W).
  2. If you have a (C,T,H,W) latent, strip the channel dim: raw_latent_shape = tuple(latent.shape[1:]) (giving (T,H,W)).
  3. Double-check against the builder's expectations: it reads raw_shape[2:5] for 5-tuples, so ordering matters (B, C, then T, H, W).

Example fix

# before
builder.build(kwargs={"raw_latent_shape": (C, T, H, W), ...})  # 4-tuple -> ValueError

# after
builder.build(kwargs={"raw_latent_shape": (T, H, W), ...})  # or (B, C, T, H, W)
Defensive patterns

Strategy: validation

Validate before calling

def norm_latent_shape(s):
    s = tuple(s)
    if len(s) == 5:
        return s[2:5]
    if len(s) == 3:
        return s
    raise ValueError(f"bad raw_latent_shape: {s}")

T, H, W = norm_latent_shape(latent.shape)  # validate before build

Type guard

def is_valid_latent_shape(s) -> bool:
    return len(tuple(s)) in (3, 5)

Prevention

When it happens

Trigger: Passing raw_latent_shape with 4 elements (channel-first without batch, a common VAE-latent convention) or a flattened scalar/6-tuple; e.g. raw_latent_shape=(16, 64, 64) is fine but (16, 64, 64, 64) fails.

Common situations: Adapting latent shapes from a diffusion VAE whose latents are (C,T,H,W); passing a torch.Size directly where dims don't line up; misreading whether B and C should be included.

Related errors


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