sgl-project/sglang · error · ValueError

unexpected hidden shape {list(hidden.shape)}, expected {expe

Error message

unexpected hidden shape {list(hidden.shape)}, expected {expected_shape}

What it means

After running the encoder (and optional conditioning projection), the resulting hidden state must be [seq_len, hidden_dim]. A different shape means the model produced something unexpected — e.g. tap misconfiguration, wrong hidden_dim, or sequence length mismatch.

Source

Thrown at python/sglang/multimodal_gen/runtime/models/encoders/minimax_h3_qwen3vl.py:388

        }
        if position_ids is not None:
            call_kwargs["position_ids"] = position_ids.to(self.device)
        if pixel_values is not None:
            call_kwargs["pixel_values"] = pixel_values.to(self.device, torch.bfloat16)
            call_kwargs["image_grid_thw"] = host_image_grid_thw
        if pixel_values_videos is not None:
            call_kwargs["pixel_values_videos"] = pixel_values_videos.to(
                self.device, torch.bfloat16
            )
            call_kwargs["video_grid_thw"] = host_video_grid_thw

        hidden = self(**call_kwargs).last_hidden_state[0]
        if self.conditioning_projection is not None:
            hidden = self.conditioning_projection(hidden)
        hidden = hidden.to(torch.bfloat16)
        expected_shape = [int(ids.shape[1]), self.hidden_dim]
        if list(hidden.shape) != expected_shape:
            raise ValueError(
                f"unexpected hidden shape {list(hidden.shape)}, "
                f"expected {expected_shape}"
            )
        return hidden

    def load_weights(
        self,
        weights: Iterable[tuple[str, torch.Tensor]],
    ) -> set[str]:
        params = dict(self.named_parameters(remove_duplicate=False))
        loaded: set[str] = set()
        for name, loaded_weight in weights:
            name = _map_checkpoint_name(name)
            if not self.should_materialize_checkpoint_weight(name):
                continue
            param_name = name
            param = params.get(param_name)
            if param is None:

View on GitHub (pinned to 0132848349)

Solutions

  1. Print hidden.shape vs expected [ids.shape[1], hidden_dim] to identify which dim diverges
  2. Verify conditioning_projection tap/config consistency (errors 1687-1690)
  3. Ensure no custom patches alter num tokens (pooling/merging) between input_ids and the tapped hidden state
Defensive patterns

Strategy: try-catch

Try / catch

try:
    hidden = encoder.encode_ids(ids, **mm_kwargs)
except ValueError as e:
    if "unexpected hidden shape" in str(e):
        logger.error("shape drift: %s", e)
    raise

Prevention

When it happens

Trigger: encode_ids where list(hidden.shape) != [token_count, self.hidden_dim] after the forward pass and projection.

Common situations: The tap layer returns a tensor whose seq length differs from input (mis-sliced attention masks); hidden_dim config drift after layer truncation; bugs in custom attention masking for multimodal positions.

Related errors


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