C.W.K.
Stream
Lesson 04 of 05 · published

Complete QLoRA Pipeline

~28 min · qlora, end-to-end, wandb, pipeline

Level 0Observer
0 XP0/43 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

The end-to-end recipe

Everything from previous lessons combined into a single, production-shaped script. Quantization → tokenizer → LoRA → SFTConfig → Trainer → save. This is what a real fine-tuning project looks like.

Code

Production QLoRA pipeline (Llama 3.1 8B + W&B)·python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import LoraConfig, prepare_model_for_kbit_training
from trl import SFTConfig, SFTTrainer
from datasets import load_dataset

# ── Config ──
model_id = "meta-llama/Llama-3.1-8B-Instruct"
output_dir = "./llama-3.1-8b-finetuned"

# ── 4-bit quantization ──
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16,
    bnb_4bit_use_double_quant=True,
)

# ── Load model + tokenizer ──
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    quantization_config=bnb_config,
    device_map="auto",
    attn_implementation="flash_attention_2",
)
model = prepare_model_for_kbit_training(model)

tokenizer = AutoTokenizer.from_pretrained(model_id)
if tokenizer.pad_token is None:
    tokenizer.pad_token = tokenizer.eos_token

# ── LoRA ──
peft_config = LoraConfig(
    r=32, lora_alpha=64,
    target_modules="all-linear",
    lora_dropout=0.05, bias="none",
    task_type="CAUSAL_LM",
)

# ── Dataset ──
dataset = load_dataset("json", data_files={
    "train":      "train.jsonl",
    "validation": "val.jsonl",
})

# ── Training config ──
args = SFTConfig(
    output_dir=output_dir,
    num_train_epochs=3,
    per_device_train_batch_size=2,
    gradient_accumulation_steps=8,    # effective batch = 16
    learning_rate=2e-4,
    lr_scheduler_type="cosine",
    warmup_ratio=0.1,
    bf16=True,
    gradient_checkpointing=True,
    gradient_checkpointing_kwargs={"use_reentrant": False},
    logging_steps=10,
    save_strategy="steps",
    save_steps=100,
    eval_strategy="steps",
    eval_steps=100,
    max_seq_length=2048,
    packing=True,
    dataset_kwargs={"assistant_only_loss": True},
    report_to="wandb",                # optional W&B tracking
)

# ── Trainer + go ──
trainer = SFTTrainer(
    model=model,
    args=args,
    train_dataset=dataset["train"],
    eval_dataset=dataset["validation"],
    processing_class=tokenizer,
    peft_config=peft_config,
)

trainer.train()
trainer.save_model(f"{output_dir}/final")
tokenizer.save_pretrained(f"{output_dir}/final")
print("Training complete!")

External links

Exercise

Run the full pipeline above on a real (or 1,000-example synthetic) dataset for Llama 3.1 8B. Use W&B for tracking. After training, log into W&B and compare your training loss curve to a published one (search 'llama 3.1 lora' on wandb.ai). Does your curve look healthy?

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.