Skip to main content

Overview

Fine-tuning adapts pre-trained language models to your specific use case, improving performance on domain-specific tasks while using fewer resources than training from scratch.

Llama 3.2 Fine-tuning

Fine-tune 1B and 3B models with LoRA

Gemma 3 Fine-tuning

Fine-tune 270M to 27B models efficiently

Why Fine-Tune?

Task-Specific Accuracy

Fine-tuning improves performance on specialized tasks:

Parameter-Efficient Fine-Tuning

LoRA (Low-Rank Adaptation)

Fine-tune large models by training only 1-2% of parameters
LoRA adds small trainable matrices to existing model layers, dramatically reducing:
  • Training time: 3-5x faster
  • Memory usage: 3-4x less VRAM
  • Storage: Adapters are 10-100MB vs full model GBs
  • Cost: Train on free Google Colab

How LoRA Works

Key Parameters:
  • r (rank): Size of low-rank matrices (typically 8-64)
  • alpha: Scaling factor (typically 16-32)
  • target_modules: Which layers to adapt

Llama 3.2 Fine-Tuning

Fine-tune Meta’s Llama 3.2 (1B or 3B) for free in Google Colab

Quick Start

1

Install Dependencies

2

Run Fine-Tuning Script

3

Use Fine-Tuned Model

Complete Implementation

Model Selection

Llama 3.2 1B Instruct

Specs:
  • Parameters: 1.2B
  • Context: 128K tokens
  • VRAM (4-bit): ~2GB
  • Training time: ~20 min (100 steps, Colab T4)
  • Use case: Fast, lightweight tasks

Training Configuration

class
Configure the fine-tuning process

LoRA Configuration

function
Add LoRA adapters to model

Dataset Preparation

The standard format for instruction fine-tuning:
Multi-turn conversations:

Google Colab Setup

1

Open Colab Notebook

2

Select GPU Runtime

Runtime → Change runtime type → T4 GPU (free tier)
3

Install Dependencies

4

Run Fine-Tuning

Copy the finetune_llama3.2.py code into a cell and run
5

Save to Google Drive


Gemma 3 Fine-Tuning

Fine-tune Google’s Gemma 3 models from 270M to 27B parameters

Model Sizes

270M

Ultra-lightweightVRAM: ~1GB

1B

Fast & efficientVRAM: ~2GB

4B

BalancedVRAM: ~6GB

12B

High qualityVRAM: ~16GB

27B

Best performanceVRAM: ~32GB

Implementation

Gemma-Specific Notes

Gemma uses a specific chat format:
The template formats messages as:
Approximate VRAM needs with 4-bit quantization:
Choose based on use case:
  • 270M: Edge devices, real-time inference, simple tasks
  • 1B: Chatbots, content moderation, classification
  • 4B: Code generation, summarization, Q&A (Colab-friendly)
  • 12B: Complex reasoning, multi-turn conversations
  • 27B: Production apps requiring GPT-3.5 level quality

Advanced Topics

Evaluation During Training

Inference After Fine-Tuning

Merging LoRA with Base Model

Quantization Options

  • VRAM: 25% of full model
  • Speed: 90% of full model
  • Quality: 98-99% of full model
  • Best for: Most use cases

Best Practices

High-quality data is more important than quantity:
  • ✅ 1,000 high-quality examples > 10,000 mediocre examples
  • ✅ Clean, consistent formatting
  • ✅ Diverse range of topics/styles
  • ✅ Representative of actual use case
  • ❌ Avoid duplicate or near-duplicate examples
  • ❌ Don’t include low-quality or incorrect data
Don’t overtrain:
Typical steps by dataset size:
  • 1K examples: 100-300 steps
  • 10K examples: 300-1000 steps
  • 100K examples: 1000-3000 steps
Start with defaults, then adjust:
Evaluate thoroughly:

Troubleshooting

Solutions:
  1. Reduce batch size:
  1. Use 4-bit quantization:
  1. Reduce sequence length:
  1. Use smaller LoRA rank:
Possible causes:
  1. Too few training steps: Increase max_steps
  2. Low-quality data: Clean and curate dataset
  3. Wrong chat template: Use correct template for model
  4. Learning rate too high: Reduce to 1e-4 or 5e-5
  5. Overfitting: Add validation set, reduce steps
Speed up training:
  1. Use smaller model (1B instead of 3B)
  2. Reduce sequence length
  3. Use gradient checkpointing:
  1. Ensure CUDA is used:

Resources

Unsloth GitHub

Fast fine-tuning library

Gemma 3 Blog

Gemma 3 fine-tuning guide

Example Code

Complete fine-tuning scripts

Tutorial

Step-by-step fine-tuning tutorial