{"record":{"id":"4bec9b3c0af5a3ef","repo":"sgl-project/sglang","slug":"unexpected-audio-latents-rank-audio-latent-model","errorCode":null,"errorMessage":"Unexpected audio latents rank: {audio_latent_model_input.ndim}, shape={tuple(audio_latent_model_input.shape)}","messagePattern":"Unexpected audio latents rank: (.+?), shape=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/ltx_2/denoising.py","lineNumber":1178,"sourceCode":"        return not (ctx.is_ltx23_variant and not ctx.use_ltx23_legacy_one_stage)\n\n    @classmethod\n    def _repeat_optional_batch_dim(\n        cls,\n        tensor: torch.Tensor | None,\n        target_batch_size: int,\n    ) -> torch.Tensor | None:\n        if tensor is None:\n            return None\n        return cls._repeat_batch_dim(tensor, target_batch_size)\n\n    @staticmethod\n    def _get_audio_num_frames_latent(audio_latent_model_input: torch.Tensor) -> int:\n        if audio_latent_model_input.ndim == 3:\n            return int(audio_latent_model_input.shape[1])\n        if audio_latent_model_input.ndim == 4:\n            return int(audio_latent_model_input.shape[2])\n        raise ValueError(\n            \"Unexpected audio latents rank: \"\n            f\"{audio_latent_model_input.ndim}, shape={tuple(audio_latent_model_input.shape)}\"\n        )\n\n    def _prepare_ltx2_model_inputs(\n        self,\n        ctx: LTX2DenoisingContext,\n        step: DenoisingStepState,\n        batch: Req,\n        server_args: ServerArgs,\n        sigma: torch.Tensor,\n    ) -> LTX2ModelInputs:\n        latent_model_input = ctx.latents.to(ctx.target_dtype)\n        audio_latent_model_input = ctx.audio_latents.to(ctx.target_dtype)\n        audio_num_frames_latent = self._get_audio_num_frames_latent(\n            audio_latent_model_input\n        )\n","sourceCodeStart":1160,"sourceCodeEnd":1196,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/ltx_2/denoising.py#L1160-L1196","documentation":"LTX-2's audio latent frames are read from either a [B, T, D] (3D) or [B, C, T, D] (4D) layout. Any other rank means the audio VAE produced an unexpected packing and the frame count can't be inferred, so a ValueError is raised.","triggerScenarios":"_prepare_ltx2_model_inputs passing audio_latent_model_input with ndim other than 3 or 4 — e.g. a flattened [B, T*D] tensor or an extra batch-like dim from a vae wrapper.","commonSituations":"Switching audio VAE or latent packing format without updating the prep stage; an upstream stage squeezing/unsqueezing dims conditionally; batched-to-unbatched audio latents rank drift across versions.","solutions":["Reshape audio latents to [B, T, D] (or [B, C, T, D]) before model input prep","Log/inspect audio_latent_model_input.shape at the audio VAE output to find where the rank changed","Pin or update the audio encoder wrapper to the packing this stage expects"],"exampleFix":"// before\naudio_latents = audio_vae.encode(x).flatten(1)  # ndim == 2 -> raises\n// after\naudio_latents = audio_vae.encode(x)  # keep [B, T, D]\nassert audio_latents.ndim in (3, 4)","handlingStrategy":"type-guard","validationCode":"if audio_latent_model_input.ndim not in (3, 4):\n    audio_latent_model_input = audio_latent_model_input.reshape(b, t, d)","typeGuard":"def is_supported_audio_latents(x) -> bool:\n    return isinstance(x, torch.Tensor) and x.ndim in (3, 4)","tryCatchPattern":null,"preventionTips":["Assert audio VAE output rank immediately after encoding","Pin the audio encoder wrapper version"],"tags":["ltx-2","audio-latents","rank-check","video-generation"],"backgroundTag":"tensor-rank-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}