Nodes/ComfyUI-DareMerge/Gradient Operations
ComfyUI Node

Gradient Operations

Combine two merge gradients into one

By 54rt1n·Created 3 years ago·Updated about a year ago· 98
Gradient Operations
  • gradient_a
  • gradient_b
  • LAYER_GRADIENT
operationmean
joininner

Gradient Operations takes two LAYER_GRADIENTs and smashes them into one. That's the whole job - and it's a bigger job than it sounds, because it's the node that lets you build compound merge strategies that no single gradient node can express.

A layer gradient is just a dict of per-layer merge ratios, so combining two of them is dict arithmetic: for every layer present in either gradient, apply an operation to the two values. The useful cases are immediate:

  • mean - average two different weighting schemes. Take a Block Gradient and an Attention Gradient, mean them, and every layer gets a compromise ratio between the two views of the model.
  • max / min - take the looser or stricter of the two per layer. Max is handy when you want to merge a layer if either scheme says so.
  • multiply - the strict one: a layer only gets a meaningful ratio if both gradients want it. This is exactly how the pack's own Model Merger (Attention/DARE) combines its shell and attention gradients internally.
  • add / subtract / divide - for when you want to express one gradient as a correction to another.

The inputs that matter

  • gradient_a, gradient_b - the two inputs.
  • operation - mean, min, max, add, subtract, multiply, divide. Default mean.
  • join - inner (default) or outer. This decides what happens to layers that appear in only one gradient. Inner drops them (the result covers only the intersection of layer keys); outer keeps them with their single-gradient value. Inner is usually right when both gradients come from the same model, since they cover the same keys anyway; outer matters when you've edited one gradient down and still want its survivors included.

The output

A single LAYER_GRADIENT - pipe it onward to Gradient Edit for fine-tuning, then into Model Merger (Advanced) or Model Merger (Advanced/DARE). You can chain several Gradient Operations in sequence to fold three or four gradients together, though at that point you're basically writing a merge strategy in node form, and it may be worth asking whether a simpler scheme does the job.

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 downloads.

The trap to watch is divide: dividing by a ratio near zero throws a layer's value to infinity, and the merger doesn't clamp it for you - a ratio above 1 on the Advanced node means "more than fully model A," which is generally nonsense output. Keep your dividers away from zero. And remember the silent-contract rule one more time: whatever the operation produces, layers that end up missing from the output (the inner join dropping single-gradient keys, for example) simply don't merge. When in doubt, run the result through Gradient Reporting and look at what's actually in it.

CategoryDareMerge/gradient

Inputs (4)

NameTypeDefaultDescription
gradient_aLAYER_GRADIENT
gradient_bLAYER_GRADIENT
operationCOMBOmean7 options: mean, min, max, add, subtract, multiply, +1
joinCOMBOinner2 options: inner, outer

Outputs (1)

NameTypeDescription
LAYER_GRADIENTLAYER_GRADIENT