Comfy-Org/ComfyUI · error · ValueError

Invalid input shape: {sample.shape}

Error message

Invalid input shape: {sample.shape}

What it means

Raised in the Encoder's output post-processing (sample recombination path) when a per-channel/uniform latent tensor's dimensionality is neither 4 (B,C,H,W) nor 5 (B,C,F,H,W). The code repeats the last channel to rebuild the mean/std pair and only has 2D and 3D variants.

Source

Thrown at comfy/ldm/lightricks/vae/causal_video_autoencoder.py:272

        if self.latent_log_var == "uniform":
            last_channel = sample[:, -1:, ...]
            num_dims = sample.dim()

            if num_dims == 4:
                # For shape (B, C, H, W)
                repeated_last_channel = last_channel.repeat(
                    1, sample.shape[1] - 2, 1, 1
                )
                sample = torch.cat([sample, repeated_last_channel], dim=1)
            elif num_dims == 5:
                # For shape (B, C, F, H, W)
                repeated_last_channel = last_channel.repeat(
                    1, sample.shape[1] - 2, 1, 1, 1
                )
                sample = torch.cat([sample, repeated_last_channel], dim=1)
            else:
                raise ValueError(f"Invalid input shape: {sample.shape}")
        elif self.latent_log_var == "constant":
            sample = sample[:, :-1, ...]
            approx_ln_0 = (
                -30
            )  # this is the minimal clamp value in DiagonalGaussianDistribution objects
            sample = torch.cat(
                [sample, torch.ones_like(sample, device=sample.device) * approx_ln_0],
                dim=1,
            )

        return sample

    def forward_orig(self, sample: torch.FloatTensor, device=None) -> torch.FloatTensor:
        r"""The forward method of the `Encoder` class."""

        max_chunk_size = get_max_chunk_size(sample.device if device is None else device) * 2  # encoder is more memory-efficient than decoder
        frame_size = sample[:, :, :1, :, :].numel() * sample.element_size()
        frame_size = int(frame_size * (self.conv_in.out_channels / self.conv_in.in_channels))

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Ensure the tensor entering the VAE path is (B, C, H, W) or (B, C, F, H, W)
  2. Unpatchify latents before passing them back if they were patchified elsewhere
  3. Add a batch dimension with x.unsqueeze(0) if it is missing

Example fix

# before
out = vae.decode(latents)  # latents.dim() == 3

# after
latents = latents.unsqueeze(0)  # -> (1, C, H, W)
out = vae.decode(latents)
Defensive patterns

Strategy: type-guard

Validate before calling

if sample.dim() not in (4, 5):
    raise ValueError(f"expected (B,C,H,W) or (B,C,F,H,W), got {tuple(sample.shape)}")

Type guard

def is_vae_latent_layout(t) -> bool:
    return t.dim() in (4, 5)

Prevention

When it happens

Trigger: Calling the encoder's output transform on a tensor with 3 or 6 dims, e.g. feeding a raw unbatched tensor or an already-patched/packed representation whose dim() is not 4 or 5.

Common situations: Custom pipelines that reshape latents before decode; feeding patchified latents directly back without unpatchifying; batch-dimension bugs that drop or add a dim.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/6eb221dc31b63707. Report an issue: GitHub.