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

Same guard as siglip.py: Siglip2VisionModel constructs its encoder with an optional num_hidden_layers_override; requesting more layers than the pretrained encoder's num_hidden_layers is invalid and aborts init.

Source

Thrown at python/sglang/srt/models/siglip2.py:442

        config: Siglip2VisionConfig,
        quant_config: Optional[QuantizationConfig] = None,
        num_hidden_layers_override: Optional[int] = None,
        require_post_norm: Optional[bool] = None,
        prefix: str = "",
    ):
        super().__init__()
        embed_dim = config.hidden_size
        self.config = config
        self.embeddings = Siglip2VisionEmbeddings(config)
        self.encoder = Siglip2Encoder(
            config,
            quant_config=quant_config,
            num_hidden_layers_override=num_hidden_layers_override,
            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."
            )

        if require_post_norm is None:
            require_post_norm = len(self.encoder.layers) == num_hidden_layers

        if require_post_norm:
            self.post_layernorm = nn.LayerNorm(embed_dim, eps=config.layer_norm_eps)
        else:
            self.post_layernorm = None

    @property
    def dtype(self) -> torch.dtype:
        return self.embeddings.patch_embedding.weight.dtype

    @property
    def device(self) -> torch.device:

View on GitHub (pinned to 0132848349)

Solutions

  1. Set the override <= num_hidden_layers or remove it
  2. Verify config.json vision num_hidden_layers matches the checkpoint
  3. Check server args for vision layer overrides before launch

Example fix

# before
override = 40  # siglip2 checkpoint has 27
# after
override = 16  # or None
Defensive patterns

Strategy: validation

Validate before calling

assert override is None or override <= config.num_hidden_layers

Prevention

When it happens

Trigger: Passing num_hidden_layers_override > config.num_hidden_layers when constructing Siglip2VisionModel at python/sglang/srt/models/siglip2.py:442.

Common situations: Vision-layer trimming server args set to a value larger than the checkpoint's vision depth; typo'd override; edited config.json with reduced num_hidden_layers.

Related errors


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