Randperm
Shuffle your model's neurons on purpose (research-grade merging)
- a (delta|param|weight)
- seed (None)
- recipe
Randperm (class Randperm Mecha Recipe) takes a model and applies a random permutation to its weights - reordering filters, attention heads, and neurons within each layer according to the permutation structure sd-mecha understands. It's the deliberately destructive sibling of Rebasin: where rebasin finds the right permutation to align two models, Randperm applies a random one.
Let me be straight about what this is for: this is a research tool, not a daily merge utility. Its natural habitat is the Git Re-Basin style of experiments - the literature (and the sd-mecha Discord's chaos lab) that asks "do models that look different actually have the same weights, just shuffled?" If that sentence makes your eyes glaze over, you can safely file Randperm under "interesting, not for me." If it intrigues you, keep reading.
How it works
The implementation is SDXL-only: it works on the sdxl-sgm config and permutes each permutation group - the query/key heads of attention, the value/output heads, and the feed-forward widths - with a fresh random ordering per group. The key point is that a permuted model is functionally identical: shuffle the rows of a matrix and the corresponding columns of its downstream matrix, and the network computes the same thing. So Randperm gives you a model that looks like noise in weight space but behaves exactly like the original.
The inputs that matter:
- a (delta|param|weight) (
MECHA_RECIPE, required) - the model to shuffle. - seed (None) - an integer seed for reproducibility. Leave it unset and each run gets a different permutation; set it and the same seed gives the same shuffle (per-group, derived from seed + group id).
- recipe (output) - the permuted model, same merge space.
The canonical trick: take a model, Randperm it with seed X, then feed the original and the permuted copy into Rebasin - and if rebasin finds a permutation that maps one onto the other, you've just demonstrated that the models are secretly identical under a shuffle. That's the "linear mode connectivity" experiment, and it's the entire point of this node.
Install
It ships in the Mecha Merge Node Pack (ljleb/comfy-mecha):
- ComfyUI Manager → Install Custom Nodes → search "mecha" → Mecha Merge Node Pack, or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/ljleb/comfy-mecha.git
pip install -r comfy-mecha/requirements.txt
Restart ComfyUI; it's under mecha. Only dependency is sd-mecha==1.1.7; no model downloads.
Common issues
- It's SDXL-only right now. The built-in permutation graph is defined for the SDXL-SGM config. Feed it an SD1 model and the config guard will refuse - this isn't a bug, it's the current coverage. If you need SD1 permutations, that's a feature request.
- Unseeded runs aren't reproducible. If you're comparing permutations across runs, always set
seed, or you'll be chasing results that were never going to repeat. - A permuted model merged without un-permuting is a broken merge. That's not the use case. Randperm is a component of experiments, not an input to production merges - if you don't have a plan for the permutation, you probably want
RebasinorN Averageinstead.
Randperm is the kind of node that makes no sense at first and perfect sense in context. It's the "generate a shuffled control group" button for people who actually study merging - and a genuinely fun way to see that "two different models" can be one model wearing a different sweater.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| a (delta|param|weight) | MECHA_RECIPE | — | |
| seed (None)opt | MECHA_RECIPE | — | |
| merge_checkpointingopt | BOOLEAN | false | Speeds up an entire branch of a merge graph that does not change often in exchange of memory. - true: store the first output of this recipe node on cpu memory in fp16. On subsequent workflow executions, as long as the inputs do not change, the cached keys are returned after being cast to the original device and dtype. - false: do not store the output. The recipe and its inputs will re-execute on subsequent workflow executions. Note that the memory used to checkpoint the output is distinct from the cache feature. In general, you probably want to either use this *or* a cache unit, but not both at the same time because the memory adds up. The difference between merge checkpointing and cache is that merge checkpointing completely re-merges from scratch if any input changes. Merge checkpointing is also generally much faster than cache in the fast path. |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| recipe | MECHA_RECIPE | — |