PEFTthe adapter library

Topic 44 of 90Module 5: Local AI Ecosystem2 min read

You know the methods (Topics 22–23); PEFT is Hugging Face's implementation of them, and you've already called it (LoraConfig, get_peft_model). What completes the picture is its lifecycle API — the verbs of adapter management:

from peft import PeftModel
 
model = PeftModel.from_pretrained(base, "you/your-adapter")  # attach
model.merge_and_unload()                                     # Topic 23's merge
model.load_adapter("you/other-adapter", adapter_name="b")    # wardrobe...
model.set_adapter("b")                                       # ...switch outfits

An adapter on disk is two tiny files — adapter_model.safetensors + adapter_config.json — which is why they're shareable, hot-swappable, and checkpoint-cheap (Topic 26's dividend). Beyond LoRA, the same LoraConfig carries the modern refinements as flags (use_dora=True for Topic 22's DoRA), and the library implements the rest of the family tree (IA³, prompt tuning) under one interface. One genuinely fun advanced verb: add_weighted_adapter — arithmetic between adapters, blending, say, 0.7 × your-style-adapter + 0.3 × json-adapter into a new one. Task vectors as a product feature.

Summary

The standard library turning Module 3's PEFT theory into a small, consistent API: configure, attach, switch, merge, blend.

Mental model

The wardrobe's management system — hangers, tags, and a tailor who can sew two outfits into one.

Mistakes to avoid

Merging when you didn't need to (you lose the swap/stack flexibility; merge only for deployment), and version drift — PEFT/transformers/TRL move fast together, so pin versions per project or enjoy mysterious breakage.

Exercise

Load your Lesson 6 adapter with PeftModel.from_pretrained, generate with it, call model.disable_adapter() (a context manager), and generate again. Toggling your fine-tune on and off around a frozen base, live, makes Topic 22 tactile in a way no diagram can.