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

LoraConfig

The settings object.

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

In PEFT, all of LoRA's settings live in one object, called LoRA config. Then one function adds the adapters to the model. Let's see both, and look at a layer before and after.

  • One settings object
  • One function to add adapters
  • Look at a layer

The LoraConfig fields

Here are the fields. R is the rank. LoRA alpha sets the strength. LoRA dropout fights overfitting. Target modules picks the layers. And task type tells PEFT what kind of model this is. Causal LM means a next token model.

FieldOur value
r8
lora_alpha16
lora_dropout0.05
target_modules["q_proj", "v_proj"]
task_type"CAUSAL_LM"

Add LoRA to the model

We load the model. We write a LoRA config, with our values. Get peft model wraps the model, and adds the adapters. Then we print one q proj layer. Run it. The original linear layer is still there, called the base layer. Next to it, a dropout, then LoRA A, which squeezes five hundred and seventy six numbers down to eight. And LoRA B, which expands eight back up to five hundred and seventy six. Exactly our picture.

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

model = AutoModelForCausalLM.from_pretrained("HuggingFaceTB/SmolLM2-135M-Instruct")
config = LoraConfig(r=8, lora_alpha=16, lora_dropout=0.05,
                    target_modules=["q_proj", "v_proj"], task_type="CAUSAL_LM")
model = get_peft_model(model, config)
print(model.base_model.model.model.layers[0].self_attn.q_proj)
Output
lora.Linear(
  (base_layer): Linear(in_features=576, out_features=576, bias=False)
  (lora_dropout): ModuleDict(
    (default): Dropout(p=0.05, inplace=False)
  )
  (lora_A): ModuleDict(
    (default): Linear(in_features=576, out_features=8, bias=False)
  )
  (lora_B): ModuleDict(
    (default): Linear(in_features=8, out_features=576, bias=False)
  )
  (lora_embedding_A): ParameterDict()
  (lora_embedding_B): ParameterDict()
  (lora_magnitude_vector): ModuleDict()
)

Summary

Let's recap. LoRA config holds the settings. Get peft model adds the adapters. And each target layer keeps its base layer, plus LoRA A, and LoRA B. Next, counting what we'll actually train.

Key takeaways
  • LoraConfig(...)
  • get_peft_model()
  • base_layer + lora_A + lora_B
Next up: get_peft_model — what gets trained