Files
facok-ComfyUI-LCS/core/patchify.py
T
facok 534bc19033 Implement LCS plugin for training-free color control in FLUX
Implements "The Latent Color Subspace" (arXiv:2603.12261v1, ICML 2026)
as a ComfyUI plugin with V3 API + V2 backward compatibility.

6 nodes across 3 categories:
- LCS/calibration: LCSCalibrate (PCA from VAE), LCSLoadData (cache load)
- LCS/intervention: LCSColorIntervene (single color), LCSColorBatch (multi)
- LCS/observe: LCSPreviewColors (latent color map), LCSStepObserver

Core modules: patchify (16ch->64d patches), bicone HSL<->LCS mapping,
alpha_t/beta_t tables (51 entries from Appendix F), PCA calibration,
and timestep normalization with residual preservation.
2026-03-17 01:43:26 +08:00

26 lines
793 B
Python

"""Patchify/unpatchify for FLUX latent tensors (patch_size=2, 16 channels → 64-dim patches)."""
from einops import rearrange
def patchify(x):
"""Convert latent [B, 16, H, W] → patch sequence [B, L, 64].
L = (H/2) * (W/2), d = 16 * 2 * 2 = 64.
Returns (patches, h_len, w_len) where h_len=H/2, w_len=W/2.
"""
B, C, H, W = x.shape
h_len = H // 2
w_len = W // 2
patches = rearrange(x, "b c (h ph) (w pw) -> b (h w) (c ph pw)", ph=2, pw=2)
return patches, h_len, w_len
def unpatchify(patches, h_len, w_len):
"""Convert patch sequence [B, L, 64] → latent [B, 16, H, W].
h_len, w_len from patchify output.
"""
return rearrange(patches, "b (h w) (c ph pw) -> b c (h ph) (w pw)",
h=h_len, w=w_len, c=16, ph=2, pw=2)