{"record":{"id":"5cb18175757ea10d","repo":"sgl-project/sglang","slug":"cannot-pad-rope-freqs-of-length-cos-shape-0-to","errorCode":null,"errorMessage":"Cannot pad RoPE freqs of length {cos.shape[0]} to shorter target {target_len}","messagePattern":"Cannot pad RoPE freqs of length (.+?) to shorter target (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/models/dits/zimage.py","lineNumber":1132,"sourceCode":"                size=(1, 1, 1),\n                start=(0, 0, 0),\n                device=device,\n            )\n            .flatten(0, 2)\n            .repeat(image_padding_len, 1)\n        )\n        image_pos_ids = torch.cat([image_ori_pos_ids, image_padding_pos_ids], dim=0)\n\n        return self.rotary_emb(cap_pos_ids), self.rotary_emb(image_pos_ids)\n\n    @staticmethod\n    def _pad_freqs_cis_to_length(\n        freqs_cis: Tuple[torch.Tensor, torch.Tensor], target_len: int\n    ) -> Tuple[torch.Tensor, torch.Tensor]:\n        cos, sin = freqs_cis\n        pad_len = target_len - cos.shape[0]\n        if pad_len < 0:\n            raise ValueError(\n                f\"Cannot pad RoPE freqs of length {cos.shape[0]} to shorter target {target_len}\"\n            )\n        if pad_len == 0:\n            return cos, sin\n        return (\n            torch.cat([cos, cos.new_zeros(pad_len, cos.shape[-1])], dim=0),\n            torch.cat([sin, sin.new_zeros(pad_len, sin.shape[-1])], dim=0),\n        )\n\n    def _build_batched_freqs_cis(\n        self,\n        images: list[torch.Tensor],\n        cap_feats: list[torch.Tensor],\n        patch_size: int,\n        f_patch_size: int,\n        image_target_len: int,\n        cap_target_len: int,\n    ) -> Tuple[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]:","sourceCodeStart":1114,"sourceCodeEnd":1150,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/models/dits/zimage.py#L1114-L1150","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\ntarget_len = min(seq.shape[0] for seq in seqs)\n# after\ntarget_len = max(seq.shape[0] for seq in seqs)","handlingStrategy":"validation","validationCode":"target_len = max(s.shape[0] for s in seqs)\nassert target_len >= cos.shape[0]","typeGuard":"def can_pad_to(cos: torch.Tensor, target: int) -> bool:\n    return target >= cos.shape[0]","tryCatchPattern":null,"preventionTips":["Always compute padded target as max sequence length across the batch"],"tags":["z-image","rope","shape-validation"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}