invoke-ai/InvokeAI · error · ValueError

Expected noise with shape {expected_shape}, got {tuple(noise

Error message

Expected noise with shape {expected_shape}, got {tuple(noise.shape)}

What it means

validate_noise_tensor_shape compares an existing noise tensor's shape to the shape get_expected_noise_shape computes for the given noise type and width/height, raising ValueError when they differ. This guards against feeding stale, seed-mismatched, or wrongly-shaped noise (e.g. an SD-shaped 4-channel tensor into FLUX, or a 4D tensor into Anima which needs a 5D (1,16,1,H/8,W/8) shape) into the sampler.

Source

Thrown at invokeai/app/invocations/latent_noise.py:49

    if noise_type == "FLUX":
        return (1, 16, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)
    if noise_type == "FLUX.2":
        return (1, 32, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)
    if noise_type == "SD3":
        return (1, 16, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)
    if noise_type == "CogView4":
        return (1, 16, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)
    if noise_type == "Z-Image":
        return (1, 16, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)
    if noise_type == "Anima":
        return (1, 16, 1, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)
    raise ValueError(f"Unsupported noise type: {noise_type}")


def validate_noise_tensor_shape(noise: torch.Tensor, noise_type: LatentNoiseType, width: int, height: int) -> None:
    expected_shape = get_expected_noise_shape(noise_type, width, height)
    if tuple(noise.shape) != expected_shape:
        raise ValueError(f"Expected noise with shape {expected_shape}, got {tuple(noise.shape)}")


def generate_noise_tensor(
    noise_type: LatentNoiseType,
    width: int,
    height: int,
    seed: int,
    device: torch.device,
    dtype: torch.dtype,
    use_cpu: bool = True,
) -> torch.Tensor:
    validate_noise_dimensions(noise_type, width, height)
    rand_device = "cpu" if use_cpu else device.type
    rand_dtype = TorchDevice.choose_torch_dtype(device=device)

    if noise_type == "SD":
        return torch.randn(
            1,

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Regenerate the noise tensor with generate_noise_tensor for the current noise_type, width, height, and seed instead of reusing a cached tensor.
  2. Check the tensor's shape against the expected formula (channels: SD=4, FLUX=16, FLUX.2=32, SD3/CogView4/Z-Image=16; Anima adds a singleton frame dim) and reshape/fix it.
  3. Ensure width/height passed to validate_noise_tensor_shape are the same values used when the noise was generated.

Example fix

// before
noise = generate_noise_tensor("SD", 512, 512, seed, device, dtype)  # (1,4,64,64)
validate_noise_tensor_shape(noise, "FLUX", 512, 512)  # raises
// after
noise = generate_noise_tensor("FLUX", 512, 512, seed, device, dtype)  # (1,16,64,64)
validate_noise_tensor_shape(noise, "FLUX", 512, 512)
Defensive patterns

Strategy: validation

Validate before calling

expected = get_expected_noise_shape(noise_type, width, height)
if tuple(noise.shape) != expected:
    print(f"Regenerating noise: have {tuple(noise.shape)}, need {expected}")
    noise = generate_noise_tensor(noise_type, width, height, seed, device, dtype)

Type guard

def noise_shape_ok(noise, noise_type: str, width: int, height: int) -> bool:
    try:
        return tuple(noise.shape) == get_expected_noise_shape(noise_type, width, height)
    except ValueError:
        return False

Try / catch

try:
    validate_noise_tensor_shape(noise, noise_type, width, height)
except ValueError as e:
    if "Expected noise with shape" in str(e):
        logger.warning("Cached noise no longer matches; regenerating")
        noise = generate_noise_tensor(noise_type, width, height, seed, device, dtype)
    else:
        raise

Prevention

When it happens

Trigger: Calling validate_noise_tensor_shape (via _prepare_noise_tensor) with a tensor whose shape differs from expected: reusing noise generated for a different resolution, generating noise for SD (1,4,h,w) and passing it to FLUX (1,16,h,w), omitting Anima's extra frame dimension, or a batched/expanded tensor with batch size > 1.

Common situations: Caching a noise tensor across node runs after the user changed width/height; cross-model workflows that pass one model's initial noise to another; custom denoise scripts that build noise with torch.rand and wrong channel count or missing leading batch dim.

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/68e2e293fdd4446f. Report an issue: GitHub.