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

Raised in CLIPTextModel.__init__ when the constructed encoder has more layers than the checkpoint's config.num_hidden_layers. This guard prevents requesting a deeper text tower than the original model was trained with, which would leave new layers randomly initialized.

Source

Thrown at python/sglang/multimodal_gen/runtime/models/encoders/clip.py:286

        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,
            num_hidden_layers_override=num_hidden_layers_override,
            prefix=f"{prefix}.encoder",
        )

        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 possible, skip post_layernorm to conserve memory
        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

    def forward(
        self,
        pixel_values: torch.Tensor,
        output_hidden_states: Optional[bool] = None,
        feature_sample_layers: list[int] | None = None,

View on GitHub (pinned to 0132848349)

Solutions

  1. Set num_hidden_layers_override <= config.num_hidden_layers (or omit it to use the config value)
  2. If you truly need fewer layers, use the override with a smaller number — truncation is allowed, growth is not
  3. Verify the loaded config's num_hidden_layers matches the checkpoint you intend to use

Example fix

# before
model = CLIPTextModel(config, num_hidden_layers_override=48)  # config has 24
# after
model = CLIPTextModel(config, num_hidden_layers_override=24)
Defensive patterns

Strategy: validation

Validate before calling

assert num_hidden_layers_override is None or num_hidden_layers_override <= config.num_hidden_layers, "cannot exceed checkpoint layer count"

Prevention

When it happens

Trigger: Setting num_hidden_layers_override to a value greater than config.num_hidden_layers when instantiating the CLIP text model.

Common situations: Trying to increase encoder depth for experiments; copying a config with a small num_hidden_layers but a large override; mismatches after editing a model config JSON.

Related errors


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