Chattiori-Model-Merger
Chattiori-Model-Merger copied to clipboard
Merges latent diffusion models at a user-defined ratio
Chattiori's Model Merger
This script merges stable-diffusion models with the settings of checkpoint merger at a user-defined ratio.
You can use ckpt and safetensors as checkpoint file.
This script isn't need to be loaded on GPU.
The mode is:
- "WS" for Weighted Sum
- "SIG" for Sigmoid Merge
- "GEO" for Geometric Merge
- "MAX" for Max Merge
- "AD" for Add Difference (requires model2)
- "sAD" for Smooth Add Difference (requires model2)
- "MD" for Multiply Difference (requires model2 and beta)
- "SIM" for Similarity Add Difference (requires model2 and beta)
- "TD" for Train Difference (requires model2)
- "TRS" for Triple Sum (requires model2 and beta)
- "TS" for Tensor Sum (requires beta)
- "ST" for Sum Twice (requires model2 and beta)
- "NoIn" for No Interporation
- "RM" for Read Metadata
- "DARE" for DARE Merge
The ratio works as follows:
- 0.5 is a 50/50 mix of model0 and model1
- 0.3 is a 70/30 mix with more influence from model0 than model1
Running it
If you aren't using Automatic's web UI or are comfortable with the command line, you can also run merge.py directly.
Just like with the .bat method, I'd recommend creating a folder within your stable-diffusion installation's main folder. This script requires torch to be installed, which you most likely will have installed in a venv inside your stable-diffusion webui install.
- Requires pytorch safetensors
- Navigate to the merge folder in your terminal
- Activate the venv
- For users of Automatic's Webui use
..\venv\Scripts\activate
- For users of sd-webui (formerly known as HLKY) you should just be able to do
conda activate ldm
- For users of Automatic's Webui use
- run merge.py with arguments
- Form:
python merge.py mode model_path model_0 model_1 --alpha 0.5 --output merged - Example:
python merge.py "WS" "C:...\Model parent file path" "FILE A.ckpt" "FILE B.safetensors" --alpha 0.45 --vae "C:...\VAE.safetensors" --prune --save_half --output "MERGED"- Optional:
--model_2sets the tertiory model, if omitted - Optional:
--alphacontrols how much weight is put on the second model. Defaults to 0, if omitted
Can be written in float value, Merge Block Weight type writing and Elemental Merge type writing. - Optional:
--rand_alpharandomizes weight put on the second model, if omitted
Need to be written in str like"MIN, MAX, SEED".
If SEED is not setted, it will be completely random (generates seed).
Or"MIN, MAX, SEED, [Elemental merge args]"if you want to specify.
Check out Elemental Random for Elemental merge args. - Optional:
--betacontrols how much weight is put on the third model. Defaults to 0, if omitted
Can be written in float value, Merge Block Weight type writing and Elemental Merge type writing. - Optional:
--rand_betarandomizes weight put on the third model, if omitted
Need to be written in str like"MIN, MAX, SEED".
If SEED is not setted, it will be completely random (generates seed).
Or"MIN, MAX, SEED, [Elemental merge args]"if you want to specify.
Check out Elemental Random for Elemental merge args. - Optional:
--vaesets the vae file by set the path, if omitted.
If not, the vae stored inside the model will automatically discarded. - Optional:
--m0_namedetermines the name that to write in the data for the model0, if omitted - Optional:
--m1_namedetermines the name that to write in the data for the model1, if omitted - Optional:
--m2_namedetermines the name that to write in the data for the model2, if omitted - Optional:
--cosine0determines to favor model0's structure with details from 1, if omitted
Check out Calcmode by hako-mikan for the information. - Optional:
--cosine1determines to favor model1's structure with details from 0, if omitted
Check out Calcmode by hako-mikan for the information. - Optional:
--use_dif_10determines to use the difference between model0 and model1 as model1, if omitted - Optional:
--use_dif_20determines to use the difference between model0 and model2 as model2, if omitted - Optional:
--use_dif_21determines to use the difference between model2 and model1 as model2, if omitted - Optional:
--finedetermines adjustment of details, if omitted
Check out Elemental EN by hako-mikan for the information. - Optional:
--save_halfdetermines whether save the file as fp16, if omitted - Optional:
--prunedetermines whether prune the model, if omitted - Optional:
--keep_emadetermines keep only ema while prune, if omitted - Optional:
--save_safetensorsdetermines whether save the file as safetensors, if omitted - Optional:
--outputis the filename of the merged file, without file extension. Defaults to "merged", if omitted - Optional:
--functndetermines whether add merge function names, if omitted - Optional:
--delete_sourcedetermines whether to delete the source checkpoint files, not vae file, if omitted - Optional:
--no_metadatasaves the checkpoint without metadata, if omitted - Optional:
--deviceis the device that's going to be used to merge the models. Unless you have a ton of VRAM, you should probably just ignore this. Defaults to 'cpu', if omitted.- For
.ckptfiles, required VRAM seems to be roughly equivalent to the size of(size of both models) * 1.15. Merging 2 models at 3.76GB resulted in rougly 8.6GB of VRAM usage on top of everything else going on. - For
.safetensors, required VRAM seems to be roughly equivalent to the size ofsize of both models. Merging 2 models at 3.76GB resulted in rougly 7.5GB of VRAM usage on top of everything else going on. - If you have enough VRAM to merge on your GPU you can use
--device "cuda:x"where x is the card corresponding to the output ofnvidia-smi -L
- For
- Optional:
- Form:
For Colab users
-
Install required scripts.
!pip install torch safetensorsfor safetensors
!pip install pytorch_lightingfor ckpt -
Clone this repo.
!cd /content/
!git clone https://github.com/Faildes/merge-models -
Make
modelsfile andvaefile.
!mkdir models
!mkdir vae -
After installing models and vaes to the right directory,
Runmerge.py.
!cd /content/merge-models/
!python merge.py...
Potential Problems & Troubleshooting
- Depending on your operating system and specific installation of python you might need to replace
pythonwithpy,python3,condaor something else entirely.
Credits
- AUTOMATIC1111 for overall designing.
- eyriewow for original merge-models.
- lopho and arenasys for Pruning system.
- idelairre for Geometric, Sigmoid and Max Sum.
- s1dlx for Multiply Difference and Similarity Add Difference.
- hako-mikan for Tensor Sum, Train Difference, Triple Sum, Sum twice, Smooth Add Difference, Finetuning, Cosine Merging and Elemental Merge.
- bbc-mc for Block Weighted Merge.
- martyn for DARE Merge.
- Eyriewow got the merging logic in
merge.pyfrom this post by r_Sh4d0w, who seems to have gotten it from mlfoundations/wise-ft