ModelMergeSimple and all merge nodes are O(N²)

Open Beginner friendly
#17,823 0 comments 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

@rsteth is already working on this.

Since Nov 18, 2026.

  • #17825 by @rsteth — open
  • #17858 by @jipeng6036-del — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
75/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, pytorch

Research direction

Start with comfy_extras/nodes_model_merging.py and inspect the listed merge nodes, then read ModelPatcher.add_patches() in comfy/model_patcher.py to understand how it handles a whole patch dictionary. Update the affected merge paths to batch patches, grouping keys by resolved ratio in ModelMergeBlocks. Confirm the patches dictionary retains the same keys and strengths, including per-block ratios.

Written by the indexing model from the issue text.

Description

Summary

Every built-in model merge node calls add_patches() once per weight tensor. ModelPatcher.add_patches() rebuilds the model's entire state dict on every call, so a single merge performs N full state-dict builds where N is the number of tensors in the model. For SDXL this is roughly 1,300 builds per merge, and chaining three models means two merges. Changing a ratio invalidates downstream nodes, so the whole cost is paid again on every tweak.

Code

comfy_extras/nodes_model_merging.py:

def merge(self, model1, model2, ratio):
    m = model1.clone()
    kp = model2.get_key_patches("diffusion_model.")
    for k in kp:
        m.add_patches({k: kp[k]}, 1.0 - ratio, ratio)
    return (m, )

comfy/model_patcher.py, ModelPatcher.add_patches():

def add_patches(self, patches, strength_patch=1.0, strength_model=1.0):
    with self.use_ejected():
        p = set()
        weights_changed = False
        model_sd = self.model.state_dict()   # rebuilt on every call
        for k in patches:
            ...

Measured impact

Synthetic module with 2,000 tensors, merge of two patchers:

Implementation Time per merge
Current (one add_patches() per key) 7.07 s
Pass the whole dict in one call 0.004 s (~1600x faster)

With real SDXL models this is several seconds of UI freeze per merge, and a two-merge chain (three checkpoints) takes roughly twice that. Experimenting with ratios is effectively unusable.

Affected nodes

  • ModelMergeSimple
  • ModelSubtract / ModelMergeSubtract
  • ModelAdd / ModelMergeAdd
  • CLIPMergeSimple, CLIPSubtract, CLIPAdd
  • ModelMergeBlocks and every model-specific merge that subclasses it

Fix

Pass the whole patch dict to add_patches() in one call:

m.add_patches(kp, 1.0 - ratio, ratio)

For ModelMergeBlocks, group keys by their resolved ratio first, then call add_patches() once per group (normally at most three groups) instead of once per key.

I verified that the resulting patches dict — keys, strengths, and per-block ratios — is identical to the old per-key path.

Environment

Reproduced on upstream master as of 2026-11-18 (also present in the current release). This is not platform-specific.

Dominant language
Python
Stars
150k
Forks
17.9k
Avg merge
1d 1h
Merged PRs (30d)
181

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Comfy-Org/ComfyUI

All issues in Comfy-Org/ComfyUI

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.