{"record":{"id":"dbb9ac8f69c4c49e","repo":"sgl-project/sglang","slug":"cannot-repeat-tensor-with-batch-tensor-shape-0","errorCode":null,"errorMessage":"Cannot repeat tensor with batch={tensor.shape[0]} to target_batch_size={target_batch_size}","messagePattern":"Cannot repeat tensor with batch=(.+?) to target_batch_size=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/ltx_2/denoising.py","lineNumber":1086,"sourceCode":"    def _ltx2_velocity_to_x0(\n        sample: torch.Tensor,\n        velocity: torch.Tensor,\n        sigma: float | torch.Tensor,\n    ) -> torch.Tensor:\n        if isinstance(sigma, torch.Tensor):\n            sigma = sigma.to(device=sample.device, dtype=torch.float32)\n            while sigma.ndim < sample.ndim:\n                sigma = sigma.unsqueeze(-1)\n            return (sample.float() - sigma * velocity.float()).to(sample.dtype)\n        return (sample.float() - float(sigma) * velocity.float()).to(sample.dtype)\n\n    @staticmethod\n    def _repeat_batch_dim(tensor: torch.Tensor, target_batch_size: int) -> torch.Tensor:\n        \"\"\"Repeat along batch dim while preserving any tokenwise timestep layout.\"\"\"\n        if tensor.shape[0] == int(target_batch_size):\n            return tensor\n        if tensor.shape[0] <= 0 or int(target_batch_size) % int(tensor.shape[0]) != 0:\n            raise ValueError(\n                \"Cannot repeat tensor with batch=\"\n                f\"{tensor.shape[0]} to target_batch_size={target_batch_size}\"\n            )\n        repeat_factor = int(target_batch_size) // int(tensor.shape[0])\n        return tensor.repeat(repeat_factor, *([1] * (tensor.ndim - 1)))\n\n    @staticmethod\n    def _build_ltx2_sp_padding_mask(\n        batch: Req,\n        *,\n        seq_len: int,\n        batch_size: int,\n        key: str,\n        device: torch.device,\n    ) -> torch.Tensor | None:\n        valid = getattr(batch, key, None)\n        if valid is None:\n            return None","sourceCodeStart":1068,"sourceCodeEnd":1104,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/ltx_2/denoising.py#L1068-L1104","documentation":"_repeat_batch_dim expands guidance/clean-state tensors along the batch dimension so each denoising pass gets its own copy. It requires target_batch_size to be an exact positive integer multiple of the tensor's batch dim; otherwise the repeat factor is undefined and it raises.","triggerScenarios":"Calling _prepare_ltx2_ti2v_clean_state (or _repeat_optional_batch_dim) with a tensor whose shape[0] is 0 or doesn't evenly divide target_batch_size, e.g. a batch-1 clean-latent tensor repeated to a CFG-guided batch of 3 passes.","commonSituations":"Non-power-of-two or unconditional-only CFG configurations where the guidance pass count isn't a multiple of the tensor batch; empty tensors from a failed upstream pack; batch dim accidentally holding a token dim.","solutions":["Make the guidance pass count (e.g. 2 for cond+uncond) divide into target_batch_size exactly","Check the tensor's shape[0] > 0 and equals the intended per-sample batch before the step","Fix upstream packing so latents/embeds share a consistent leading batch dim"],"exampleFix":"// before: batch=1 tensor, target_batch_size=3\nx = stage._repeat_batch_dim(clean, 3)  # ValueError\n// after: use a divisible target (cond+uncond = 2)\nx = stage._repeat_batch_dim(clean, 2)","handlingStrategy":"validation","validationCode":"b = tensor.shape[0]\nassert b > 0 and target_batch_size % b == 0, f\"cannot repeat batch {b} -> {target_batch_size}\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep guidance pass counts powers/divisors of expanded batch sizes","Log pass_specs and batch sizes before expansion"],"tags":["ltx-2","batch-dim","cfg-guidance","shape-validation"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}