A comprehensive framework for training reasoning models using Supervised Fine-Tuning (SFT) and Reinforcement Learning with Human Feedback (RLHF), optimized for consumer GPUs.
This project provides a complete pipeline for building robust reasoning models that can be trained on consumer-grade hardware. It incorporates insights from leading LLM research and implements Parameter-Efficient Fine-Tuning (PEFT) techniques to enable training on limited resources.
- Multi-stage Training Pipeline: SFT → Reward Model → RLHF (PPO)
- Consumer GPU Optimized: Uses LoRA, QLoRA, and gradient accumulation
- Cross-platform Support: Works on CUDA, Apple MPS, and CPU
- Synthetic Data Generation: Built-in tools for generating reasoning datasets
- Flexible Configuration: YAML-based configuration with command-line overrides
- Comprehensive Logging: Detailed training metrics and progress tracking
# Clone the repository
git clone <repository-url>
cd reasoning_model_trainer
# Install dependencies
pip install -r requirements.txtpython generate_data.py --output_dir ./data# Step 1: Supervised Fine-Tuning (SFT)
python train.py --mode sft --config configs/sft_config.yaml
# Step 2: Train Reward Model
python train.py --mode reward --config configs/reward_config.yaml
# Step 3: RLHF Training
python train.py --mode rlhf --config configs/rlhf_config.yamlpython inference.py --sft_model_path ./sft_output/final_sft_model --prompt "What is 25 + 17?"The framework consists of several key components:
- Device Manager: Automatic device detection and management
- Configuration System: Flexible YAML-based configuration
- Logging System: Comprehensive training and metrics logging
- Dataset Loaders: Support for SFT, reward, and RLHF data formats
- Data Collators: Efficient batching for different training modes
- Synthetic Data Generator: Built-in generation of reasoning examples
- Data Validator: Quality checks and data cleaning
- Base Model Wrapper: Unified interface for causal LMs and reward models
- PEFT Integration: LoRA and QLoRA support for memory efficiency
- Policy-Value Model: Combined architecture for PPO training
- SFT Trainer: Supervised fine-tuning with PEFT
- Reward Trainer: Preference-based reward model training
- PPO Trainer: Reinforcement learning with human feedback
# Model Configuration
model_name: "mistralai/Mistral-7B-v0.1"
output_dir: "./sft_output"
# Training Parameters
num_epochs: 3
per_device_train_batch_size: 1
gradient_accumulation_steps: 4
learning_rate: 1e-4
# LoRA Configuration
use_lora: true
lora_r: 16
lora_alpha: 32
lora_dropout: 0.05
# Dataset
dataset_path: "./data/synthetic_sft_dataset.jsonl"For consumer GPUs with limited VRAM:
- Use smaller models: Start with 7B parameter models
- Enable LoRA: Set
use_lora: true - Use 4-bit quantization: Set
use_4bit_quantization: true - Reduce batch size: Use
per_device_train_batch_size: 1 - Increase gradient accumulation: Use
gradient_accumulation_steps: 4-8
{"prompt": "What is 15 + 27?", "completion": " Let me solve this step by step.\n\n15 + 27 = 42\n\nTherefore, the answer is 42."}{"prompt": "What is 15 + 27?", "chosen": " 15 + 27 = 42", "rejected": " I think it's 43."}{"prompt": "Solve this math problem: 15 + 27 = ?"}- GPU: 8GB VRAM (with 4-bit quantization)
- RAM: 16GB system RAM
- Storage: 50GB free space
- GPU: 16GB+ VRAM (RTX 4080, RTX 4090, or equivalent)
- RAM: 32GB+ system RAM
- Storage: 100GB+ free space
- NVIDIA GPUs with CUDA support
- Apple Silicon Macs with MPS support
- CPU-only training (slower but functional)
Replace the synthetic datasets with your own:
# Validate your data
python -c "from src.data.data_validator import DataValidator; validator = DataValidator(); validator.validate_dataset('your_data.jsonl', 'sft')"
# Update config files to point to your data
# Then run training as usualThe framework supports various base models:
- Mistral 7B: Excellent performance-to-size ratio
- Llama 2 7B/13B: Well-tested and widely supported
- Qwen 7B/14B: Strong reasoning capabilities
- TinyLlama 1.1B: For resource-constrained environments
Key hyperparameters to adjust:
- Learning Rate: Start with 1e-4 for SFT, 1e-5 for reward/RLHF
- LoRA Rank: Higher values (16-64) for more expressiveness
- Batch Size: Balance between memory and training stability
- Sequence Length: Longer sequences for complex reasoning
-
Out of Memory (OOM)
- Reduce batch size to 1
- Enable 4-bit quantization
- Reduce sequence length
- Use smaller LoRA rank
-
Slow Training
- Increase batch size if memory allows
- Use gradient accumulation
- Enable mixed precision training
-
Poor Model Performance
- Increase training epochs
- Use higher quality data
- Adjust learning rate
- Try different base models
- Use appropriate model size for your hardware
- Monitor GPU utilization and adjust batch size accordingly
- Use gradient checkpointing for memory efficiency
- Enable compilation with
torch.compile()for speed
Contributions are welcome! Please see our contributing guidelines for details.
This project is licensed under the MIT License - see the LICENSE file for details.
This framework incorporates insights from:
- Stanford Alpaca and LLaMA-Adapter
- Vicuna and FastChat
- DeepSeek reasoning models
- TinyZero and Journey Learning approaches
- Lightning-AI LitGPT framework
If you use this framework in your research, please cite:
@software{reasoning_model_trainer,
title={Reasoning Model Trainer: A Framework for Consumer GPU Training},
author={Muhammad Abiodun SULAIMAN},
year={2024},
url={https://github.com/Behordeun/reasoning-model-trainer}
}