{"record":{"id":"e45f59032297071d","repo":"sgl-project/sglang","slug":"cannot-duplicate-image-of-batch-size-latent-con","errorCode":null,"errorMessage":"Cannot duplicate `image` of batch size {latent_condition.shape[0]} to {batch_size} text prompts.","messagePattern":"Cannot duplicate `image` of batch size (.+?) to (.+?) text prompts\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/configs/pipeline_configs/qwen_image.py","lineNumber":569,"sourceCode":"    def preprocess_condition_image(\n        self, image, target_width, target_height, _vae_image_processor\n    ):\n        return resize(image, target_height, target_width, resize_mode=\"default\"), (\n            target_width,\n            target_height,\n        )\n\n    def postprocess_image_latent(self, latent_condition, batch):\n        batch_size = batch.batch_size\n        if batch_size > latent_condition.shape[0]:\n            if batch_size % latent_condition.shape[0] == 0:\n                # expand init_latents for batch_size\n                additional_image_per_prompt = batch_size // latent_condition.shape[0]\n                image_latents = latent_condition.repeat(\n                    additional_image_per_prompt, 1, 1, 1\n                )\n            else:\n                raise ValueError(\n                    f\"Cannot duplicate `image` of batch size {latent_condition.shape[0]} to {batch_size} text prompts.\"\n                )\n        else:\n            image_latents = latent_condition\n        image_latent_height, image_latent_width = image_latents.shape[3:]\n        num_channels_latents = self.dit_config.arch_config.in_channels // 4\n        image_latents = _pack_latents(\n            image_latents,\n            batch_size,\n            num_channels_latents,\n            image_latent_height,\n            image_latent_width,\n        )\n\n        return image_latents\n\n    def prepare_pos_cond_kwargs(self, batch, device, rotary_emb, dtype):\n        return self._prepare_edit_cond_kwargs(","sourceCodeStart":551,"sourceCodeEnd":587,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/configs/pipeline_configs/qwen_image.py#L551-L587","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\npipe(prompt=[\"p1\", \"p2\", \"p3\"], image=[img1, img2])  # 3 % 2 != 0\n\n# after\npipe(prompt=[\"p1\", \"p2\", \"p3\", \"p4\"], image=[img1, img2])  # 4 % 2 == 0","handlingStrategy":"validation","validationCode":"n_img, n_prompt = len(image), len(prompt)\nassert n_prompt % n_img == 0, (\n    f\"num prompts ({n_prompt}) must be a multiple of num images ({n_img})\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["qwen-image","batch-mismatch","image-latents","img2img"],"backgroundTag":"batch-size-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}