{"record":{"id":"491a7f6b454f0506","repo":"sgl-project/sglang","slug":"cannot-duplicate-reference-image-of-batch-size-la","errorCode":null,"errorMessage":"Cannot duplicate reference image of batch size {latent_condition.shape[0]} to {batch_size} prompts.","messagePattern":"Cannot duplicate reference image of batch size (.+?) to (.+?) prompts\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/configs/pipeline_configs/longcat_image.py","lineNumber":512,"sourceCode":"        # VL image token count, which is unknown at latent preparation time.\n        return None\n\n    # --- ImageVAEEncodingStage hooks ---\n\n    def preprocess_vae_encode(self, image, vae):\n        # AutoencoderKL is a 2D image VAE; drop the frames dim added by\n        # ImageVAEEncodingStage ([B, C, 1, H, W] -> [B, C, H, W]).\n        if image.dim() == 5 and image.shape[2] == 1:\n            image = image.squeeze(2)\n        return image\n\n    def postprocess_image_latent(self, latent_condition, batch):\n        if latent_condition.dim() == 5 and latent_condition.shape[2] == 1:\n            latent_condition = latent_condition.squeeze(2)\n        batch_size = batch.batch_size\n        if batch_size > latent_condition.shape[0]:\n            if batch_size % latent_condition.shape[0] != 0:\n                raise ValueError(\n                    f\"Cannot duplicate reference image of batch size \"\n                    f\"{latent_condition.shape[0]} to {batch_size} prompts.\"\n                )\n            latent_condition = latent_condition.repeat(\n                batch_size // latent_condition.shape[0], 1, 1, 1\n            )\n        _, num_channels_latents, height, width = latent_condition.shape\n        return _pack_latents(\n            latent_condition, batch_size, num_channels_latents, height, width\n        )\n\n    # --- Denoising hooks ---\n\n    def shard_latents_for_sp(self, batch, latents):\n        # (h/2)*(w/2) is odd at most ~1MP edit resolutions, so SP has to pad, and\n        # the pads stay unmasked (USPAttention rejects a mask alongside the\n        # replicated text prefix). Repeat the last token instead of the base\n        # class's zeros, which would carry the RoPE of text token 0.","sourceCodeStart":494,"sourceCodeEnd":530,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/configs/pipeline_configs/longcat_image.py#L494-L530","documentation":"In Longcat image-to-image / reference-image generation, postprocess_image_latent duplicates a reference latent across the prompt batch. Duplication is only possible when the prompt batch size is an integer multiple of the reference-image batch size; otherwise this ValueError is raised.","triggerScenarios":"Passing N prompts with M reference images where N > M and N % M != 0 — e.g. 3 prompts with 2 reference images, or 5 prompts with 3 references. latent_condition after squeezing has shape[0]=M and batch.batch_size=N.","commonSituations":"Mixing prompt counts and reference image counts when building image-edit batches; dynamically sized user prompt lists paired with a fixed album of reference images; off-by-one when filtering prompts/references so counts desynchronize.","solutions":["Make the number of prompts equal the number of reference images, or an exact multiple of it (drop the remainder prompts or pad references)","Repeat reference images to match prompts yourself before the call so shapes already align","Audit batch construction to ensure prompts and reference images are zipped 1:1"],"exampleFix":"# before\nprompts = [\"p1\", \"p2\", \"p3\"]\nref_images = [img1, img2]  # 3 prompts, 2 refs -> raises\n\n# after\nprompts = [\"p1\", \"p2\"]\nref_images = [img1, img2]  # 1:1","handlingStrategy":"validation","validationCode":"n_prompts = len(prompts); n_refs = latent_condition.shape[0]\nassert n_prompts == n_refs or (n_prompts > n_refs and n_prompts % n_refs == 0), \\\n    f\"prompts={n_prompts} not a multiple of refs={n_refs}\"","typeGuard":null,"tryCatchPattern":"except ValueError as e:\n    if \"Cannot duplicate reference image\" in str(e):\n        k = len(prompts) // n_refs * n_refs\n        rerun(prompts[:k], refs)  # trim to a multiple","preventionTips":["Zip prompts and reference images 1:1 when building batches","Assert count alignment before submit","Trim remainder prompts client-side"],"tags":["sglang","longcat-image","batch-size","reference-image","image-to-image"],"backgroundTag":"batch-size-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}