Skip to main content

Evolutionary model merging for LLMs

Project description

mergenetic - evolve LLMs

🧪 Mergenetic: Evolutionary Model Merging for LLMs

mergenetic is a flexible library for merging large language models (LLMs) via evolutionary optimization. It frames model merging as a black-box optimization problem and uses techniques like genetic algorithms and smart performance estimators to search for optimal weight combinations — enabling high-performance merges, even on consumer hardware.

✨ Why Mergenetic?

  • State‑of‑the‑art merging techniques – linear soups, SLERP, TIES/DARE, Task Arithmetic and more.
  • Hardware‑friendly – search in parameter space, not gradient space; no model must fit in memory twice.
  • Modular & hackable – plug‑and‑play problems, searchers, mergers and evaluators.
  • Familiar tools under the hood – mergekit for merging, pymoo for optimisation, and lm‑eval‑harness for metrics.

📚 Table of Contents

  1. Installation
  2. Quickstart
  3. Key Concepts
  4. Usage Examples
  5. Project Layout
  6. Learn More
  7. Contributing
  8. Citation
  9. License

🛠️ Installation

conda create --name mergenetic python=3.11 -y
conda activate mergenetic
pip install -r requirements.txt
pip install -e .

Heads‑up: some merge methods require bfloat16 support. Make sure your CUDA / ROCm stack is recent enough.

⚡ Quickstart

The fastest way to see Mergenetic in action is the Colab notebook here Open In Colab

🔑 Key Concepts

1. Merging Strategies

Strategy Multi‑model? Needs base model? Paper
Linear / Model Soups arXiv:2203.05482
SLERP
Task Arithmetic arXiv:2212.04089
TIES arXiv:2306.01708
DARE arXiv:2311.03099

2. Evolutionary Algorithms

Mergenetic wraps every single‑ and multi‑objective optimiser in pymoo – GA, DE, CMA‑ES, NSGA‑II/III and many more. Simply import the one you need:

from pymoo.algorithms.soo.genetic_algorithm import GA
algorithm = GA(pop_size=32)

3. Evaluation & Fitness

  • Native support for LM‑Eval Harness tasks
  • Low‑cost proxies: IRT estimators or random sampling
  • Bring‑your‑own metric by writing a single function

🚀 Usage Examples

Python API

from mergenetic.searcher import Searcher
from mergenetic.optimization.predefined_problems import CrossLingualMathProblem
from mergenetic.merging import SlerpMerger
from mergenetic.utils import ConfigLmEval

config = ConfigLmEval(**yaml.load(open("path/to/config.yaml"), Loader=yaml.FullLoader))

merger = SlerpMerger(...)
problem = CrossLingualMathProblem(...)
algorithm = GA(...)
searcher = Searcher(problem, algorithm, config.path_to_store_config, config.n_iter, config.run_id, config.seed)

searcher.search()
searcher.test()

Command‑Line Interface

python -m mergenetic.cli \
  --merge-type single \
  --eval-method lm-eval \
  --models mistral-7b math-7b \
  --task ita_gsm8k

An interactive wizard will guide you through the remaining options. See cli/README.md for the full reference.

Graphical Interface

Run the Gradio dashboard locally:

cd gui
pip install -r requirements.txt
python3 gui.py

…and configure experiments with dropdowns – no code required! See gui/README.md for the full details.

🗂️ Project Layout

mergenetic/
├── merging/          # adapters around mergekit strategies
├── optimization/     # pymoo problems for various tasks
├── evaluation/       # LM‑Eval & custom fitness functions
├── estimator/        # fast score predictors (IRT, sampling)
├── searcher/         # evolutionary loop orchestration
└── utils/            # config, logging, GPU helpers, …

Detailed docs for each module live in src/mergenetic/README.md.

📒 Learn More

🤝 Contributing

Bug reports, feature requests and pull requests are very welcome! Please read CONTRIBUTING.md before you start.

🧠 Citation

@misc{minut2025mergeneticsimpleevolutionarymodel,
      title={Mergenetic: a Simple Evolutionary Model Merging Library}, 
      author={Adrian Robert Minut and Tommaso Mencattini and Andrea Santilli and Donato Crisostomi and Emanuele Rodolà},
      year={2025},
      eprint={2505.11427},
      archivePrefix={arXiv},
      primaryClass={cs.LG},
      url={https://arxiv.org/abs/2505.11427}, 
}

📄 License

Licensed under the Apache 2.0 licence – see the LICENSE file for details.

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

mergenetic-0.1.1.tar.gz (42.2 kB view details)

Uploaded Source

Built Distribution

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

mergenetic-0.1.1-py3-none-any.whl (51.1 kB view details)

Uploaded Python 3

File details

Details for the file mergenetic-0.1.1.tar.gz.

File metadata

  • Download URL: mergenetic-0.1.1.tar.gz
  • Upload date:
  • Size: 42.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mergenetic-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d117716f843d534c5296b2d869bd9a5d7970e1608b3ca1b4b54c4e9dc85b105c
MD5 6d3b4d7fdb7849b67684b41cd186334e
BLAKE2b-256 a18eef0593bb8c8b6b6390cb05ffa336eac9fdd5182e11eadd69940ec51ec529

See more details on using hashes here.

Provenance

The following attestation bundles were made for mergenetic-0.1.1.tar.gz:

Publisher: publish-to-pypi.yml on tommasomncttn/mergenetic

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

File details

Details for the file mergenetic-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mergenetic-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 51.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mergenetic-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4c9a0e25c24e0fb5aea33653cd9f597830ff992d91c6800420ca2ea21549c981
MD5 6d4ba734ddc1c87106942d8f6a63f0d5
BLAKE2b-256 888a33302c5dd1cb28e9d49b435783cc5dd1b2ab91b94f684bdb50c99b7e0765

See more details on using hashes here.

Provenance

The following attestation bundles were made for mergenetic-0.1.1-py3-none-any.whl:

Publisher: publish-to-pypi.yml on tommasomncttn/mergenetic

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