Comfy-Org/ComfyUI · error · ValueError

Invalid input shape: {x.shape}

Error message

Invalid input shape: {x.shape}

What it means

The Wan 2.2 VAE patchify() only reshapes 4-D spatial tensors (B, C, q*H, r*W) and 5-D spatio-temporal tensors (B, C, F, q*H, r*W). Any other rank cannot be interpreted as patchable image/video data and is rejected.

Source

Thrown at comfy/ldm/wan/vae2_2.py:176

                x = layer(x)
        return x + self.shortcut(old_x)


def patchify(x, patch_size):
    if patch_size == 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, r=patch_size)
    elif x.dim() == 5:
        x = rearrange(
            x,
            "b c f (h q) (w r) -> b (c r q) f h w",
            q=patch_size,
            r=patch_size,
        )
    else:
        raise ValueError(f"Invalid input shape: {x.shape}")

    return x


def unpatchify(x, patch_size):
    if patch_size == 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, r=patch_size)
    elif x.dim() == 5:
        x = rearrange(
            x,
            "b (c r q) f h w -> b c f (h q) (w r)",
            q=patch_size,
            r=patch_size,
        )

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Ensure input is 4-D (B,C,H,W) or 5-D (B,C,F,H,W); add the batch dim if missing (x.unsqueeze(0)).
  2. Confirm spatial dims are divisible by patch_size — otherwise use unpatchify correctly rather than nesting calls.
  3. Check intermediate shapes in the pipeline with prints/asserts before patchify.

Example fix

# before
x = patchify(img_c_h_w)  # dim()==3 -> raises
# after
x = patchify(img_c_h_w.unsqueeze(0))  # (1,C,H,W)
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: Calling patchify on a 3-D tensor (C,H,W), a 6-D tensor, or a batch-of-lists converted incorrectly (e.g. double batch dims).

Common situations: Forgetting the batch dim; stacking an extra dim from dataloader output; feeding patchified output back into patchify.

Related errors


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