Sign In

diffusion_model_pretraining_merge

3

Jul 22, 2025

(Updated: 4 months ago)

tool guide
diffusion_model_pretraining_merge

Original repo with instructions here:ย https://github.com/marduk191/diffusion_model_pretraining_merge

# ๐Ÿ”€ SafeTensor Model Merger: Advanced Model Blending Tool

Merge multiple AI models with precision using SMA, EMA, and WMA averaging methods!

Are you tired of choosing between different fine-tuned models? Want to create the perfect blend that combines the best aspects of your favorite checkpoints? This powerful Python tool lets you merge SafeTensor models using advanced mathematical averaging techniques, giving you complete control over the final result.

## ๐Ÿš€ What Makes This Special?

Unlike simple model mixing tools, this merger offers three scientifically-backed averaging methods that give you precise control over how models are combined:

- ๐ŸŽฏ Memory Efficient: Handle large models without running out of RAM

- โšก GPU Accelerated: CUDA support for lightning-fast processing

- ๐Ÿงฎ Advanced Math: SMA, EMA, and WMA algorithms for professional results

- ๐ŸŽ›๏ธ Full Control: Custom weights and parameters for perfect blends

- ๐Ÿ“Š Smart Metadata: Track merge settings and source models

## ๐Ÿ“ฅ Quick Setup

```bash

pip install torch tqdm

```

That's it! Download the script and you're ready to start merging.

## ๐ŸŽจ Three Powerful Merging Methods

### 1. ๐Ÿ“Š Simple Moving Average (SMA) - "Democracy Mode"

Perfect for: Balanced blends where all models matter equally

```bash

python safetensor_merger.py ./models ./balanced_blend.safetensors --method sma

```

Every model gets equal vote in the final result. Great for creating stable, well-rounded models.

### 2. ๐Ÿ“ˆ Exponential Moving Average (EMA) - "Recency Bias"

Perfect for: When newer/later models should dominate

```bash

# Favor recent models heavily

python safetensor_merger.py ./models ./modern_blend.safetensors --method ema --alpha 0.8

# Slight preference for recent models

python safetensor_merger.py ./models ./subtle_blend.safetensors --method ema --alpha 0.6

```

Alpha Cheat Sheet:

- 0.1-0.3: Early models dominate

- 0.4-0.6: Balanced with slight recency bias

- 0.7-0.9: Recent models take control

### 3. โš–๏ธ Weighted Moving Average (WMA) - "Custom Control"

Perfect for: When you know exactly which models should contribute most

```bash

# Make first model 50% of the blend

python safetensor_merger.py ./models ./custom_blend.safetensors --method wma --weights "0.5,0.3,0.2"

# Ignore middle model completely

python safetensor_merger.py ./models ./binary_blend.safetensors --method wma --weights "0.5,0,0.5"

```

## ๐ŸŽฏ Real-World Usage Examples

### Creating a Photorealistic Portrait Master

```bash

# Blend 3 portrait models with emphasis on the most detailed one

python safetensor_merger.py ./portrait_models ./portrait_master.safetensors \

--method wma --weights "0.6,0.3,0.1" --device cuda

```

### Evolution-Based Merging

```bash

# Use EMA to create a model that evolves toward your latest training

python safetensor_merger.py ./training_checkpoints ./evolved_model.safetensors \

--method ema --alpha 0.7 --device cuda

```

### Style Transfer Blend

```bash

# Equal parts anime, realistic, and artistic styles

python safetensor_merger.py ./style_models ./triple_style.safetensors \

--method sma --device cuda

```

## โš™๏ธ Command Reference

Basic Syntax:

```bash

python safetensor_merger.py <input_directory> <output_file> [OPTIONS]

```

Key Options:

- --method sma|ema|wma - Choose your blending algorithm

- --alpha 0.1-0.9 - EMA smoothing factor (higher = more recent bias)

- --weights "0.4,0.3,0.3" - Custom WMA weights (comma-separated)

- --device cuda|cpu - Use GPU acceleration when available

## ๐Ÿ’ก Pro Tips for Better Merges

### ๐ŸŽฏ Method Selection Guide:

- Use SMA when all models are equally good and you want balance

- Use EMA when you have a progression of models (training epochs, refinements)

- Use WMA when you have specific models you trust more than others

### โšก Performance Optimization:

- Always use --device cuda if you have a NVIDIA GPU

- For models over 6GB, consider using CPU to avoid memory issues

- Process multiple merges in batch for efficiency

### ๐Ÿงช Experimental Combinations:

Try these interesting weight patterns:

```bash

# "Bookend Blend" - Ignore middle models

--weights "0.4,0,0,0.6"

# "Pyramid Fade" - Gradual decline

--weights "0.5,0.3,0.15,0.05"

# "Focus Burst" - Emphasize one model heavily

--weights "0.1,0.8,0.1"

```

## ๐Ÿ” What You'll See During Merging

The tool provides detailed feedback:

```

๐Ÿ” Discovered 3 models:

โ†’ realistic_model_v2.safetensors

โ†’ anime_style_xl.safetensors

โ†’ artistic_blend.safetensors

๐Ÿงฎ Found 247 common tensors

โš™๏ธ Using EMA with ฮฑ=0.6 on CUDA

๐Ÿš€ Processing: 100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 247/247 [00:15<00:00, 16.2it/s]

โœ… Merge complete!

๐Ÿ“ Saved: ./my_perfect_blend.safetensors

```

## ๐Ÿ”ฌ Technical Details

Supported Tensor Types: All standard formats including F32, F16, BF16, INT8, and scalar tensors

Memory Management: Intelligent chunking prevents OOM errors even with huge models

Metadata Preservation: Every merged model stores complete information about the merge process

## ๐Ÿšจ Troubleshooting

"No models found" โ†’ Make sure your directory contains .safetensors files

"Weight mismatch" โ†’ Count your models and provide the same number of weights

"CUDA unavailable" โ†’ Install CUDA PyTorch or use --device cpu

## ๐ŸŽ‰ Ready to Create Your Perfect Model?

Whether you're blending checkpoint variations, combining different training stages, or experimenting with style transfers, this tool gives you the mathematical precision to create exactly the model you envision.

Download the script, choose your method, and start creating model magic!

---

Have you created an amazing blend? Share your results and method combinations in the comments below! ๐ŸŽจ

## ๐Ÿ“‹ Full Command Examples

```bash

# Quick balanced merge

python safetensor_merger.py ./models ./output.safetensors

# GPU-accelerated EMA blend

python safetensor_merger.py ./models ./ema_blend.safetensors --method ema --alpha 0.7 --device cuda

# Custom weighted merge

python safetensor_merger.py ./models ./custom_blend.safetensors --method wma --weights "0.6,0.25,0.15" --device cuda

# Conservative merge favoring early models

python safetensor_merger.py ./models ./conservative.safetensors --method ema --alpha 0.2

```

Happy merging! ๐Ÿš€

3