Sub topic 12
Saving, loading and merging adapters
This chapter assumes you have trained a LoRA adapter with SFTTrainer (see the previous chapters) and can run a Hugging Face transformers model for inference. After training, you have two ways to ship the result: keep the adapter separate from the base model, or merge it into the base weights.
What gets saved
A PEFT model saves only the adapter. The base model weights are not written. For a rank-16 adapter on a small model, the files are typically a few tens of megabytes, while the full model is gigabytes.
The adapter folder contains two main files:
adapter_config.jsonrecords the rank, alpha, dropout, target modules, and the name of the base model.adapter_model.safetensorsholds the trained A and B matrices.
The adapter is useless alone. It needs the same base model it was trained on.
Loading an adapter
To use an adapter, load the base model first. Then attach the adapter with PeftModel.from_pretrained. The adapter stays separate, so the base model can serve other adapters later.
Merging an adapter
Merging folds the LoRA update into the base weights. For each targeted layer, the merged weight is W + (alpha/r)BA. The result is an ordinary transformers model. It needs no PEFT library at inference time and adds no extra latency from the adapter path.
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
base_id = "Qwen/Qwen2.5-0.5B-Instruct"
base = AutoModelForCausalLM.from_pretrained(base_id)
model = PeftModel.from_pretrained(base, "lora-out/adapter")
merged = model.merge_and_unload()
merged.save_pretrained("merged-model")
AutoTokenizer.from_pretrained(base_id).save_pretrained("merged-model")merge_and_unload() adds the update to the weights and removes the adapter modules. It returns the base model object with the merged weights. Save the tokenizer next to the model so the folder is self-contained.
Adapter or merged model?
| Aspect | Separate adapter | Merged model |
|---|---|---|
| Size on disk | Small (megabytes) | Full model size |
| Needs PEFT at inference | Yes | No |
| Swap tasks | Load a different adapter | Load a different full model |
| Inference speed | Slightly slower unless merged | Same as the base model |
| Can be undone | Yes, drop the adapter | No, unless you keep the original |
Keep the adapter if you serve several tasks from one base model. Merge it if you deploy a single task and want the simplest runtime.
Common errors
- Wrong base model. Loading an adapter onto a different base, or a different revision, gives shape errors or silently poor output. Check
base_model_name_or_pathinadapter_config.json. - Missing tokenizer.
save_pretrainedon the merged model does not write the tokenizer. Save it yourself, as in the example above. - Merging into a quantized base. If you trained with QLoRA, the base is in 4-bit. Merging directly into 4-bit weights loses precision. The usual approach is to reload the base in 16-bit (for example
torch_dtype=torch.bfloat16), attach the adapter, then merge. - Overwriting the only copy. A merge cannot be reversed. Keep the adapter folder.
Exercise
You trained a QLoRA adapter on a 4-bit base and want a standalone model for deployment. How should you merge it, and why?
Show answer
Reload the base model in 16-bit precision (such as bfloat16) instead of 4-bit, attach the adapter with PeftModel.frompretrained, then call mergeand_unload and save the model and tokenizer. Merging into 4-bit weights would round the merged values and lose precision, while a 16-bit base keeps the update accurate.
Exercise
Your team serves five different tasks from one base model, each with its own adapter. Should you merge each adapter into its own full model? Give one reason for your answer.
Show answer
Usually no. Keeping five small adapters on one shared base saves disk and memory, since you store the large base once and swap adapters per task. Five merged models would store five full copies of the base weights.