sgl-project/sglang · error · ValueError

wrong pixel_values size: {pixel_values.shape}

Error message

wrong pixel_values size: {pixel_values.shape}

What it means

When pixel_embeds is None, pixel_values must be a 4-D batch-of-images tensor so self.embeddings can process it. Any other rank (e.g., 3-D flattened patches) is rejected.

Source

Thrown at python/sglang/srt/models/internvl.py:481

        output_hidden_states = (
            output_hidden_states
            if output_hidden_states is not None
            else self.config.output_hidden_states
        )
        return_dict = (
            return_dict if return_dict is not None else self.config.use_return_dict
        )

        if pixel_values is None and pixel_embeds is None:
            raise ValueError("You have to specify pixel_values or pixel_embeds")

        if pixel_embeds is not None:
            hidden_states = pixel_embeds
        else:
            if len(pixel_values.shape) == 4:
                hidden_states = self.embeddings(pixel_values)
            else:
                raise ValueError(f"wrong pixel_values size: {pixel_values.shape}")

        if self.use_data_parallel:
            encoder_outputs = run_dp_sharded_vision_model(hidden_states, self.encoder)
            last_hidden_state = encoder_outputs
        else:
            encoder_outputs = self.encoder(
                inputs_embeds=hidden_states,
                output_hidden_states=output_hidden_states,
                return_dict=return_dict,
            )
            last_hidden_state = encoder_outputs.last_hidden_state
        pooled_output = last_hidden_state[:, 0, :]

        if not return_dict:
            return (last_hidden_state, pooled_output) + encoder_outputs[1:]

        if self.use_data_parallel:
            return BaseModelOutputWithPooling(

View on GitHub (pinned to 0132848349)

Solutions

  1. Supply pixel_values as [batch, channels, height, width] (rank 4)
  2. If data is pre-embedded, pass it via pixel_embeds instead
  3. Unsqueeze missing batch dimension before calling forward

Example fix

# before
model.vision_model(pixel_values=patches_3d)  # (C, H, W)

# after
model.vision_model(pixel_values=patches_3d.unsqueeze(0))  # (1, C, H, W)
Defensive patterns

Strategy: validation

Validate before calling

if pixel_values is not None and pixel_values.dim() != 4:
    pixel_values = pixel_values.unsqueeze(0)

Type guard

def is_batched_images(t) -> bool:
    return t is not None and t.dim() == 4

Prevention

When it happens

Trigger: InternVisionModel.forward with pixel_values of shape rank != 4 — typically pre-flattened patch embeddings or a single image without a batch dim.

Common situations: Passing already-patchified data from a custom preprocessor, or shape drift after an image-processing pipeline change.

Related errors


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