{"record":{"id":"639bd1bd452afce7","repo":"AUTOMATIC1111/stable-diffusion-webui","slug":"a-tensor-with-nans-was-produced-use-disable-nan","errorCode":null,"errorMessage":"A tensor with NaNs was produced. Use --disable-nan-check commandline argument to disable this check.","messagePattern":"A tensor with NaNs was produced\\. Use --disable-nan-check commandline argument to disable this check\\.","errorType":"exception","errorClass":"NansException","httpStatus":null,"severity":"critical","filePath":"modules/devices.py","lineNumber":265,"sourceCode":"        return\n\n    if where == \"unet\":\n        message = \"A tensor with NaNs was produced in Unet.\"\n\n        if not shared.cmd_opts.no_half:\n            message += \" This could be either because there's not enough precision to represent the picture, or because your video card does not support half type. Try setting the \\\"Upcast cross attention layer to float32\\\" option in Settings > Stable Diffusion or using the --no-half commandline argument to fix this.\"\n\n    elif where == \"vae\":\n        message = \"A tensor with NaNs was produced in VAE.\"\n\n        if not shared.cmd_opts.no_half and not shared.cmd_opts.no_half_vae:\n            message += \" This could be because there's not enough precision to represent the picture. Try adding --no-half-vae commandline argument to fix this.\"\n    else:\n        message = \"A tensor with NaNs was produced.\"\n\n    message += \" Use --disable-nan-check commandline argument to disable this check.\"\n\n    raise NansException(message)\n\n\n@lru_cache\ndef first_time_calculation():\n    \"\"\"\n    just do any calculation with pytorch layers - the first time this is done it allocates about 700MB of memory and\n    spends about 2.7 seconds doing that, at least with NVidia.\n    \"\"\"\n\n    x = torch.zeros((1, 1)).to(device, dtype)\n    linear = torch.nn.Linear(1, 1).to(device, dtype)\n    linear(x)\n\n    x = torch.zeros((1, 1, 3, 3)).to(device, dtype)\n    conv2d = torch.nn.Conv2d(1, 1, (3, 3)).to(device, dtype)\n    conv2d(x)\n\n","sourceCodeStart":247,"sourceCodeEnd":283,"githubUrl":"https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/82a973c04367123ae98bd9abdf80d9eda9b910e2/modules/devices.py#L247-L283","documentation":"NansException raised by modules.devices debug-on-exception hooks (torch autograd anomaly detection / nan_and_inf detection installed per where='unet'/'vae') when a forward or backward pass produced NaN/Inf activations. The message tail advertises --disable-nan-check as the escape hatch; the head is context-specific: unet NaNs suggest fp16 precision issues or an unsupported half-precision GPU, vae NaNs suggest the VAE encoding/decoding overflowed in half precision.","triggerScenarios":"Generating (txt2img/img2img) on a GPU that poorly supports fp16 (older cards, some MX/Intel/iGPUs, certain driver versions); fp16 overflow in the UNet at high resolutions or with --xformers variants; VAE decode NaNs at 4x8x upscaling resolutions on fp16 VAEs; also genuinely diverging training in the train tab. Any of these while the nan check is active (default; disabled only via --disable-nan-check).","commonSituations":"First runs on unsupported/newer GPUs (e.g. when a new arch lacked proper fp16 kernels); SDXL/Flux at high res with fp16 VAE; corrupted or incompatible checkpoints; batch-size/memory pressure causing garbage computation; users who just want generation to continue disabling the check and shipping black images.","solutions":["For VAE NaNs: add --no-half-vae (or set Settings -> Stable Diffusion -> 'Upcast cross attention layer to float32')","For UNet NaNs: run with --no-half to compute in fp32, or enable upcast of cross attention layers","Update PyTorch/CUDA and GPU drivers so fp16 kernels for your card are correct; on unsupported cards use --precision full --no-half","As a last resort to keep the pipeline running (black-image risk): launch with --disable-nan-check"],"exampleFix":"# before\n./webui.sh\n\n# after (typical fp16 VAE failure)\n./webui.sh --no-half-vae\n# or full precision:\n./webui.sh --no-half","handlingStrategy":"fallback","validationCode":"# detect fp16 capability before running with half precision\nimport torch\nfp16_safe = torch.cuda.is_available() and torch.cuda.get_device_capability(0) >= (7, 0)\n# pass --no-half-vae / --no-half in launch args when not fp16_safe","typeGuard":"def gpu_supports_fp16() -> bool:\n    return torch.cuda.is_available() and torch.cuda.get_device_capability(0) >= (7, 0)","tryCatchPattern":"from modules.devices import NansException\ntry:\n    processed = processing.process_images(p)\nexcept NansException as e:\n    if 'VAE' in str(e):\n        enable_no_half_vae_and_rerun(p)   # fallback precision route\n    else:\n        raise","preventionTips":["Use --no-half-vae on setups that hit VAE NaNs; --no-half for UNet NaNs","Update PyTorch/CUDA when a new GPU arch misbehaves in fp16","Avoid --disable-nan-check except for debugging; it masks black-image outputs","Watch for NaN warnings in the console log during first generations after config changes"],"tags":["pytorch","nan","fp16","gpu","configuration","stable-diffusion-webui"],"backgroundTag":null,"analyzedSha":"82a973c04367123ae98bd9abdf80d9eda9b910e2","analyzedAt":"2026-08-14T16:46:43.225Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}