sgl-project/sglang · error · ValueError

The original encoder only has {num_hidden_layers} layers, bu

Error message

The original encoder only has {num_hidden_layers} layers, but you requested {len(self.encoder.layers)} layers.

What it means

CLIPVisionModel can optionally build fewer encoder layers than the config declares, but constructing MORE layers than config.num_hidden_layers is inconsistent and rejected. Typically raised when num_hidden_layers was reduced in config but layer construction ignored it.

Source

Thrown at python/sglang/srt/models/clip.py:457

        self.config = config
        embed_dim = config.hidden_size

        self.embeddings = CLIPVisionEmbeddings(config)

        # NOTE: This typo of "layrnorm" is not fixed on purpose to match
        # the original transformers code and name of the model weights.
        self.pre_layrnorm = nn.LayerNorm(embed_dim, eps=config.layer_norm_eps)

        self.encoder = CLIPEncoder(
            config=config,
            quant_config=quant_config,
            prefix=add_prefix("encoder", prefix),
        )

        num_hidden_layers = config.num_hidden_layers
        if len(self.encoder.layers) > config.num_hidden_layers:
            raise ValueError(
                f"The original encoder only has {num_hidden_layers} "
                f"layers, but you requested {len(self.encoder.layers)} layers."
            )

        self.post_layernorm = nn.LayerNorm(embed_dim, eps=config.layer_norm_eps)

    @property
    def device(self) -> torch.device:
        return self.encoder.layers[0].layer_norm1.weight.device

    def forward(
        self,
        pixel_values: torch.Tensor,
    ) -> torch.Tensor:
        hidden_states = self.embeddings(pixel_values.to(self.device))
        hidden_states = self.pre_layrnorm(hidden_states)

        return_all_hidden_states = False

View on GitHub (pinned to 0132848349)

Solutions

  1. Ensure the encoder builds at most config.num_hidden_layers layers (align num_hidden_layers with layer construction params)
  2. Restore the original num_hidden_layers if you did not intend to prune
Defensive patterns

Strategy: validation

Validate before calling

assert len(encoder.layers) <= config.num_hidden_layers

Prevention

When it happens

Trigger: Loading a CLIP vision config with a reduced num_hidden_layers while the encoder still builds the original layer count (e.g. mismatched config after editing layer counts).

Common situations: Pruning or truncating CLIP vision layers by editing config.json without updating every layer-construction parameter.

Related errors


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