Spaces:
Sleeping
Sleeping
| # CampusGPT Uganda β Learning Roadmap & One-Day Mastery Checklist | |
| ## Complete Unsloth Fine-Tuning Ecosystem in One Project | |
| --- | |
| ## Your Learning Journey | |
| By building CampusGPT Uganda, you have touched every major concept in the | |
| Unsloth ecosystem. This document is your personal mastery checklist and | |
| study guide. | |
| --- | |
| ## Phase 1: Foundations (Morning β 2 hours) | |
| ### β Concept 1: What is Unsloth? | |
| **You learned:** | |
| - Unsloth is a Python library for faster LLM fine-tuning | |
| - It achieves 2Γ speed + 80% memory reduction through: | |
| - Custom CUDA kernels (C-level code for attention + FFN) | |
| - Manual backpropagation (no storing redundant activations) | |
| - Smart memory layout (packed data, minimal fragmentation) | |
| - Same accuracy as HuggingFace β just faster | |
| **Key file:** `training/finetune.py` β top docstring + `load_model_and_tokenizer()` | |
| **Self-test:** Can you explain why Unsloth is faster to a classmate? | |
| --- | |
| ### β Concept 2: LoRA β Low-Rank Adaptation | |
| **You learned:** | |
| - Full fine-tuning modifies all 8 billion parameters β impractical | |
| - LoRA adds two small matrices A (mΓr) and B (rΓn) to each weight | |
| - Only A and B are trained (1β5% of total parameters) | |
| - The adapted weight: W' = W + (A Γ B) Γ (alpha/r) | |
| - r=16 is the recommended default for instruction tuning | |
| **Key file:** `training/finetune.py` β `add_lora_adapters()` | |
| **Formula to remember:** | |
| ``` | |
| W' = W_frozen + (A_trainable Γ B_trainable) Γ scaling_factor | |
| ``` | |
| **Self-test:** Why does LoRA use TWO matrices (A and B) instead of one? | |
| *Answer: A single low-rank update matrix is the product of two smaller matrices. | |
| The product AΓB has rank β€ r, achieving compression. A single matrix of rank r | |
| cannot be parameterised as efficiently.* | |
| --- | |
| ### β Concept 3: QLoRA β Quantised LoRA | |
| **You learned:** | |
| - Quantisation stores weights in fewer bits: float32 (4 bytes) β int4 (0.5 bytes) | |
| - QLoRA = 4-bit quantised base model + full-precision LoRA adapters | |
| - Llama-3 8B: 32 GB (fp32) β ~5 GB (QLoRA) β fits on a T4 GPU! | |
| - NF4 (NormalFloat4) is used β better distribution for neural network weights | |
| - ~1-2% quality loss vs full fine-tuning β totally acceptable for most tasks | |
| **Key file:** `training/finetune.py` β `load_in_4bit=True` parameter | |
| **Memory calculation:** | |
| ``` | |
| Model size (GB) β (parameters Γ bits) / (8 Γ 1024Β³) | |
| Llama 8B in 4-bit β (8 Γ 10βΉ Γ 4) / (8 Γ 1024Β³) β 4 GB | |
| ``` | |
| --- | |
| ### β Concept 4: PEFT β Parameter-Efficient Fine-Tuning | |
| **You learned:** | |
| - PEFT is the HuggingFace library managing adapters | |
| - `get_peft_model()` attaches LoRA to the base model | |
| - After calling it: base weights FROZEN, only adapter weights trainable | |
| - Adapter weights are ~10β100 MB (vs full model ~6β16 GB) | |
| **Key file:** `training/finetune.py` β `add_lora_adapters()` | |
| --- | |
| ## Phase 2: Data Engineering (Mid-Morning β 2 hours) | |
| ### β Concept 5: Dataset Formats | |
| **You learned the 4 major formats:** | |
| | Format | Fields | Use Case | | |
| |--------|--------|----------| | |
| | JSONL | question, answer | Simple storage | | |
| | Alpaca | instruction, input, output | General instruction tuning | | |
| | OpenAI Messages | [{role, content}] | Chat models (MOST COMMON) | | |
| | ShareGPT | conversations:[{from, value}] | Multi-turn chat | | |
| **Key file:** `data/generate_dataset.py` β all `to_*_format()` functions | |
| **Self-test:** Convert this Q&A to all 4 formats by hand: | |
| - Q: "What is the attendance policy?" | |
| - A: "Students must attend 75% of lectures." | |
| --- | |
| ### β Concept 6: Tokenisation | |
| **You learned:** | |
| - Text β Tokens β Input IDs (integers) | |
| - "MUST university" β ["MUST", " university"] β [123, 456] | |
| - Every model has its OWN vocabulary β ALWAYS use the model's tokeniser | |
| - Chat templates format conversations with model-specific special tokens | |
| - `apply_chat_template()` handles this automatically | |
| **Key file:** `training/finetune.py` β `load_and_prepare_dataset()` | |
| **Qwen chat template example:** | |
| ``` | |
| <|im_start|>system | |
| You are CampusGPT...<|im_end|> | |
| <|im_start|>user | |
| What are the fees?<|im_end|> | |
| <|im_start|>assistant | |
| The fees are...<|im_end|> | |
| ``` | |
| --- | |
| ### β Concept 7: Synthetic Data Generation | |
| **You learned:** | |
| - Read PDFs β Extract text β Chunk β LLM generates QA pairs | |
| - Deduplication with Jaccard similarity (n-gram overlap) | |
| - Quality filtering (minimum length, must be a question, no non-answers) | |
| - Key insight: LLMs can generate their own training data! | |
| **Key file:** `data/synthetic_generator.py` | |
| **Pipeline:** | |
| ``` | |
| PDF β pypdf β chunks of 500 words β LLM prompt β JSON output β dedup β filter β JSONL | |
| ``` | |
| --- | |
| ## Phase 3: RAG System (Late Morning β 2 hours) | |
| ### β Concept 8: Vector Embeddings | |
| **You learned:** | |
| - Text β dense vector of floats (768β1024 dimensions) | |
| - Similar meaning β similar vector (close in vector space) | |
| - Cosine similarity measures angle between vectors (1.0 = identical) | |
| - Models: BGE-M3 (multilingual, best for Uganda), MiniLM (fast), Qwen (highest quality) | |
| **Key file:** `embeddings/embedding_pipeline.py` | |
| **Key formula:** | |
| ``` | |
| cos_similarity(A, B) = (A Β· B) / (|A| Γ |B|) | |
| Range: 0 (different) to 1 (identical meaning) | |
| ``` | |
| --- | |
| ### β Concept 9: RAG β Retrieval-Augmented Generation | |
| **You learned the complete pipeline:** | |
| ``` | |
| Student Question | |
| β | |
| Embed Query β [0.23, -0.11, ...] | |
| β | |
| ChromaDB (vector similarity search) | |
| β | |
| Top-3 relevant document passages | |
| β | |
| Prompt = System + Retrieved Context + Question | |
| β | |
| LLM generates grounded answer | |
| β | |
| Response + Source Citations | |
| ``` | |
| **Why RAG is better than pure fine-tuning for some tasks:** | |
| - Can handle new documents without retraining | |
| - Answers are grounded in retrieved text (less hallucination) | |
| - Can cite sources (important for academic contexts) | |
| **Key file:** `rag/rag_pipeline.py` | |
| --- | |
| ### β Concept 10: ChromaDB | |
| **You learned:** | |
| - ChromaDB = vector database (stores text + embeddings + metadata) | |
| - HNSW index: approximate nearest-neighbour in O(log n) time | |
| - Cosine distance space: closer = more similar | |
| - Persistent: data survives between sessions | |
| --- | |
| ## Phase 4: Fine-Tuning (Afternoon β 3 hours) | |
| ### β Concept 11: SFT β Supervised Fine-Tuning | |
| **You learned:** | |
| - Show the model thousands of (question, answer) pairs | |
| - Train it to predict the correct answer given the question | |
| - Loss computed ONLY on assistant tokens (not system/user tokens) | |
| - SFTTrainer from TRL handles everything automatically | |
| **Key file:** `training/trainer.py` and `training/finetune.py` | |
| --- | |
| ### β Concept 12: Key Hyperparameters | |
| | Parameter | Recommended | Effect | | |
| |-----------|-------------|--------| | |
| | r | 16 | LoRA capacity | | |
| | lora_alpha | 32 | Update strength | | |
| | learning_rate | 2e-4 | Step size | | |
| | batch_size | 2 | Examples per step | | |
| | grad_accum | 4 | Effective batch = 8 | | |
| | epochs | 3 | Dataset passes | | |
| | warmup_steps | 20 | Gentle start | | |
| | lr_scheduler | cosine | Decay shape | | |
| **Key file:** `training/hyperparams.py` | |
| **Diagnostic rules:** | |
| - Loss explodes β halve learning rate | |
| - Loss not decreasing β double learning rate | |
| - Overfitting β add dropout, reduce epochs | |
| - OOM β reduce batch size or r | |
| --- | |
| ## Phase 5: Model Export (Late Afternoon β 1 hour) | |
| ### β Concept 13: Three Export Options | |
| | Format | Size | Use Case | | |
| |--------|------|----------| | |
| | LoRA adapters | ~50 MB | Development, sharing adapters | | |
| | Merged 16-bit | ~6-16 GB | Production, vLLM | | |
| | GGUF (Q4_K_M) | ~2-4 GB | Ollama, local inference | | |
| **Key file:** `training/export.py` | |
| **Merge operation:** `W' = W + (A Γ B) Γ (alpha/r)` | |
| --- | |
| ## Phase 6: Deployment (Evening β 2 hours) | |
| ### β Concept 14: Ollama Deployment | |
| **You learned:** | |
| - Ollama wraps GGUF with a clean REST API | |
| - Modelfile = LLM configuration (like a Dockerfile) | |
| - OpenAI-compatible API: change base_url, same code works | |
| - `ollama run campusgpt` to chat locally | |
| **Key files:** `deployment/ollama/` | |
| --- | |
| ### β Concept 15: vLLM Deployment | |
| **You learned:** | |
| - PagedAttention: allocates KV cache in pages on demand | |
| - Continuous batching: new requests join mid-generation | |
| - 10-100Γ higher throughput than Ollama for multiple concurrent users | |
| - For university servers serving hundreds of students | |
| **Key file:** `deployment/vllm/deploy_vllm.py` | |
| --- | |
| ### β Concept 16: HuggingFace Hub | |
| **You learned:** | |
| - Upload LoRA adapter, merged model, or GGUF | |
| - Model Card = documentation for your model | |
| - HuggingFace Spaces = free Gradio demo hosting | |
| - Ollama can pull directly from HuggingFace: `ollama pull username/campusgpt` | |
| **Key file:** `deployment/huggingface/push_to_hub.py` | |
| --- | |
| ### β Concept 17: Evaluation | |
| **You learned four metrics:** | |
| | Metric | What it measures | Range | | |
| |--------|-----------------|-------| | |
| | ROUGE-L | Word overlap (recall) | 0β1 | | |
| | BLEU | N-gram precision | 0β1 | | |
| | BERTScore | Semantic similarity | 0β1 | | |
| | Hallucination Rate | Key fact recall | 0β1 (lower=better) | | |
| **Key file:** `evaluation/evaluate_model.py` | |
| **The key experiment:** Base model vs Fine-tuned model comparison. | |
| If fine-tuning worked, you should see: | |
| - Higher ROUGE-L (+0.05 to +0.2) | |
| - Higher BERTScore (+0.02 to +0.1) | |
| - Lower hallucination rate (-0.05 to -0.2) | |
| --- | |
| ## ONE-DAY UNSLOTH MASTERY CHECKLIST | |
| Check off each item as you complete it: | |
| ### π Morning (Hours 1β3) | |
| - [ ] Read `README.md` and understand the full architecture | |
| - [ ] Run `python data/generate_dataset.py` β generate 3100 training examples | |
| - [ ] Run `python data/synthetic_generator.py` β understand PDF-to-QA pipeline | |
| - [ ] Open `data/processed/combined_all_openai_messages.jsonl` β inspect the data | |
| - [ ] Open `data/processed/combined_all_alpaca.jsonl` β compare formats | |
| ### π€οΈ Late Morning (Hours 3β5) | |
| - [ ] Read `training/finetune.py` top docstring β understand LoRA vs QLoRA | |
| - [ ] Study `LORA_CONFIG` in `finetune.py` β understand each parameter | |
| - [ ] Study `TRAINING_CONFIG` β understand each training argument | |
| - [ ] Run `python training/hyperparams.py` β get hardware recommendation | |
| - [ ] (Optional) Run `python embeddings/embedding_pipeline.py` β see semantic search | |
| ### βοΈ Afternoon (Hours 5β8) β REQUIRES GPU | |
| - [ ] Open Google Colab (colab.research.google.com) with T4 GPU | |
| - [ ] Install Unsloth: `!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"` | |
| - [ ] Copy `training/finetune.py` to Colab | |
| - [ ] Run training β watch the loss decrease | |
| - [ ] Note peak GPU memory usage | |
| - [ ] Run `training/export.py` β export LoRA adapters | |
| ### π Late Afternoon (Hours 8β10) | |
| - [ ] Run `python rag/rag_pipeline.py --build` β build knowledge base | |
| - [ ] Run `python rag/rag_pipeline.py` β test RAG queries | |
| - [ ] Run `python inference/chat.py` β chat with your model | |
| - [ ] Compare base model vs fine-tuned model responses manually | |
| ### π Evening (Hours 10β12) | |
| - [ ] Run `python evaluation/evaluate_model.py` β compute metrics | |
| - [ ] Run `python frontend/app.py` β launch the Gradio UI | |
| - [ ] Try uploading a PDF in the UI | |
| - [ ] Run `bash deployment/ollama/deploy_ollama.sh` β deploy locally | |
| - [ ] (Optional) Push to HuggingFace Hub | |
| --- | |
| ## Concepts Mastered After This Project | |
| After completing CampusGPT Uganda, you understand: | |
| 1. β **Unsloth** β what it is and why it's faster | |
| 2. β **LoRA** β low-rank adaptation mathematics | |
| 3. β **QLoRA** β quantisation + LoRA for memory efficiency | |
| 4. β **PEFT** β parameter-efficient fine-tuning library | |
| 5. β **Dataset formats** β JSONL, Alpaca, OpenAI Messages, ShareGPT | |
| 6. β **Tokenisation** β text to tokens to input IDs | |
| 7. β **Chat templates** β model-specific conversation formatting | |
| 8. β **SFT** β supervised fine-tuning with SFTTrainer | |
| 9. β **Evaluation** β ROUGE, BERTScore, hallucination metrics | |
| 10. β **Inference** β streaming, temperature, top-p sampling | |
| 11. β **RAG** β retrieval-augmented generation end-to-end | |
| 12. β **Vector embeddings** β semantic search with BGE/Qwen | |
| 13. β **ChromaDB** β vector store, HNSW, cosine similarity | |
| 14. β **Model export** β LoRA adapters, merged model, GGUF | |
| 15. β **Ollama** β local deployment with Modelfile | |
| 16. β **vLLM** β production serving with PagedAttention | |
| 17. β **HuggingFace Hub** β model sharing and demo hosting | |
| 18. β **Gradio** β building ML web UIs | |
| 19. β **Hyperparameter tuning** β what each parameter does and how to tune it | |
| 20. β **Synthetic data generation** β PDF β QA pairs with LLMs | |
| --- | |
| ## What to Build Next | |
| Now that you understand the full stack, here are next project ideas: | |
| 1. **Luganda-English Translation Fine-tune** β Use your NLLB/Qwen pipeline + QLoRA | |
| 2. **Medical QA for Uganda** β Fine-tune on Ugandan health guidelines | |
| 3. **Legal AI for Uganda** β Fine-tune on Ugandan law documents | |
| 4. **AgriBot** β Combine with your PotatoGuard project + LLM | |
| 5. **Multi-university CampusGPT** β Expand to Makerere, KIU, UCU | |
| 6. **Voice CampusGPT** β Add speech-to-text (Whisper) + text-to-speech | |
| --- | |
| ## Resources | |
| - **Unsloth docs:** https://docs.unsloth.ai | |
| - **Unsloth GitHub:** https://github.com/unslothai/unsloth | |
| - **TRL docs:** https://huggingface.co/docs/trl | |
| - **ChromaDB docs:** https://docs.trychroma.com | |
| - **Gradio docs:** https://www.gradio.app/docs | |
| - **vLLM docs:** https://docs.vllm.ai | |
| - **BGE-M3 paper:** https://arxiv.org/abs/2402.03216 | |
| - **QLoRA paper:** https://arxiv.org/abs/2305.14314 | |
| --- | |
| *Built by Joseph Ssemuli, Computer Science student at MUST, during Sunbird AI internship.* | |
| *This project is your evidence of mastering the complete Unsloth ecosystem.* πΊπ¬ | |