AI Hints
Home›Fine-Tuning LLMs Tutorial›LoRA with PEFT
Fine-Tuning LLMs · Lesson 45 of 120

get_peft_model

What gets trained.

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

After get peft model, which parameters will actually train? PEFT freezes everything else for us. Let's prove it, and compare our two choices of target modules.

  • Which parameters train?
  • PEFT freezes the rest
  • Compare two choices

Trainable parameters

We try both choices, in a loop. Each time, we load a fresh model, add LoRA, and call print trainable parameters. Run it. With q proj and v proj, we train four hundred and sixty thousand parameters. That's a third of one percent, exactly what our calculator said. With all linear, about two point four million. Still under two percent. Every other weight is frozen.

app.pypython
from transformers import AutoModelForCausalLM
from peft import LoraConfig, get_peft_model

name = "HuggingFaceTB/SmolLM2-135M-Instruct"
for targets in [["q_proj", "v_proj"], "all-linear"]:
    model = AutoModelForCausalLM.from_pretrained(name)
    config = LoraConfig(r=8, lora_alpha=16, lora_dropout=0.05,
                        target_modules=targets, task_type="CAUSAL_LM")
    model = get_peft_model(model, config)
    print(targets)
    model.print_trainable_parameters()
Output
['q_proj', 'v_proj']
trainable params: 460,800 || all params: 134,975,808 || trainable%: 0.3414
all-linear
trainable params: 2,442,240 || all params: 136,957,248 || trainable%: 1.7832

Under the hood

What did get peft model do? It set requires grad to false on every original weight. So PyTorch never computes their gradients, and the optimizer never stores states for them. Only the LoRA A and B weights have requires grad set to true. That's where all the memory savings come from.

keyvalue
Original weightsrequires_grad = False
No gradientsno Adam states
LoRA A and Brequires_grad = True
Resulthuge memory savings

Quick check

Let's check. Which target modules setting usually learns best? Pause, and think about it. The answer is C, all linear. It puts adapters on every linear layer, except the LM head.

Quick check
Which target_modules setting usually learns best?

Summary

Let's recap. Get peft model freezes the original weights. Print trainable parameters shows what will train. And even all linear trains under two percent of our model. Next, preparing data for instruction tuning.

Key takeaways
  • Originals frozen
  • print_trainable_parameters()
  • all-linear: < 2%
Next up: Instruction Data Formats — how examples are written