Skip to content

Abeshith/FineTuning_LanguageModels

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: MIT Python 3.8+ Colab

๐Ÿš€ Quick Start (5 Minutes)

# Install dependencies
!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"

# Load and fine-tune in 3 lines
from unsloth import FastLanguageModel
model, 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 LanguageModels Basic concepts & simple examples Beginners, students 1-2 hours
FineTuning LargeLanguageModels Advanced techniques & model-specific guides Practitioners, researchers 3-5 hours
FineTuning VisionModel Vision-language model fine-tuning Computer vision researchers 2-3 hours
Advanced FineTuning Methods Multi-LoRA & LoRA composition techniques Advanced researchers, ML engineers 3-4 hours
Task Specific FineTuning Specialized task fine-tuning (Code generation) Domain specialists, developers 2-3 hours
Reinforcement Learning Based FineTning Human preference alignment (DPO, RLHF) Advanced users, alignment researchers 2-4 hours
docs/ Comprehensive guides & troubleshooting All skill levels Reference

๐Ÿค” 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

Model Size Min GPU Memory Training Time (100 steps) Best Use Case
Phi-3 Mini 3.8B 6GB 10 mins Coding tasks, efficiency
Llama-3-8B 8B 12GB 20 mins General conversation
Qwen2.5-7B 7.6B 10GB 18 mins Multilingual, math
Qwen2-VL-7B 7B 8GB 25 mins Vision-language tasks

๐Ÿ’ก Tip: All memory requirements assume 4-bit quantization with LoRA

๐Ÿ› ๏ธ Installation

Option 1: Google Colab (Recommended)

!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
!pip install --no-deps "trl<0.9.0" transformers datasets

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

Python PyTorch ๐Ÿค— Transformers Unsloth Jupyter CUDA LoRA PEFT

Advanced Optimization

TRL BitsAndBytes Datasets Accelerate WandB DPO RLHF Quantization

๐Ÿค” 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?

Approach Cost Time Data Needed Results
Training from scratch $1M+ Months Billions of tokens 100%
Fine-tuning $10-100 Hours Thousands of examples 95%+

โœ… 99% cost reduction while maintaining excellent performance!

๐Ÿง  Understanding the Process

Step 1: Choose Your Base Model

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

Step 2: Add Task-Specific Training

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

Step 3: Efficient Training with LoRA

# Train only 1% of parameters
model = 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

Aspect LoRA Adapters
Parameter Count 0.1-1% 2-4%
Training Speed Faster Moderate
Modularity Limited High

๐Ÿ“ 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

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors