Skip to main content

Efficiently select and merge expert LoRAs at Test-Time

Project description

Test-Time Model Merging (TTMM)

A library for efficiently selecting and merging expert LoRAs at Test-Time

Documentation

Please cite our work if you use this library in your research (bibtex below):

Installation

pip install ttmerge

Usage Example

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from sentence_transformers import SentenceTransformer
from ttmerge import TestTimeMergingModel

# 1. Load base components
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-1B")
base_model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-1B")
encoder = SentenceTransformer("all-mpnet-base-v2")

# 2. Load normalized corpus embeddings that represent expert domains
# Each row corresponds to an expert: row 0 is the normalized embedding vector of expert 0, row 1 is for expert 1, etc.
# Typically, you would load these pre-computed embeddings from a file
corpus_normalised_embeddings = torch.load("path/to/corpus_embeddings.pt")  # Shape: [n_experts, embedding_dim]

# Directory containing numbered subdirectories (0/, 1/, etc.) where expert LoRAs are stored.
# The individual expert LoRAs should be in the format that the PEFT library uses, with each directory containing adapter_config.json and adapter_model.safetensors files.
adapter_location = "path/to/adapters" 

# 3. Initialize the TestTimeMergingModel
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
merging_model = TestTimeMergingModel(
    corpus=corpus_normalised_embeddings,
    tokenizer=tokenizer,
    encoder=encoder,
    base_model=base_model,
    device=device,
    adapter_location="path/to/adapters"
    verbose=True
)

# 4. Generate text using relevant expert models.
prompt = "Quantum computing is"
input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)

# The model automatically selects and merges the most relevant expert adapters
generated_ids = merging_model.generate(
    input_ids,
    max_length=512,
    temperature=0.7,
    do_sample=True
)

generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
print(generated_text)

Development

CI checks

  • The code is auto-formatted using black ..
  • Static type checks can be run using pyright.
  • Tests can be run using pytest test.

Citation


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

ttmerge-0.0.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

ttmerge-0.0.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file ttmerge-0.0.1.tar.gz.

File metadata

  • Download URL: ttmerge-0.0.1.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.11.5 Darwin/24.0.0

File hashes

Hashes for ttmerge-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e7fbf2e95b1c537f7a1339e05d3248c9862d4ce6b36d57031ad1be4ce83dfe94
MD5 9f693ce3247051f2c3dbeeee305e1c41
BLAKE2b-256 41f139d2de25c4e6ca1fa4bb2a0bcdedc26d52788b0fd2a4060eef14d25f0def

See more details on using hashes here.

File details

Details for the file ttmerge-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: ttmerge-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.11.5 Darwin/24.0.0

File hashes

Hashes for ttmerge-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a8e242ef335ca3353acdb6d785f3141a32849e4eb84fb13f8be828ca92b782e7
MD5 6db0f4af13ba60ddf7d331c6c2f6627f
BLAKE2b-256 3bbf31be872eb25bc23e9a800ab52c0a060edece8097163ed88c5e7191078adc

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