Sub topic 13
Serving adapters and merged models
This chapter assumes you have trained a LoRA adapter, saved it, and can load models with transformers and peft. It covers the two ways to deploy the result and how to choose between them.
Two deployment options
After training you have a small adapter directory and the original base model. You can serve them in either of two ways.
- Merged model: the adapter is folded into the base weights, giving one standalone model. Inference has no PEFT dependency and no extra latency.
- Separate adapter: the base model stays loaded and the adapter is applied on top. You can keep several adapters, one per task or customer, and share one copy of the base weights.
Serving a merged model
The merge from the previous chapter produces a normal Hugging Face model directory. Load it as you would any other model.
from transformers import AutoModelForCausalLM, AutoTokenizer
path = "merged-model"
tokenizer = AutoTokenizer.from_pretrained(path)
model = AutoModelForCausalLM.from_pretrained(path)
messages = [{"role": "user", "content": "Summarize: LoRA trains small low-rank matrices."}]
inputs = tokenizer.apply_chat_template(
messages, add_generation_prompt=True, return_tensors="pt", return_dict=True
)
output = model.generate(**inputs, max_new_tokens=64)
print(tokenizer.decode(output[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))Because the merged model is a plain checkpoint, inference engines such as vLLM or Text Generation Inference can load it directly.
Serving without merging
With PEFT you can load the base model once and attach several adapters by name. Switching the active adapter is cheap because only the small matrices change.
from transformers import AutoModelForCausalLM
from peft import PeftModel
base = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
model = PeftModel.from_pretrained(base, "lora-out/adapter", adapter_name="summarizer")
model.load_adapter("other-adapter", adapter_name="support")
model.set_adapter("summarizer")
# ... generate with the summarizer behavior ...
model.set_adapter("support")
# ... generate with the support behavior ...The path other-adapter is a placeholder for a second adapter trained on the same base model. Every adapter must have been trained against exactly this base model.
Production servers go further. vLLM, for example, can load several LoRA adapters and batch requests for different adapters together. Check the current documentation for the flags, since they change between versions.
Trade-offs
| Concern | Merged model | Separate adapters |
|---|---|---|
| Latency | Same as the base model | Small extra matrix multiplications per layer |
| Storage per variant | Full model copy | Adapter only, usually megabytes |
| Dependencies | Transformers only | PEFT or a server with LoRA support |
| Switching behavior | Load another model | Change the active adapter |
| Rollback | Keep the old checkpoint | Swap the adapter file |
Common mistakes
- Mismatched base model. An adapter loaded onto a different base, or a different revision of it, loads without complaint if the shapes match but produces poor output. Record the base model ID and revision next to each adapter.
- Merging into a quantized base. If you trained with QLoRA, merging into 4-bit weights loses precision. Reload the base in 16-bit precision, then merge the adapter into it.
- Missing tokenizer. Save the tokenizer with the merged model, and use the same chat template that you used in training.
Exercise
You serve 20 customers, each with their own adapter trained on the same base model. Why might you avoid merging, and what is the cost of that choice?
Show answer
Merging would create 20 full model copies, each needing its own GPU memory and storage. Keeping one base model and 20 small adapters shares the base weights and lets you switch adapters per request. The cost is a little extra computation per layer and a dependency on PEFT or a server that supports LoRA adapters.