Unsloth Tutorial 2026: Fine-Tune LLMs 2x Faster on One GPU, Step by Step With Real Code
Fine-tuning used to mean days of waiting and careful VRAM math. Unsloth (70k stars) cuts training time and memory dramatically. This tutorial runs a real fine-tune from install to inference.
💡 What You Will Learn
Fine-tuning used to mean days of waiting and careful VRAM math. Unsloth (70k stars) cuts training time and memory dramatically. This tutorial runs a real fine-tune from install to inference.
📜 Table of Contents
Why Unsloth Changed Fine-Tuning
Unsloth (70,038 stars, fetched 2026-08-12) is the open source library that made consumer-GPU fine-tuning practical. Its tricks - manual attention kernels, fused operations, smarter memory management - deliver roughly 2x training speed and up to 70% less VRAM versus standard PEFT/LoRA paths. Same results, fewer resources.
Step 1: Install
pip install unsloth
One command, no compilation. Unsloth patches Hugging Face Transformers at runtime; you keep your normal workflow.
Step 2: Load a Model With 4-Bit
from unsloth import FastLanguageModel
import torch
model, tokenizer = FastLanguageModel.from_pretrained(
model_name="unsloth/Qwen2.5-7B-bnb-4bit",
max_seq_length=2048,
load_in_4bit=True,
)
model = FastLanguageModel.get_peft_model(
model,
r=16,
lora_alpha=16,
lora_dropout=0,
target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
)
A 7B model now fits in ~6 GB VRAM. On a 24 GB GPU you have room for a 14B model with the same setup.
Step 3: Prepare Data
Use the ChatML format with a system prompt, or use the built-in apply_chat_template:
def format_prompt(examples):
texts = [tokenizer.apply_chat_template([{"role": "user", "content": q}], tokenize=False)
+ f" {a}" for q, a in zip(examples["question"], examples["answer"])]
return {"text": texts}
For a first run, take 1,000 examples from an existing dataset on Hugging Face (e.g. a QA subset) - enough to see real loss movement in minutes.
Step 4: Train
from trl import SFTTrainer
from transformers import TrainingArguments
trainer = SFTTrainer(
model=model, tokenizer=tokenizer,
train_dataset=dataset.map(format_prompt, batched=True),
args=TrainingArguments(
per_device_train_batch_size=2,
gradient_accumulation_steps=4,
learning_rate=2e-4,
max_steps=200,
output_dir="outputs",
),
)
trainer.train()
Watch the loss: with a clean dataset it should drop noticeably within the first 50 steps.
Step 5: Inference and Save
model.save_pretrained_merged("qwen-tuned", tokenizer, save_method="merged_16bit")
The merged 16-bit model runs anywhere (Ollama, vLLM, llama.cpp) without Unsloth installed. That last point is what makes Unsloth practical: training is Unsloth, serving is whatever you already use.
The Real Numbers
On a single RTX 4090 (24 GB), a 7B LoRA fine-tune that takes ~2 hours with standard PEFT typically finishes in ~1 hour with Unsloth, using less VRAM. On smaller 8 GB cards, 7B 4-bit becomes possible at all - the reason Unsloth is the default recommendation in fine-tuning threads.
FAQ
Is Unsloth free? The library is open source (Apache 2.0). Unsloth Studio is the paid no-code layer on top.
Does it work with any model? It supports the major open families: Llama, Qwen, Mistral, Gemma, DeepSeek and more.
Can I use my own dataset? Yes - any Hugging Face dataset or local JSON in the right format.
Related reads: Unsloth QLoRA Tutorial 2026, Unsloth LoRA Tutorial 2026, LLM Fine-Tuning Tools Compared 2026.
