ComfyUI Node

Add Cosine B

The more forgiving similarity merge — Add Cosine A, plus magnitude

By ljleb·Created 3 years ago·Updated about a month ago· 101
Add Cosine B
  • a (weight)
  • b (weight)
  • alpha (1.0)
  • recipe
merge_checkpointingfalse

Add Cosine B is the little sibling of Add Cosine A with one genuine difference: it blends a magnitude-aware similarity score in with the plain cosine similarity before deciding how much of model b to add to model a. Cosine similarity is scale-blind - it only cares about direction - which is great until you're merging two checkpoints trained at different scales, where a direction match can hide a big size mismatch. B folds both signals together: (cosine_similarity + magnitude_similarity) / 2, then the same lerp-based blending as its sibling.

The practical difference

Both nodes answer "how much should b contribute, per weight?" with "a lot, if a and b agree there." The difference is what counts as agreement. A is pure direction; B is direction plus magnitude, so it's a touch more conservative when the two models disagree in scale but happen to point the same way. In practice, for same-base merges you'll struggle to tell them apart. Where B earns its keep is when your parents come from different training runs with different effective scales - exactly the case where the scale-blindness of A can let a badly-scaled key through. If you're only going to wire up one of them, B is the safer default; A is the purist option.

Inputs & wiring

  • a (weight) and b (weight) - model recipes in weight space.
  • alpha - default 1.0. The similarity threshold that gates how much of b is added.

Output: one recipe (MECHA_RECIPE) in weight space. Chain it into a Weighted Sum, Add Difference, or straight to Mecha Merger. The node computes per-key, streams through lazy evaluation, and carries the usual merge_checkpointing toggle to cache the branch on CPU in fp16 when the recipe is stable - worth flipping on for big merges you iterate on.

The mechanism is straightforward sd-mecha code: normalize, compute the two similarity scores, average them, then k = 1 − clamp(sim − alpha, 0, 1) and lerp(a, b, k). Same family as the masked-add tricks in this pack (Train Difference Mask, Add Opposite), but cheaper - one similarity computation per key instead of a full mask construction.

When it's worth it

Reach for it when a plain weighted sum gives you a muddy blend and you want the merge to preserve each parent's character where they agree. It won't rescue incompatible models - near-zero similarity just means "don't add here," and a merge of two fundamentally different architectures stays broken. But for the common case of "two fine-tunes of the same base that feel slightly off in combination," this is a cheap, principled improvement over the naive formula.

Install

ComfyUI Manager → search mecha → "Mecha 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 after. Dependency is sd-mecha==1.1.7. Feed it weight-space recipes - deltas will confuse the similarity math and silently weaken the result.

Categorymecha

Inputs (4)

NameTypeDefaultDescription
a (weight)MECHA_RECIPE
b (weight)MECHA_RECIPE
alpha (1.0)optMECHA_RECIPE1
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