Mergoo
mergoo is a library for easily merging multiple LLM experts, and efficiently train the merged LLM. With mergoo, you can efficiently integrate the knowledge of different generic or domain-based LLM experts.
🚀 Features
- Supports several merging methods: Mixture-of-Experts, Mixture-of-Adapters, and Layer-wise merging
- Flexible merging for each layer
- Base Models supported : Llama(including LLaMa3), Mistral, Phi3, and BERT
- Trainers supported : 🤗 Trainer, SFTrainer, PEFT
- Device Supported: CPU, MPS, GPU
- Training choices: Only Router of MoE layers, or Fully fine-tuning of Merged LLM
If you like the project, consider leaving a ⭐️
Installation
Install by pip:
pip install mergoo
Install latest unstable version on Github:
pip install git+https://github.com/Leeroo-AI/mergoo
Install it from the source:
git clone https://github.com/Leeroo-AI/mergoo
cd mergoo
pip install -e .
Quick Start
Configuration Setup
Specify the config for merging:
model_type: type of base model. choices:mistral,llama, orbert.num_experts_per_token: Number of experts for each token of MoE.experts: config for experts to merge. includesexpert_nameand Hugging Face 🤗model_id.router_layers: layers chosen for applying Mixture-of-Experts.
Fully Fine-tuned Experts
This is a sample config when merging fully fine-tuned LLM experts.
config = {
"model_type": "mistral",
"num_experts_per_tok": 2,
"experts": [
{"expert_name": "base_expert", "model_id": "mistralai/Mistral-7B-v0.1"},
{"expert_name": "expert_1", "model_id": "meta-math/MetaMath-Mistral-7B"},
{"expert_name": "expert_2", "model_id": "ajibawa-2023/Code-Mistral-7B"}
],
"router_layers": ["gate_proj", "up_proj", "down_proj"]
}
For the above example, we merged math and code mistral-based experts. Please refer to this notebook for further details!
Mixture of Adapters (MoE on LoRA)
This is a sample config when merging LoRA fine-tuned LLM experts. mergoo builds a routing layer on top of LoRAs, resulting in a mixture of adapters.
config = {
"model_type": "mistral",
"num_experts_per_tok": 2,
"base_model": "mistralai/Mistral-7B-v0.1",
"experts": [
{"expert_name": "adapter_1", "model_id": "predibase/customer_support"},
{"expert_name": "adapter_2", "model_id": "predibase/customer_support_accounts"},
{"expert_name": "adapter_3", "model_id": "predibase/customer_support_orders"},
{"expert_name": "adapter_4", "model_id": "predibase/customer_support_payments"}
],
}
The expert_name starts with adapter instead of expert. Please refer to this notebook for further details!
Merge Experts
Following the config setup, mergoo creates the merged LLM as:
import torch
from mergoo.compose_experts import ComposeExperts
# create checkpoint
model_id = "data/mistral_lora_moe"
expertmerger = ComposeExperts(config, torch_dtype=torch.float16)
expertmerger.compose()
expertmerger.save_checkpoint(model_id)
Load / Finetune Merged Expert
Now, you can easily train the merged LLM with Hugging Face Trainer:
from transformers import Trainer
from mergoo.models.modeling_mistral import MistralForCausalLM
model = MistralForCausalLM.from_pretrained("data/mistral_lora_moe")
# NOTE: 'gate' / router layers are untrained hence weight loading warning would appeare for them
trainer = Trainer( ... )
trainer.train()
📚 Learn More:
After finishing the Quick Start guide, you can explore the tutorials below to further familiarize yourself with mergoo.
| Notebook | Details |
|---|---|
| MoE with fully fine-tuned LLM experts | Build a unifined Mixture-of-Experts model with fully fine-tuned experts. Inspired by BTX Research (Meta AI). |
| MoE with LoRA fine-tuned experts | Build a Mixture of Adaptes expert. Inspired by xlora | Mixture-of-LoRAs | MoLE | PHATGOOSE | MoELoRA |
| Hugging Face Blog | Deep dive into research details behind the merging methods of mergoo library |
| LLaMa3-based Experts | Build your own MoE-style LLM experts by integrating LLaMa3-based domain experts |
| Phi3-based Experts | Create MoE-style LLM architecture by merging Phi3-based fine-tuned models |
Mergoo Roadmap and Contributing
As an open-source library in a fast evolving domain, we welcome contributions, whether it is introducing new features, enhancing infrastructure, or improving documentation.
Here is mergoo roadmap:
- Support MoE for Transformer Block
- Compatibility with Huggingface 🤗
- Support Trainer, SFTrainer
- Loading Unified Checkpoint in BTX
- Feature: Convertible QKV linear layers
- Feature: Convertible FF linear layers
- Feature: Routers only for a list of decoder layers indexes
- Sharded Safetensor Saving
- Support experts based on LLaMa and Mistral
- Support experts based on Phi3
- Support Mixture of LORA Experts (Mixture of Adapters)
- Router Load balancing loss
- Lazy loading of tensors for low memory usage in Merging
- Support other Layer-wise merging methods, including Mergekit
- Support experts based on Gemma and Mamba
- Support flash-attention
- Support Mixture of Depths Transformer
Feel free to suggest new features and/or contribute to mergoo roadmap!
Join our community!
🚀 We love to here your feedback, please join Leeroo community:
Have a question not listed here? Open a GitHub Issue or send us an email!
Metadata
Release files for mergoo 0.0.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mergoo-0.0.10.tar.gz | 82.2 kB | Without provenance | Details for mergoo-0.0.10.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mergoo-0.0.10-py3-none-any.whl | Python 3 | none | any | Details for mergoo-0.0.10-py3-none-any.whl |
Total release size: 166.4 kB
Release files / mergoo-0.0.10.tar.gz
| Download URL | mergoo-0.0.10.tar.gz |
|---|---|
| Size | 82.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
47d300b24df0c0b2dc3589ee95b6a63515c835fb0877651ef0d4678cb9c68a89
|
|
BLAKE2b-256 checksum How to use checksums |
7e6f473464a146ea6c08e72876c1f04b6681be7d3da4a6844f63b73510c1d56f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Release files / mergoo-0.0.10-py3-none-any.whl
| Download URL | mergoo-0.0.10-py3-none-any.whl |
|---|---|
| Size | 84.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
452d39875577710a11abd579b8233e0d436c7a3cfffdbade2bb5773de56aaf86
|
|
BLAKE2b-256 checksum How to use checksums |
6405f231cc63d6033193d92f94965317212cc33379753f15f1e511f5c546a610
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Security for mergoo 0.0.10
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.