sgl-project/sglang · error · ValueError

Invalid tokens_per_frame={tokens_per_frame} from {latent_hei

Error message

Invalid tokens_per_frame={tokens_per_frame} from {latent_height=} {latent_width=} {self.patch_size=}

What it means

After computing tokens_per_frame = (latent_height // patch_size) * (latent_width // patch_size), the config asserts it is positive. In practice this is unreachable if the earlier checks pass (latent dims > 0 and patch_size > 0), so hitting it indicates corrupted state or bypassed validation, such as NaN/None values coerced oddly or integer overflow in exotic configurations.

Source

Thrown at python/sglang/multimodal_gen/configs/pipeline_configs/ltx_2.py:362

        if latent_height <= 0 or latent_width <= 0:
            raise ValueError(
                "Invalid latent H/W computed from batch.height/width: "
                f"{batch.height=} {batch.width=} {self.vae_scale_factor=}"
            )
        if (latent_height % int(self.patch_size)) != 0 or (
            latent_width % int(self.patch_size)
        ) != 0:
            raise ValueError(
                "Invalid spatial patching for packed token latents. Expected latent H/W "
                "to be divisible by patch_size, got "
                f"{latent_height=} {latent_width=} {self.patch_size=}."
            )

        post_patch_h = latent_height // int(self.patch_size)
        post_patch_w = latent_width // int(self.patch_size)
        tokens_per_frame = int(post_patch_h) * int(post_patch_w)
        if tokens_per_frame <= 0:
            raise ValueError(
                f"Invalid tokens_per_frame={tokens_per_frame} from "
                f"{latent_height=} {latent_width=} {self.patch_size=}"
            )
        if int(seq_len) % int(tokens_per_frame) != 0:
            raise ValueError(
                f"LTX-2 token latents seq_len={seq_len} is not divisible by "
                f"tokens_per_frame={tokens_per_frame}. Cannot time-shard for SP."
            )
        latent_num_frames = int(seq_len) // int(tokens_per_frame)
        return int(latent_num_frames), int(tokens_per_frame)

    def shard_latents_for_sp(self, batch, latents):
        """Shard LTX-2 packed token latents across SP ranks by latent time (frame) dimension."""
        sp_world_size = get_sp_world_size()
        if sp_world_size <= 1:
            return latents, False

        # Default behavior for 5D latents.

View on GitHub (pinned to 0132848349)

Solutions

  1. Inspect latent_height, latent_width, and patch_size immediately before the call to see which value is degenerate
  2. Ensure no monkey-patching or subclass skips the preceding positivity checks
  3. Report as a bug if all preceding validations genuinely passed — this branch should be unreachable
Defensive patterns

Strategy: validation

Validate before calling

tpf = (batch.height // config.vae_scale_factor // config.patch_size) * (batch.width // config.vae_scale_factor // config.patch_size)
assert tpf > 0, tpf

Prevention

When it happens

Trigger: Calling shard_latents_for_sp after earlier guards were bypassed (e.g. subclass overriding checks, monkey-patching in tests) leaving latent_height, latent_width, or patch_size in a state that yields a non-positive product.

Common situations: Test monkey-patching that skips validation; extremely degenerate inputs where floor division underflows; defensive dead-code path triggered by refactors that change earlier checks.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


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