{"record":{"id":"d72219a64fc871d3","repo":"sgl-project/sglang","slug":"img-position-ids-must-be-1-s-3-got-list-img","errorCode":null,"errorMessage":"img_position_ids must be [1, S, 3], got {list(img_position_ids.shape)}","messagePattern":"img_position_ids must be \\[1, S, 3\\], got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/models/dits/minimax_h3.py","lineNumber":425,"sourceCode":"class MiniMaxH3Rope(nn.Module):\n    \"\"\"3D rope over (t, h, w); rotates 96 of 128 head dims (rotary_percent 0.75).\n\n    Frequency layout concatenates temporal, height, and width embeddings twice,\n    with 16 frequencies per axis (inv_freq = base^-(arange(0,32,2)/32)).\n    \"\"\"\n\n    def __init__(self, inv_freq_len: int) -> None:\n        super().__init__()\n        self.register_buffer(\n            \"inv_freq\",\n            torch.empty(inv_freq_len, dtype=_FP32_DTYPE),\n            persistent=True,\n        )\n\n    def forward(self, img_position_ids: torch.Tensor) -> torch.Tensor:\n        \"\"\"img_position_ids: [1, S, 3] (t, h, w) -> freqs [S, rot_dim=96].\"\"\"\n        if img_position_ids.dim() != 3 or img_position_ids.shape[0] != 1:\n            raise ValueError(\n                \"img_position_ids must be [1, S, 3], got \"\n                f\"{list(img_position_ids.shape)}\"\n            )\n        pos = img_position_ids[0].to(_FP32_DTYPE)  # [S, 3]\n        per_axis = pos.unsqueeze(-1) * self.inv_freq.view(1, 1, -1)  # [S, 3, 16]\n        t_f, h_f, w_f = per_axis.unbind(dim=1)  # each [S, 16]\n        half = torch.cat((t_f, h_f, w_f), dim=-1)  # [S, 48]\n        return torch.cat((half, half), dim=-1)  # [S, 96]\n\n\ndef _rope_cos_sin_cache(freqs: torch.Tensor, *, dtype: torch.dtype) -> torch.Tensor:\n    \"\"\"Build the activation-dtype cos|sin cache for fused Q/K RoPE.\"\"\"\n    half = freqs.shape[-1] // 2\n    return (\n        torch.cat(\n            (torch.cos(freqs[:, :half]), torch.sin(freqs[:, :half])),\n            dim=-1,\n        )","sourceCodeStart":407,"sourceCodeEnd":443,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/models/dits/minimax_h3.py#L407-L443","documentation":"MiniMax H3's RoPE embedding module expects image position ids of exact shape [1, S, 3] (batch 1, sequence S, coords t/h/w). It validates dims and shape[0]==1 and raises otherwise, since downstream indexing img_position_ids[0] assumes that layout.","triggerScenarios":"Passing img_position_ids with batch > 1 (e.g. [B,S,3] from a batched pipeline), a squeezed [S,3] tensor, or a 4-D tensor; common when callers broadcast or stack per-sample position ids.","commonSituations":"Batched generation code producing [B,S,3] position ids; position-id builders from other models (LLM-style [1,S] or [B,S]) reused here; accidental unsqueeze/expand of the coords.","solutions":["Reshape to exactly [1, S, 3] before calling forward: ids.squeeze(0) if batched per-sample then process one at a time, or ids[0:1] to take batch dim of 1","If truly batching, loop over the batch or ensure the module is called per sample since it only supports batch 1","Fix the position-id generator to emit [1, S, 3] (t, h, w stacked on the last axis)"],"exampleFix":"# before\nfreqs = rope(img_position_ids)  # shape [B, S, 3]\n\n# after\nassert img_position_ids.shape[0] == 1\nfreqs = rope(img_position_ids)  # pass ids[0:1] or per-sample slice","handlingStrategy":"type-guard","validationCode":"assert img_position_ids.dim() == 3 and img_position_ids.shape[0] == 1 and img_position_ids.shape[-1] == 3, 'need [1, S, 3]'","typeGuard":"def is_valid_pos_ids(t: torch.Tensor) -> bool:\n    return t.dim() == 3 and t.shape[0] == 1 and t.shape[2] == 3","tryCatchPattern":null,"preventionTips":["Standardize position-id builders to emit [1, S, 3]","Process batched samples one at a time through RoPE modules that assume batch 1"],"tags":["rope","position-ids","shape-mismatch","minimax-h3","input-validation"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}