invoke-ai/InvokeAI · error · ValueError

Wan reference condition requires {width}x{height} latent spa

Error message

Wan reference condition requires {width}x{height} latent spatial dimensions; got {condition.shape[4]}x{condition.shape[3]}.

What it means

The spatial dimensions of the reference condition (shape[3]/shape[4] = latent height/width) must equal the latent-space height/width derived from the run's width/height after VAE downsampling. The message reports expected width x height latent dims and the observed ones (note the swapped print order: shape[4]xshape[3]).

Source

Thrown at invokeai/app/invocations/wan_denoise.py:113

def _validate_ref_condition_shape(
    condition: torch.Tensor,
    *,
    channels: int,
    frames: int,
    height: int,
    width: int,
) -> None:
    if condition.ndim != 5:
        raise ValueError(f"Wan reference condition must be a 5D tensor; got shape {tuple(condition.shape)}.")
    if condition.shape[0] != 1:
        raise ValueError(f"Wan reference condition requires batch size 1; got {condition.shape[0]}.")
    if condition.shape[1] != channels:
        raise ValueError(f"Wan reference condition requires {channels} channels; got {condition.shape[1]}.")
    if condition.shape[2] != frames:
        expected = "a single latent frame" if frames == 1 else f"{frames} latent frames"
        raise ValueError(f"Wan reference condition requires {expected}; got {condition.shape[2]}.")
    if condition.shape[3:] != (height, width):
        raise ValueError(
            f"Wan reference condition requires {width}x{height} latent spatial dimensions; "
            f"got {condition.shape[4]}x{condition.shape[3]}."
        )


def _scheduler_path_for_transformer(context: InvocationContext, transformer_field: WanTransformerField) -> Path | None:
    """Return the on-disk ``scheduler/`` directory for the main model, or None."""
    config = context.models.get_config(transformer_field.transformer)
    model_root = context.models.get_absolute_path(config)
    if model_root.is_file():
        return None
    candidate = model_root / "scheduler"
    if (candidate / "scheduler_config.json").exists():
        return candidate
    return None


def _default_scheduler_for_variant(variant: WanVariantType):

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Resize/crop the reference image/video so that (dims / vae_spatial_scale) equals the run's latent height/width, then re-encode the condition
  2. Set the denoise invocation's width/height to match the existing condition's latent spatial size (latent_w*32-style back-calculation per variant)
  3. Re-run the conditioning node after any resolution change
  4. Check the swapped print order (got shape[4]xshape[3]) when comparing reported vs expected values

Example fix

// before
ref = encode(image_512x512)   # latent 32x32
run.width, run.height = 832, 480  # latent 52x30
// after
ref = encode(resize(image, 832, 480))  # latent dims now match the run
Defensive patterns

Strategy: validation

Validate before calling

latent_scale = 16  # Wan 2.2 VAE spatial scale (adjust per variant)
expected_hw = (height // latent_scale, width // latent_scale)
if tuple(condition.shape[3:]) != expected_hw:
    ref_input = resize(ref_input, width, height)  # re-encode at matching size

Type guard

def spatial_dims_match(t, latent_h: int, latent_w: int) -> bool:
    import torch
    return isinstance(t, torch.Tensor) and t.ndim == 5 and t.shape[3:] == (latent_h, latent_w)

Try / catch

try:
    result = denoise.invoke(context)
except ValueError as e:
    if "latent spatial dimensions" in str(e):
        # re-encode the reference at the run's resolution, then retry
        condition = wan_conditioning_node.invoke(context)
        denoise.ref_condition = condition
        result = denoise.invoke(context)
    else:
        raise

Prevention

When it happens

Trigger: Condition latent encoded at a different resolution than the denoise run's width/height (e.g. ref image 512x512 but generation at 832x480), rounding differences between the run's latent size and the encoded condition.

Common situations: Changing output resolution in the workflow after encoding the reference condition, cropping/resizing the reference image without re-encoding, sharing workflows whose ref image size differs from the target size.

Understand the failure class

Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/0d40f810cf8ad7d2. Report an issue: GitHub.