Train Difference Mask
The training trick that says 'change here, not there' — as a merge mask
- a (delta|param|weight)
- b (delta|param|weight)
- c (delta|param|weight)
- alpha (1.0)
- recipe
This is a node from the training world that wandered into merging, and it's a good one to know even if you never train. Train Difference Mask computes a per-tensor mask that answers the question: where do B and C actually differ, without B also differing from A? The output is a param recipe - a set of 0-to-1 weighting values - that you can multiply into other merge operations to focus their effect on specific regions of the network.
The formula, in plain words
Give it three models: a, b, and c. It computes |b − a| / (|b − a| + |b − c|) per weight, scales by 1.8 and alpha, and clamps the noise. Read that ratio: the numerator is "how much B changed relative to A"; the denominator adds "how much B also changed relative to C." So the mask is high where B's change is unique to the A-to-B direction and low where B is just a generic drift. In the training community this exact mask is used to tell a fine-tune "emphasize the weights that actually matter for this task and leave the rest alone" - which, applied to merging, translates to "apply my delta mainly where it's doing something real."
Inputs:
a,b,c- three recipe inputs (models fromModel Mecha Recipe, or deltas). All takedelta|param|weight.alpha- default 1.0. Scales the whole mask.
Output: one recipe (MECHA_RECIPE) in param space. That's the part to get right: this isn't a model, it's a weighting layer. You use it by feeding it into a param slot of another merge node - e.g. as the alpha input of a Scale or a masked operation - so the operation applies per-key weights instead of one flat scalar.
Why you'd reach for it
The honest pitch: it's a surgical tool for merges that keep polluting unrelated weights. Say you're adding a style delta and it's bleeding into the parts of the model you wanted untouched. Compute the train-difference mask between the involved models and route the delta through it - the add now happens hardest where the delta is genuinely informative, and near-zero elsewhere. The downside is that a per-key mask adds a full extra computation over the model, which is why the pack's lazy streaming and the merge_checkpointing toggle (cache this branch on CPU in fp16 once it's stable) matter here more than on most nodes.
Install & gotchas
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.
The usual trap: treating the param output as if it were a model and wiring it into Mecha Merger directly. It's a mask - it multiplies other things, it doesn't stand alone. And don't expect it to fix a fundamentally bad merge; it makes the placement of a change smarter, it doesn't make the change itself good.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| a (delta|param|weight) | MECHA_RECIPE | — | |
| b (delta|param|weight) | MECHA_RECIPE | — | |
| c (delta|param|weight) | MECHA_RECIPE | — | |
| alpha (1.0)opt | MECHA_RECIPE | 1 | — |
| merge_checkpointingopt | BOOLEAN | false | Speeds 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)
| Name | Type | Description |
|---|---|---|
| recipe | MECHA_RECIPE | — |