sgl-project/sglang · error · ValueError

Kimi-K3 processor feature length does not match image grids:

Error message

Kimi-K3 processor feature length does not match image grids: {pixel_values.shape[0]} != {sum(patch_counts)}

What it means

Raised in prepare_image_features when the first dimension of pixel_values returned by the HF processor does not equal the sum of patch counts derived from image_grid_thw. The processor output must be internally consistent because features are split per image by these counts.

Source

Thrown at python/sglang/srt/multimodal/processors/kimi_k3.py:354

                image_bias,
                self._patch_size,
                to_chw=_k3_to_cuda_chw,
                post_resize=lambda x: _fill_transparent_bg(
                    x, self._transparent_bg_config
                ),
            )
        else:
            # The checkpoint CPU processor couples prompt composition with media
            # preprocessing. A synthetic prompt keeps that API but is discarded;
            # image features and grids are independent of its text.
            output = self._cpu_call(self._image_token * len(images), images)
            pixel_values = output["pixel_values"]
            grid_thws = output["image_grid_thw"]

        grids = [tuple(int(value) for value in grid) for grid in grid_thws.tolist()]
        patch_counts = [math.prod(grid) for grid in grids]
        if sum(patch_counts) != pixel_values.shape[0]:
            raise ValueError(
                "Kimi-K3 processor feature length does not match image grids: "
                f"{pixel_values.shape[0]} != {sum(patch_counts)}"
            )
        return (
            list(pixel_values.split(patch_counts)),
            image_sizes,
            resize_configs,
            grids,
        )


class KimiK3ImageProcessor(
    KimiGridMMDataMixin,
    MediaArtifactCacheMixin,
    SGLangBaseProcessor,
):
    models = [KimiK3ForConditionalGeneration]
    artifact_modality = Modality.IMAGE

View on GitHub (pinned to 0132848349)

Solutions

  1. Pin/upgrade transformers to the version matching the Kimi-K3 processor config
  2. Pass raw images and let the processor resize; don't mix precomputed grids with fresh pixel_values
  3. Log grid_thw and pixel_values.shape[0] to identify which image breaks the invariant
Defensive patterns

Strategy: fallback

Validate before calling

import math
def check_processor_output(output):
    grids = output["image_grid_thw"].tolist()
    expected = sum(math.prod(g) for g in grids)
    return output["pixel_values"].shape[0] == expected

Try / catch

try:
    feats = prepare_image_features(batch)
except ValueError:
    feats = [prepare_image_features([img])[0] for img in batch]  # per-image fallback

Prevention

When it happens

Trigger: Calling the HF image processor (or prepare_artifact_batch) with images whose returned pixel_values rows != math.prod(grid) summed over image_grid_thw; happens with patched/partial vision backends that drop rows or return merged grids.

Common situations: Mismatched transformers/processor version emitting different patch merging (e.g. spatial_merge_size change); passing pre-resized images with inconsistent resize configs; a custom processor overriding image_grid_thw.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/43182e13527a3014. Report an issue: GitHub.