{"record":{"id":"c83fd25fc5291aee","repo":"sgl-project/sglang","slug":"packed-pixel-values-token-count-does-not-match-spa","errorCode":null,"errorMessage":"Packed pixel_values token count does not match spatial_shapes: {pixel_values_flat.shape[0]} vs {total_tokens}.","messagePattern":"Packed pixel_values token count does not match spatial_shapes: (.+?) vs (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/models/siglip2.py","lineNumber":95,"sourceCode":"        Returns:\n            (1, total_tokens, embed_dim) packed embeddings.\n        \"\"\"\n        assert spatial_shapes.device.type == \"cpu\", (\n            \"Expected `spatial_shapes` on CPU to avoid device-to-host sync in \"\n            \"variable-length packing.\"\n        )\n\n        if pixel_values_packed.dim() == 3:\n            assert pixel_values_packed.shape[0] == 1\n            pixel_values_flat = pixel_values_packed[0]\n        else:\n            pixel_values_flat = pixel_values_packed\n\n        lengths = (spatial_shapes[:, 0] * spatial_shapes[:, 1]).to(dtype=torch.int64)\n        lengths_list = lengths.tolist()\n        total_tokens = int(sum(lengths_list))\n        if total_tokens != pixel_values_flat.shape[0]:\n            raise ValueError(\n                \"Packed pixel_values token count does not match spatial_shapes: \"\n                f\"{pixel_values_flat.shape[0]} vs {total_tokens}.\"\n            )\n\n        target_dtype = self.patch_embedding.weight.dtype\n        patch_embeds = self.patch_embedding(pixel_values_flat.to(dtype=target_dtype))\n\n        positional_embeddings = self.position_embedding.weight.reshape(\n            self.position_embedding_size, self.position_embedding_size, -1\n        )\n        packed_pos_embeds = self.resize_positional_embeddings_packed(\n            positional_embeddings,\n            spatial_shapes,\n            lengths_list=lengths_list,\n        )\n\n        embeddings = patch_embeds + packed_pos_embeds\n        return embeddings.unsqueeze(0)","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/models/siglip2.py#L77-L113","documentation":"Siglip2VisionModel.forward with packed (flattened, variable-resolution) pixel_values checks that the first dimension of pixel_values equals the sum of H*W over spatial_shapes. A mismatch means the image tensor and the per-image shape metadata disagree, so patches cannot be correctly unflattened.","triggerScenarios":"Feeding packed pixel_values whose rows != sum(spatial_shapes[:,0]*spatial_shapes[:,1]) — e.g. preprocessing produced shapes for a different image set, wrong patch size, or channel/format mismatch after flattening.","commonSituations":"Custom multimodal preprocessing pipelines for siglip2-based VLMs, resizing images differently than the processor that generated spatial_shapes, or concatenating pixel_values from multiple items without merging spatial_shapes accordingly.","solutions":["Regenerate pixel_values and spatial_shapes with the same processor/preprocessing pass so they stay consistent","Debug-print sum(spatial_shapes[:,0]*spatial_shapes[:,1]) vs pixel_values.shape[0] to find which item diverges","If packing multiple images, ensure spatial_shapes has one row per image and lengths sum to the packed token count"],"exampleFix":"# before\npixel_values = preprocess(img)               # shapes from another config\n# after\nout = processor(images=img, return_spatial_shapes=True)\npixel_values, spatial_shapes = out.pixels, out.shapes  # same pass","handlingStrategy":"validation","validationCode":"lens = (spatial_shapes[:,0]*spatial_shapes[:,1]).sum().item()\nassert pixel_values.shape[0] == lens, (pixel_values.shape[0], lens)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always produce pixel_values and spatial_shapes in one processor call","Add a unit test for your preprocessing with two different image sizes"],"tags":["siglip2","multimodal","preprocessing","shape-mismatch"],"backgroundTag":"input-shape-metadata-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}