FusionBench Skill
FusionBench is a comprehensive benchmark/toolkit for deep model fusion (model merging).
Paper: arXiv:2406.03280
PyPI: pip install fusion-bench
Repo: https://code.tanganke.com/tanganke/fusion_bench
Docs: https://tanganke.github.io/fusion_bench/
Quick Start
CODEBLOCK0
Architecture Overview
CODEBLOCK1
Key Components
- 1. ModelPool: Loads and manages pre-trained/fine-tuned models
-
AutoModelPool: Auto-selects based on config
-
CLIPVisionModelPool: For CLIP ViT models
-
CausalLMPool: For Llama, GPT-2, etc.
- 2. Method: The merging algorithm
- Inherits from
BaseModelFusionAlgorithm
- Implements
run(modelpool) → merged model
- 3. TaskPool: Evaluation tasks
- CLIP: 8-38 classification tasks
- LLM: ARC, HellaSwag, MMLU, etc.
Supported Merging Methods
Basic
| Method | Config Name | Description |
|---|
| Simple Average | INLINECODE6 | Uniform weight averaging |
| Weighted Average |
weighted_average | Learnable task weights |
| Task Arithmetic |
task_arithmetic | task_vector = fine-tuned - base |
| Slerp |
slerp | Spherical interpolation |
Sparse/Pruning
| Method | Config Name | Description |
|---|
| TIES | INLINECODE10 | Trim, Elect, Sign + merge |
| DARE |
dare | Drop And REscale |
| Magnitude Pruning |
magnitude_pruning | Prune by magnitude |
Advanced
| Method | Config Name | Description |
|---|
| AdaMerging | INLINECODE13 | Learn layer-wise coefficients |
| Fisher Merging |
fisher_merging | Fisher-weighted merging |
| RegMean |
regmean | Regression mean (closed-form) |
| RegMean++ |
regmean_plusplus | Enhanced RegMean with cross-layer deps |
MoE-Based
| Method | Config Name | Description |
|---|
| WE-MoE | INLINECODE17 | Weight Ensembling MoE |
| PWE-MoE |
pwe_moe | Pareto-optimal WE-MoE |
| RankOne-MoE |
rankone_moe | Rank-1 expert decomposition |
| Sparse-WE-MoE |
sparse_we_moe | Sparse weight ensembling |
Continual Merging
| Method | Config Name | Description |
|---|
| OPCM | INLINECODE21 | Orthogonal Projection Continual Merging |
| DOP |
dop | Dual Orthogonal Projection |
| Gossip |
gossip | Gossip-based continual merging |
Specialized
| Method | Config Name | Description |
|---|
| ISO-C/CTS | INLINECODE24 | Isotropic merging in common/task subspace |
| AdaSVD |
ada_svd | SVD-based adaptive merging |
| WUDI |
wudi | Wasserstein distance merging |
| ExPO |
expo | Exponential task vectors |
Running Experiments
1. Basic Merging (CLI)
CODEBLOCK2
2. LLM Merging
CODEBLOCK3
3. Using Fabric (Distributed/Mixed Precision)
CODEBLOCK4
Adding a New Method
Step 1: Create method file
CODEBLOCK5
Step 2: Register in __init__.py
CODEBLOCK6
Step 3: Create config
CODEBLOCK7
Step 4: Run
CODEBLOCK8
Model Pool Configuration
CLIP Models
CODEBLOCK9
LLM Models
CODEBLOCK10
Utilities
State Dict Arithmetic
CODEBLOCK11
Lazy State Dict
CODEBLOCK12
Common Workflows
1. Evaluate a single merged model
CODEBLOCK13
2. Hyperparameter search
CODEBLOCK14
3. Compare multiple methods
CODEBLOCK15
Tips
- 1. Memory: Use
fabric=deepspeed_stage_2 for large models - Caching: Models are cached in INLINECODE30
- Reproducibility: Set
seed=42 in config - Debugging: Use
hydra.verbose=true for detailed logs - Web UI: Run
fusion_bench_webui for interactive exploration
Related Papers
- 1. FusionBench (arXiv:2406.03280) - The benchmark paper
- SMILE (arXiv:2408.10174) - Sparse MoE from pre-trained models
- WE-MoE - Weight Ensembling MoE for multi-task merging
- OPCM/DOP - Continual model merging methods
- RegMean++ (arXiv:2508.03121) - Enhanced RegMean
FusionBench 技能
FusionBench 是一个用于深度模型融合(模型合并)的综合基准测试/工具包。
论文: arXiv:2406.03280
PyPI: pip install fusion-bench
仓库: https://code.tanganke.com/tanganke/fusion_bench
文档: https://tanganke.github.io/fusion_bench/
快速开始
bash
安装
pip install fusion-bench
运行简单实验(CLIP ViT-B/32,8个任务上的任务算术)
fusion
bench method=taskarithmetic modelpool=clip-vit-base-patch32 taskpool=clip-vit-base-patch32_8tasks
使用不同的合并方法运行
fusion
bench method=tiesmerging modelpool=clip-vit-base-patch32 taskpool=clip-vit-base-patch32_8tasks
架构概览
fusion_bench/
├── method/ # 合并算法(30+)
├── modelpool/ # 模型加载与管理
├── config/ # Hydra YAML 配置
├── tasks/ # 任务评估
├── utils/ # 辅助工具(state_dict 操作、懒加载等)
└── scripts/ # CLI 和 Web UI
核心组件
- 1. ModelPool:加载和管理预训练/微调模型
- AutoModelPool:根据配置自动选择
- CLIPVisionModelPool:用于 CLIP ViT 模型
- CausalLMPool:用于 Llama、GPT-2 等
- 2. Method:合并算法
- 继承自 BaseModelFusionAlgorithm
- 实现 run(modelpool) → 合并后的模型
- 3. TaskPool:评估任务
- CLIP:8-38 个分类任务
- LLM:ARC、HellaSwag、MMLU 等
支持的合并方法
基础方法
| 方法 | 配置名称 | 描述 |
|---|
| 简单平均 | simpleaverage | 均匀权重平均 |
| 加权平均 |
weightedaverage | 可学习的任务权重 |
| 任务算术 | task_arithmetic | 任务向量 = 微调 - 基础 |
| Slerp | slerp | 球面插值 |
稀疏/剪枝方法
| 方法 | 配置名称 | 描述 |
|---|
| TIES | ties_merging | 修剪、选择、符号 + 合并 |
| DARE |
dare | 丢弃并重新缩放 |
| 幅度剪枝 | magnitude_pruning | 按幅度剪枝 |
高级方法
| 方法 | 配置名称 | 描述 |
|---|
| AdaMerging | adamerging | 学习逐层系数 |
| Fisher Merging |
fisher_merging | Fisher 加权合并 |
| RegMean | regmean | 回归均值(闭式解) |
| RegMean++ | regmean_plusplus | 增强版 RegMean,带跨层依赖 |
基于 MoE 的方法
| 方法 | 配置名称 | 描述 |
|---|
| WE-MoE | wemoe | 权重集成 MoE |
| PWE-MoE |
pwemoe | 帕累托最优 WE-MoE |
| RankOne-MoE | rankone_moe | 秩-1 专家分解 |
| Sparse-WE-MoE | sparse
wemoe | 稀疏权重集成 |
持续合并方法
| 方法 | 配置名称 | 描述 |
|---|
| OPCM | opcm | 正交投影持续合并 |
| DOP |
dop | 双正交投影 |
| Gossip | gossip | 基于 Gossip 的持续合并 |
专用方法
| 方法 | 配置名称 | 描述 |
|---|
| ISO-C/CTS | isotropicmerging | 公共/任务子空间中的各向同性合并 |
| AdaSVD |
adasvd | 基于 SVD 的自适应合并 |
| WUDI | wudi | Wasserstein 距离合并 |
| ExPO | expo | 指数任务向量 |
运行实验
1. 基础合并(CLI)
bash
CLIP ViT-B/32 上的任务算术
fusion_bench \
method=task_arithmetic \
modelpool=clip-vit-base-patch32 \
taskpool=clip-vit-base-patch32_8tasks
带自定义缩放的 TIES 合并
fusion_bench \
method=ties_merging \
method.scaling_coefficient=0.3 \
modelpool=clip-vit-base-patch32 \
taskpool=clip-vit-base-patch32_8tasks
2. LLM 合并
bash
合并 Llama 模型
fusion_bench \
method=task_arithmetic \
modelpool=llama2-7b \
taskpool=llama2-7b_tasks
使用 DARE
fusion_bench \
method=dare \
method.type=task_arithmetic \
modelpool=llama2-7b
3. 使用 Fabric(分布式/混合精度)
bash
fusion_bench \
fabric=deepspeedstage2 \
method=adamerging \
modelpool=clip-vit-base-patch32
添加新方法
步骤 1:创建方法文件
python
fusionbench/method/mymethod.py
from fusion
bench.method.basealgorithm import BaseModelFusionAlgorithm
from fusion_bench.modelpool import BaseModelPool
import torch
class MyMergingAlgorithm(BaseModelFusionAlgorithm):
我的自定义合并算法。
def init(self, scaling_coefficient: float = 1.0, kwargs):
super().init(kwargs)
self.scalingcoefficient = scalingcoefficient
@torch.no_grad()
def run(self, modelpool: BaseModelPool):
# 1. 加载基础模型
basemodel = modelpool.loadmodel(base)
basesd = basemodel.state_dict()
# 2. 计算合并后的任务向量
merged_tv = {}
for modelname in modelpool.modelnames:
if modelname == base_:
continue
model = modelpool.loadmodel(modelname)
tv = {k: v - basesd[k] for k, v in model.statedict().items()}
# 在此处编写合并逻辑
for k in tv:
if k not in merged_tv:
mergedtv[k] = tv[k] * self.scalingcoefficient
else:
mergedtv[k] += tv[k] * self.scalingcoefficient
# 3. 应用合并后的任务向量
for k in base_sd:
basesd[k] += mergedtv.get(k, 0)
basemodel.loadstatedict(basesd)
return base_model
步骤 2:在 init.py 中注册
python
fusion_bench/method/init.py
importstructure = {
...
my_method: [MyMergingAlgorithm],
}
步骤 3:创建配置
yaml
config/method/my_method.yaml
target: fusion
bench.method.mymethod.MyMergingAlgorithm
scaling_coefficient: 1.0
步骤 4:运行
bash
fusionbench method=mymethod modelpool=clip-vit-base-patch32
模型池配置
CLIP 模型
yaml
config/modelpool/clip-vit-base-patch32.yaml
target: fusion_bench.modelpool.CLIPVisionModelPool
model_names:
-
base
- Cars
- DTD
- EuroSAT
- GTSRB
- MNIST
- RESISC45
- SUN397
- SVHN
model
dir: ${oc.env:HOME}/.cache/fusionbench/models
LLM 模型
yaml
config/modelpool/llama2-7b.yaml
target: fusion_bench.modelpool.CausalLMPool
model_names:
-
base
- arc
- hellaswag
- mmlu
model
dir: ${oc.env:HOME}/.cache/fusionbench/llama_models
工具函数
状态字典算术
python
from fusionbench.utils.statedict_arithm