My Brain CellsMy Brain Cells
HomeBlogAbout

© 2026 My Brain Cells

XGitHubLinkedIn
Finetune LLMs 2-5x Faster: An In-Depth Guide to Unsloth

Finetune LLMs 2-5x Faster: An In-Depth Guide to Unsloth

AS
Anthony Sandesh

If you've ever tried to finetune a modern Large Language Model (LLM) like Llama 3 8B or Mistral 7B, you've almost certainly run into the same dreaded error: CUDA: out of memory.
Finetuning LLMs is incredibly powerful, but it's also computationally brutal. It demands massive amounts of VRAM, pushing it out of reach for most developers and researchers who don't have access to top-tier A100 or H100 GPUs. Even on a free T4 GPU on Google Colab, you're severely limited.
But what if you could finetune 2-5x faster and use 70% less memory, all with a few minor changes to your existing Hugging Face code?
That's the promise of Unsloth. This post is an in-depth guide to what Unsloth is, why it's a game-changer, and a complete, step-by-step project to show you exactly how to use it.

What is Unsloth (And Why Is It So Fast)?

Unsloth is an open-source AI library designed to significantly speed up LLM finetuning and reduce memory usage.
Unlike other methods that might use heavy approximation or new, complex model architectures, Unsloth's magic comes from handwritten GPU kernels. The team manually derived all the math for the backward and forward passes (the core of training) and wrote custom Triton kernels.
This means Unsloth isn't approximating the math; it's just doing the same math far more efficiently.
The result?
  • 2-5x Faster Training: It rewrites the model's backend on the fly to be more efficient.
  • 70% Less Memory: It intelligently manages memory, allowing you to finetune larger models on consumer GPUs (like an RTX 3080 or even a Colab T4) that would normally crash.
  • 100% Lossless: Because it's an exact rewrite of the operations, you get the same accuracy as a standard Hugging Face finetune, just faster.
  • Automatic: You don't need to learn a new framework. It patches directly into the existing Hugging Face ecosystem (transformers, peft, trl).

In-Depth Project: Finetuning Llama 3 8B with Unsloth

Let's prove it. Here is a complete, end-to-end project to finetune the Llama 3 8B Instruct model on a 4-bit (QLoRA) setup. This exact code will run on a free Google Colab T4 GPU.
The goal is to teach the model to respond in a specific JSON format using a simple dataset.

Step 1: Installation

First, install Unsloth. Their [colab-new] package includes all the necessary bits like xformers, peft, and trl.

Step 2: Load the Model (The Unsloth Way)

This is the most important part. Instead of using AutoModelForCausalLM from Hugging Face, you use FastLanguageModel from Unsloth.
Notice two things:
  1. We set load_in_4bit=True for QLoRA finetuning.
  1. We can specify max_seq_length right at the start. Unsloth will handle the patching.
 
Why this step is crucial: As soon as you call FastLanguageModel.from_pretrained, Unsloth patches the model in memory. It replaces the standard attention and other modules with its own high-speed Triton kernels.

Step 3: Add LoRA Adapters

Next, we need to prepare the model for LoRA (Low-Rank Adaptation). Unsloth provides a helper function for this that integrates with PEFT. We define which modules to target (like the query, key, value, and output projectors in the attention blocks).
 

Step 4: Load Data & Set Up the Trainer

This part is 100% standard Hugging Face. This is a key advantage of Unsloth: you don't have to relearn your data processing or training pipeline. We'll use the SFTTrainer from TRL.
 

Step 5: Train!

Now, the magic. Just call trainer.train().
 
When you run this, you'll see Unsloth's custom logs. You'll immediately notice that the steps/s (iterations per second) is significantly higher and the VRAM usage is lower than a standard transformers run.

Step 6: Inference

Once training is done, you can run inference. Unsloth's FastLanguageModel automatically merges the LoRA adapters for you for faster inference.
 
And that's it! You've successfully finetuned and run inference with Llama 3 8B on a single, free GPU, a task that is often impossible with standard libraries due to memory constraints.

The Advantages: Why You Should Use Unsloth

Based on the project above, here's a clear breakdown of the advantages Unsloth has over the standard finetuning process:
  1. Massive Memory Savings (70% Less): This is the biggest win. It's the difference between a project failing with a CUDA: out of memory error and it succeeding. It lets you use larger batch sizes, longer context lengths (max_seq_length), or even finetune larger models on the same hardware.
  1. Significant Speedup (2-5x Faster): By using custom kernels, Unsloth simply processes the training steps faster. This saves you valuable compute time, whether you're paying for it by the hour or just waiting on a Colab notebook.
  1. Near-Zero Code Change: This is its most elegant feature. You don't have to learn a new complex API. You just change AutoModelForCausalLM to FastLanguageModel, and the rest of your trl and peft code remains virtually identical.
  1. No Accuracy Loss: Because Unsloth uses exact math (not approximation), your final model is just as "smart" as one trained with the standard, slower methods.
  1. Broad Model Support: Unsloth supports a huge range of popular models, including Llama 3, Mistral, Gemma, Phi-3, and Qwen2.

Conclusion

Unsloth is a genuine game-changer for the open-source AI community. It dramatically lowers the barrier to entry for finetuning, taking it from a "big-GPU-only" club to something accessible to anyone with a modern consumer graphics card.
By focusing on deep, kernel-level optimizations without changing the user-facing API, it provides the best of all worlds: speed, memory efficiency, and ease of use. If you're finetuning an LLM, you should be using Unsloth.

References:
Unsloth - Open source Fine-tuning & RL for LLMsUnsloth - Open source Fine-tuning & RL for LLMsUnsloth AI - Open Source Fine-tuning & RL for LLMs
Unsloth AI - Open Source Fine-tuning & RL for LLMs

Unsloth AI - Open Source Fine-tuning & RL for LLMs

Open source fine-tuning & reinforcment learning (RL) for gpt-oss, Llama 4, DeepSeek-R1 and Qwen3 LLMs! Beginner friendly.

Unsloth - Open source Fine-tuning & RL for LLMsUnsloth - Open source Fine-tuning & RL for LLMs
Unsloth Docs | Unsloth Documentation
Unsloth Docs | Unsloth Documentation

Unsloth Docs | Unsloth Documentation

Train your own model with Unsloth, an open-source framework for LLM fine-tuning and reinforcement learning.

Github:
 

More posts

Guide to “RAY” by Anyscale

Guide to “RAY” by Anyscale

DeepSpeed

DeepSpeed

Beyond Linear Chains: A Deep Dive into “LangGraph” for Building Stateful AI Agents

Beyond Linear Chains: A Deep Dive into “LangGraph” for Building Stateful AI Agents

DeepSpeed

Newer

DeepSpeed

A Deep Dive into CrewAI for Collaborative AI Systems

Older

A Deep Dive into CrewAI for Collaborative AI Systems

On this page

  1. What is Unsloth (And Why Is It So Fast)?
  2. In-Depth Project: Finetuning Llama 3 8B with Unsloth
  3. Step 1: Installation
  4. Step 2: Load the Model (The Unsloth Way)
  5. Step 3: Add LoRA Adapters
  6. Step 4: Load Data & Set Up the Trainer
  7. Step 5: Train!
  8. Step 6: Inference
  9. The Advantages: Why You Should Use Unsloth
  10. Conclusion