sgl-project/sglang · error · ValueError

Unexpected audio latents rank: {audio_latent_model_input.ndi

Error message

Unexpected audio latents rank: {audio_latent_model_input.ndim}, shape={tuple(audio_latent_model_input.shape)}

What it means

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.

Source

Thrown at python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/ltx_2/denoising.py:1178

        return not (ctx.is_ltx23_variant and not ctx.use_ltx23_legacy_one_stage)

    @classmethod
    def _repeat_optional_batch_dim(
        cls,
        tensor: torch.Tensor | None,
        target_batch_size: int,
    ) -> torch.Tensor | None:
        if tensor is None:
            return None
        return cls._repeat_batch_dim(tensor, target_batch_size)

    @staticmethod
    def _get_audio_num_frames_latent(audio_latent_model_input: torch.Tensor) -> int:
        if audio_latent_model_input.ndim == 3:
            return int(audio_latent_model_input.shape[1])
        if audio_latent_model_input.ndim == 4:
            return int(audio_latent_model_input.shape[2])
        raise ValueError(
            "Unexpected audio latents rank: "
            f"{audio_latent_model_input.ndim}, shape={tuple(audio_latent_model_input.shape)}"
        )

    def _prepare_ltx2_model_inputs(
        self,
        ctx: LTX2DenoisingContext,
        step: DenoisingStepState,
        batch: Req,
        server_args: ServerArgs,
        sigma: torch.Tensor,
    ) -> LTX2ModelInputs:
        latent_model_input = ctx.latents.to(ctx.target_dtype)
        audio_latent_model_input = ctx.audio_latents.to(ctx.target_dtype)
        audio_num_frames_latent = self._get_audio_num_frames_latent(
            audio_latent_model_input
        )

View on GitHub (pinned to 0132848349)

Solutions

  1. Reshape audio latents to [B, T, D] (or [B, C, T, D]) before model input prep
  2. Log/inspect audio_latent_model_input.shape at the audio VAE output to find where the rank changed
  3. Pin or update the audio encoder wrapper to the packing this stage expects

Example fix

// before
audio_latents = audio_vae.encode(x).flatten(1)  # ndim == 2 -> raises
// after
audio_latents = audio_vae.encode(x)  # keep [B, T, D]
assert audio_latents.ndim in (3, 4)
Defensive patterns

Strategy: type-guard

Validate before calling

if audio_latent_model_input.ndim not in (3, 4):
    audio_latent_model_input = audio_latent_model_input.reshape(b, t, d)

Type guard

def is_supported_audio_latents(x) -> bool:
    return isinstance(x, torch.Tensor) and x.ndim in (3, 4)

Prevention

When it happens

Trigger: _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.

Common situations: 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.

Related errors


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