Nodes/ComfyUI_MS_Diffusion/MS-Diffusion Apply to MODEL (Native)
ComfyUI Node

MS-Diffusion Apply to MODEL (Native)

Two Subjects, Two Boxes, One UNet

By smthemex·Created 2 years ago·Updated about a month ago· 58
MS-Diffusion Apply to MODEL (Native)
  • model
  • ms_adapter
  • ms_embeds
  • MODEL
◄scale0.80►
◄start_step5►

Two characters, one image, no training. That's the job this node does, and it's the job SDXL has always been bad at. IP-Adapter can hand the model a reference, but it has no idea which of the two people in your scene that reference belongs to, so you get the classic failure where the dog's fur migrates onto the cat. Regional prompting fixes the layout and ignores the references. MS-Diffusion does both at once: reference images plus a box saying where each one lives.

Fair warning before you build a workflow around it. MS-Diffusion is a research method - ICLR 2025, Xierui Wang and friends - that never became a community thing; search the usual subreddits and you find essentially nothing. It only loads on stock-SDXL-shaped UNets. On an SDXL or Illustrious workflow with two specific subjects to place, it's genuinely interesting. On Flux, close the tab.

What the node does to your model

It doesn't load a model. It patches one.

The MODEL you wire in gets cloned, and then the node walks the UNet and installs a callback on every attn2 layer - the text cross-attention blocks, 70 of them on stock SDXL. Each callback rewrites that layer into a two-branch version, which is the whole trick of the method:

  • the text branch runs as normal, your prompt still works;
  • an image branch runs beside it, with its own key/value projection weights (to_k_ip / to_v_ip, one pair per layer, that's what your ms_adapter file holds);
  • the result is text_out + scale * ip_out, so the reference features are added on top of the text rather than replacing it.

The image branch doesn't see raw CLIP embeddings. It sees the tokens MSDiffusionConditioning built: 4 learnable "background" tokens plus 16 tokens per subject. Those tokens are masked per-layer against your boxes - anything outside a subject's box gets its attention score shoved down by -10000, the same trick ControlNet-era code uses to hard-mask attention. Each subject's tokens can only look at their own rectangle, and the dummy tokens soak up whatever no box claimed, so the background doesn't get force-fed a face.

Then there's the part people miss. With mask_threshold > 0, the hard boxes are only used for the first few steps. The first patched layer records where the prompt's subject words are actually attending, layer by layer, step by step; once start_step steps have gone by, those recorded maps - min-max normalised and cut at your threshold - replace the box mask. So the layout starts as a hard rectangle and relaxes into wherever the model thinks [dog] is.

The inputs that matter

model is your checkpoint's MODEL output. ms_adapter comes from the loader, ms_embeds from the conditioning node - both have to be from the same run, they're not interchangeable between sessions in any meaningful way.

scale (default 0.8) is a straight multiplier on the image branch. Zero gives you the base model back exactly, which is a genuinely useful A/B switch when you suspect the adapter is the problem. Push it up and you lean harder on the references, at the cost of the textures going a bit stiff.

start_step (default 5) does nothing unless mask_threshold is above zero, per the author's own tooltip. It's the swap point between box masks and pseudo masks above.

One output, MODEL, and it's an ordinary patched model. KSampler, LoRA, ControlNet, FreeU, whatever - all stock nodes downstream, in whatever order you like. That's the nicest thing about this port versus the diffusers-based implementations: nothing else in your graph has to know MS-Diffusion exists.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/smthemex/ComfyUI_MS_Diffusion

Restart. There is no requirements.txt and the pack's pyproject.toml declares zero dependencies - the whole native chain is plain torch against ComfyUI core. That's unusual for this author, who normally ships packs that drag in half of PyPI. ComfyUI Manager can also find it by searching the pack title.

You need three files, and the README only tells you about two of them:

  • any SDXL checkpoint in models/checkpoints (the README says sd_xl_base_1.0.safetensors, and honestly the base model is the right place to start before you try it on a finetune);
  • ms_adapter.bin from doge1516/MS-Diffusion into models/photomaker (per the README) or models/ipadapter (the loader scans both);
  • clip_vision_g.safetensors in models/clip_vision - never mentioned in the README, but the shipped example workflow won't run without it.

Where it breaks

"no attn2 modules found. This adapter requires a stock SDXL UNet." You wired a Flux, SD 1.5, or SD3 model in. The image branch only makes sense against the architecture it was trained for.

"UNet has N attn2 modules but the checkpoint has 70 adapter groups." Same story one layer deeper: the model is SDXL-ish but not SDXL-shaped. The node checks the hidden sizes layer by layer and refuses rather than applying the wrong weights to the wrong block, which is the correct behaviour and a lot better than silently producing mush.

A second attention patcher on the same graph. The node writes into patches_replace["attn2"] keyed per layer, so a competing patch on the same keys overwrites rather than stacks. If you're also running another IP-Adapter-style attn2 patch, run one of them.

Nothing happens. Check that you're not bypassed, that scale isn't 0, and that mask_threshold on the conditioning node isn't 0 if you were counting on boxes. With layout off you're just doing vanilla subject-driven injection - which works, but you lose the reason you came.

CategoryMSdiffusion/Native

Inputs (5)

NameTypeDefaultDescription
modelMODEL—
ms_adapterMS_ADAPTER—
ms_embedsMS_EMBEDS—
scaleFLOAT0.800–2—
start_stepINT50–1024pseudo attention mask kicks in after this many denoising steps (only when mask_threshold > 0)

Outputs (1)

NameTypeDescription
MODELMODEL—