{"record":{"id":"fb2e84417edd4b59","repo":"sgl-project/sglang","slug":"f-unsupported-patch-size-type-type-patch-size","errorCode":null,"errorMessage":"f\"Unsupported patch_size type: {type(patch_size)}\"","messagePattern":"f\"Unsupported patch_size type: (.+?)\"","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/layers/visual_embedding.py","lineNumber":133,"sourceCode":"    def __init__(\n        self,\n        patch_size=(1, 2, 2),\n        in_chans=384,\n        embed_dim=2048,\n        bias=True,\n        dtype=None,\n        prefix: str = \"\",\n    ):\n        super().__init__()\n        del prefix\n        if isinstance(patch_size, list | tuple):\n            if len(patch_size) != 3:\n                raise ValueError(\n                    f\"patch_size must have length 3, got {len(patch_size)}\"\n                )\n            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            )","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/layers/visual_embedding.py#L115-L151","documentation":"The visual embedding layer's __init__ accepts patch_size only as an int (converted to a 3-tuple) or a 3-element sequence. Any other type — a string, a single float, a dict, or a tuple/list of length != 3 — reaches the else branch and raises this ValueError.","triggerScenarios":"Constructing the camera/projector embedding layer with patch_size passed as e.g. \"2x16x16\" (a string), 16.0 (a float), a dict from a JSON config, or a 2-element tuple like (16, 16). Only int or length-3 list/tuple of ints is accepted.","commonSituations":"Loading a model config from JSON/YAML where patch_size got serialized as a string or nested list; hand-written model definitions copying HF config strings like patch_size=(1,16) with a typo; config round-tripping converting tuples to strings.","solutions":["Pass patch_size as an int (e.g. 16, meaning (16,16,16)) or a length-3 sequence of ints like (2,16,16) (temporal, height, width)","If the value comes from a config file, parse it before construction: json.loads(value) if isinstance(value, str)","Add a sanity check on the config: assert isinstance(patch_size, (int, list, tuple)) and len(patch_size) in (1, 3)"],"exampleFix":"# before\nlayer = VisualEmbedding(patch_size=\"2x16x16\", ...)\n# after\nlayer = VisualEmbedding(patch_size=(2, 16, 16), ...)","handlingStrategy":"validation","validationCode":"def coerce_patch_size(v):\n    if isinstance(v, int):\n        return (v, v, v)\n    if isinstance(v, (list, tuple)) and len(v) == 3:\n        return tuple(int(x) for x in v)\n    raise TypeError(f\"bad patch_size: {v!r}\")\n\npatch_size = coerce_patch_size(model_config['patch_size'])","typeGuard":"def is_valid_patch_size(v) -> bool:\n    return isinstance(v, int) or (\n        isinstance(v, (list, tuple)) and len(v) == 3 and all(isinstance(x, int) for x in v)\n    )","tryCatchPattern":null,"preventionTips":["Normalize patch_size to a 3-tuple of ints right after reading the config file","Validate config schemas before model construction"],"tags":["config","multimodal","validation","constructor"],"backgroundTag":"invalid-model-config-value","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}