Package: sd-mecha
sd-mechasd-mecha is a memory-efficient general-purpose model merger. It can merge any model architecture given appropriate configuration:diffusion modelsLLMsDepth modelsScorers...FeaturesMemory efficient model merging -- merge a very large number of models at the same timeMecha recipes as a textual and interpretable format (.mecha)Extension API through python:add new architectures (i.e. Stable Cascade, Stable Diffusion 3, etc.)add new model types (i.e. OFT networks, LoKr, etc.)add new merge methodsRecipe variables for general recipe templatesCompose recipe templates to create mega recipesBuiltin support for popular model architectures:SD1.5SDXLMerge LoRAs together and to checkpointsBlock-wise hyperparameters for precise control of blocksClass-wise hyperparameters for precise control of layer typesComing soon:Support arbitrary model types and versions using thesd_mecha.extensionsmoduleMerge SDXL LoRAs to models and with other LoRAsSave LoRA from delta merge spaceInstallpip install sd-mecha torchsd-mecha depends additionally on:torch>=2.0.1The pypi package does not ship withtorchso that you can install the appropriate version for your system.UsageGet Model-Specific InformationThe interface for block/class hyperparameters requires prior knowledge of the blocks and classes of the architecture being merged.
The commandinfowas made to discover the names of the blocks and/or classes to use.To show the registered model architectures:python -m sd_mecha infoMecha has builtin support for the SD1.x and the SDXL architectures:Available architectures:
- sd1
- sdxlTo view the available blocks and classes of an architecture, specify the architecture:python -m sd_mecha info sd1Component "txt":
  Blocks:
  - in0
  - in1
  - in2
  ...
  Classes:
  - final_layer_norm
  - layer_norm1
  - layer_norm2
  - mlp_fc1
  ...
Component "unet":
  Blocks:
  ...
  Classes:
  ...Given this information, it is possible to set i.e. the value of blockin2in thetxtcomponent specifically:importsd_mecharecipe=sd_mecha.weighted_sum("ghostmix_v20Bakedvae","dreamshaper_332BakedVaeClipFix",alpha=(sd_mecha.default("sd1","txt",0.33)|sd_mecha.blocks("sd1","txt",in2=0.75)),)See themerging API sectionbelow for more info.If run as verbose, it also shows the keys that are associated with each block/class:python -m sd_mecha info sd1 -vComponent "txt":
  Blocks:
    in0:
    - model.diffusion_model.input_blocks.0.0.bias
    - model.diffusion_model.input_blocks.0.0.weight
    in1:
    - model.diffusion_model.input_blocks.1.0.emb_layers.1.bias
    - model.diffusion_model.input_blocks.1.0.emb_layers.1.weight
    - model.diffusion_model.input_blocks.1.0.in_layers.0.bias
    - model.diffusion_model.input_blocks.1.0.in_layers.0.weight
    ...
  ...
...Merge modelsTo merge models, mecha needs a recipe as input. There are multiple ways to provide a recipe:using the python merging APIusing the CLI with .mecha recipesUsing the python merging APIHere's an example simple sum-twice merge setup:importsd_mecha# create a simple weighted sum recipe# all builtin merge methods are direct properties of the `sd_mecha` package for conveniencerecipe=sd_mecha.weighted_sum(sd_mecha.weighted_sum("ghostmix_v20Bakedvae","deliberate_v2",alpha=0.5,),"dreamshaper_332BakedVaeClipFix",alpha=0.33,)# merger contains default parametersmerger=sd_mecha.RecipeMerger(models_dir=r"E:\sd\models\Stable-diffusion",)# perform the entire merge plan and save to output pathmerger.merge_and_save(recipe,output="basic_merge")See theexamplesdirectory for more examples.Using the CLI with .mecha recipesIt is alternatively possible to merge recipes previously serialized to.mecha.
This is only possible if the recipe is concrete. (i.e. all potential parameters have been replaced with actual models)python -m sd_mecha merge path/to/recipe.mechaFor more information:python -m sd_mecha merge --helpCompose recipesIt is possible to compose recipes together to create more complex recipes.
For this to work, the base recipe must be general: (i.e. the parameters to replace must exist in the base recipe)python -m sd_mecha compose path/to/recipe.mecha [options]For example, here we compose the recipeincompatible_fusion.mechawith another recipe for parameter "a" and
SD1.5 base for parameter "c":python -m sd_mecha compose examples/recipes/incompatible_fusion.mecha \
  -p a examples/recipes/weighted_sum.mecha \
  -p c v1-5-pruned.safetensorsFor more information:python-msd_mechacompose--helpMotivationKeeping track of full merge recipes has always been annoying.
I needed something that allows to store merge recipes in a readable format while also being executable.
I also needed something that allows to fully merge an entire tree of models without having to save intermediate models to disk.Typically, mergers load all models in memory before initiating the merge process.
This can be very inefficient when the merge focuses on each key individually:sd-mecha doesn't have this problem as it saves keys as soon as it can:This allows to merge a very large number of models simultaneously on low-end hardware.