sgl-project/sglang · error · ValueError

LTX-2 conditioning token count mismatch: {packed.shape[1]=}

Error message

LTX-2 conditioning token count mismatch: {packed.shape[1]=} {expected_tokens=}.

What it means

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.

Source

Thrown at python/sglang/multimodal_gen/runtime/pipelines_core/stages/image_encoding.py:796

                )

                # 3. Encode
                if use_condition_encoder:
                    latent = self._condition_encode(video_condition, server_args).to(
                        dtype=encode_dtype
                    )
                else:
                    latent = self._vae_encode(
                        video_condition, server_args, batch.generator
                    )

                packed = server_args.pipeline_config.maybe_pack_latents(
                    latent, latent.shape[0], batch
                )
                if not (isinstance(packed, torch.Tensor) and packed.ndim == 3):
                    raise ValueError("Expected packed image latents [B, S0, D].")
                if int(packed.shape[1]) != expected_tokens:
                    raise ValueError(
                        f"LTX-2 conditioning token count mismatch: "
                        f"{packed.shape[1]=} {expected_tokens=}."
                    )
                packed_latents.append(packed)

        batch.image_latent = (
            packed_latents[0] if len(packed_latents) == 1 else packed_latents
        )
        batch.ltx2_num_image_tokens = int(packed_latents[0].shape[1])

        if batch.debug:
            logger.info(
                "LTX2 TI2V: %d tokens (shape=%s) for %sx%s",
                batch.ltx2_num_image_tokens,
                tuple(packed_latents[0].shape),
                batch.width,
                batch.height,
            )

View on GitHub (pinned to 0132848349)

Solutions

  1. 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
  2. Ensure width/height in the request match the values used to derive expected_tokens in the pipeline config
  3. Check the VAE spatial compression factor matches what expected_tokens assumes (e.g. 8x vs 16x)
  4. Keep all conditioning images in the batch at the same resolution

Example fix

// before
image = load_image("ref.png")  # arbitrary size

// after
image = load_image("ref.png").resize((height, width))  # match request height/width used for expected_tokens
Defensive patterns

Strategy: validation

Validate before calling

expected = height // vae_scale * width // vae_scale  # mirror config formula
tokens = (h // patch) * (w // patch)
assert tokens == expected_tokens, f"{tokens} != {expected_tokens}: fix image resolution"

Type guard

def token_count_matches(img, expected_tokens: int, patch: int) -> bool:
    _, h, w = img.shape[-3:]
    return (h // patch) * (w // patch) == expected_tokens

Prevention

When it happens

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

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

Related errors


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