{"record":{"id":"3736dd656846b8bc","repo":"Comfy-Org/ComfyUI","slug":"passing-a-list-or-tuple-of-seeds-to-batchedbrownia","errorCode":null,"errorMessage":"Passing a list or tuple of seeds to BatchedBrownianTree requires a length matching the batch size.","messagePattern":"Passing a list or tuple of seeds to BatchedBrownianTree requires a length matching the batch size\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"comfy/k_diffusion/sampling.py","lineNumber":105,"sourceCode":"\n    return lambda sigma, sigma_next: torch.randn(x.size(), dtype=x.dtype, layout=x.layout, device=x.device, generator=generator)\n\n\nclass BatchedBrownianTree:\n    \"\"\"A wrapper around torchsde.BrownianTree that enables batches of entropy.\"\"\"\n\n    def __init__(self, x, t0, t1, seed=None, **kwargs):\n        self.cpu_tree = kwargs.pop(\"cpu\", True)\n        t0, t1, self.sign = self.sort(t0, t1)\n        w0 = kwargs.pop('w0', None)\n        if w0 is None:\n            w0 = torch.zeros_like(x)\n        self.batched = False\n        if seed is None:\n            seed = (torch.randint(0, 2 ** 63 - 1, ()).item(),)\n        elif isinstance(seed, (tuple, list)):\n            if len(seed) != x.shape[0]:\n                raise ValueError(\"Passing a list or tuple of seeds to BatchedBrownianTree requires a length matching the batch size.\")\n            self.batched = True\n            w0 = w0[0]\n        else:\n            seed = (seed,)\n        if self.cpu_tree:\n            t0, w0, t1 = t0.detach().cpu(), w0.detach().cpu(), t1.detach().cpu()\n        self.trees = tuple(torchsde.BrownianTree(t0, w0, t1, entropy=s, **kwargs) for s in seed)\n\n    @staticmethod\n    def sort(a, b):\n        return (a, b, 1) if a < b else (b, a, -1)\n\n    def __call__(self, t0, t1):\n        t0, t1, sign = self.sort(t0, t1)\n        device, dtype = t0.device, t0.dtype\n        if self.cpu_tree:\n            t0, t1 = t0.detach().cpu().float(), t1.detach().cpu().float()\n        w = torch.stack([tree(t0, t1) for tree in self.trees]).to(device=device, dtype=dtype) * (self.sign * sign)","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy/k_diffusion/sampling.py#L87-L123","documentation":"Raised by BatchedBrownianTree.__init__ when seed is a list/tuple whose length differs from x.shape[0]. When per-sample seeds are supplied, the sampler builds one torchsde.BrownianTree per seed, so the seed sequence must cover the whole batch dimension exactly.","triggerScenarios":"Constructing BrownianTreeNoiseSampler(x, ..., seed=[s1, s2]) for a latent batch of size != 2, or a custom sampler passing extra_args['seed'] as a list that was built for a different batch size than the current latent.","commonSituations":"Workflows that vary batch_size dynamically (batch size widget changed after a per-seed list was computed); API scripts generating seed lists from a stale shape; custom nodes forwarding user seed lists without resizing to the actual batch.","solutions":["Make len(seed) == x.shape[0]: generate exactly batch_size seeds","Or pass a single int seed, which torch.randint-expands internally to the whole batch","Recompute the seed list whenever batch size changes rather than caching it"],"exampleFix":"# before\nnoise_sampler = BrownianTreeNoiseSampler(x, sigma_min, sigma_max, seed=[123, 456])  # x.shape[0] == 4\n# after\nnoise_sampler = BrownianTreeNoiseSampler(x, sigma_min, sigma_max, seed=[123, 456, 789, 1011])","handlingStrategy":"validation","validationCode":"if isinstance(seeds, (list, tuple)):\n    assert len(seeds) == x.shape[0], f'need {x.shape[0]} seeds, got {len(seeds)}'\nnoise_sampler = BrownianTreeNoiseSampler(x, sigma_min, sigma_max, seed=seeds)","typeGuard":"def seeds_match_batch(seeds, x: torch.Tensor) -> bool:\n    return not isinstance(seeds, (list, tuple)) or len(seeds) == x.shape[0]","tryCatchPattern":null,"preventionTips":["Pass a single int seed unless you truly need per-sample noise","Recompute seed lists whenever batch size changes"],"tags":["sampling","sde","noise-sampler","batch-size","seed"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}