{"record":{"id":"cd274f3336fdfe98","repo":"sgl-project/sglang","slug":"ltx-2-conditioning-token-count-mismatch-packed-s","errorCode":null,"errorMessage":"LTX-2 conditioning token count mismatch: {packed.shape[1]=} {expected_tokens=}.","messagePattern":"LTX-2 conditioning token count mismatch: (.+?) (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/pipelines_core/stages/image_encoding.py","lineNumber":796,"sourceCode":"                )\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,\n                batch.height,\n            )","sourceCodeStart":778,"sourceCodeEnd":814,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/image_encoding.py#L778-L814","documentation":"After packing conditioning-image latents, the LTX-2 stage checks that the packed sequence length S0 equals expected_tokens, which is derived from the configured conditioning resolution/aspect ratio. A mismatch means the number of image tokens produced by the VAE + packing step does not match what the transformer's conditioning slots expect, so the shapes would not line up in attention.","triggerScenarios":"Passing a conditioning/reference image whose dimensions (after resize/crop and VAE downsampling) yield a different token count than expected_tokens computed from pipeline config (e.g. resolution 512x512 vs a config expecting 768x512), or using a VAE downscale factor inconsistent with the config's expected_tokens computation.","commonSituations":"Changing width/height or aspect-ratio request fields without updating the LTX-2 conditioning token expectation; using a custom VAE with a different spatial compression ratio; mixing per-image resolutions in one batch; stale config after a resolution-format change.","solutions":["Print packed.shape[1] and expected_tokens and reconcile: resize the conditioning image so that (H/patch)*(W/patch) * tokens-per-patch equals expected_tokens","Ensure width/height in the request match the values used to derive expected_tokens in the pipeline config","Check the VAE spatial compression factor matches what expected_tokens assumes (e.g. 8x vs 16x)","Keep all conditioning images in the batch at the same resolution"],"exampleFix":"// before\nimage = load_image(\"ref.png\")  # arbitrary size\n\n// after\nimage = load_image(\"ref.png\").resize((height, width))  # match request height/width used for expected_tokens","handlingStrategy":"validation","validationCode":"expected = height // vae_scale * width // vae_scale  # mirror config formula\ntokens = (h // patch) * (w // patch)\nassert tokens == expected_tokens, f\"{tokens} != {expected_tokens}: fix image resolution\"","typeGuard":"def token_count_matches(img, expected_tokens: int, patch: int) -> bool:\n    _, h, w = img.shape[-3:]\n    return (h // patch) * (w // patch) == expected_tokens","tryCatchPattern":null,"preventionTips":["Resize conditioning images to the request height/width before submission","Keep one resolution per batch","Compute expected_tokens with the same formula as the config"],"tags":["ltx-2","token-count","conditioning","resolution","shape-mismatch"],"backgroundTag":"sequence-length-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}