Fine-tune a 27B model with QLoRA on one 24 GB graphics card
You get a LoRA adapter trained on a 4-bit 27B model, saved to disk, converted to a GGUF adapter file and loaded in llama.cpp, with a plan for checking it against the untuned model. It is for anyone with one consumer card of 24 GB and a Linux or WSL2 install.
Requirements: one NVIDIA card with 24 GB · Linux or Windows with WSL2 · current Unsloth with Transformers v5, TRL and PEFT · a recent llama.cpp checkout and build
QLoRA in two sentences: you freeze the base model and store it in 4-bit, so it takes roughly a quarter of the memory of 16-bit weights. You train only a small set of added low-rank matrices, the adapter, in higher precision, and save only those.
Requirements
- One NVIDIA card with 24 GB, ideally one that does not also drive a display, since the desktop holds some of its memory.
- Enough system RAM to hold the input embedding when you offload it, plus the usual Python overhead.
- Linux, or Windows with WSL2.
- Disk space for the 4-bit model, the Python environment and the adapters.
- Training text already in the model's chat format, one example per row in a
textfield. - A llama.cpp checkout and build if you want a GGUF adapter.
Steps
1. Decide your sequence length before you start
At 4-bit the weights of a 27B model take most of a 24 GB card, so what is left goes to activations, which grow with sequence length. For what fits, our QLoRA test recorded the following on one 24 GB card at batch size 1 with gradient accumulation 4:
| Sequence length | Peak allocated | Peak reserved | Per optimizer step |
|---|---|---|---|
| 2,048 | 20,010 MiB | 21,380 MiB | 10.46 s |
| 4,096 | 20,737 MiB | 21,388 MiB | 19.98 s |
| 8,192 | 22,152 MiB | 23,018 MiB | 40.82 s |
At 8,192 tokens the card is full, and on plain Linux, where the driver fails at the limit rather than spilling to system RAM, that length may not complete. Start at 4,096 or below.
2. Install Unsloth
Use the install method on Unsloth's Qwen3.8 training page. On Linux and WSL2 it is:
curl -fsSL https://unsloth.ai/install.sh | sh
Read the script first if you prefer. The page asks for Transformers v5. Once your first run works, write down the installed versions and pin them, so a later upgrade does not change memory use under you.
3. Load the 4-bit model on the card
This is the documented load. offload_embedding keeps the large input embedding in system RAM to save memory on the card.
from unsloth import FastModel
max_seq_length = 4096
model, tokenizer = FastModel.from_pretrained(
model_name = "unsloth/Qwen3.8-27B-unsloth-bnb-4bit",
max_seq_length = max_seq_length,
load_in_4bit = True,
full_finetuning = False,
offload_embedding = True,
)
Set max_seq_length to the length you chose in step 1.
4. Add the adapter
Rank 16 with alpha 16 is Unsloth's documented starting point. Turn the vision layers off for text-only data.
model = FastModel.get_peft_model(
model,
finetune_vision_layers = False,
finetune_language_layers = True,
finetune_attention_modules = True,
finetune_mlp_modules = True,
r = 16,
lora_alpha = 16,
lora_dropout = 0,
bias = "none",
use_gradient_checkpointing = "unsloth",
random_state = 3407,
use_rslora = False,
loftq_config = None,
)
5. Train a short first run
Use the TRL trainer from Unsloth's page, with batch size 1, accumulation 4 and the 8-bit optimizer. Set max_steps low for the first run so you see memory and step time before committing hours; the documented example uses 100.
from trl import SFTTrainer, SFTConfig
trainer = SFTTrainer(
model = model,
tokenizer = tokenizer,
train_dataset = dataset,
args = SFTConfig(
dataset_text_field = "text",
max_seq_length = max_seq_length,
per_device_train_batch_size = 1,
gradient_accumulation_steps = 4,
warmup_steps = 10,
max_steps = 20,
learning_rate = 2e-4,
logging_steps = 1,
optim = "adamw_8bit",
output_dir = "outputs",
seed = 3407,
dataset_num_proc = 1,
report_to = "none",
),
)
trainer.train()
Expect the first step to be much slower than the rest while kernels are compiled.
6. Watch memory and stop early
Print PyTorch's peaks after the first few steps:
import torch
print(torch.cuda.max_memory_allocated() // 2**20, "MiB allocated")
print(torch.cuda.max_memory_reserved() // 2**20, "MiB reserved")
In a second terminal, watch the card as a whole, including what the display holds, with nvidia-smi:
nvidia-smi --query-gpu=memory.used,memory.total --format=csv -l 5
On WSL2 also watch shared GPU memory in Windows Task Manager: the driver can move memory to system RAM instead of failing, so a run can look fine while it slows down. If reserved memory gets within about 1 GiB of the card's total, stop and lower max_seq_length.
7. Save and reload the adapter
Save only the adapter and the tokenizer.
model.save_pretrained("my_lora")
tokenizer.save_pretrained("my_lora")
To load it again in Python on top of the base model, use PEFT as its quicktour shows:
from peft import PeftModel
model = PeftModel.from_pretrained(base_model, "my_lora")
8. Convert the adapter to GGUF
llama.cpp's convert_lora_to_gguf.py turns a PEFT adapter into a GGUF adapter file. --base points at a local folder with the base model's Hugging Face config and tokenizer files; the weights are not needed. Without it, the script fetches the config named in the adapter's own config from the Hub.
python convert_lora_to_gguf.py --base <base-config-dir> --outtype f16 --outfile my_lora-f16.gguf my_lora
9. Serve it with llama.cpp
Start the server with the base GGUF and the adapter. The base must be the same model the adapter was trained on.
llama-server -m <models-dir>/base-27b.gguf --lora my_lora-f16.gguf --host <server-address> --port 8080
Check it worked
Send a GET request to the server's /lora-adapters endpoint. The llama.cpp server documentation shows the response as a list of adapters, each with an id, a path and a scale. Your adapter should be listed with its file name, and with a scale of 1, which is the default for adapters given with --lora. Do not rely on the startup log to tell you it is applied.
Then measure it. Start the same base without --lora, run the same held-out prompts with the same settings on both, and score both with an automatic check rather than by reading answers. Look at pass rate first, then time per task.
Common problems
- The load is refused because some modules were placed on the CPU, which the 4-bit loader cannot quantize: keep the whole model on one card by passing
device_map = {"": 0}, the Transformers placement option, and keepoffload_embedding = True. - Memory is higher than expected on a text-only run: make sure
finetune_vision_layers = False, and close anything else that uses the card. - The run slows sharply on WSL2 while memory looks fine: the driver is spilling to shared memory. Lower
max_seq_length. - The converter cannot load the base configuration from the Hub, for example because its pinned Transformers version does not know the architecture: pass
--basewith a local folder that holds only the config and tokenizer files. Leave the weight index file out, or the script looks for weights. - Out of memory while saving a merged or GGUF model from Unsloth: its saving page suggests lowering
maximum_memory_usage.
Our recommendation
We'd train text only, with the embedding in system RAM, rank 16 and a 4,096-token cap, because that leaves headroom on a 24 GB card and 8,192 does not. If you have the choice, we'd use a card that does not drive a display. We'd save the adapter alone and convert it, rather than merging into a new 4-bit file, because one base GGUF can then serve with or without the adapter for a fair comparison. And we'd measure against the base before trusting any adapter: a run that trains is not evidence that it helps.
Further reading
- QLoRA on Qwen3.8-27B with one 24 GB card, our measurements of what fits and how long a step takes
- Unsloth's Qwen3.8 training page
- PEFT quicktour
- TRL SFTTrainer documentation
- llama.cpp server documentation