Tuning Large Language Models for Real-World ApplicationsChapter 54

Step 4: Attach LoRA Adapters

Section 4 of 13-~ 3 min read-Synced from Cuantum content
from peft import LoraConfig, get_peft_model lora_config = LoraConfig(    r=16,    lora_alpha=32,    target_modules=["q_proj", "v_proj"],    lora_dropout=0.05,    bias="none",    task_type="CAUSAL_LM") model = get_peft_model(model, lora_config)model.print_trainable_parameters()

Code Breakdown

  • What LoraConfig is doing
  • LoraConfig defines how LoRA will be applied to the model. Think of it as a blueprint that tells PEFT which weights to augment with small trainable matrices, and how large those matrices should be.
  • This is the key idea of PEFT: instead of training all of Mistral’s parameters, you train a tiny set of adapter parameters that can steer the model’s behavior.
  • The most important hyperparameters
  • r=16
  • The rank of the LoRA update matrices.
  • Higher r means the adapters have more capacity to learn changes, but it increases VRAM usage and training time.
  • In practice, r values like 8, 16, or 32 are common.
  • lora_alpha=32
  • A scaling factor that controls the effective strength of the LoRA update.
  • You will often see lora_alpha set to roughly 2 × r, but it is a tunable parameter.
  • lora_dropout=0.05
  • Dropout applied inside the LoRA adapters during training.
  • This helps reduce overfitting when your dataset is small or repetitive.
  • Where LoRA is attached (target_modules)
  • target_modules=["q_proj", "v_proj"]
  • This tells PEFT to inject LoRA adapters into the query and value projection layers inside each attention block.
  • These layers are a strong default because they are central to how attention “routes” information.
  • You can expand this list in experiments (for example k_proj, o_proj, and some MLP layers), but q_proj and v_proj is a widely used starting point for Mistral/LLaMA-style architectures.
  • Why bias="none"
  • Bias parameters are left untouched.
  • This keeps the adapter as small as possible and is the most common LoRA setting.
  • Why tasktype="CAUSALLM"
  • This tells PEFT the base model is a causal language model (decoder-only), which affects how PEFT configures and validates the adapter setup.
  • Actually applying LoRA to the model
  • model = get_peft_model(model, lora_config) wraps the base model and inserts LoRA layers at the locations you specified.
  • After this point, calling trainer.train() (later in Step 7) will update only adapter weights (and any other parameters explicitly unfrozen).
  • Sanity-check: trainable parameter count
  • model.print_trainable_parameters() prints how many parameters will be trained.
  • You should typically see well under 1% trainable for LoRA on a 7B model.

You should see less than 1% of parameters trainable.

This is efficient adaptation.