sgl-project/sglang · error · ValueError

time must have shape [batch]

Error message

time must have shape [batch]

What it means

create_sinusoidal_pos_embedding expects the time input to be a 1-D tensor with shape [batch], one timestamp per sequence element. If time has more axes (e.g. [batch, seq] or a scalar 0-d tensor), the broadcasting logic downstream would silently produce wrong shapes, so the function guards ndim == 1 and raises. Called from embed_suffix.

Source

Thrown at python/sglang/multimodal_gen/runtime/models/vlas/pi05_core.py:835

            depth=18,
            mlp_dim=16_384,
            num_heads=8,
            num_kv_heads=1,
            head_dim=256,
        )
    raise ValueError(f"Unknown Pi05 Gemma variant: {variant}")


def create_sinusoidal_pos_embedding(
    time: torch.Tensor,
    dimension: int,
    min_period: float,
    max_period: float,
) -> Tensor:
    if dimension % 2 != 0:
        raise ValueError(f"dimension ({dimension}) must be divisible by 2")
    if time.ndim != 1:
        raise ValueError("time must have shape [batch]")
    fraction = torch.linspace(
        0.0,
        1.0,
        dimension // 2,
        dtype=torch.float64,
        device=time.device,
    )
    period = min_period * (max_period / min_period) ** fraction
    scaling = 1.0 / period * 2 * math.pi
    sin_input = scaling[None, :] * time[:, None].to(torch.float64)
    return torch.cat([torch.sin(sin_input), torch.cos(sin_input)], dim=1)


def make_att_2d_masks(
    pad_masks: torch.Tensor,
    att_masks: torch.Tensor,
) -> torch.Tensor:
    if att_masks.ndim != 2 or pad_masks.ndim != 2:

View on GitHub (pinned to 0132848349)

Solutions

  1. Flatten the time tensor before calling: time = time.reshape(-1) or time.squeeze()
  2. If time is a scalar, materialize a 1-D tensor: time = torch.full((batch_size,), t, device=...)
  3. Add an assert time.ndim == 1 upstream in your pipeline to catch rank drift early

Example fix

# before
time = torch.full((batch, 1), t, device=dev)  # shape [B, 1]
emb = create_sinusoidal_pos_embedding(time, dim, pmin, pmax)

# after
time = torch.full((batch,), t, device=dev)  # shape [B]
emb = create_sinusoidal_pos_embedding(time, dim, pmin, pmax)
Defensive patterns

Strategy: validation

Validate before calling

assert isinstance(time, torch.Tensor) and time.ndim == 1, (
    f"time must be [batch], got shape {tuple(time.shape)}"
)

Type guard

def is_batched_time(time: torch.Tensor) -> bool:
    return isinstance(time, torch.Tensor) and time.ndim == 1

Try / catch

try:
    emb = create_sinusoidal_pos_embedding(time, dim, pmin, pmax)
except ValueError:
    time = time.reshape(-1) if isinstance(time, torch.Tensor) else torch.as_tensor([time])
    emb = create_sinusoidal_pos_embedding(time, dim, pmin, pmax)

Prevention

When it happens

Trigger: Passing time with shape [batch, 1], [batch, seq], or a 0-d scalar tensor; commonly from squeezing/unsqueezing mistakes or from feeding per-timestep tensors from the denoising loop without flattening.

Common situations: Adapting a diffusion timestep loop that yields [B,1] tensors; passing time = t.unsqueeze(0) by mistake; refactors that changed tensor rank.

Related errors


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