Nodes/MD Nodes/MD: Universal Latent Sanitizer (Audio/Video/Image)
ComfyUI Node

MD: Universal Latent Sanitizer (Audio/Video/Image)

When your audio decode crackles or your video flickers, run the latents through this

By MDMAchine·Created about a year ago·Updated 3 months ago· 15
MD: Universal Latent Sanitizer (Audio/Video/Image)
  • samples
  • vae
  • sanitized_latents
  • patched_vae
  • sample_rate_int
  • log_info
fix_nanstrue
clamp_sigma4.5
vae_sample_rateUnchanged
disable_tilingtrue
debug_mode1 - Info
enable_profilingfalse

If you've generated audio or video with diffusion models, you know the failure modes: a silent "black hole" where audio should be, a high-pitched whine, crackle that appears every few seconds, or frames that flicker like a broken fluorescent light. Those are usually the VAE's fault - the latent has wild outlier values or NaNs in it, and the decoder turns them into garbage. MD_LatentSanitizer sits between your KSampler and VAE Decode and tries to fix exactly that.

This is one of the more genuinely useful nodes in the MD Nodes pack, because it targets a real, annoying class of bug. The ACE-Step audio scene in particular has spent months chasing static, pops, and drift - this is the author's field-tested answer, and it's multi-modal: it works on audio, video, and image latents alike.

How it works

The node does three distinct jobs, and it does all of them to the tensors and the VAE:

  1. NaN/Inf repair - replaces broken values with 0.0 (torch.nan_to_num), so a single bad sample can't poison the whole decode.
  2. Outlier clamping - anything above clamp_sigma gets clamped back. The sigma limit is calibrated to the latent's expected scale, so audio doesn't blow out the decoder and video doesn't flicker.
  3. VAE patching - it returns a patched VAE alongside the cleaned latents: tiling can be forced off to kill seam crackle and grid lines, and the VAE's sample_rate metadata can be rewritten if drift/aliasing is pitch-shifting your audio.

That third part is why it needs the VAE input, and why the tooltip insists you run it before VAE Decode - the patched VAE is what actually decodes.

The inputs that matter

  • samples - the LATENT out of your KSampler. Required, obviously.
  • vae - required, and it must be the same VAE you're about to decode with.
  • clamp_sigma - the hard clamp limit (default 4.5). Tighten to 3.0 if you're hearing crackle; loosen toward 6.0 if you think it's eating detail.
  • disable_tiling - default on. It eliminates seam artifacts but costs real VRAM, so expect a memory bump.
  • vae_sample_rate - leave on Unchanged unless you're specifically correcting drift/aliasing.

Outputs: sanitized_latents (into VAE Decode), patched_vae (also into VAE Decode), sample_rate_int (handy for filename metadata), and a log_info string that tells you whether anything was actually clamped or fixed. That last one matters - watch it, because if the log is all "no clamping needed," your crackle has a different cause.

Installing

It's 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 via ComfyUI Manager under MD Nodes, then restart. This node itself is pure PyTorch - no models to download. The heavier requirements (librosa, soundfile) only matter for the pack's audio file handling nodes.

Common issues

Don't expect it to fix every decode problem. If your audio is consistently broken rather than intermittently crackly, the issue is more likely a model/VAE mismatch or an fp16 overflow upstream - in which case the sibling MD_ModelStateReset node (force fp32, force eval) is the better first move. And remember the VRAM trade-off: with tiling disabled you're decoding whole files at once, and on a small GPU that's a real cost, not a rounding error.

CategoryMD_Nodes/Maintenance

Inputs (8)

NameTypeDefaultDescription
samplesLATENTLATENT INPUT • Purpose: The latent chunks from your KSampler. • Requirement: Connect this BEFORE VAE Decode.
vaeVAEVAE INPUT (REQUIRED) • Purpose: The VAE used for decoding. • Requirement: Must be passed through this node to apply rate/tiling fixes.
fix_nansBOOLEANtrueFIX NANS / INFINITIES • Purpose: Replaces broken numerical values with 0.0. • Effect: Prevents 'Black Hole' audio/video and silent outputs. • Trade-offs: None (broken values are useless anyway). ⭐ Recommended: Enabled
clamp_sigmaFLOAT4.51–20HARD CLAMP LIMIT (SIGMA) • Purpose: Limits the maximum loudness/variance of the latent data. • Audio Use: Prevents VAE 'blowout' (digital clipping/crackle). • Video Use: Prevents 'flickering' frames. • Range: 3.0 (Strict) to 6.0 (Loose). ⭐ Recommended: 3.0 to 4.5 if hearing crackle.
vae_sample_rateCOMBOUnchangedFORCE VAE SAMPLE RATE (Audio Only) • Purpose: Patches the VAE metadata to expected Hz to fix 'Drift'. • Effect: Prevents pitch shifting or high-pitched aliasing/whine. • Note: Ignored for Image/Video VAEs. ⭐ Recommended: Unchanged (unless correcting a known issue).
disable_tilingBOOLEANtrueDISABLE VAE TILING • Purpose: Forces the VAE to decode the whole file at once. • Audio: Eliminates 'seam crackle' every few seconds. • Image: Eliminates 'grid lines' in high-res upscales. • Trade-offs: Uses significantly more VRAM when enabled. ⭐ Recommended: Enabled (fixes boundary artifacts).
debug_modeCOMBO1 - InfoLOGGING VERBOSITY • Purpose: Controls console output detail level. • Options: 0 (Production), 1 (Analytics Report), 2 (Full trace). ⭐ Recommended: 1 - Info.
enable_profilingBOOLEANfalsePERFORMANCE PROFILING • Purpose: Enable detailed operation timing. • Note: Automatically enabled when debug_mode >= 1.

Outputs (4)

NameTypeDescription
sanitized_latentsLATENT
patched_vaeVAE
sample_rate_intINT
log_infoSTRING