{"record":{"id":"41662c7660148ad0","repo":"sgl-project/sglang","slug":"expected-packed-image-latents-b-s0-d","errorCode":null,"errorMessage":"Expected packed image latents [B, S0, D].","messagePattern":"Expected packed image latents \\[B, S0, D\\]\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/pipelines_core/stages/image_encoding.py","lineNumber":794,"sourceCode":"                    device=device,\n                    dtype=encode_dtype,\n                )\n\n                # 3. Encode\n                if use_condition_encoder:\n                    latent = self._condition_encode(video_condition, server_args).to(\n                        dtype=encode_dtype\n                    )\n                else:\n                    latent = self._vae_encode(\n                        video_condition, server_args, batch.generator\n                    )\n\n                packed = server_args.pipeline_config.maybe_pack_latents(\n                    latent, latent.shape[0], batch\n                )\n                if not (isinstance(packed, torch.Tensor) and packed.ndim == 3):\n                    raise ValueError(\"Expected packed image latents [B, S0, D].\")\n                if int(packed.shape[1]) != expected_tokens:\n                    raise ValueError(\n                        f\"LTX-2 conditioning token count mismatch: \"\n                        f\"{packed.shape[1]=} {expected_tokens=}.\"\n                    )\n                packed_latents.append(packed)\n\n        batch.image_latent = (\n            packed_latents[0] if len(packed_latents) == 1 else packed_latents\n        )\n        batch.ltx2_num_image_tokens = int(packed_latents[0].shape[1])\n\n        if batch.debug:\n            logger.info(\n                \"LTX2 TI2V: %d tokens (shape=%s) for %sx%s\",\n                batch.ltx2_num_image_tokens,\n                tuple(packed_latents[0].shape),\n                batch.width,","sourceCodeStart":776,"sourceCodeEnd":812,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/image_encoding.py#L776-L812","documentation":"The LTX-2 image encoding stage packs per-image VAE latents via pipeline_config.maybe_pack_latents() and requires the result to be a 3-D torch.Tensor of shape [B, S0, D] (batch, packed token slots, latent dim). If maybe_pack_latents returns a non-tensor (e.g. None because packing is not enabled or not applicable) or a tensor of the wrong rank, the stage refuses to continue because downstream transformer conditioning expects exactly this layout.","triggerScenarios":"Calling the multimodal generation pipeline with an LTX-2 image-encoding config where maybe_pack_latents() is a no-op returning the input latent unchanged (rank != 3), or where the VAE output for a reference/conditioning image has unexpected shape (e.g. a 4-D [B,C,H,W] latent or a tuple/distribution object) while the LTX-2 packing path is active.","commonSituations":"Mixing a non-LTX-2 VAE/config with the LTX-2 conditioning path; enabling or disabling latent packing flags inconsistently with the pipeline config; passing precomputed latents of different rank; version changes that alter maybe_pack_latents return contract.","solutions":["Check what pipeline_config.maybe_pack_latents returns for your config — if packing is disabled it returns the latent unchanged, so enable the LTX-2 packing option in pipeline_config","Inspect latent.shape before the call: a rank-4 [B,C,H,W] VAE latent must be reshaped/packed to [B, S0, D] first","Verify server_args.pipeline_config actually corresponds to the LTX-2 task/pipeline you are running (not a generic image pipeline)","Update to a matching sglang version where maybe_pack_latents and the image_encoding stage agree on the packed-latent contract"],"exampleFix":"// before\npacked = server_args.pipeline_config.maybe_pack_latents(latent, latent.shape[0], batch)\n\n// after (ensure VAE latent is flattened to tokens before packing)\nif latent.ndim == 4:\n    latent = latent.reshape(latent.shape[0], -1, latent.shape[-1])\npacked = server_args.pipeline_config.maybe_pack_latents(latent, latent.shape[0], batch)","handlingStrategy":"validation","validationCode":"packed = server_args.pipeline_config.maybe_pack_latents(latent, latent.shape[0], batch)\nassert isinstance(packed, torch.Tensor) and packed.ndim == 3, f\"bad packed latent: {type(packed)} {getattr(packed, 'shape', None)}\"","typeGuard":"def is_packed_latents(x) -> bool:\n    return isinstance(x, torch.Tensor) and x.ndim == 3","tryCatchPattern":null,"preventionTips":["Enable LTX-2 latent packing in pipeline_config before the image-encoding stage","Log latent.shape before packing in custom pipelines","Keep VAE and pipeline config from the same model release"],"tags":["ltx-2","image-encoding","latent-shape","packing","torch"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}