{"record":{"id":"10cee981364781a8","repo":"sgl-project/sglang","slug":"feature-size-mismatch-features-size-0-vs-leng","errorCode":null,"errorMessage":"Feature size mismatch: {features.size(0)} vs {lengths.sum().item()}","messagePattern":"Feature size mismatch: (.+?) vs (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/models/mimo_audio.py","lineNumber":815,"sourceCode":"    def get_output_length(self, mel_len):\n        tgt_len = mel_len + 3 - self.config.kernel_size\n        return (tgt_len + 2 - self.config.kernel_size) // self.config.stride_size + 1\n\n    @torch.no_grad()\n    def encode(self, mels, input_lens, use_quantizer=True):\n        input_features = mels\n        encoder_output_length = self.get_output_length(input_lens)\n        hidden_states, hidden_states_packed, encoder_output_length, codes = (\n            self.encoder.encode(\n                input_features, input_lens=input_lens, use_quantizer=use_quantizer\n            )\n        )\n        return hidden_states, hidden_states_packed, encoder_output_length, codes\n\n\ndef group_by_length(features: torch.Tensor, lengths: torch.Tensor, max_length: int):\n    if features.size(0) != lengths.sum().item():\n        raise ValueError(\n            f\"Feature size mismatch: {features.size(0)} vs {lengths.sum().item()}\"\n        )\n\n    split_points = []\n    current_sum = 0\n\n    for i, seq_len in enumerate(lengths):\n        if current_sum + seq_len > max_length and current_sum > 0:\n            split_points.append(i)\n            current_sum = seq_len.item()\n        else:\n            current_sum += seq_len.item()\n\n    # Convert split points to group sizes\n    group_sizes = []\n    prev = 0\n    for point in split_points:\n        group_sizes.append(point - prev)","sourceCodeStart":797,"sourceCodeEnd":833,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/models/mimo_audio.py#L797-L833","documentation":"group_by_length splits a packed feature tensor into per-sample chunks using the supplied lengths; it asserts features.size(0) equals lengths.sum(). A mismatch means the features and lengths describe different amounts of data (e.g. padded vs packed features, wrong lengths tensor).","triggerScenarios":"Calling group_by_length(features, lengths, max_length) where features was padded (batch-major) instead of packed, or lengths counts frames for a different feature tensor / was computed pre-padding.","commonSituations":"Audio batch encoding pipelines where an upstream op pads features before packing; off-by-one or unit mismatch between encoder frame counts and lengths; reusing lengths from a previous batch.","solutions":["Ensure features is the packed (concatenated along dim 0) tensor produced by encode_batch, not the padded batch tensor","Recompute lengths from the same features tensor: lengths = torch.tensor([f.size(0) for f in feats])","Verify lengths.sum().item() == features.size(0) before calling (see validation snippet)"],"exampleFix":"// before\ngrouped = group_by_length(padded_features, lengths, max_length=4096)\n// after\npacked = torch.cat([f for f in feats], dim=0)\nlengths = torch.tensor([f.size(0) for f in feats])\ngrouped = group_by_length(packed, lengths, max_length=4096)","handlingStrategy":"validation","validationCode":"assert features.size(0) == lengths.sum().item(), (\n    features.shape, lengths.tolist())\nout = group_by_length(features, lengths, max_length)","typeGuard":"def lengths_match(features: torch.Tensor, lengths: torch.Tensor) -> bool:\n    return features.dim() == 2 and features.size(0) == int(lengths.sum().item())","tryCatchPattern":"try:\n    grouped = group_by_length(features, lengths, max_length)\nexcept ValueError as e:\n    raise RuntimeError(f\"packing invariant broken: {e}\") from e","preventionTips":["Always derive lengths from the same features you pack","Assert the sum invariant right after encoding, before any padding step"],"tags":["mimo-audio","tensor-shape","batching","audio"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}