Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

62 Commits

Repository files navigation

🎯 Fine-Tuning Language Models

A comprehensive, beginner-friendly repository for fine-tuning large language models using modern techniques like LoRA, DPO, and Unsloth optimizations.

Banner

License: MITPython 3.8+Colab

🚀 Quick Start (5 Minutes)

# Install dependencies
!pipinstall"unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"# Load and fine-tune in 3 linesfromunslothimportFastLanguageModelmodel, tokenizer=FastLanguageModel.from_pretrained("unsloth/llama-3-8b-bnb-4bit")
model=FastLanguageModel.get_peft_model(model, r=16)
# Add your training code here

🧭 Navigation Guide

📁 Folder🎯 Purpose👥 Best For⏱️ Time Needed
FineTuning LanguageModelsBasic concepts & simple examplesBeginners, students1-2 hours
FineTuning LargeLanguageModelsAdvanced techniques & model-specific guidesPractitioners, researchers3-5 hours
FineTuning VisionModelVision-language model fine-tuningComputer vision researchers2-3 hours
Advanced FineTuning MethodsMulti-LoRA & LoRA composition techniquesAdvanced researchers, ML engineers3-4 hours
Task Specific FineTuningSpecialized task fine-tuning (Code generation)Domain specialists, developers2-3 hours
Reinforcement Learning Based FineTningHuman preference alignment (DPO, RLHF)Advanced users, alignment researchers2-4 hours
docs/Comprehensive guides & troubleshootingAll skill levelsReference

🤔 Which Technique Should I Use?

🔰 I'm New to Fine-Tuning

→ Start with Basic LoRA Fine-tuning

  • ✅ Easy to understand
  • ✅ Low memory requirements
  • ✅ Good results for most tasks

💻 I Have Limited GPU Memory

→ Use 4-bit Quantized LoRA

  • ✅ Works on Google Colab T4
  • ✅ 70% less memory usage
  • ✅ Minimal performance loss

🎯 I Want Better Instruction Following

→ Try DPO Training

  • ✅ Improves response quality
  • ✅ Better human alignment
  • ✅ No reward model needed

🖼️ I Need Vision-Language Models

→ Use Vision Model Fine-tuning

  • ✅ Image + text processing
  • ✅ Mathematical formula recognition
  • ✅ Multimodal AI applications

🧬 I Need Multiple Domain Expertise

→ Try Multi-LoRA Methods

  • ✅ Multiple skills without forgetting
  • ✅ Dynamic adapter switching
  • ✅ Combine different expertises

💻 I Need Specialized Code Generation

→ Use Task-Specific Fine-tuning

  • ✅ Advanced code generation
  • ✅ Multi-language programming support
  • ✅ Optimized with Qwen2.5-Coder

📊 Memory & Performance Guide

ModelSizeMin GPU MemoryTraining Time (100 steps)Best Use Case
Phi-3 Mini3.8B6GB10 minsCoding tasks, efficiency
Llama-3-8B8B12GB20 minsGeneral conversation
Qwen2.5-7B7.6B10GB18 minsMultilingual, math
Qwen2-VL-7B7B8GB25 minsVision-language tasks

💡 Tip: All memory requirements assume 4-bit quantization with LoRA

🛠️ Installation

Option 1: Google Colab (Recommended)

!pipinstall"unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
!pipinstall--no-deps"trl<0.9.0"transformersdatasets

Option 2: Local Installation

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
pip install "unsloth[cu121-ampere-torch230] @ git+https://github.com/unslothai/unsloth.git"

🔧 Troubleshooting

❌ Getting CUDA out of memory errors? → See Memory Optimization Guide

❌ Model not following instructions?
→ Try DPO Training

❌ Training too slow? → Check Performance Optimization Tips

📚 Learning Path

  1. Week 1: Basic Fine-tuning Concepts
  2. Week 2: LoRA and PEFT Techniques
  3. Week 3: Vision-Language Models
  4. Week 4: Human Preference Alignment
  5. Week 5: Advanced Multi-LoRA Methods
  6. Week 6: Task-Specific Applications

🛠️ Technologies

Core Technologies

PythonPyTorch🤗 TransformersUnslothJupyterCUDALoRAPEFT

Advanced Optimization

TRLBitsAndBytesDatasetsAccelerateWandBDPORLHFQuantization

🤔 What is Fine-Tuning?

Fine-tuning is like teaching a smart student a new skill:

  • 🧠 Pre-trained model = Student who already knows language
  • 📚 Your dataset = Textbook for the new skill
  • ⚙️ Fine-tuning = Practice sessions to master the skill
  • 🎯 Fine-tuned model = Expert in your specific task

💡 Why Fine-Tuning?

ApproachCostTimeData NeededResults
Training from scratch$1M+MonthsBillions of tokens100%
Fine-tuning$10-100HoursThousands of examples95%+

99% cost reduction while maintaining excellent performance!

🧠 Understanding the Process

Step 1: Choose Your Base Model

# Start with a pre-trained modelmodel="unsloth/llama-3-8b-bnb-4bit"# Already knows language

Step 2: Add Task-Specific Training

# Your specialized datasetdataset= [
{"instruction": "Explain photosynthesis", "output": "Photosynthesis is..."},
{"instruction": "Write code", "output": "def function():..."}
]

Step 3: Efficient Training with LoRA

# Train only 1% of parametersmodel=FastLanguageModel.get_peft_model(model, r=16) # LoRA magic

🎯 Repository Overview

This repository contains everything you need to master fine-tuning, from beginner-friendly tutorials to advanced optimization techniques.

🔰 Beginner Level

Learn the fundamentals with hands-on examples and step-by-step guides.

🚀 Intermediate Level

Explore advanced techniques like DPO training and model optimization.

🔬 Expert Level

Master vision-language models and human preference alignment.

🆘 Need Help?

📄 License

MIT License - see LICENSE file for details.


Star this repo if it helped you fine-tune your models!

🔄 Full Fine-Tuning

Updates all model parameters during training. Requires high computational resources but achieves best task-specific performance.

⚡ Parameter-Efficient Fine-Tuning (PEFT)

Updates only a small subset of parameters with 90% reduction in computational cost while matching full fine-tuning performance.

📊 Quantization: Memory Optimization

Quantization reduces model weight precision from 32-bit to lower precision (8-bit, 4-bit), dramatically reducing memory usage while maintaining performance.

🎯 Precision Formats

  • FP32: 4 bytes per parameter (baseline)
  • INT8: 1 byte per parameter (75% memory reduction)
  • INT4: 0.5 bytes per parameter (87.5% memory reduction)

📈 Example: 7B Parameter Model

FP32: 7B × 4 bytes = 28 GB
INT8: 7B × 1 byte = 7 GB (75% reduction)
INT4: 7B × 0.5 bytes = 3.5 GB (87.5% reduction)

🎯 LoRA: Low-Rank Adaptation

LoRA decomposes weight updates into low-rank matrices, reducing trainable parameters by 99% while maintaining performance.

📐 Mathematical Foundation

W' = W + ΔW
ΔW = A × B
  • W: Original pre-trained weights (frozen)
  • A: Low-rank matrix (d × r)
  • B: Low-rank matrix (r × k)
  • r: Rank (much smaller than d or k)

🔢 Rank Selection Guidelines

  • r = 8-16: Standard choice, good balance
  • r = 32-64: Complex adaptations
  • r = 128+: Approaching full fine-tuning

📊 Parameter Reduction Example

4096 × 4096 layer with r=16:

Original: 4096 × 4096 = 16,777,216 parameters
LoRA: (4096 × 16) + (16 × 4096) = 131,072 parameters
Reduction: 99.2% fewer parameters

🔧 Adapters: Modular Fine-Tuning

Small neural network modules inserted between transformer layers for task-specific adaptation.

🏗️ Architecture

Input → Layer Norm → Adapter → Residual Connection → Output

⚖️ LoRA vs Adapters

AspectLoRAAdapters
Parameter Count0.1-1%2-4%
Training SpeedFasterModerate
ModularityLimitedHigh

📁 Repository Structure

This repository contains six specialized folders:

Foundational fine-tuning techniques and inference examples for beginners.

Advanced model-specific fine-tuning using UnSloth, LoRA, and quantization techniques.

Vision-language model fine-tuning for multimodal AI applications.

Cutting-edge Multi-LoRA and LoRA composition techniques for multiple domain expertise.

Specialized fine-tuning for domain-specific tasks like code generation.

Human preference alignment using Direct Preference Optimization (DPO) and RLHF.

🚀 Getting Started

# Clone repository
git clone https://github.com/Abeshith/FineTuning_LanguageModels.git
cd FineTuning_LanguageModels
# Install dependencies
pip install transformers datasets torch torchvision
pip install unsloth peft bitsandbytes trl accelerate

About

🎯 Fine-tune large language models and use them for text-related tasks. This repository provides a straightforward approach to fine-tuning models like Gemma, Llama 🦙, and Mistral 🌪️ for various NLP tasks. 🔧 It includes training 📚, fine-tuning 🛠️, and inference pipelines ⚙️. 🚀

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages