sgl-project/sglang · error · ValueError
Cannot pad RoPE freqs of length
Error message
Cannot pad RoPE freqs of length {cos.shape[0]} to shorter target {target_len} What it means
Raised by ZImage._pad_freqs_cis_to_length when the precomputed RoPE cos/sin table is longer than the requested target length. Padding only extends tables; truncation is refused because silently dropping positional frequencies would misalign position ids.
Solutions
- Compute target_len as max(cos.shape[0] for all sequences in the batch)
- Fix the caller computing image_seq_len_target so it is never below the actual token count
- Truncate explicitly before calling if truncation is genuinely intended
Example fix
# before target_len = min(seq.shape[0] for seq in seqs) # after target_len = max(seq.shape[0] for seq in seqs)
Defensive patterns
Strategy: validation
Validate before calling
target_len = max(s.shape[0] for s in seqs) assert target_len >= cos.shape[0]
Type guard
def can_pad_to(cos: torch.Tensor, target: int) -> bool:
return target >= cos.shape[0] Prevention
- Always compute padded target as max sequence length across the batch
When it happens
Trigger: The batched RoPE builder targets a sequence length smaller than an individual sequence's cos table length — e.g. a longer image sequence batched with a max-length computed from a shorter member.
Common situations: Mixed-resolution image batches where the target length (e.g. SP-local padded length) is computed from the wrong tensor; off-by-one in seq_len calculation.
Understand the failure class
Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.
Related errors
- cos and sin must have matching [S, D/2] shapes
- Expected x.shape[-1] to be even for split rotary, got
- Expected x.shape[-1] to be even for split rotary, got
- head_dim must be a multiple of 8, got
- head_dim must be positive, even, and <= 128
AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28).
Data as JSON: /api/errors/5cb18175757ea10d.
Report an issue: GitHub.
Appendix: source
Thrown at python/sglang/multimodal_gen/runtime/models/dits/zimage.py:1132
size=(1, 1, 1),
start=(0, 0, 0),
device=device,
)
.flatten(0, 2)
.repeat(image_padding_len, 1)
)
image_pos_ids = torch.cat([image_ori_pos_ids, image_padding_pos_ids], dim=0)
return self.rotary_emb(cap_pos_ids), self.rotary_emb(image_pos_ids)
@staticmethod
def _pad_freqs_cis_to_length(
freqs_cis: Tuple[torch.Tensor, torch.Tensor], target_len: int
) -> Tuple[torch.Tensor, torch.Tensor]:
cos, sin = freqs_cis
pad_len = target_len - cos.shape[0]
if pad_len < 0:
raise ValueError(
f"Cannot pad RoPE freqs of length {cos.shape[0]} to shorter target {target_len}"
)
if pad_len == 0:
return cos, sin
return (
torch.cat([cos, cos.new_zeros(pad_len, cos.shape[-1])], dim=0),
torch.cat([sin, sin.new_zeros(pad_len, sin.shape[-1])], dim=0),
)
def _build_batched_freqs_cis(
self,
images: list[torch.Tensor],
cap_feats: list[torch.Tensor],
patch_size: int,
f_patch_size: int,
image_target_len: int,
cap_target_len: int,
) -> Tuple[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]:View on GitHub (pinned to 0132848349)