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.
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 AutoModelForCausalLMfrom peft import LoraConfig, get_peft_modelname = "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()
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.
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.
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.