{"record":{"id":"1eedadc47789bc43","repo":"sgl-project/sglang","slug":"name-must-be-rank-rank-got-shape-list-tensor","errorCode":null,"errorMessage":"{name} must be rank {rank}, got shape={list(tensor.shape)}","messagePattern":"(.+?) must be rank (.+?), got shape=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/minimax_h3/packed_tokens.py","lineNumber":20,"sourceCode":"from __future__ import annotations\n\nfrom collections.abc import Sequence\n\nimport torch\n\n\ndef _int_tuple(value: Sequence[int], name: str, length: int) -> tuple[int, ...]:\n    if len(value) != length:\n        raise ValueError(f\"{name} must have length {length}, got {list(value)!r}\")\n    out = tuple(int(item) for item in value)\n    if any(item <= 0 for item in out):\n        raise ValueError(f\"{name} values must be positive, got {list(value)!r}\")\n    return out\n\n\ndef _rank(tensor: torch.Tensor, name: str, rank: int) -> None:\n    if tensor.ndim != rank:\n        raise ValueError(f\"{name} must be rank {rank}, got shape={list(tensor.shape)}\")\n\n\ndef minimax_h3_patchify_video_latent(\n    latent: torch.Tensor,\n    *,\n    patch_size: Sequence[int],\n) -> torch.Tensor:\n    \"\"\"Pack SGLang video latent [B,C,T,H,W] into DiT token rows.\"\"\"\n\n    _rank(latent, \"video latent\", 5)\n    pt, ph, pw = _int_tuple(patch_size, \"patch_size\", 3)\n    batch, channel, full_t, full_h, full_w = (int(dim) for dim in latent.shape)\n    if full_t % pt or full_h % ph or full_w % pw:\n        raise ValueError(\n            \"video latent spatial/time dims must be divisible by patch_size: \"\n            f\"shape={list(latent.shape)}, patch_size={[pt, ph, pw]}\"\n        )\n    t, h, w = full_t // pt, full_h // ph, full_w // pw","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/minimax_h3/packed_tokens.py#L2-L38","documentation":"The _rank helper enforces the tensor rank (number of dimensions) expected by the minimax_h3 token packing functions: video latents and video token rows must be rank 5 and 2 respectively, audio token inputs rank-checked via minimax_h3_unpack_audio_tokens. Passing a tensor with the wrong number of dims fails immediately with the observed shape in the message.","triggerScenarios":"Passing a [B,T,H,W,C] (rank 5 but channels-last, still passes rank check but wrong layout) vs a [C,T,H,W] single-sample (rank 4) video latent; passing video token rows of rank 3 (e.g. with an extra batch dim) to minimax_h3_unpatchify_video_tokens.","commonSituations":"Unsqueezing/squeezing a batch dim inconsistently across pipeline stages; feeding a per-sample latent from a loop (missing batch dim) into a function expecting batched input; tensor arriving as rank-3 after a .mean() or slicing bug.","solutions":["Reshape to the documented rank: video latent [B,C,T,H,W] (rank 5), video token rows rank 2, audio tokens per the audio unpacker's expected rank.","Audit squeeze/unsqueeze calls in the preceding stage; the mismatch is usually one dim off.","Print tensor.shape right before the call during debugging to confirm."],"exampleFix":"# before\nrows = minimax_h3_patchify_video_latent(latent[0], patch_size=patch)  # rank-4 input\n\n# after\nrows = minimax_h3_patchify_video_latent(latent, patch_size=patch)  # keep [B,C,T,H,W]","handlingStrategy":"validation","validationCode":"assert latent.ndim == 5, f\"expected [B,C,T,H,W], got shape {list(latent.shape)}\"\nassert rows.ndim == 2, f\"expected rank-2 token rows, got shape {list(rows.shape)}\"","typeGuard":"import torch\n\ndef is_batched_video_latent(t: torch.Tensor) -> bool:\n    return t.ndim == 5","tryCatchPattern":null,"preventionTips":["Standardize batch handling (always keep the batch dim) across pipeline stages.","Log tensor shapes at stage boundaries during development."],"tags":["validation","tensor-shape","rank","minimax-h3"],"backgroundTag":"invalid-tensor-shape","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}