{"record":{"id":"363330257431d7e4","repo":"sgl-project/sglang","slug":"you-have-passed-a-list-of-generators-of-length-le-363330","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/glm_image.py","lineNumber":1066,"sourceCode":"    def prepare_latents(\n        self,\n        batch_size,\n        num_channels_latents,\n        height,\n        width,\n        dtype,\n        device,\n        generator,\n    ):\n\n        shape = (\n            batch_size,\n            num_channels_latents,\n            int(height) // self.vae_scale_factor,\n            int(width) // self.vae_scale_factor,\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        latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype)\n        return latents\n\n    def check_inputs(\n        self,\n        prompt,\n        height,\n        width,\n        callback_on_step_end_tensor_inputs,\n        prompt_embeds=None,\n    ):\n        if (\n            height is not None\n            and height % (self.vae_scale_factor * self.transformer.config.patch_size)\n            != 0","sourceCodeStart":1048,"sourceCodeEnd":1084,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/glm_image.py#L1048-L1084","documentation":"prepare_latents validates that when generator is a list (per-sample generators for reproducible sampling), its length must equal the effective batch size of the request. A mismatch means some samples would have no defined generator, so the initial noise latents cannot be drawn deterministically.","triggerScenarios":"Calling the pipeline with generator=[g1, g2] but a prompt list of length 3 (or a single prompt repeated into batch size 3 via num_images_per_prompt); any case where len(generator list) != computed batch_size of the latents shape.","commonSituations":"Setting num_images_per_prompt > 1 while passing one generator per prompt; batching prompts and reusing a stale generator list from a previous single-prompt run; torch.Generator lists built with a hard-coded length.","solutions":["Pass a single torch.Generator instead of a list when per-sample control is not needed","Or build the list to match: generator=[torch.Generator(device).manual_seed(s) for s in range(batch_size)]","Account for num_images_per_prompt: effective batch = len(prompt) * num_images_per_prompt"],"exampleFix":"# before\npipe(prompt=[\"a\", \"b\", \"c\"], generator=[g0, g1])\n\n# after\ngens = [torch.Generator(\"cuda\").manual_seed(42 + i) for i in range(3)]\npipe(prompt=[\"a\", \"b\", \"c\"], generator=gens)\n# or simply: pipe(prompt=[\"a\", \"b\", \"c\"], generator=torch.Generator(\"cuda\").manual_seed(42))","handlingStrategy":"validation","validationCode":"batch_size = len(prompt) * num_images_per_prompt\nif isinstance(generator, list) and len(generator) != batch_size:\n    generator = generator[:1] * batch_size  # or rebuild\n# simplest: pass a single generator","typeGuard":"def generator_matches(generator, batch_size) -> bool:\n    return not isinstance(generator, list) or len(generator) == batch_size","tryCatchPattern":"except ValueError as e:\n    if \"list of generators\" in str(e):\n        pipe(prompt=prompts, generator=generator[0])  # single generator fallback\n    else:\n        raise","preventionTips":["Default to a single torch.Generator unless per-sample seeds are needed","Derive generator list length from len(prompt) * num_images_per_prompt","Rebuild generator lists per request instead of reusing stale ones"],"tags":["glm-image","generator","batch-size","latents","input-validation","valueerror"],"backgroundTag":"batch-size-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}