MS-Diffusion Conditioning (Native)
Brackets and Boxes, and Exactly One Word Per Subject
- ms_adapter
- clip
- clip_vision_output
- ms_embeds
- prompt_clean
This is the node where MS-Diffusion's rules live, and if your graph is exploding with validation errors, it's almost certainly this one talking. It's also the node that makes the method work: everything downstream is just attention math. Here you turn reference images into tokens, tell the model which words in the prompt those tokens correspond to, and say where on the canvas each one goes.
A quick word on why you'd bother. Handing SDXL two reference images normally means two IP-Adapter branches and hoping the model sorts out attribution - the classic multi-character bleed, where traits from one reference leak onto the other. MS-Diffusion's answer is to bind each reference to a named word in the prompt and a rectangle in the latent, so the attention mask can enforce both. This node is where you supply them.
What it actually computes
Two things leave this node as sockets, and a third gets computed and carried inside one of them.
First, it scrapes your prompt for bracketed names. a [dog] and a [cat] on the beach gives it dog and cat, in that order. That order is a contract: it has to match the order of the images arriving at clip_vision_output, which comes from the native ImageBatch stack. Image one is the dog, image two is the cat, and the prompt names them in the same sequence.
Second, it builds the tokens. Your clip_vision_output must carry penultimate hidden states - the 257×1664 CLIP-ViT-bigG features, not the last layer. Those go through the Resampler inside ms_adapter, grounded with two extra signals: a 768-dim CLIP-L pooled embedding of each subject word, and the box coordinates. Out comes 16 tokens per subject plus 4 shared background tokens, each 2048-dim, which is exactly the width SDXL's cross-attention wants. That's why the adapter file is small and the conditioning is per-image.
Third, it works out where each subject's words sit in the tokenized prompt. It tokenizes prompt_clean (your prompt with the brackets swapped for spaces), counts token positions word by word, and stores a start/end index per subject. Those indices are how the Apply node later pulls the attention maps for [dog] specifically and turns them into layout guidance. If the text encoder you actually use doesn't see the same prompt this node measured, the index points at the wrong tokens and the guidance drifts. Hence the second output.
The inputs and outputs
ms_adapter from the loader, clip (the CLIP that came out of your SDXL checkpoint - it borrows clip_l for those phrase embeddings), and clip_vision_output from a CLIPVisionEncode fed by your batched reference images.
prompt is the one with brackets. Single words only, inside the brackets - [a dog] is rejected, [dog] is fine. It's a blunt rule but the whole phrase-position calculation depends on it.
boxes is one x1,y1,x2,y2 per subject, normalised 0–1, separated by semicolons: 0.0, 0.25, 0.4, 0.75;0.6, 0.25, 1., 0.75. Read those as left-top-right-bottom of each rectangle.
mask_threshold (default 0.5) is the on/off switch for the entire layout system. Per the author's tooltip, 0 disables box guidance and pseudo attention masks completely, restoring upstream behaviour; anything above 0 turns both on and makes boxes meaningful.
drop_grounding_tokens is a boolean passed straight into the adapter's grounding step; leave it off unless you're reproducing a specific upstream experiment.
Outputs: ms_embeds goes to MSDiffusionApply. prompt_clean is a STRING - a plain-Jane version of your prompt with the brackets stripped. Wire it into the positive CLIPTextEncode. Not "you may." You should. The example workflow does exactly this, and it's why the positive encoder in the shipped JSON reads best quality, high quality, a dog on the beach with the odd double space still in it.
Install and models
cd ComfyUI/custom_nodes
git clone https://github.com/smthemex/ComfyUI_MS_Diffusion
Restart ComfyUI, or install it through Manager by searching the pack name. Nothing gets pip-installed: no requirements.txt, empty dependency list in the pyproject, no diffusers or transformers anywhere in the native chain - which is the entire point of this rewrite.
Files: any SDXL checkpoint, ms_adapter.bin from doge1516/MS-Diffusion into models/photomaker or models/ipadapter, and clip_vision_g.safetensors into models/clip_vision. That last one is absent from the README and present in the example workflow, so it's the thing people forget. Use clip_vision_g, not ViT-H.
The errors you'll actually hit
"got 2 subject images but 1 [subject] names in prompt." Your batch and your bracket count disagree. Watch out for repeats too: repeated names are collapsed to one entry, so [cat] twice with two images fails.
"subject names must be enclosed in [ ] inside the prompt." You typed the prompt without brackets, or used a different bracket style.
"subject 'big dog' must be a single word." Multi-word names aren't supported. Call it [dog].
CLIP-Vision wrong width. Anything that isn't 1664-dim CLIP-bigG gets rejected with a message telling you to use clip_vision_g.safetensors. Say thank you - the alternative is silent garbage.
Boxes. Too few boxes for the subject count raises instead of guessing, because a missing box would silently delete that subject's tokens. And if mask_threshold is 0, your boxes are ignored entirely, which is a surprisingly long five minutes of debugging.
The two-prompt trap. There are now two prompts in your graph: the bracketed one here, and the positive CLIPTextEncode. Edit one and not the other and you get a model conditioned on a prompt whose token positions don't match what the attention maps measured. Wire prompt_clean and stop thinking about it.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| ms_adapter | MS_ADAPTER | — | |
| clip | CLIP | — | |
| clip_vision_output | CLIP_VISION_OUTPUT | — | |
| prompt | STRING | best quality, high quality, a [dog] on the beach | — |
| boxes | STRING | 0.25, 0.25, 0.75, 0.75 | one 'x1,y1,x2,y2' per subject, separated by ';'. Ignored when mask_threshold is 0. |
| mask_threshold | FLOAT | 0.500–1 | 0 disables layout guidance entirely (upstream behaviour). >0 enables box guidance + pseudo attention mask. |
| drop_grounding_tokens | BOOLEAN | false | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| ms_embeds | MS_EMBEDS | — |
| prompt_clean | STRING | — |