{"record":{"id":"f2c2628769de6830","repo":"sgl-project/sglang","slug":"you-have-passed-a-list-of-generators-of-length-le-f2c262","errorCode":null,"errorMessage":"You have passed a list of generators of length {len(generator)}, but requested an effective batch size of {batch_size}. Make sure the batch size matches the length of the generators.","messagePattern":"You have passed a list of generators of length (.+?), but requested an effective batch size of (.+?)\\. Make sure the batch size matches the length of the generators\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/qwen_image_layered.py","lineNumber":465,"sourceCode":"                )\n            else:\n                image_latents = torch.cat([image_latents], dim=0)\n\n            image_latent_height, image_latent_width = image_latents.shape[3:]\n            image_latents = image_latents.permute(\n                0, 2, 1, 3, 4\n            )  # (b, c, f, h, w) -> (b, f, c, h, w)\n            image_latents = self._pack_latents(\n                image_latents,\n                batch_size,\n                num_channels_latents,\n                image_latent_height,\n                image_latent_width,\n                1,\n            )\n\n        if isinstance(generator, list) and len(generator) != batch_size:\n            raise ValueError(\n                f\"You have passed a list of generators of length {len(generator)}, but requested an effective batch\"\n                f\" size of {batch_size}. Make sure the batch size matches the length of the generators.\"\n            )\n        if latents is None:\n            latents = randn_tensor(\n                shape, generator=generator, device=device, dtype=dtype\n            )\n            latents = self._pack_latents(\n                latents, batch_size, num_channels_latents, height, width, layers + 1\n            )\n        else:\n            latents = latents.to(device=device, dtype=dtype)\n\n        return latents, image_latents\n\n    def forward(\n        self,\n        batch: Req,","sourceCodeStart":447,"sourceCodeEnd":483,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/qwen_image_layered.py#L447-L483","documentation":"prepare_latents validates per-sample noise generators: if generator is a list, its length must equal the effective batch size so each sample gets reproducible, independent noise. A length mismatch makes seed-to-sample correspondence undefined, so it raises before sampling.","triggerScenarios":"Passing generator=[torch.Generator(), torch.Generator()] (len 2) while batch_size is 1, 3, 4, ... — any combination where len(generator) != batch_size after prompt expansion/latent duplication determines batch_size.","commonSituations":"Hardcoding a fixed generator list while varying num_prompts; forgetting that prompt expansion multiplies the effective batch; refactoring from single generator to list without updating count; diffusers-style reproducibility code copied with wrong count.","solutions":["Pass exactly batch_size generators: [torch.Generator(device).manual_seed(s) for s in range(batch_size)].","Or pass a single (non-list) generator to let one seed drive the whole batch.","Compute batch_size (prompts x variants) first, then build the generator list to match."],"exampleFix":"# before\nstage(..., prompts=[\"a\", \"b\", \"c\"], generator=[g1, g2])\n\n# after\ngens = [torch.Generator(device=\"cuda\").manual_seed(i) for i in range(3)]\nstage(..., prompts=[\"a\", \"b\", \"c\"], generator=gens)","handlingStrategy":"validation","validationCode":"if isinstance(generator, list):\n    assert len(generator) == batch_size, f\"{len(generator)} generators vs batch {batch_size}\"","typeGuard":"def generators_match_batch(generator, batch_size: int) -> bool:\n    return not isinstance(generator, list) or len(generator) == batch_size","tryCatchPattern":null,"preventionTips":["Build the generator list from computed batch_size at call time.","Pass a single generator when per-sample seeds are not needed."],"tags":["qwen-image","diffusers","generator","reproducibility","batch-size"],"backgroundTag":"generator-count-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}