AI Hints
Home›Fine-Tuning LLMs Tutorial›Fast Inference, Many Adapters and Costs
Fine-Tuning LLMs · Lesson 101 of 120

Serving Many Adapters

One model, many skills.

⏱ 2 min read▶ Video at 2:35:32🎓 Free
Watch this lesson in the Fine-Tuning LLMs courseWatch on YouTube ↗Full Fine-Tuning LLMs course

Earlier, we switched between two adapters, one request at a time. But a real server gets requests for different adapters, all mixed together. Can one batch use different adapters?

  • Switching one at a time
  • Mixed requests
  • One batch, many adapters?

A mixed batch

Yes, it can. We load both adapters onto one base model. Each job names an adapter, and a message. We build one batch, with the right system message for each. Then adapter names tells generate which adapter to use, for each row. Run it. In one batch, ticket rows get JSON, and reply rows get friendly answers.

app.pypython
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
NAME = "HuggingFaceTB/SmolLM2-135M-Instruct"
tok = AutoTokenizer.from_pretrained(NAME, padding_side="left")
model = PeftModel.from_pretrained(AutoModelForCausalLM.from_pretrained(NAME),
                                  "ticket-lora", adapter_name="tickets")
model.load_adapter("reply-lora", adapter_name="reply")
model.eval()
SYSTEMS = {"tickets": "Read the customer's message. Reply with JSON: the intent and the book.",
           "reply": "Write a short, friendly reply to the customer."}
jobs = [("tickets", "Is Dune in stock?"), ("reply", "Is Dune in stock?"),
        ("tickets", "My Circe arrived torn."), ("reply", "My Circe arrived torn.")]
chats = [[{"role": "system", "content": SYSTEMS[a]}, {"role": "user", "content": m}]
         for a, m in jobs]
ids = tok.apply_chat_template(chats, add_generation_prompt=True, padding=True,
                              return_dict=True, return_tensors="pt")
out = model.generate(**ids, max_new_tokens=30, do_sample=False, adapter_names=[a for a, m in jobs])
for (a, m), row in zip(jobs, out):
    print(f"{a:8}", tok.decode(row[ids["input_ids"].shape[1]:], skip_special_tokens=True)[:76])
Output
tickets  {"intent": "stock", "book": "Dune"}
reply    Good question! I'm checking our shelves for Dune now, and I'll confirm by em
tickets  {"intent": "complaint", "book": "Circe"}
reply    I'm so sorry about your Circe. I've passed this to our manager, who will con
Also uses _setup.py
import shutil
shutil.copytree('/Users/hussain/Docker Complete Course/shared-data/ft_cache/ticket-lora', 'ticket-lora')
shutil.copytree('/Users/hussain/Docker Complete Course/shared-data/ft_cache/reply-lora', 'reply-lora')

Why it matters

This is how companies serve hundreds of fine-tuned models cheaply. One base model sits in memory. Each customer, or task, has its own small adapter. And requests for different adapters share the same batch. VLLM, and other servers, do this automatically.

keyvalue
Memoryone base model
Per taska small adapter
Batchesmixed adapters
ServersvLLM: --enable-lora

Summary

Let's recap. One base model can hold many adapters. Adapter names mixes them in one batch. And this is how large services run many fine-tuned models cheaply. Next, what fine-tuning really costs.

Key takeaways
  • Many adapters, one base
  • adapter_names=[...]
  • Cheap at scale
Next up: Costs — time and money