{"record":{"id":"81d776dc4607ff6b","repo":"sgl-project/sglang","slug":"kimi-k3-deferred-feature-length-does-not-match-ima","errorCode":null,"errorMessage":"Kimi-K3 deferred feature length does not match image grids","messagePattern":"Kimi-K3 deferred feature length does not match image grids","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/models/kimi_k3.py","lineNumber":3482,"sourceCode":"                    elif backend == \"cpu\":\n                        from sglang.srt.multimodal.kimi_k3_image_processing import (\n                            materialize_kimi_k3_cpu_features,\n                        )\n\n                        pixel_values = materialize_kimi_k3_cpu_features(\n                            group_items, self._encoder_image_processor\n                        )\n                    else:\n                        raise ValueError(\n                            f\"Unsupported Kimi-K3 deferred preprocessing backend: {backend}\"\n                        )\n\n                    patch_counts = [\n                        int(grid_thws_host[index].prod().item())\n                        for index in global_indices\n                    ]\n                    if sum(patch_counts) != pixel_values.shape[0]:\n                        raise ValueError(\n                            \"Kimi-K3 deferred feature length does not match image grids\"\n                        )\n                    for index, feature in zip(\n                        indices, pixel_values.split(patch_counts), strict=True\n                    ):\n                        materialized[index] = feature\n\n                return materialize_multimodal_features(\n                    materialized,\n                    device=device,\n                    dtype=target_dtype,\n                )\n\n            features = []\n            for item in selected_items:\n                if not isinstance(item.feature, torch.Tensor):\n                    raise TypeError(\n                        \"Kimi-K3 image feature must be a torch.Tensor, \"","sourceCodeStart":3464,"sourceCodeEnd":3500,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/models/kimi_k3.py#L3464-L3500","documentation":"Raised in Kimi-K3's multimodal feature materialization (kimi_k3.py:3482) when deferred image features arrive whose total patch count (computed from each selected image's grid_thw on host) does not equal the number of rows in the incoming pixel_values tensor. This is an internal consistency check between the preprocessor's grid metadata and the batched visual embeddings. It signals that the vision encoder's patch-packing got out of sync with the grid bookkeeping, usually due to request batching/broadcast mismatches or a corrupted mm-processor cache.","triggerScenarios":"Calling get_image_feature -> materialize_item_features with a batch where sum(prod(grid_thws_host[i]) for selected deferred items) != pixel_values.shape[0]. Happens with mixed deferred/cached image batches, sliced/re-batched req inputs, or a stale grid_thws cache entry.","commonSituations":"Custom multi-image workloads, resume/caching of mm inputs, chunked prefill re-batching, or version mismatch between processor and model code after upgrading SGLang.","solutions":["Ensure each request's images and grid_thw metadata travel together unmodified (no manual slicing/rebatching of mm inputs)","Clear any multimodal cache (restart server / disable mm cache) so grid metadata is recomputed","Reproduce single-image vs batched requests to isolate the offending item and check its grid_thw","If persistent, report with a repro script - the invariant between processor and encoder is broken"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"total = sum(int(g.prod().item()) for g in grids)\nassert total == pixel_values.shape[0], f\"{total} != {pixel_values.shape[0]}\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep image payloads and their grid_thw metadata atomically paired through any batching","Avoid hand-slicing multimodal batches; let the scheduler batch mm inputs","Pin model and processor to the same SGLang version"],"tags":["kimi-k3","multimodal","shape-mismatch","vision"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}