Nodes/ComfyUI-DareMerge/Attention Gradient
ComfyUI Node

Attention Gradient

Weight a merge by attention, norm, or feed-forward

By 54rt1n·Created 3 years ago·Updated about a year ago· 98
Attention Gradient
  • model
  • LAYER_GRADIENT
process_normtrue
process_attntrue
process_ff_nettrue
norm1.00
attn1.00
ff_net1.00

Attention Gradient builds a LAYER_GRADIENT that weights the merge by what kind of layer a parameter belongs to, instead of where in the UNet it sits. Every transformer block in a diffusion model is three interleaved pieces - a norm layer, an attention block, and a feed-forward network - and this node gives you one ratio for each kind. It's the answer to the question "can I merge just the attention weights?"

Turns out that question comes up a lot. Attention weights are where a model's compositional behavior lives - how it relates objects to each other - while feed-forward layers carry more of the learned style and texture. So a common experiment is: keep model A's feed-forward, take model B's attention, and see whether you get B's composition with A's rendering. This node exists to make that split explicit.

The inputs

  • model - any MODEL; the node reads its state dict to find the layer keys.
  • process_norm, process_attn, process_ff_net - three booleans, all default true. Unchecking one means "don't include that layer kind in the gradient at all" - so turning off process_ff_net and process_norm leaves you with an attention-only gradient, and every layer that isn't attention gets skipped (that is, keeps model A).
  • norm, attn, ff_net - the three ratio values. 1 = keep model A, 0 = take model B, same convention as everywhere else in the pack. All default to 1, so an untouched node produces a gradient that keeps model A entirely.

The output

A LAYER_GRADIENT, which is a dict mapping layer keys to ratios. Wire it into Model Merger (Advanced) or Model Merger (Advanced/DARE) - those are the two consumers. You can also chain it through Gradient Operations (blend it with a Block Gradient, multiply it, take the max) or edit individual entries with Gradient Edit before it reaches the merger.

A concrete start

If your merge's composition is drifting - backgrounds getting mushy, subjects collapsing together - try leaving the attention ratio at 1 (keep model A's attention) while dropping ff_net toward 0 (take model B's feed-forward), or the reverse. With the DARE variant, pair the gradient with the magnitude-mask protection flow and a fixed seed, then roll a few seeds before judging.

Install and gotchas

ComfyUI Manager (search "DareMerge") or:

cd ComfyUI/custom_nodes
git clone https://github.com/54rt1n/ComfyUI-DareMerge

Restart ComfyUI. Dependencies: matplotlib, numpy, torch, pillow, no model downloads.

The one gotcha to remember: a gradient only affects layers it actually names. If you turn off process_norm, the norm layers aren't "kept at model A" by an explicit ratio - they're just absent from the gradient, which the merger treats the same way (no ratio, no merge, stays A). Same end result, but it's worth knowing the node is building a filter, not setting every layer. Check what you actually got with Gradient Reporting before you commit to the merge.

CategoryDareMerge/gradient

Inputs (7)

NameTypeDefaultDescription
modelMODEL
process_normBOOLEANtrue
process_attnBOOLEANtrue
process_ff_netBOOLEANtrue
normFLOAT1.000–1
attnFLOAT1.000–1
ff_netFLOAT1.000–1

Outputs (1)

NameTypeDescription
LAYER_GRADIENTLAYER_GRADIENT