{"record":{"id":"68e2e293fdd4446f","repo":"invoke-ai/InvokeAI","slug":"expected-noise-with-shape-expected-shape-got-t","errorCode":null,"errorMessage":"Expected noise with shape {expected_shape}, got {tuple(noise.shape)}","messagePattern":"Expected noise with shape (.+?), got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/latent_noise.py","lineNumber":49,"sourceCode":"    if noise_type == \"FLUX\":\n        return (1, 16, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)\n    if noise_type == \"FLUX.2\":\n        return (1, 32, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)\n    if noise_type == \"SD3\":\n        return (1, 16, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)\n    if noise_type == \"CogView4\":\n        return (1, 16, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)\n    if noise_type == \"Z-Image\":\n        return (1, 16, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)\n    if noise_type == \"Anima\":\n        return (1, 16, 1, height // LATENT_SCALE_FACTOR, width // LATENT_SCALE_FACTOR)\n    raise ValueError(f\"Unsupported noise type: {noise_type}\")\n\n\ndef validate_noise_tensor_shape(noise: torch.Tensor, noise_type: LatentNoiseType, width: int, height: int) -> None:\n    expected_shape = get_expected_noise_shape(noise_type, width, height)\n    if tuple(noise.shape) != expected_shape:\n        raise ValueError(f\"Expected noise with shape {expected_shape}, got {tuple(noise.shape)}\")\n\n\ndef generate_noise_tensor(\n    noise_type: LatentNoiseType,\n    width: int,\n    height: int,\n    seed: int,\n    device: torch.device,\n    dtype: torch.dtype,\n    use_cpu: bool = True,\n) -> torch.Tensor:\n    validate_noise_dimensions(noise_type, width, height)\n    rand_device = \"cpu\" if use_cpu else device.type\n    rand_dtype = TorchDevice.choose_torch_dtype(device=device)\n\n    if noise_type == \"SD\":\n        return torch.randn(\n            1,","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/latent_noise.py#L31-L67","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Regenerate the noise tensor with generate_noise_tensor for the current noise_type, width, height, and seed instead of reusing a cached tensor.","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.","Ensure width/height passed to validate_noise_tensor_shape are the same values used when the noise was generated."],"exampleFix":"// before\nnoise = generate_noise_tensor(\"SD\", 512, 512, seed, device, dtype)  # (1,4,64,64)\nvalidate_noise_tensor_shape(noise, \"FLUX\", 512, 512)  # raises\n// after\nnoise = generate_noise_tensor(\"FLUX\", 512, 512, seed, device, dtype)  # (1,16,64,64)\nvalidate_noise_tensor_shape(noise, \"FLUX\", 512, 512)","handlingStrategy":"validation","validationCode":"expected = get_expected_noise_shape(noise_type, width, height)\nif tuple(noise.shape) != expected:\n    print(f\"Regenerating noise: have {tuple(noise.shape)}, need {expected}\")\n    noise = generate_noise_tensor(noise_type, width, height, seed, device, dtype)","typeGuard":"def noise_shape_ok(noise, noise_type: str, width: int, height: int) -> bool:\n    try:\n        return tuple(noise.shape) == get_expected_noise_shape(noise_type, width, height)\n    except ValueError:\n        return False","tryCatchPattern":"try:\n    validate_noise_tensor_shape(noise, noise_type, width, height)\nexcept ValueError as e:\n    if \"Expected noise with shape\" in str(e):\n        logger.warning(\"Cached noise no longer matches; regenerating\")\n        noise = generate_noise_tensor(noise_type, width, height, seed, device, dtype)\n    else:\n        raise","preventionTips":["Do not cache noise tensors across runs where width, height, or model type may change; key the cache on (noise_type, width, height, seed).","Always generate noise through generate_noise_tensor rather than hand-rolled torch.rand calls.","For Anima, remember the extra 5D frame dimension; for cross-model workflows, regenerate per model.","Call validate_noise_tensor_shape immediately after producing/loading noise to fail fast."],"tags":["validation","shape-mismatch","noise","tensor"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}