sgl-project/sglang · error · ValueError

Cannot duplicate `image` of batch size {image_latents.shape[

Error message

Cannot duplicate `image` of batch size {image_latents.shape[0]} to {batch_size} text prompts.

What it means

In prepare_latents, when image latents are provided for image editing/generation, the number of latent batches must either match the number of text prompts or divide it evenly so latents can be duplicated via repeat/repeat_interleave. If batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] != 0, duplication is impossible and the error is raised.

Source

Thrown at python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/qwen_image_layered.py:445

            image = align_tensor_to_module_dtype(image, self.vae, device=device)
            if image.shape[1] != self.latent_channels:
                image_latents = self._encode_vae_image(image=image, generator=generator)
            else:
                image_latents = image
            if (
                batch_size > image_latents.shape[0]
                and batch_size % image_latents.shape[0] == 0
            ):
                # expand init_latents for batch_size
                additional_image_per_prompt = batch_size // image_latents.shape[0]
                image_latents = torch.cat(
                    [image_latents] * additional_image_per_prompt, dim=0
                )
            elif (
                batch_size > image_latents.shape[0]
                and batch_size % image_latents.shape[0] != 0
            ):
                raise ValueError(
                    f"Cannot duplicate `image` of batch size {image_latents.shape[0]} to {batch_size} text prompts."
                )
            else:
                image_latents = torch.cat([image_latents], dim=0)

            image_latent_height, image_latent_width = image_latents.shape[3:]
            image_latents = image_latents.permute(
                0, 2, 1, 3, 4
            )  # (b, c, f, h, w) -> (b, f, c, h, w)
            image_latents = self._pack_latents(
                image_latents,
                batch_size,
                num_channels_latents,
                image_latent_height,
                image_latent_width,
                1,
            )

View on GitHub (pinned to 0132848349)

Solutions

  1. Make batch_size divisible by image_latents.shape[0]: pass one image per prompt (repeat the image path list to match prompt count) or adjust the prompt list length.
  2. Check batch_size = num_prompts * num_variants before the call and tile images accordingly.
  3. Ensure you are not unintentionally duplicating only some prompts.

Example fix

# before
prompts = ["edit a", "edit b", "edit c"]
image_path = ["img.png"]  # 1 latent vs 3 prompts

# after
image_path = ["img.png", "img.png", "img.png"]  # or reduce to 1 prompt
Defensive patterns

Strategy: validation

Validate before calling

n_lat = image_latents.shape[0] if image_latents is not None else None
if n_lat is not None:
    assert batch_size % n_lat == 0, f"batch {batch_size} not divisible by {n_lat} images"

Type guard

def batch_divides_latents(batch_size: int, image_latents) -> bool:
    return image_latents is None or batch_size == image_latents.shape[0] or batch_size % image_latents.shape[0] == 0

Prevention

When it happens

Trigger: Passing one reference image (image_latents batch 1) with 3 text prompts (batch_size 3), or 2 images with 5 prompts — any non-divisible combination where prompts outnumber latents.

Common situations: Prompt expansion (e.g. cfg or multiple variants per prompt) multiplying batch_size to a number not divisible by the number of input images; mixing batched prompts with a single image incorrectly; off-by-one in prompt list construction.

Related errors


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