Nodes/More Math/VAE Math
ComfyUI Node

VAE Math

Merge VAEs with the same per-layer expression trick as Model Math

By mcDandy·Created about a year ago·Updated 3 days ago· 5
VAE Math
  • a
  • b
  • c
  • d
  • VAE
Modela*(1-w)+b*w
length_mismatcherror
w0.00
x0.00
y0.00
z0.00

VAE Math is Model Math's smaller sibling: it merges up to four VAEs by running a math expression over their weights. The default a*(1-w)+b*w blends VAE a with VAE b by weight w, which is the same interpolation core ComfyUI's model merge uses, applied to the encoder/decoder instead of the diffusion model. If your images come out grey or washed out and you've been swapping VAEs to fix it, this is how you'd blend two VAEs into one instead of choosing between them.

How it works

A VAE, like a diffusion model, is a pile of weights in a state_dict. This node iterates over the union of weight keys across all input VAEs, binds each layer's weights to a, b, c, d (missing layers get zeros), and evaluates your expression with the usual floats (w, x, y, z) and layer variables (L/layer, LC/layer_count, K/key). The result per layer is diffed against the base VAE, and only the differences become patches applied to a clone of a.

Practically, that means a*(1-w)+b*w gives you a standard weighted blend, and more elaborate expressions let you, say, blend only certain layer ranges. It's real, non-destructive merging - the base VAE is cloned, so your input files never change.

The inputs that matter

  • a - the main VAE (base), required.
  • b, c, d - optional additional VAEs.
  • Model - the expression applied to the weights. (Yes, the field is literally called Model here - an author quirk carried over from the model node.) Default a*(1-w)+b*w.
  • length_mismatch - tile / error / pad, default error; the tooltip notes this usually ends up broadcasting for model-like inputs.
  • w, x, y, z - floats.

Output is a single VAE, ready for a VAEDecode.

Installing it

Part of More Math (mcDandy/more_math). ComfyUI Manager - search "More Math" - or:

cd ComfyUI/custom_nodes
git clone https://github.com/mcDandy/more_math
cd more_math
pip install -r requirements.txt

Restart. Dependencies are just antlr4-python3-runtime and torch, no downloads. Needs a current ComfyUI.

Where people get burned

The same class of traps as Model Math, at smaller scale. Merging VAEs of different architectures zero-fills the gaps and produces nonsense - blend VAEs from the same family (same SD 1.5/XL/Flux lineage). And there's the performance note: it evaluates the expression per layer, so it's not instant, though VAEs are far smaller than diffusion models, so this is the fast merge node in the pack.

Worth knowing the context for why you'd bother at all: a mismatched or missing VAE is the classic cause of grey, washed-out output, and standard practice is just to load the right VAE. Blending becomes useful when you want a middle ground - a VAE that keeps one's color character and another's stability. That's a genuine niche, and this node is the clean way to get there. Like its model counterpart, the classic form is deprecated in favor of an autogrow variant; both ship and work.

Categorysd

Inputs (10)

NameTypeDefaultDescription
aVAEMain VAE (base)
ModelSTRINGa*(1-w)+b*wExpression to apply on weights
length_mismatchCOMBOerrorHow to handle mismatched layer counts. For models, this usually defaults to broadcast (zero for missing layers).
boptVAEOptional 2nd VAE
coptVAEOptional 3rd VAE
doptVAEOptional 4th VAE
woptFLOAT0.00
xoptFLOAT0.00
yoptFLOAT0.00
zoptFLOAT0.00

Outputs (1)

NameTypeDescription
VAEVAE