QLoRA Fine-Tuning Tutorial: Train a Llama Model on One GPU

Train a Llama model on one GPU with QLoRA: load the base in 4-bit, configure a LoRA adapter, prepare data and train with SFTTrainer, step by step.

By Galactic News Editorial · Published 2026-11-18T04:47:30+00:00 · Updated 2026-11-18T04:47:30+00:00

QLoRA fine-tuning tutorial to train a Llama model on one GPU

QLoRA Fine-Tuning Tutorial: Train a Llama Model on One GPU

Full fine-tuning of a large language model needs far more memory than one GPU can hold. QLoRA fixes that. It freezes the base model in 4-bit precision and trains only small adapter matrices. The result is a tuned model on a single consumer GPU. This tutorial walks through the full process with a Llama model, using the Hugging Face stack.

What QLoRA is and why it works

QLoRA builds on LoRA. LoRA freezes the pretrained weights and trains two small low-rank matrices beside them. The trained update is tiny. The adapter file is small enough to keep one per task. QLoRA adds 4-bit quantization on top. The frozen base model is stored in 4-bit NormalFloat, or NF4. That cuts the base model memory to roughly a quarter of 16-bit storage.

The QLoRA paper reports fine-tuning a 65-billion-parameter model on a single 48 GB GPU. It did this while preserving 16-bit fine-tuning task performance. The method introduces NF4, double quantization and paged optimizers. Double quantization compresses the quantization constants themselves. Paged optimizers spill memory spikes to CPU RAM instead of crashing. The paper also reports that its Guanaco models reached 99.3 percent of ChatGPT performance on the Vicuna benchmark after 24 hours of fine-tuning on one GPU.

QLoRA fits a specific job. Use it to teach a model a format, a tone or a style. Use it to adapt a model to your domain. Do not use it to teach new facts. For facts, retrieval works better. A tuned model still needs good data and a clear goal.

QLoRA fine tuning tutorial: what you need first

You need a GPU with enough memory for your model size. A 7B or 8B model in 4-bit fits on a 24 GB card for training in most setups. The paper used a single 48 GB GPU for a 65B model. Bigger models need bigger cards. You also need Python 3.10 or newer, PyTorch with CUDA, and disk space for the model and checkpoints.

Install the libraries first. You need transformers, peft, bitsandbytes, datasets, accelerate and trl. The TRL docs describe peft_config as the way to pass LoRA settings to its trainers. Install bitsandbytes for 4-bit and 8-bit quantization support. Check that your versions work together before you start training. Version mismatches are the most common cause of failed runs.

Pick your base model. This tutorial uses a Llama 3 8B model from Meta. Llama weights are gated on Hugging Face. Accept the model license on the model page first. Then sign in with a Hugging Face token that has access. Use the instruct version if you plan to build a chat-style model.

Step 1: Install the libraries

1. Create a fresh virtual environment for the project.

2. Install the stack with pip: transformers, peft, bitsandbytes, datasets, accelerate and trl.

3. Check the install by importing each library in Python.

4. Confirm your GPU is visible with a short PyTorch check.

Fix install errors now. A broken environment wastes hours later. Pin versions once you find a working set.

Step 2: Load the base model in 4-bit

1. Build a BitsAndBytesConfig with load_in_4bit set to True.

2. Set the 4-bit quant type to nf4 for NormalFloat precision.

3. Turn on double quantization to save more memory.

4. Set the compute dtype to bfloat16 for stable training.

5. Load the model with AutoModelForCausalLM, passing the quantization config and a device map of auto.

Load the tokenizer from the same model id. Set a padding token if the tokenizer has none. For Llama models, the end-of-sequence token often serves as the pad token. Check the tokenizer config before training.

Step 3: Configure the LoRA adapter

1. Create a LoraConfig with the task type set to causal language modelling.

2. Set the rank r to 16 for a first run. Higher rank trains more parameters. It also uses more memory.

3. Set lora_alpha to 32. The alpha value scales the adapter update.

4. Set lora_dropout to 0.05 to reduce overfitting on small data.

5. Set target_modules to the attention projections, such as q_proj, k_proj, v_proj and o_proj.

6. Wrap the model with get_peft_model and print the trainable parameter count.

Expect the trainable share to be well under one percent of the total. That is normal. The adapter holds the learning. The base model stays frozen.

Step 4: Prepare your training data

1. Gather instruction examples in a simple format. Each example needs an instruction, an input if any, and a response.

2. Aim for quality over size. A few hundred clean examples beat thousands of noisy ones.

3. Keep the style consistent. The model learns your format from the pattern in the data.

4. Split off a small held-out set for checks. Use it to spot overfitting early.

5. Format the data as chat turns if you train an instruct model. Use the tokenizer chat template to apply the right format.

Bad data teaches bad habits. Remove duplicates. Remove examples with wrong answers. Check a sample by hand before training.

Step 5: Train with SFTTrainer

1. Build TrainingArguments with an output directory, a small learning rate and a modest number of epochs.

2. Start with one to three epochs. More epochs overfit small data fast.

3. Enable gradient checkpointing to save memory. It slows training a little, but it prevents out-of-memory errors.

4. Set a per-device batch size that fits your GPU. Use gradient accumulation to reach a larger effective batch.

5. Pass the model, the dataset and the peft_config to SFTTrainer, then call train.

Watch the loss curve as it runs. Loss should fall steadily, then level off. A loss that jumps or explodes means the learning rate is too high. Stop early and lower it.

Step 6: Save, merge and test the adapter

1. Save the trained adapter with save_pretrained. The adapter is only a few megabytes.

2. Load the base model again and attach the adapter with PeftModel.

3. Merge the adapter into the base weights with merge_and_unload for deployment.

4. Test the merged model on prompts from your held-out set.

5. Compare the output with the base model on the same prompts.

Keep the adapter separate as well as the merged model. The adapter is cheap to store and easy to swap. One base model can serve many adapters, one per task.

Common problems and fixes

Out-of-memory errors are the most common failure. Lower the batch size first. Turn on gradient checkpointing. Shorten the maximum sequence length. Each of these frees memory fast.

Slow training is normal for QLoRA. Every forward pass must dequantize the base weights. Expect slower steps than plain LoRA. Do not raise the learning rate to compensate. That causes unstable training.

Repetitive or degraded output usually means overfitting. Train for fewer epochs. Add more varied data. Raise the dropout slightly. Check the output on held-out prompts, not training prompts.

Bottom line

QLoRA makes fine-tuning a Llama model on one GPU a practical job. Load the base in 4-bit NF4, train a small LoRA adapter, and keep your data clean. Start with an 8B model and a few hundred good examples. Test on held-out prompts before you ship anything. The method is proven. The work is in the data.