Skip to main content

State dict recipe merger

Reason this release was yanked:

only purpose was to test new publishing strategy

Project description

sd-mecha

PyPI version Discord Server

import sd_mecha

# create the merge plan
a = sd_mecha.model("path/to/model_a.safetensors")
b = sd_mecha.model("path/to/model_b.safetensors")
recipe = sd_mecha.weighted_sum(a, b, alpha=0.5)

# merge!
sd_mecha.merge(recipe, output="path/to/model_out.safetensors")

sd-mecha is a general memory-efficient model merging library. It can merge any model:

  • Diffusion models
  • LLMs
  • VLMs
  • Aesthetic scorers
  • etc.

Features

  • Memory efficient model merging: merge a very large number of models in a single execution
  • Textual and interpretable format for storage and execution (.mecha)
  • Extensible library interface:
    • add custom models
    • add custom merge methods
  • Builtin support for popular diffusion models:
    • Stable Diffusion 1.5
    • Stable Diffusion XL
    • Stable Diffusion 3
    • FLUX Schnell/Dev
  • Merge LyCORIS networks together and to checkpoints
  • Block-wise hyperparameters for precise control of blocks (aka MBW)

Install

pip install sd-mecha

Make sure to install the appropriate release of torch to get the best performance.

Usage

Merge models

To merge models, mecha uses recipes. A recipe is a list of instructions that describes the exact steps needed to derive a state dict from inputs.

Here's an example script that merges three models:

import sd_mecha

# create the merge plan
model_a = sd_mecha.model("path/to/model_a.safetensors")
model_b = sd_mecha.model("path/to/model_b.safetensors")
recipe = sd_mecha.weighted_sum(model_a, model_b, alpha=0.5)

# merge!
sd_mecha.merge(recipe, output="path/to/model_out.safetensors")

See the examples directory for more examples.

Get Model-Specific Information

The library uses a "model config" to designate any specific set of keys of a certain shape.

It is possible to list all available model configs through the sd_mecha.extensions.model_configs module:

from sd_mecha.extensions import model_configs

all_configs = model_configs.get_all()

print([config.identifier for config in all_configs])
# ["sd1-ldm-base", "sdxl-sgm-base", "sd3-sgm-base", ...]

A component of a model config is a subset of keys of the config that belong to the same logical group. For example, all keys starting with "first_stage_model." in Stable Diffusion models belong to the component "vae".

It is possible to query the different components of a model config:

from sd_mecha.extensions import model_configs

config = model_configs.resolve("sd1-ldm")
for component_id, component in config.components().items():
      # component.keys contains the state dict keys that the component owns
      print(f"{component_id}")

# this prints:
#   clip_l
#   vae
#   diffuser

Motivation

Keeping track of full merge recipes has always been a problem for me. 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:

image of typical merge graph

sd-mecha doesn't have this problem as it saves keys as soon as it can:

image of sd-mecha merge graph

This allows to merge a very large number of models simultaneously on low-end hardware.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sd_mecha-1.1.4rc1.tar.gz (159.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sd_mecha-1.1.4rc1-py3-none-any.whl (180.7 kB view details)

Uploaded Python 3

File details

Details for the file sd_mecha-1.1.4rc1.tar.gz.

File metadata

  • Download URL: sd_mecha-1.1.4rc1.tar.gz
  • Upload date:
  • Size: 159.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sd_mecha-1.1.4rc1.tar.gz
Algorithm Hash digest
SHA256 5ef5a810e89b55a14994376831fca1f96fda02fc838f47d6bec43217410512fd
MD5 de890f1f7e434ead36c7a70f3c0780f8
BLAKE2b-256 7041bbd094fce16ef5982948166fa5a697f384c80b0056d46ec9efaa78d23c8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for sd_mecha-1.1.4rc1.tar.gz:

Publisher: release.yaml on ljleb/sd-mecha

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sd_mecha-1.1.4rc1-py3-none-any.whl.

File metadata

  • Download URL: sd_mecha-1.1.4rc1-py3-none-any.whl
  • Upload date:
  • Size: 180.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sd_mecha-1.1.4rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 f1695f9900be41d4a562415bad6d1b2acc078a412cad1ebb087440b9aedf0900
MD5 5fbba5187d4aa31deab739696792d063
BLAKE2b-256 4327c7dc02989326d7e4a0d4bce480fb464deab29a813e10336d11c283938439

See more details on using hashes here.

Provenance

The following attestation bundles were made for sd_mecha-1.1.4rc1-py3-none-any.whl:

Publisher: release.yaml on ljleb/sd-mecha

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page