Ask anything about this article
Hi! I've read this article.
What would you like to know?
@farhan
Fine-tuning an LLM means taking a pre-trained model and training it further on your specific data to make it better at a particular task. But here's the secret most tutorials won't tell you: you probably don't need to fine-tune at all.
Full fine-tuning of a 7B parameter model requires ~28GB of GPU memory. That's expensive. LoRA (Low-Rank Adaptation) solves this by freezing the original model weights and training small adapter layers instead.
Original Weight Matrix (frozen): W ∈ R^(d×d)
LoRA Adapters: A ∈ R^(d×r), B ∈ R^(r×d) where r << d
Modified forward pass: y = Wx + BAx
With rank r=8 and d=4096, you're training 65K parameters instead of 16M — a 250x reduction.
QLoRA takes it further by quantizing the frozen weights to 4-bit precision, allowing you to fine-tune a 65B model on a single 48GB GPU.
python# Format: instruction-input-output
dataset = [
{
"instruction": "Classify the sentiment of this review",
"input": "The product arrived damaged and customer service was unhelpful",
"output": "Negative"
},
{
"instruction": "Classify the sentiment of this review",
"input": "Absolutely love this! Best purchase I've made all year",
"output": "Positive"
}
]
pythonfrom transformers import AutoModelForCausalLM, AutoTokenizer
from peft import LoraConfig, get_peft_model
from trl import SFTTrainer
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-2-7b-hf",
load_in_4bit=True
)
lora_config = LoraConfig(
r=16,
lora_alpha=32,
target_modules=["q_proj", "v_proj"],
lora_dropout=0.05,
task_type="CAUSAL_LM"
)
model = get_peft_model(model, lora_config)
print(f"Trainable params: {model.print_trainable_parameters()}")
pythontrainer = SFTTrainer(
model=model,
train_dataset=train_dataset,
args=TrainingArguments(
output_dir="./results",
num_train_epochs=3,
per_device_train_batch_size=4,
learning_rate=2e-4,
warmup_steps=100
)
)
trainer.train()
model.save_pretrained("./fine-tuned-model")
| Method | GPU Required | Cost (Cloud) | Training Time |
|---|---|---|---|
| Full Fine-Tune (7B) | A100 80GB | ~$15/hour | 4-8 hours |
| LoRA (7B) | A100 40GB | ~$8/hour | 2-4 hours |
| QLoRA (7B) | T4 16GB | ~$1/hour | 4-6 hours |