sgl-project/sglang · error · ValueError

Cannot repeat tensor with batch={tensor.shape[0]} to target_

Error message

Cannot repeat tensor with batch={tensor.shape[0]} to target_batch_size={target_batch_size}

What it means

_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.

Source

Thrown at python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/ltx_2/denoising.py:1086

    def _ltx2_velocity_to_x0(
        sample: torch.Tensor,
        velocity: torch.Tensor,
        sigma: float | torch.Tensor,
    ) -> torch.Tensor:
        if isinstance(sigma, torch.Tensor):
            sigma = sigma.to(device=sample.device, dtype=torch.float32)
            while sigma.ndim < sample.ndim:
                sigma = sigma.unsqueeze(-1)
            return (sample.float() - sigma * velocity.float()).to(sample.dtype)
        return (sample.float() - float(sigma) * velocity.float()).to(sample.dtype)

    @staticmethod
    def _repeat_batch_dim(tensor: torch.Tensor, target_batch_size: int) -> torch.Tensor:
        """Repeat along batch dim while preserving any tokenwise timestep layout."""
        if tensor.shape[0] == int(target_batch_size):
            return tensor
        if tensor.shape[0] <= 0 or int(target_batch_size) % int(tensor.shape[0]) != 0:
            raise ValueError(
                "Cannot repeat tensor with batch="
                f"{tensor.shape[0]} to target_batch_size={target_batch_size}"
            )
        repeat_factor = int(target_batch_size) // int(tensor.shape[0])
        return tensor.repeat(repeat_factor, *([1] * (tensor.ndim - 1)))

    @staticmethod
    def _build_ltx2_sp_padding_mask(
        batch: Req,
        *,
        seq_len: int,
        batch_size: int,
        key: str,
        device: torch.device,
    ) -> torch.Tensor | None:
        valid = getattr(batch, key, None)
        if valid is None:
            return None

View on GitHub (pinned to 0132848349)

Solutions

  1. Make the guidance pass count (e.g. 2 for cond+uncond) divide into target_batch_size exactly
  2. Check the tensor's shape[0] > 0 and equals the intended per-sample batch before the step
  3. Fix upstream packing so latents/embeds share a consistent leading batch dim

Example fix

// before: batch=1 tensor, target_batch_size=3
x = stage._repeat_batch_dim(clean, 3)  # ValueError
// after: use a divisible target (cond+uncond = 2)
x = stage._repeat_batch_dim(clean, 2)
Defensive patterns

Strategy: validation

Validate before calling

b = tensor.shape[0]
assert b > 0 and target_batch_size % b == 0, f"cannot repeat batch {b} -> {target_batch_size}"

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/dbb9ac8f69c4c49e. Report an issue: GitHub.