A comprehensive toolkit for fine-tuning Microsoft's Phi-2 and Phi-3.5 language models, featuring memory-efficient training, interactive chat, and model comparison capabilities.
-
Fine-tuning with LoRA
- Memory-efficient training optimized for Apple Silicon (MPS) and CUDA
- Small adapter files (~20MB vs full model ~2.7GB)
- Detailed layer control (see PHI2 Layer Guide)
-
Interactive Tools
dialogue.py
: Chat with base or fine-tuned modelscompare_models.py
: Compare base vs fine-tuned responsesmain.py
: Train and create LoRA adapters
- macOS 12.3+ with Apple Silicon OR Linux with CUDA
- Python 3.9+
- PyTorch 2.2.0+ (with MPS or CUDA support)
- ~21GB available memory for model comparison
- Installation
git clone https://github.com/lpalbou/phi2-finetuning.git
cd phi2-finetuning
python -m venv .venv
source .venv/bin/activate # On Unix/macOS
pip install -r requirements.txt
- Fine-tune a Model
python src/main.py \
--output_dir ./output/my_model \
--dataset_path ./data/my_dataset.jsonl
- Chat with Models
# Use base model
python src/dialogue.py
# Use fine-tuned model
python src/dialogue.py --adapter_path output/my_model/final_adapter
- Compare Models
python src/compare_models.py --adapter_path output/my_model/final_adapter
.
├── src/
│ ├── config/ # Training configurations
│ ├── trainers/ # Training implementations
│ ├── callbacks/ # Training callbacks
│ ├── utils/ # Utility functions
│ ├── main.py # Training entry point
│ ├── dialogue.py # Interactive chat
│ └── compare_models.py # Model comparison
├── data/ # Training datasets
└── docs/ # Additional documentation
The primary tool for fine-tuning models. See our Training Guide for detailed instructions and parameters.
Interactive chat interface supporting:
- Base model (Phi-2 or others)
- Fine-tuned model (base + LoRA adapter)
- Custom prompts and parameters
Side-by-side comparison tool to evaluate fine-tuning effects:
- Interactive REPL mode for live testing
- Batch mode with YAML question files
- Visual output with colored responses
- Training Guide - Comprehensive training documentation
- PHI2 Layer Guide - Detailed model layer explanations
- Examples - Sample datasets and configurations
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Open a Pull Request
- Microsoft for the Phi-2 model
- Hugging Face for the transformers library
- PEFT library for LoRA implementation