Skip to main content

Package for Mixing Stable Diffusiion XL Models by Segmind

Project description

SegMoE: Segmind Mixture of Diffusion Experts

image/png

SegMoE is a powerful framework for dynamically combining Stable Diffusion Models into a Mixture of Experts within minutes without training. The framework allows for creation of larger models on the fly which offer larger knowledge, better adherence and better image quality. It is inspired by mergekit's mixtral branch but for Stable Diffusion models.

Installation

pip install segmoe

Usage

Load Checkpoint from Hugging Face

We release 3 merges on Hugging Face,

They can be loaded as follows:

from segmoe import SegMoEPipeline

pipeline = SegMoEPipeline("segmind/SegMoE-4x2-v0", device = "cuda")

prompt = "cosmic canvas, orange city background, painting of a chubby cat"
negative_prompt = "nsfw, bad quality, worse quality"
img = pipeline(
    prompt=prompt,
    negative_prompt=negative_prompt,
    height=1024,
    width=1024,
    num_inference_steps=25,
    guidance_scale=7.5,
).images[0]
img.save("image.png")

Comparison

The Prompt Understanding seems to improve as shown in the images below. From Left to Right SegMoE-2x1-v0, SegMoE-4x2-v0, Base Model (RealVisXL_V3.0)

image

three green glass bottles

image

panda bear with aviator glasses on its head

image

the statue of Liberty next to the Washington Monument

Creating your Own Model

Create a yaml config file, config.yaml, with the following structure:

base_model: Base Model Path, Model Card or CivitAI Download Link
num_experts: Number of experts to use
moe_layers: Type of Layers to Mix (can be "ff", "attn" or "all"). Defaults to "attn"
num_experts_per_tok: Number of Experts to use 
type: Type of the individual models (can be "sd" or "sdxl"). Defaults to "sdxl"
experts:
  - source_model: Expert 1 Path, Model Card or CivitAI Download Link
    positive_prompt: Positive Prompt for computing gate weights
    negative_prompt: Negative Prompt for computing gate weights
  - source_model: Expert 2 Path, Model Card or CivitAI Download Link
    positive_prompt: Positive Prompt for computing gate weights
    negative_prompt: Negative Prompt for computing gate weights
  - source_model: Expert 3 Path, Model Card or CivitAI Download Link
    positive_prompt: Positive Prompt for computing gate weights
    negative_prompt: Negative Prompt for computing gate weights
  - source_model: Expert 4 Path, Model Card or CivitAI Download Link
    positive_prompt: Positive Prompt for computing gate weights
    negative_prompt: Negative Prompt for computing gate weights

Any number of models can be combined, An Example config can be found here. For detailed information on how to create a config file, please refer to the Config Parameters

Note Both Huggingface Models and CivitAI Models are supported. For CivitAI models, paste the download link of the model, For Example: "https://civitai.com/api/download/models/239306"

Then run the following command:

segmoe config.yaml segmoe_v0

This will create a folder called segmoe_v0 with the following structure:

├── model_index.json
├── scheduler
│   └── scheduler_config.json
├── text_encoder
│   ├── config.json
│   └── model.safetensors
├── text_encoder_2
│   ├── config.json
│   └── model.safetensors
├── tokenizer
│   ├── merges.txt
│   ├── special_tokens_map.json
│   ├── tokenizer_config.json
│   └── vocab.json
├── tokenizer_2
│   ├── merges.txt
│   ├── special_tokens_map.json
│   ├── tokenizer_config.json
│   └── vocab.json
├── unet
│   ├── config.json
│   └── diffusion_pytorch_model.safetensors
└──vae
    ├── config.json
    └── diffusion_pytorch_model.safetensors

Alternatively, you can also use the following command to create a mixture of experts model:

from segmoe import SegMoEPipeline

pipeline = SegMoEPipeline("config.yaml", device="cuda")

pipeline.save_pretrained("segmoe_v0")

Push to Hub

The Model can be pushed to the hub via the huggingface-cli

huggingface-cli upload segmind/segmoe_v0 ./segmoe_v0

Detailed usage can be found here

SDXL Turbo

To use SDXL Turbo style models, just change the scheduler to DPMSolverMultistepScheduler. Example config can be found here

Usage:

from segmoe import SegMoEPipeline

pipeline = SegMoETurboPipeline("segomoe_config_turbo.yaml", device = "cuda")
pipeline.pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.pipe.scheduler.config)

prompt = "cosmic canvas, orange city background, painting of a chubby cat"

image = pipe(prompt=prompt, num_inference_steps=6, guidance_scale=2).images[0]  

image.save("image.png")

Stable Diffusion 1.5 Support

Stable Diffusion 1.5 Models are also supported and work natively. Example config can be found here

Note: Stable Diffusion 1.5 Models can be combined with other SD1.5 Models only.

Memory Requirements

  • SDXL 2xN : 19GB
  • SDXL 4xN : 25GB
  • SD1.5 4xN : 7GB

Advantages

  • Benefits from The Knowledge of Several Finetuned Experts
  • Training Free
  • Better Adaptability to Data
  • Model Can be upgraded by using a better finetuned model as one of the experts.

Limitations

  • Though the Model improves upon the fidelity of images as well as adherence, it does not be drastically better than any one expert without training and relies on the knowledge of the experts.
  • This is not yet optimized for speed.
  • The framework is not yet optimized for memory usage.

Research Roadmap

  • Optimize for Speed
  • Optimize for Memory Usage
  • Add Support for LoRAs
  • Add Support for More Models
  • Add Support for Training

Config Parameters

Base Model

The base model is the model that will be used to generate the initial image. It can be a Huggingface model card, a CivitAI model download link or a local path to a safetensors file.

Number of Experts

The number of experts to use in the mixture of experts model. The number of experts must be greater than 1. The Number of experts can be anything greater than 2 as long as the GPU fits it.

MOE Layers

The type of layers to mix. Can be "ff", "attn" or "all". Defaults to "attn". "ff" merges only the feedforward layers, "attn" merges only the attention layers and "all" merges all layers.

Type

The type of the models to mix. Can be "sd" or "sdxl". Defaults to "sdxl".

Experts

The Experts are the models that will be used to generate the final image. Each expert must have a source model, a positive prompt and a negative prompt. The source model can be a Huggingface model card, a CivitAI model download link or a local path to a safetensors file. The positive prompt and negative prompt are the prompts that will be used to compute the gate weights for each expert and impact the quality of the final model, choose these carefully.

Citation

@misc{segmoe,
  author = {Yatharth Gupta, Vishnu V Jaddipal, Harish Prabhala},
  title = {SegMoE: Segmind Mixture of Diffusion Experts},
  year = {2024},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/segmind/segmoe}}
}

Project details


Download files

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

Source Distribution

segmoe-0.0.4.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

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

segmoe-0.0.4-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file segmoe-0.0.4.tar.gz.

File metadata

  • Download URL: segmoe-0.0.4.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for segmoe-0.0.4.tar.gz
Algorithm Hash digest
SHA256 e118a3e1aec4ba738d54cc56f3c90fecb68d68ed372cd36b91f68a2c15c86379
MD5 245b947dc24008498b0845afc3d91433
BLAKE2b-256 21e9fc4cc3fa432d0925aa1f4bbe671bfb4a46aa06c9f82f0cbbeecfb1d4f12c

See more details on using hashes here.

File details

Details for the file segmoe-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: segmoe-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for segmoe-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f25d9f7deb1e57f1385472cb5203ea0135f918ee7dcb5f2c68fc39e29e3dfde9
MD5 431c8fecc433d7ee8d896ffc797f8f6e
BLAKE2b-256 aa8285a4f7fb38d3513bbd4e88f43df17f4d079dcd16b5532ebbc1cafbf3d47d

See more details on using hashes here.

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