How to Fine-Tune Llama 4 with QLoRA: A Practical Guide
QLoRA lets you fine-tune Llama 4 on one high-memory GPU, training small adapter weights while the base model stays in 4-bit. Here is the setup, data prep and checks.
On this page
Short answer
Fine-tuning Llama 4 with QLoRA means adapting Meta's Llama 4 model to your own task by training a small set of adapter weights while the base model stays frozen in 4-bit precision. It gives you a specialised model without the hardware cost of full fine-tuning.
What is fine-tuning Llama 4 with QLoRA?
Fine-tuning Llama 4 with QLoRA is a way to adapt Meta's Llama 4 model to a specific task, such as answering support questions or drafting replies in a company tone, by training only a small set of extra weights called adapters. The base model is loaded in 4-bit precision, so it uses far less memory than a full-precision copy, and the adapters learn the new behaviour.
QLoRA stands for quantized low-rank adaptation. It combines two ideas: quantization, which stores the base model weights in 4 bits instead of 16 or 32, and low-rank adaptation, which freezes those weights and trains a pair of small matrices that adjust the model's output. The trained adapters are saved separately and can be merged into the base model later if you want a single file.
The result is a model that behaves like a fine-tuned Llama 4 but was trained on a single high-memory GPU instead of a cluster. That makes it a practical choice for teams that have one good machine and a clear task, rather than a research budget. For the broader picture, how to fine-tune an LLM covers the general workflow.
Which Llama 4 model should you start with?
Start with Llama 4 Scout if you are fine-tuning on one machine, because it is the smaller of the two Llama 4 models and fits more comfortably in a single GPU's memory when quantized to 4 bits. Llama 4 Maverick is the larger model and needs substantially more memory and time, so it suits teams with bigger hardware or a task where the extra capability clearly pays off.
Both models are mixture-of-experts models, which means only part of the model is active for each token. That design keeps inference fast for their size, but it does not change the fine-tuning workflow: you still prepare data, set the adapter size and train for a few epochs. The choice is mostly about memory and cost, not about a different method.
Both models are also natively multimodal, accepting text and images. If your task is text only, that extra capability does not help and does not hurt; you simply train on text. Pick the model that fits your hardware, and test it on your task before committing to a long run. The general trade-offs are covered in RAG vs fine-tuning, which helps you decide whether tuning is even the right tool.
What do you need before you start?
Before you start, you need a GPU with enough memory to hold the quantized model plus the adapters and the optimizer state, a training dataset in a consistent format, and the standard open-source training stack. Most teams use the Hugging Face transformers library, the peft library for the adapters, trl for the training loop, bitsandbytes for 4-bit loading, and accelerate to manage the device.
| Part | What to use | Why it matters |
|---|---|---|
| Base model | Llama 4 Scout or Maverick | Sets memory needs and capability |
| Quantization | 4-bit, bitsandbytes | Shrinks the model to fit one GPU |
| Adapters | LoRA via peft | Only these weights are trained |
| Training loop | TRL supervised fine-tuning | Handles data, batching and logging |
| Dataset | Your own prompt and answer pairs | Decides what the model learns |
You also need a way to measure success. Pick a small held-out set of examples and a simple scoring rule, such as whether the answer matches a reference or passes a checklist, before training starts. Without that, you cannot tell whether a run helped or made things worse. A service for LLM fine-tuning can help if you would rather not build the pipeline yourself.
How do you prepare training data for QLoRA?
Prepare training data as prompt and answer pairs that show the behaviour you want, written in the same format the model will see at run time. If the model will answer customer questions, train on questions and the answers you want, with the same tone, length and structure. Consistency matters more than volume: a few hundred clean examples usually beat thousands of messy ones.
Clean the data before training. Remove duplicates, fix broken formatting, and cut examples that are too long or that answer a different question than the one asked. Mix in a small share of general examples so the model does not forget how to handle ordinary requests, and hold out a separate set of examples for testing so you can measure honestly.
Keep the format simple. Most teams use a chat template with a system message, a user message and an assistant reply, and they train only on the assistant reply so the model learns to produce answers, not to repeat questions. Check a few formatted examples by eye before launching a run, because a formatting mistake in the data is the most common reason a tuned model behaves oddly.
What are the steps to fine-tune Llama 4 with QLoRA?
Fine-tuning Llama 4 with QLoRA follows a short sequence: load the quantized base model, attach the adapters, train on your data, then test the result on held-out examples. The run itself usually takes hours, not days.
- Load the base model in 4-bit. Use bitsandbytes to load Llama 4 in 4-bit precision, with double quantization on and the compute type set to bfloat16.
- Attach LoRA adapters. Set the adapter rank, often 16 or 32, and the target modules, usually the attention and feed-forward projections. Higher rank learns more but trains slower.
- Prepare the dataset. Format your prompt and answer pairs with the chat template, tokenize them, and split off a held-out test set.
- Train for a few epochs. Use a small learning rate and a modest batch size with gradient accumulation, for two to three epochs, and stop if the loss plateaus.
- Save the adapters. Save only the adapter weights, which are small, and keep the training config alongside them so the run can be repeated.
- Test on held-out examples. Run the merged or adapter-loaded model on your test set and score it with the rule you chose before training.
Start with a short run on a small slice of data to check that the pipeline works end to end, then run the full training. If the loss does not fall, check the data format first; it is the usual cause. The habits that keep a run on track are the same ones in fine-tuning best practices.
How do you check the tuned model?
Check the tuned model by comparing it with the base model on the same held-out examples, using the same prompts and the same scoring rule. A tuned model should answer your task better than the base model and should not have lost its general ability to answer ordinary questions, so test both sides.
Read a sample of outputs yourself. Automatic scores miss tone, formatting and subtle errors, and a human read of twenty or thirty answers will catch problems that a number will not. Look for answers that are too long, that repeat the question, or that drift into a different style than the one you trained for.
Also test the failure cases you care about: questions outside the training topic, prompts with missing information, and inputs that should be refused or escalated. A tuned model that handles the happy path but fails badly on edge cases is not ready to ship. Keep the best adapter checkpoint, note the settings that produced it, and retest after any change to the data or the settings.
Example (hypothetical): tuning a support model on Llama 4 Scout
Example (hypothetical): a small software company wants a support model that answers common questions in its own help-centre tone. It collects 1,200 question and answer pairs from past tickets, each averaging 180 tokens, which gives 216,000 tokens of training text. It holds out 200 pairs for testing and trains on the other 1,000.
It loads Llama 4 Scout in 4-bit, attaches LoRA adapters with rank 16, and trains for 3 epochs. With a per-device batch of 4 and gradient accumulation of 4, each step covers 16 examples, so one epoch is 1,000 divided by 16, or about 63 steps, and 3 epochs come to about 189 steps. The run takes a few hours on one high-memory GPU.
On the held-out set, the tuned model answers 162 of 200 questions correctly against the team's checklist, up from 118 for the base model, and its answers match the help-centre tone. The team ships the adapters behind its support widget and keeps the base model untouched, so it can retrain when the help centre changes.
What are the common mistakes?
The most common QLoRA mistakes are training on messy data, choosing an adapter rank that is too high or too low, and skipping the held-out test. Each one is easy to avoid once you know to look for it.
- Training on messy data. Duplicates, mixed formats and wrong answers teach the model the wrong behaviour. Clean the data first.
- Too many epochs. Training for too long makes the model memorise the training set and answer new questions worse. Two to three epochs is usually enough.
- Wrong adapter rank. A rank that is too low cannot learn the task; a rank that is too high trains slowly and overfits. Start at 16 and adjust from the test results.
- No held-out test. Judging a model on the data it trained on always looks good. Keep a separate test set and score honestly.
- Skipping the base-model comparison. If you do not test the base model the same way, you cannot tell whether tuning helped at all.
Key takeaways
- QLoRA trains small adapter weights while the Llama 4 base model stays frozen in 4-bit
- Llama 4 Scout fits a single high-memory GPU; Maverick needs substantially more hardware
- Clean, consistently formatted prompt and answer pairs matter more than dataset size
- Two to three epochs with a held-out test set is the usual starting point
- Compare the tuned model with the base model on the same examples before shipping
Frequently asked questions
What is QLoRA in simple terms?
QLoRA is a fine-tuning method that loads a model in 4-bit precision and trains only a small set of adapter weights on top of it. The base model stays frozen, so training needs far less memory than full fine-tuning while still adapting the model to a specific task.
Can I fine-tune Llama 4 on one GPU?
Llama 4 Scout can be fine-tuned with QLoRA on a single high-memory GPU because the 4-bit base model and the small adapters fit together. Llama 4 Maverick is larger and needs more memory, so it usually requires a bigger machine or a different setup.
How much training data do I need for QLoRA?
A few hundred to a few thousand clean, consistent examples are usually enough for a focused task such as support answers or drafting in a company tone. Quality matters more than volume, and a held-out test set is needed to judge whether the tuning worked.
What adapter rank should I use?
A rank of 16 is a common starting point, with 32 for harder tasks. A higher rank can learn more but trains slower and risks overfitting, while a lower rank may not capture the task. Test the result and adjust from there.
Does QLoRA change the base model?
No. QLoRA freezes the base model weights and trains only the adapter weights, which are saved separately. You can load the adapters on top of the base model at run time, or merge them into a single model file if you prefer.
Is QLoRA better than full fine-tuning?
QLoRA is cheaper and faster, and for many focused tasks it matches full fine-tuning closely. Full fine-tuning can learn more when the task is very different from the base model's training, but it needs far more hardware and carries a higher risk of damaging general ability.