Nodes/Mecha Merge Node Pack/Geometric Median
ComfyUI Node

Geometric Median

The merge that doesn't get dragged by outliers — a median instead of a mean

By ljleb·Created 3 years ago·Updated about a month ago· 101
Geometric Median
  • models (delta|param|weight)
  • eps (1e-06)
  • ftol (1e-20)
  • maxiter (100)
  • recipe
merge_checkpointingfalse

Geometric Median Mecha Recipe is the answer to a specific, recurring merge problem: one of your models is a weirdo, and it's dragging the average. A plain mean of N models gives every model equal pull, so a single noisy or divergent checkpoint bends the whole merge toward it. The geometric median - the point minimizing the sum of distances to all inputs rather than the sum of squared distances - resists that. It's the "vote instead of poll" merge.

In the pack it's one of the recipe nodes in the TIES family, and it shares the DNA of the methods used for the big multi-model merges: when you have a dozen fine-tunes and want the "center" that doesn't over-represent any one of them, this is the robust center.

How it works

The underlying sd-mecha method is Weiszfeld's algorithm, the standard iterative solver: start from the weighted average, then repeatedly re-weight each model by the inverse of its distance to the current median, recompute, and iterate until convergence. The output of a Weiszfeld run is genuinely median-like - robust to the outlier that a mean would chase.

Inputs from the schema:

  • models (delta|param|weight) - a MECHA_RECIPE_LIST (default empty) of the models to take the median of. As with the other list-based nodes, wire a Mecha Recipe List here.
  • eps (1e-6) - the floor for distances inside the iteration; stops division-by-zero blowups when a model sits exactly on the median.
  • maxiter (100) - the iteration cap. The loop usually converges long before this; it's a safety valve.
  • ftol (1e-20) - the relative tolerance that declares convergence. Leave it alone unless you're chasing reproducibility.
  • merge_checkpointing - the usual branch-cache toggle.

Output: one recipe (MECHA_RECIPE).

When you'd reach for it

Three-plus models, similar architectures, and you suspect one is skewing things. The plain N Average Mecha Recipe is the fast default; Geometric Median is the defensive version. It's also a classic component of the "merge a swarm of LoRAs into a single strong one" pipeline - drop your LoRA recipes into the list, get the robust center, then add it back to a base with Add Difference.

Install & gotchas

ComfyUI Manager → search mechaMecha Merge Node Pack, or:

cd ComfyUI/custom_nodes
git clone https://github.com/ljleb/comfy-mecha.git
pip install -r comfy-mecha/requirements.txt

Restart; only dependency is sd-mecha==1.1.7.

Two cautions. First, it's iterative and slower than averaging - on a full SDXL checkpoint with maxiter at 100 it's noticeably slower per key, and with many models the per-iteration distance computations add up. Start with the defaults before touching maxiter. Second, the models socket takes a list, not a single wire - the "empty list does nothing" trap from the other list nodes applies here too. If your merge comes back identical to one input, check the Recipe List feeding it.

Categorymecha

Inputs (5)

NameTypeDefaultDescription
models (delta|param|weight)optMECHA_RECIPE_LIST
eps (1e-06)optMECHA_RECIPE0.000001
ftol (1e-20)optMECHA_RECIPE1e-20
maxiter (100)optMECHA_RECIPE100
merge_checkpointingoptBOOLEANfalseSpeeds up an entire branch of a merge graph that does not change often in exchange of memory. - true: store the first output of this recipe node on cpu memory in fp16. On subsequent workflow executions, as long as the inputs do not change, the cached keys are returned after being cast to the original device and dtype. - false: do not store the output. The recipe and its inputs will re-execute on subsequent workflow executions. Note that the memory used to checkpoint the output is distinct from the cache feature. In general, you probably want to either use this *or* a cache unit, but not both at the same time because the memory adds up. The difference between merge checkpointing and cache is that merge checkpointing completely re-merges from scratch if any input changes. Merge checkpointing is also generally much faster than cache in the fast path.

Outputs (1)

NameTypeDescription
recipeMECHA_RECIPE