Comfy-Org/ComfyUI · error · ValueError

Invalid input shape: {x.shape}

Error message

Invalid input shape: {x.shape}

What it means

Raised by the patchify helper in causal_video_autoencoder.py when x.dim() is neither 4 (2D images) nor 5 (3D video). Patch folding is only implemented for image and video layouts; other dimensionalities cannot be rearranged.

Source

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


def patchify(x, patch_size_hw, patch_size_t=1):
    if patch_size_hw == 1 and patch_size_t == 1:
        return x
    if x.dim() == 4:
        x = rearrange(
            x, "b c (h q) (w r) -> b (c r q) h w", q=patch_size_hw, r=patch_size_hw
        )
    elif x.dim() == 5:
        x = rearrange(
            x,
            "b c (f p) (h q) (w r) -> b (c p r q) f h w",
            p=patch_size_t,
            q=patch_size_hw,
            r=patch_size_hw,
        )
    else:
        raise ValueError(f"Invalid input shape: {x.shape}")

    return x


def unpatchify(x, patch_size_hw, patch_size_t=1):
    if patch_size_hw == 1 and patch_size_t == 1:
        return x

    if x.dim() == 4:
        x = rearrange(
            x, "b (c r q) h w -> b c (h q) (w r)", q=patch_size_hw, r=patch_size_hw
        )
    elif x.dim() == 5:
        x = rearrange(
            x,
            "b (c p r q) f h w -> b c (f p) (h q) (w r)",
            p=patch_size_t,
            q=patch_size_hw,

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Ensure input is (B, C, H, W) or (B, C, F, H, W) before calling patchify
  2. Add the batch dimension if it was squeezed away
  3. Skip patchify when patch_size_hw == 1 and patch_size_t == 1, as it is a no-op

Example fix

# before
x = patchify(clip_tensor, patch_size_hw=2)  # clip_tensor.dim() == 3

# after
x = patchify(clip_tensor.unsqueeze(0), patch_size_hw=2)
Defensive patterns

Strategy: type-guard

Validate before calling

if x.dim() not in (4, 5):
    raise ValueError(f"patchify expects 4D or 5D input, got {x.dim()}D")

Type guard

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

Prevention

When it happens

Trigger: Calling patchify (or a caller that uses it, e.g. per-latent patch handling) on a 3-dim or 6-dim tensor, such as an unbatched tensor or one with an extra frame-packing axis.

Common situations: Custom node code that slices latents and loses the batch dim; mixing 2D and 3D VAE code paths; feeding patchified tensors in twice.

Related errors


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