Nodes/MD Nodes/MD: NaN Guardian
ComfyUI Node

MD: NaN Guardian

Never silently generate from a corrupted latent again

By MDMAchine·Created about a year ago·Updated 3 months ago· 15
MD: NaN Guardian
  • latents
  • latents
actionGraceful Interrupt
debug_mode0 - Silent
enable_profilingfalse

NaN. The four-letter word of AI generation. When a tensor picks up a NaN (not-a-number) or Infinity - usually from an fp16 overflow, a sampler bug, or a model pushed past its limits - the damage spreads silently: your output comes out black, your audio comes out silent or screeching, and your video flickers to garbage. MD_NaN_Guardian is a tripwire for exactly that: it checks your latents for NaN/Inf after the sampler and decides what happens when it finds them.

The real value here is for unattended batch queues. Without a guardian, a single corrupted latent means hours of burned GPU time producing trash. With it, the queue halts the moment corruption appears - or, if you pick the rescue mode, the bad values get zeroed out so the run completes with something instead of nothing.

How it works

Right after your KSampler, the node runs torch.isnan(...).any() and torch.isinf(...).any() over the latent samples - a cheap scan with minimal overhead on a normal pass. If nothing's wrong, your latents pass through untouched. If corruption is found, it acts according to your action choice:

  • Raise Hard Error - halts the queue and throws a red UI error.
  • Graceful Interrupt - safely cancels the current run, behaving like clicking Cancel. The tooltip recommends this for unattended batches because it stops cleanly without leaving ComfyUI in an error state.
  • Zero Out (Rescue) - replaces corrupted values with zeros and continues, so you at least get an output you can inspect rather than a dead queue.

The debug_mode and enable_profiling options control how much it logs; the profiling is useful once when you're curious about the scan's overhead, then off.

The inputs that matter

  • latents - wire the output of your main KSampler straight in here.
  • action - the behavior on corruption. Graceful Interrupt for unattended queues, Zero Out if you want salvage, Raise for interactive debugging where you want to know now.

Output: latents, a pass-through (cleaned or untouched) to continue to VAE Decode.

Installing

In the MD Nodes pack:

cd path/to/ComfyUI/custom_nodes
git clone https://github.com/MDMAchine/ComfyUI_MD_Nodes.git
cd ComfyUI_MD_Nodes
pip install -r requirements.txt

Or ComfyUI Manager → MD Nodes → install → restart. Pure PyTorch, no models to download.

Common issues

Know what it can't do: this node detects and responds to corruption - it doesn't fix the cause. If you're getting NaNs every run, the guardian will dutifully stop the queue every run, and the real fix is upstream (fp16 overflow → force fp32 via MD_ModelStateReset; decode artifacts → MD_LatentSanitizer; both in this pack). Also note the scan is over the whole latent tensor, so it's all-or-nothing per batch item - a single bad frame in a video latent triggers the action for the whole chunk. For the classic "black images on older GPUs" problem, the troubleshooting advice about pre-Turing hardware still applies; the guardian is your alarm, not your repair kit.

CategoryMD_Nodes/Debugging

Inputs (4)

NameTypeDefaultDescription
latentsLATENTINPUT LATENTS • Purpose: The latent tensor output from a sampler or encoder. • Range: Standard ComfyUI LATENT dictionary. • Trade-offs: Minimal performance overhead to pass through. ⭐ Recommended: Connect immediately after your main KSampler.
actionCOMBOGraceful InterruptDEFENSE ACTION • Purpose: What to do when corrupted math (NaN/Inf) is detected. • Options: - Raise Hard Error: Halts queue and throws red UI error. - Graceful Interrupt: Safely halts (acts like clicking 'Cancel' in UI). - Zero Out (Rescue): Replaces corrupted tensors with zeros. ⭐ Recommended: Graceful Interrupt for unattended batch queues.
debug_modeCOMBO0 - SilentLOGGING VERBOSITY • Purpose: Controls console output detail level. • Options: 0 (Silent), 1 (Basic Stats), 2 (Verbose Tracing). ⭐ Recommended: 0 for standard production runs.
enable_profilingBOOLEANfalsePERFORMANCE PROFILING • Purpose: Enable detailed tensor scan timing. • Note: Automatically enabled if debug_mode is 1 - Info or higher. ⭐ Recommended: False.

Outputs (1)

NameTypeDescription
latentsLATENT