{"record":{"id":"948c6ecf050bfc77","repo":"sgl-project/sglang","slug":"unexpected-hidden-shape-list-hidden-shape-expe","errorCode":null,"errorMessage":"unexpected hidden shape {list(hidden.shape)}, expected {expected_shape}","messagePattern":"unexpected hidden shape (.+?), expected (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/models/encoders/minimax_h3_qwen3vl.py","lineNumber":388,"sourceCode":"        }\n        if position_ids is not None:\n            call_kwargs[\"position_ids\"] = position_ids.to(self.device)\n        if pixel_values is not None:\n            call_kwargs[\"pixel_values\"] = pixel_values.to(self.device, torch.bfloat16)\n            call_kwargs[\"image_grid_thw\"] = host_image_grid_thw\n        if pixel_values_videos is not None:\n            call_kwargs[\"pixel_values_videos\"] = pixel_values_videos.to(\n                self.device, torch.bfloat16\n            )\n            call_kwargs[\"video_grid_thw\"] = host_video_grid_thw\n\n        hidden = self(**call_kwargs).last_hidden_state[0]\n        if self.conditioning_projection is not None:\n            hidden = self.conditioning_projection(hidden)\n        hidden = hidden.to(torch.bfloat16)\n        expected_shape = [int(ids.shape[1]), self.hidden_dim]\n        if list(hidden.shape) != expected_shape:\n            raise ValueError(\n                f\"unexpected hidden shape {list(hidden.shape)}, \"\n                f\"expected {expected_shape}\"\n            )\n        return hidden\n\n    def load_weights(\n        self,\n        weights: Iterable[tuple[str, torch.Tensor]],\n    ) -> set[str]:\n        params = dict(self.named_parameters(remove_duplicate=False))\n        loaded: set[str] = set()\n        for name, loaded_weight in weights:\n            name = _map_checkpoint_name(name)\n            if not self.should_materialize_checkpoint_weight(name):\n                continue\n            param_name = name\n            param = params.get(param_name)\n            if param is None:","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/models/encoders/minimax_h3_qwen3vl.py#L370-L406","documentation":"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.","triggerScenarios":"encode_ids where list(hidden.shape) != [token_count, self.hidden_dim] after the forward pass and projection.","commonSituations":"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.","solutions":["Print hidden.shape vs expected [ids.shape[1], hidden_dim] to identify which dim diverges","Verify conditioning_projection tap/config consistency (errors 1687-1690)","Ensure no custom patches alter num tokens (pooling/merging) between input_ids and the tapped hidden state"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    hidden = encoder.encode_ids(ids, **mm_kwargs)\nexcept ValueError as e:\n    if \"unexpected hidden shape\" in str(e):\n        logger.error(\"shape drift: %s\", e)\n    raise","preventionTips":["Log expected vs actual shapes in tests for every encoder config change","Re-run shape smoke tests after changing tap/layer configs"],"tags":["minimax-h3","encode-ids","output-shape","sanity-check"],"backgroundTag":"output-shape-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}