{"record":{"id":"7f26e06bf04ebc1b","repo":"sgl-project/sglang","slug":"qwen-image-layered-generated-latent-shapes-must-ma","errorCode":null,"errorMessage":"Qwen-Image-Layered generated latent shapes must match, got {generated_shapes}.","messagePattern":"Qwen-Image-Layered generated latent shapes must match, got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/configs/pipeline_configs/qwen_image.py","lineNumber":824,"sourceCode":"        cond_kwargs = {\n            \"txt_seq_lens\": txt_seq_lens,\n            \"img_shapes\": img_shapes,\n            \"freqs_cis\": (img_cache, txt_cache),\n            \"additional_t_cond\": torch.tensor([0], device=device, dtype=torch.long),\n            \"encoder_hidden_states_mask\": encoder_hidden_states_mask,\n        }\n        return cond_kwargs\n\n    def _unpad_and_unpack_latents(self, latents, batch):\n        channels = self.dit_config.arch_config.in_channels\n        batch_size = latents.shape[0]\n\n        img_shapes = batch.img_shapes\n        generated_shapes = img_shapes[0][:-1] if img_shapes and img_shapes[0] else []\n        if not generated_shapes:\n            raise ValueError(\"Qwen-Image-Layered requires generated latent shapes.\")\n        if len({tuple(shape) for shape in generated_shapes}) != 1:\n            raise ValueError(\n                \"Qwen-Image-Layered generated latent shapes must match, got \"\n                f\"{generated_shapes}.\"\n            )\n        layers = len(generated_shapes)\n        _, latent_height, latent_width = generated_shapes[0]\n        height = 2 * int(latent_height)\n        width = 2 * int(latent_width)\n\n        latents = maybe_unpad_latents(latents, batch)\n        latents = latents.view(\n            batch_size, layers, height // 2, width // 2, channels // 4, 2, 2\n        )\n        latents = latents.permute(0, 1, 4, 2, 5, 3, 6)\n\n        latents = latents.reshape(\n            batch_size, layers, channels // (2 * 2), height, width\n        )\n        latents = latents.permute(0, 2, 1, 3, 4)  # (b, c, f, h, w)","sourceCodeStart":806,"sourceCodeEnd":842,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/configs/pipeline_configs/qwen_image.py#L806-L842","documentation":"Layered Qwen-Image generation unpacks latents assuming every layer was generated at the same resolution. If the per-layer generated latent shapes in batch.img_shapes differ (mixed resolutions/aspect ratios across layers), the stack/unpack would be ragged, so it raises listing the offending shapes.","triggerScenarios":"Constructing a layered request where different layers have different height/width (e.g. layer 1 at 1024x1024, layer 2 at 768x512); code that sets per-layer img_shapes from user-supplied heterogeneous sizes.","commonSituations":"UI letting users pick per-layer resolution; porting old code that generated layers in separate calls at different sizes and then merging them into one batch; aspect-ratio normalization applied to only some layers.","solutions":["Force all layers to a single shared resolution before building the layered batch","When merging separately generated layers, resize/pad all latents to one shape first (or regenerate at a common size)","Validate shapes client-side: len({tuple(s) for s in generated_shapes}) == 1 before submitting"],"exampleFix":"# before\nlayer_shapes = [(4, 128, 128), (4, 96, 160)]  # mismatched\n\n# after\nlayer_shapes = [(4, 128, 128), (4, 128, 128)]  # all layers same latent shape","handlingStrategy":"validation","validationCode":"generated = [tuple(s) for s in (batch.img_shapes[0][:-1] if batch.img_shapes else [])]\nassert generated and len(set(generated)) == 1, f\"layer shapes differ: {generated}\"","typeGuard":"def layers_uniform(batch) -> bool:\n    shapes = {tuple(s) for s in (batch.img_shapes[0][:-1] if batch.img_shapes and batch.img_shapes[0] else [])}\n    return len(shapes) == 1","tryCatchPattern":null,"preventionTips":["Force one shared resolution across all layers at request build time","Validate shape uniformity before submitting layered jobs"],"tags":["qwen-image","layered-generation","shape-mismatch","latent-shapes"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}