This repository contains code for fine-tuning the Meta-Llama 3.1 8B Instruct model using Greedy Reward Policy Optimization (GRPO) with the Unsloth library for accelerated training.
This project implements GRPO fine-tuning for improving code generation capabilities with multiple reward functions. It uses the Unsloth library to accelerate training by optimizing memory usage and enabling faster inference.
- Fine-tunes Meta-Llama 3.1 8B Instruct model
- Uses GRPO (Greedy Reward Policy Optimization) for training
- Implements multiple code-specific reward functions
- Accelerates training with Unsloth's optimizations
- Supports 4-bit quantization for efficient memory usage
- Integrates with Weights & Biases for experiment tracking
- Python 3.8+
- PyTorch
- CUDA-compatible GPU
- Hugging Face account with access to Meta-Llama 3.1 models
# Clone the repository
git clone https://github.com/your-username/grpo-llama-unsloth.git
cd grpo-llama-unsloth
# Install dependencies
pip install -r requirements.txtCreate a .env file with your Hugging Face token:
HF_TOKEN=your_hugging_face_token_here
main.py- The main training scriptutils/dataset.py- Dataset preparation utilitiesutils/GRPOrewards.py- Custom reward functions for code generation
The model is trained with multiple reward functions specifically designed for code generation:
xmlcount_reward_func- Rewards proper XML tag usagesoft_format_reward_func- Evaluates formatting quality with flexible criteriastrict_format_reward_func- Enforces strict formatting requirementsint_reward_func- Rewards correct integer handlingcorrectness_reward_func- Evaluates overall code correctness
python main.pyThe script uses the following key configuration parameters:
max_seq_length: 2042 tokenslora_rank: 32 (Higher values = more capacity but slower training)- Quantization: 4-bit loading for memory efficiency
- Learning rate: 5e-6 with cosine scheduler
- Training steps: 250 with checkpoints saved every 250 steps
The script includes several memory optimization techniques:
- 4-bit quantization
- Gradient checkpointing
- vLLM fast inference
- Configurable GPU memory utilization
Training progress can be monitored with Weights & Biases. Set your W&B configuration in the environment variables:
WANDB_ENTITY=your_entity
WANDB_PROJECT=your_project
WANDB_RUN_GROUP=your_run_group
If you encounter any issues, please check if they're related to the fixed issue in: unslothai/unsloth#2298
[Your chosen license]