Nodes/More Math/Model Math
ComfyUI Node

Model Math

A model merger where the merge is a formula, not a slider

By mcDandy·Created about a year ago·Updated 3 days ago· 5
Model Math
  • V
  • F
  • Expression
  • stack
  • MODEL
  • STACK
length_mismatcherror
use_compute_devicetrue

Stock ComfyUI's "ModelMergeSimple" gives you one slider per block and stops there. Model Math from More Math replaces the whole merge UI with a formula over the model's weights. Same idea, vastly more expressive: instead of "block 3 at 0.4", you write I0*(1-F0)+I1*F0 and decide how two checkpoints combine for every tensor in the model.

How it works

A model is a pile of state-dict tensors, and this node iterates over the union of keys from every input model, evaluating your expression per tensor. The default I0*(1-F0)+I1*F0 is a weighted average of model V0 and model V1 by float F0 - a plain merge with one blend knob, exactly like the simple merger. But because it's an expression, you can do real work: mix different layers with different weights using if on the layer name, apply clamp, or compute a merge factor from other variables. The model-like nodes (this one, plus the CLIP and VAE variants) share one implementation, which is why they behave identically.

The union-of-keys behavior matters: if the two models don't share an architecture, keys missing from one input are filled with whatever the other provides (broadcast, with zeros for missing layers - the tooltip says it plainly). That's how you merge patch sets across architectures without the merger exploding on missing tensors.

Inputs and outputs

V is the autogrow list of models (V0, V1, ... up to 50), F the floats (F0, ...). Expression is the merge formula - the I0/I1 notation is the pack's way of referring to model inputs. length_mismatch defaults to error, with tile, pad, and do nothing alternatives; for models the docs note it "usually defaults to broadcast" since missing layers are zero-filled anyway.

Outputs are MODEL and STACK. The model plugs into anything a model plugs into - a KSampler, a LoRA applier, a second merge. Note the node clones the reference model and applies the computed patches, so your inputs aren't mutated.

Installing it

Same as every node in this pack:

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

Restart ComfyUI, or install "More math" from ComfyUI Manager. Dependency: antlr4-python3-runtime on top of torch. No model files to download - the models are yours.

What to watch out for

This is the compute-heavy member of the family. Evaluating an expression across every tensor of a 7B model takes a while, and the node shows a progress bar while it churns, so don't expect instant merges on big checkpoints. Keep expressions cheap (a lerp is fine; a for loop over every tensor is not). And remember the pack is brand new and solo-maintained - model merging is fiddly enough that you should test merged outputs at low steps before trusting a clever formula. If you just want to A/B two checkpoints at different block weights, the sliders are still faster; this node earns its keep when the merge itself needs logic.

CategoryMore math

Inputs (6)

NameTypeDefaultDescription
VCOMFY_AUTOGROW_V3
FCOMFY_AUTOGROW_V3
ExpressionSTRING,SYNTAX_TREEV0Expression to apply on weights
length_mismatchCOMBOerrorHow to handle mismatched layer counts. For models, this usually defaults to broadcast (zero for missing layers).
use_compute_deviceBOOLEANtrueTemporarily copies model tensors to the compute device for math and moves the patches back afterwards.
stackoptSTACKAccess stack between nodes

Outputs (2)

NameTypeDescription
MODELMODEL
STACKSTACK