ComfyUI Node

Compute PCA

Compute PCA

By abuzreq·Created about a year ago·Updated 3 months ago· 21
Compute PCA
  • model
  • positive
  • negative
  • sampler
  • sigmas
  • latent_image
  • PCS
  • Samples
cfg8.00
n_batches1

This is the cleverest node in the pack, and also the least "just plug it in." Compute PCA runs a bunch of generations while recording the activations at the middle block of your UNet - the "h-space" - then runs PCA over those activations to find the principal directions of variation. The result is a set of steering vectors you can push the model along with HSpace Bending. It's the diffusion-world take on the StyleGAN h-space editing trick, and honestly, it's neat.

The mechanism, plainly: for each batch it samples the full image, and a forward hook on the center of the middle block captures that layer's output at every denoising step. All those captured activations get stacked, standardized, and fed to PCA (n_components = min(10, n_batches)). The output PCS is the set of principal components reshaped back to the feature shape; the second output, Samples, is every latent your runs produced, concatenated. HSpace Bending then reads a component out by direction index and adds it, scaled, into the middle block at each step - nudging the whole generation along a direction the model itself says is "natural."

The inputs are the full sampling rig (model, positive, negative, sampler, sigmas, latent_image, cfg at the SD-flavored default of 8) plus one knob that actually matters: n_batches, default 1, max 100. This controls both the size of your PCA dataset and the number of components you get (you can't get more components than batches). For anything meaningful, bump it well past 1 - 10+ batches gives you a handful of usable directions; the provided PCA_HSpace_bending.json workflow uses 100.

Real talk on the cost: this is the most expensive node in the pack. It runs a full generation per batch, on top of PCA fitting. On a mid-range GPU, ten batches is ten generations before you've bent anything. And it's marked experimental with a hard dependency on scikit-learn (with tqdm needed for the progress bar) - the code will refuse to run without sklearn, so a bare install won't cut it.

Install: ComfyUI Manager → search "ComfyUI-Model-Bending" → install → restart. Manual:

cd ComfyUI/custom_nodes
git clone https://github.com/abuzreq/ComfyUI-Model-Bending
pip install -r requirements.txt   # kornia, scikit-learn

The honest expectation: principal components from a handful of random prompts are a rough sketch of a model's latent space, not a polished steering wheel - expect directions that do things, and then spend time finding which direction index does the thing you like. But it's the closest thing in this pack to a principled, math-backed knob, and once you see direction 0 pushing coherent variation across a whole image, you'll get why it exists.

Categorymodel_bending

Inputs (8)

NameTypeDefaultDescription
modelMODEL
cfgFLOAT8.000–100
positiveCONDITIONING
negativeCONDITIONING
samplerSAMPLER
sigmasSIGMAS
latent_imageLATENT
n_batchesINT11–100

Outputs (2)

NameTypeDescription
PCSLATENT
SamplesLATENT