{"record":{"id":"5bfddb66efd1db4d","repo":"sgl-project/sglang","slug":"f-input-shape-tuple-x-shape-must-be-divisible-b","errorCode":null,"errorMessage":"f\"Input shape {tuple(x.shape)} must be divisible by patch_size {self.patch_size}\"","messagePattern":"f\"Input shape (.+?) must be divisible by patch_size (.+?)\"","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/layers/visual_embedding.py","lineNumber":149,"sourceCode":"            patch_size = tuple(patch_size)\n        else:\n            raise ValueError(f\"Unsupported patch_size type: {type(patch_size)}\")\n\n        self.patch_size = patch_size\n        pt, ph, pw = self.patch_size\n        self.in_features = in_chans * pt * ph * pw\n        self.proj = nn.Linear(self.in_features, embed_dim, bias=bias, dtype=dtype)\n\n    def forward(self, x: torch.Tensor) -> torch.Tensor:\n        if x.dim() != 5:\n            raise ValueError(\n                f\"Expected camera embedding shape [B, C, F, H, W], got {tuple(x.shape)}\"\n            )\n\n        bsz, channels, frames, height, width = x.shape\n        pt, ph, pw = self.patch_size\n        if (frames % pt) != 0 or (height % ph) != 0 or (width % pw) != 0:\n            raise ValueError(\n                f\"Input shape {tuple(x.shape)} must be divisible by patch_size {self.patch_size}\"\n            )\n\n        x = x.view(\n            bsz,\n            channels,\n            frames // pt,\n            pt,\n            height // ph,\n            ph,\n            width // pw,\n            pw,\n        )\n        x = x.permute(0, 2, 4, 6, 1, 3, 5, 7).reshape(bsz, -1, self.in_features)\n        return self.proj(x)\n\n\nclass Timesteps(_Timesteps):","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/layers/visual_embedding.py#L131-L167","documentation":"After confirming the input is 5-D, forward() checks that the temporal, height and width dimensions are exactly divisible by the configured patch_size (pt, ph, pw). If frames % pt, height % ph, or width % pw is nonzero, it raises this ValueError because the patchify view() would be invalid.","triggerScenarios":"Calling forward with e.g. patch_size=(2,16,16) on a clip of 15 frames, or a 1023x1023 image with patch 16. Any non-multiple spatial/temporal extent triggers it.","commonSituations":"Variable-FPS video sampling producing odd frame counts with a temporal patch of 2; resolution not a multiple of the patch size (e.g. 1000px with patch 16); mixing a model config's patch_size with a different preprocessing resolution.","solutions":["Crop or pad the input so F,H,W are multiples of pt,ph,pw (e.g. center-crop to 1024, trim frames to an even count)","Align preprocessing resolution with the model's patch_size from config","For odd frame counts with pt=2, drop the extra frame: x = x[:, :, : x.shape[2] // pt * pt]"],"exampleFix":"# before\nemb = layer(video)  # video: [B,3,15,1023,1023], patch (2,16,16)\n# after\nvideo = video[:, :, :14, :1024, :1024]\nemb = layer(video)","handlingStrategy":"validation","validationCode":"pt, ph, pw = layer.patch_size\nf, h, w = x.shape[2], x.shape[3], x.shape[4]\nx = x[:, :, : f // pt * pt, : h // ph * ph, : w // pw * pw]  # crop to multiples","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Center-crop/pad frames and spatial dims to multiples of patch_size during preprocessing","Keep resolution and frame-count settings aligned with the model config's patch_size"],"tags":["multimodal","tensor-shape","patch-embedding"],"backgroundTag":"tensor-shape-not-divisible","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}