Add spatial smoothing to remove latent grid artifacts

The per-element correction creates a visible mesh pattern at the VAE's
8x8 patch boundaries. A 3x3 box blur in latent space (24x24 pixels)
smooths adjacent corrections while preserving the large-scale
correction structure.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-04 18:36:07 +01:00
parent fa21c01afc
commit 8c88b3213c

View File

@@ -1,4 +1,5 @@
import torch
import torch.nn.functional as F
class SMCCFGCtrl:
@@ -101,6 +102,13 @@ class SMCCFGCtrl:
phi = s.std().clamp(min=1e-6)
u_sw = -K_eff * torch.tanh(s / phi)
# Spatial smoothing: the per-element correction creates a grid
# pattern at latent boundaries (each latent = 8x8 pixels). A mild
# 3x3 box blur in latent space removes these artifacts while
# preserving the large-scale correction structure.
if u_sw.ndim == 4:
u_sw = F.avg_pool2d(u_sw, kernel_size=3, stride=1, padding=1)
# Corrected guidance error (in normalized noise space)
guidance_eps = guidance_eps + u_sw