{"record":{"id":"f92459b3de02e001","repo":"sgl-project/sglang","slug":"the-original-encoder-only-has-num-hidden-layers-f92459","errorCode":null,"errorMessage":"The original encoder only has {num_hidden_layers} layers, but you requested {len(self.encoder.layers)} layers.","messagePattern":"The original encoder only has (.+?) layers, but you requested (.+?) layers\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/models/siglip.py","lineNumber":268,"sourceCode":"        embed_dim = config.hidden_size\n\n        self.embeddings = SiglipVisionEmbeddings(\n            config, use_data_parallel=use_data_parallel\n        )\n\n        self.encoder = SiglipEncoder(\n            config=config,\n            qkv_backend=qkv_backend,\n            act_layer=act_layer,\n            flatten_batch=flatten_batch,\n            use_data_parallel=use_data_parallel,\n            quant_config=quant_config,\n            prefix=add_prefix(\"encoder\", prefix),\n        )\n\n        num_hidden_layers = config.num_hidden_layers\n        if len(self.encoder.layers) > config.num_hidden_layers:\n            raise ValueError(\n                f\"The original encoder only has {num_hidden_layers} \"\n                f\"layers, but you requested {len(self.encoder.layers)} layers.\"\n            )\n\n        # VisionAttention in SiglipEncoderLayer is multihead attention\n        self.post_layernorm = nn.LayerNorm(embed_dim, eps=config.layer_norm_eps)\n\n    @property\n    def device(self) -> torch.device:\n        return self.embeddings.patch_embedding.weight.device\n\n    def forward(\n        self,\n        pixel_values: torch.Tensor,\n    ) -> torch.Tensor:\n        hidden_states = self.embeddings(pixel_values.to(self.device)).to(\n            self.post_layernorm.weight.dtype\n        )","sourceCodeStart":250,"sourceCodeEnd":286,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/models/siglip.py#L250-L286","documentation":"SiglipVisionModel builds its encoder with an optional layer-count override; if the constructed encoder ends up with more layers than config.num_hidden_layers, the checkpoint is inconsistent with the request and init aborts. This mirrors HF's check that you cannot request more layers than the pretrained encoder has.","triggerScenarios":"Passing a num_hidden_layers_override (or a config where the override exceeds num_hidden_layers) when constructing SiglipVisionModel, making len(self.encoder.layers) > config.num_hidden_layers at python/sglang/srt/models/siglip.py:268.","commonSituations":"Server args like --num-hiddenLayers-override style vision-layer trimming features, or a mismatched config.json where num_hidden_layers was edited down; typically an override value typo (larger instead of smaller).","solutions":["Set the layer override to a value <= num_hidden_layers (layer trimming only reduces)","Drop the override entirely to use all pretrained layers","Check config.json num_hidden_layers matches the checkpoint you loaded"],"exampleFix":"# before\noverride = 40  # checkpoint only has 27\n# after\noverride = 16  # or None to use all layers","handlingStrategy":"validation","validationCode":"n = config.num_hidden_layers\nassert override is None or override <= n, f\"override {override} > {n} layers\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat layer overrides as trim-only; clamp to [1, num_hidden_layers]"],"tags":["siglip","vision-encoder","layer-override"],"backgroundTag":"layer-count-override-invalid","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}