sgl-project/sglang · error · ValueError
Cannot duplicate `image` of batch size {latent_condition.sha
Error message
Cannot duplicate `image` of batch size {latent_condition.shape[0]} to {batch_size} text prompts. What it means
When batching image-to-image/conditioning, QwenImage can broadcast init latents only when latent_condition divides evenly into batch_size. If batch_size is not a multiple of latent_condition.shape[0] (e.g. 2 condition latents with 3 text prompts), the repeat count batch_size // latent.shape[0] would drop or misalign latents, so postprocess_image_latent raises.
Source
Thrown at python/sglang/multimodal_gen/configs/pipeline_configs/qwen_image.py:569
def preprocess_condition_image(
self, image, target_width, target_height, _vae_image_processor
):
return resize(image, target_height, target_width, resize_mode="default"), (
target_width,
target_height,
)
def postprocess_image_latent(self, latent_condition, batch):
batch_size = batch.batch_size
if batch_size > latent_condition.shape[0]:
if batch_size % latent_condition.shape[0] == 0:
# expand init_latents for batch_size
additional_image_per_prompt = batch_size // latent_condition.shape[0]
image_latents = latent_condition.repeat(
additional_image_per_prompt, 1, 1, 1
)
else:
raise ValueError(
f"Cannot duplicate `image` of batch size {latent_condition.shape[0]} to {batch_size} text prompts."
)
else:
image_latents = latent_condition
image_latent_height, image_latent_width = image_latents.shape[3:]
num_channels_latents = self.dit_config.arch_config.in_channels // 4
image_latents = _pack_latents(
image_latents,
batch_size,
num_channels_latents,
image_latent_height,
image_latent_width,
)
return image_latents
def prepare_pos_cond_kwargs(self, batch, device, rotary_emb, dtype):
return self._prepare_edit_cond_kwargs(View on GitHub (pinned to 0132848349)
Solutions
- Use one image for all prompts (latent batch 1 divides anything)
- Make len(image) == len(prompt) so the ratio is exactly 1
- Ensure effective batch_size (including CFG factor) is a multiple of the number of condition images
Example fix
# before pipe(prompt=["p1", "p2", "p3"], image=[img1, img2]) # 3 % 2 != 0 # after pipe(prompt=["p1", "p2", "p3", "p4"], image=[img1, img2]) # 4 % 2 == 0
Defensive patterns
Strategy: validation
Validate before calling
n_img, n_prompt = len(image), len(prompt)
assert n_prompt % n_img == 0, (
f"num prompts ({n_prompt}) must be a multiple of num images ({n_img})") Prevention
- Default to one condition image for batched prompts
- Keep len(image) == len(prompt) in edit APIs
- Account for CFG doubling when reasoning about effective batch size
When it happens
Trigger: Calling image editing with image=[img1, img2] but 3 (or any non-multiple) prompt strings; latent_condition batch dim from multiple input images while num_prompts % num_images != 0.
Common situations: Batched img2img requests where the number of input images doesn't divide the number of prompts; per-prompt negative examples (CFG doubling) making effective batch_size odd relative to the image count.
Related errors
- The number of initial states is expected to be equal to the
- Mismatched batch sizes: mixed_qkv.shape[0]={B}, a.shape[0]={
- Mismatched batch sizes: mixed_qkv.shape[0]={B}, a.shape[0]={
- `write_pos` must have shape {(batch,)}.
- QwenImageEditPlus expects either one shared condition image
AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28).
Data as JSON: /api/errors/e45f59032297071d.
Report an issue: GitHub.