Implementation of OREO (Offline REasoning Optimization) - an offline reinforcement learning method for enhancing large language models' multi-step reasoning capabilities.
OREO is an offline RL algorithm that improves LLM reasoning by:
Paper: Offline Reinforcement Learning for LLM Multi-Step Reasoning Original Code: https://github.com/jwhj/oreo
# Install OREO-specific dependencies
pip install -r requirements_oreo.txt
# Alternatively, install individually:
pip install transformers>=4.36.0 datasets>=2.14.0 accelerate>=0.25.0
# Download GSM8K dataset
python dataset/download_data.py --dataset gsm8k
# Or use the script (Windows)
scripts\01_download_data.bat
For full OREO training, collect reasoning trajectories:
# Collect trajectories (takes 15-30 mins for 100 problems)
python -m dataset.trajectory_collector \
--base_model "Qwen/Qwen2.5-Math-1.5B" \
--data_file dataset/gsm8k_train.jsonl \
--output_file dataset/trajectories_gsm8k.jsonl \
--num_trajectories_per_problem 4 \
--max_problems 100
# Or use the script (Windows)
scripts\02_collect_trajectories.bat
Phase 1: Supervised Fine-Tuning
python main.py \
--mode oreo_sft \
--base_model "Qwen/Qwen2.5-Math-1.5B" \
--reasoning_dataset gsm8k \
--epochs 3 \
--batch_size 4 \
--lr 1e-5
# Or use the script (Windows)
scripts\03_train_sft.bat
Phase 2: OREO Offline RL
python main.py \
--mode oreo_rl \
--base_model "Qwen/Qwen2.5-Math-1.5B" \
--trajectory_file dataset/trajectories_gsm8k.jsonl \
--epochs 10 \
--batch_size 4 \
--policy_lr 3e-6 \
--value_lr 1e-5
# Or use the script (Windows)
scripts\04_train_oreo_rl.bat
python main.py \
--mode test \
--base_model "Qwen/Qwen2.5-Math-1.5B" \
--ckpt_name oreo_policy_e10 \
--reasoning_dataset gsm8k
# Or use the script (Windows)
scripts\05_evaluate.bat
# Step 0: Download data
python dataset/download_data.py --dataset gsm8k
# Step 1: Collect trajectories
python -m dataset.trajectory_collector \
--base_model "Qwen/Qwen2.5-Math-1.5B" \
--data_file dataset/gsm8k_train.jsonl \
--output_file dataset/trajectories_gsm8k.jsonl \
--max_problems 100
# Step 2: SFT training
python main.py --mode oreo_sft \
--base_model "Qwen/Qwen2.5-Math-1.5B" \
--reasoning_dataset gsm8k \
--epochs 3 --batch_size 4
# Step 3: OREO RL training
python main.py --mode oreo_rl \
--trajectory_file dataset/trajectories_gsm8k.jsonl \
--epochs 10 --batch_size 4
# Step 4: Evaluation
python main.py --mode test \
--ckpt_name oreo_policy_e10 \
--reasoning_dataset gsm8k
| Parameter | Default | Description |
|---|---|---|
--base_model | Qwen/Qwen2.5-Math-1.5B | HuggingFace model name |
--max_seq_length | 512 | Maximum token sequence length |
--policy_lr | 3e-6 | Policy model learning rate |
--value_lr | 1e-5 | Value model learning rate |
--temperature | 0.1 | Temperature for soft Bellman equation |
--gamma | 0.99 | RL discount factor |
--batch_size | 4 | Batch size (per GPU) |
--epochs | 10 | Number of training epochs |
--use_gradient_checkpointing | true | Enable gradient checkpointing (saves memory) |
--share_backbone | true | Share transformer between policy and value |
# Use these settings for small GPU:
--batch_size 2 \
--use_gradient_checkpointing true \
--max_seq_length 256 \
--precision float16 # If supported
# Using torchrun (recommended)
torchrun --nproc_per_node=2 main.py \
--distributed true \
--mode oreo_sft \
...
# Effective batch size = batch_size * num_gpus
OREO/
βββ models/OREO/ # OREO models
β βββ policy_model.py # Policy (LLM)
β βββ value_model.py # Value function
β βββ oreo_config.py # Configuration
βββ dataset/ # Data handling
β βββ download_data.py # Download datasets
β βββ trajectory_collector.py # Collect trajectories
β βββ oreo_data_provider.py # Data loading
βββ learning/ # Training logic
β βββ oreo_task.py # OREO task class
β βββ oreo_engine.py # Training loops
βββ utils/ # Utilities
β βββ oreo_losses.py # Loss functions
β βββ oreo_metrics.py # Evaluation metrics
β βββ generation_utils.py # Text generation
β βββ answer_extraction.py # Answer parsing
βββ scripts/ # Training scripts
β βββ 01_download_data.bat
β βββ 02_collect_trajectories.bat
β βββ 03_train_sft.bat
β βββ 04_train_oreo_rl.bat
β βββ 05_evaluate.bat
βββ checkpoints/ # Saved models (created automatically)
GSM8K (Grade School Math, 8.5K problems)
python dataset/download_data.py --dataset gsm8kMATH (Competition Math)
python dataset/download_data.py --dataset mathTest (Small synthetic dataset)
python dataset/download_data.py --dataset testTrajectories are stored as JSONL with format:
{
"problem": "What is 25% of 80?",
"solution": "25% = 0.25. 0.25 * 80 = 20",
"predicted_answer": "20",
"ground_truth": "20",
"is_correct": true,
"reward": 1.0
}
--share_backbone true--batch_size 2 \
--use_gradient_checkpointing true \
--max_seq_length 256 \
--max_problems 50 # For trajectory collection
--batch_size 8 \
--epochs 5 \
--max_problems 50 # Fewer trajectories
--epochs 20 \
--policy_lr 1e-6 \ # Lower learning rate
--temperature 0.05 \ # Lower temperature
--max_problems 500 # More trajectories
Solution 1: Reduce batch size
--batch_size 1 # or 2
Solution 2: Enable gradient checkpointing
--use_gradient_checkpointing true
Solution 3: Reduce sequence length
--max_seq_length 256 # or 128
Solution 4: Use smaller model
--base_model "Qwen/Qwen2-0.5B" # 500M params instead of 1.5B
Solution 1: Use multi-GPU
torchrun --nproc_per_node=2 main.py --distributed true ...
Solution 2: Reduce data size
--max_problems 50 # Fewer trajectory examples
Solution 3: Fewer epochs
--epochs 5 # Instead of 10-20
Solution 1: Train longer
--epochs 20 # More epochs
Solution 2: More trajectories
--max_problems 500 # More training data
Solution 3: Adjust hyperparameters
--policy_lr 1e-6 \ # Lower LR
--temperature 0.05 # Lower temperature
Solution: Adjust generation parameters
--temperature 0.7 \ # Try 0.5-1.0
--top_p 0.9 \ # Try 0.8-0.95
--top_k 50 # Try 20-100
Example output:
==================================================
EVALUATION RESULTS
==================================================
accuracy: 0.7500
avg_reward: 0.7500
avg_trajectory_length: 3.2
num_predictions: 100
==================================================
| Method | GSM8K Accuracy | Training Data |
|---|---|---|
| Base Model | 55-60% | - |
| SFT Only | 65-70% | Solutions |
| OREO (SFT + RL) | 75-80% | Solutions + Trajectories |
{"problem": "...", "solution": "...", "answer": "..."}
Modify oreo_data_provider.py to load your dataset
Train with --reasoning_dataset custom
--base_model "meta-llama/Llama-2-7b" \ # LLaMA
--base_model "deepseek-ai/deepseek-math-7b-instruct" # DeepSeek
Key hyperparameters to tune:
policy_lr: Try 1e-6 to 1e-5value_lr: Try 3e-6 to 3e-5temperature: Try 0.05 to 0.2gamma: Try 0.95 to 0.99Paper: Offline Reinforcement Learning for LLM Multi-Step Reasoning
Original Implementation:
Pretrained Models:
jwhj/Qwen2.5-Math-1.5B-OREOjwhj/Qwen2.5-Math-1.5B-OREO-ValueQ: Do I need to collect trajectories? A: For full OREO training (RL phase), yes. For SFT only, no.
Q: How long does training take? A: SFT: 30-60 min, RL: 1-2 hours (GTX 1060, batch_size=2, 100 problems)
Q: Can I use my own dataset? A: Yes! Create JSONL with problem/solution/answer fields.
Q: What GPU do I need? A: Minimum 6GB (GTX 1060). Recommended: 8GB+ (RTX 3060).
Q: Does it work on CPU? A: Yes, but very slow (not recommended).
Q: Can I skip SFT and train RL directly? A: Not recommended. SFT provides good initialization for RL.
All OREO components have been implemented:
You're now ready to train OREO models for multi-step reasoning! Start with the Quick Start section above.
For questions or issues, refer to:
OREO_IMPLEMENTATION_PLAN.md (detailed technical plan)Happy reasoning! π§ β¨
1 commits
Python
92.5%
Shell
7.5%
Implementation of OREO (Offline REasoning Optimization) - an offline reinforcement learning method for enhancing large language models' multi-step reasoning capabilities.
OREO is an offline RL algorithm that improves LLM reasoning by:
Paper: Offline Reinforcement Learning for LLM Multi-Step Reasoning Original Code: https://github.com/jwhj/oreo
# Install OREO-specific dependencies
pip install -r requirements_oreo.txt
# Alternatively, install individually:
pip install transformers>=4.36.0 datasets>=2.14.0 accelerate>=0.25.0
# Download GSM8K dataset
python dataset/download_data.py --dataset gsm8k
# Or use the script (Windows)
scripts\01_download_data.bat
For full OREO training, collect reasoning trajectories:
# Collect trajectories (takes 15-30 mins for 100 problems)
python -m dataset.trajectory_collector \
--base_model "Qwen/Qwen2.5-Math-1.5B" \
--data_file dataset/gsm8k_train.jsonl \
--output_file dataset/trajectories_gsm8k.jsonl \
--num_trajectories_per_problem 4 \
--max_problems 100
# Or use the script (Windows)
scripts\02_collect_trajectories.bat
Phase 1: Supervised Fine-Tuning
python main.py \
--mode oreo_sft \
--base_model "Qwen/Qwen2.5-Math-1.5B" \
--reasoning_dataset gsm8k \
--epochs 3 \
--batch_size 4 \
--lr 1e-5
# Or use the script (Windows)
scripts\03_train_sft.bat
Phase 2: OREO Offline RL
python main.py \
--mode oreo_rl \
--base_model "Qwen/Qwen2.5-Math-1.5B" \
--trajectory_file dataset/trajectories_gsm8k.jsonl \
--epochs 10 \
--batch_size 4 \
--policy_lr 3e-6 \
--value_lr 1e-5
# Or use the script (Windows)
scripts\04_train_oreo_rl.bat
python main.py \
--mode test \
--base_model "Qwen/Qwen2.5-Math-1.5B" \
--ckpt_name oreo_policy_e10 \
--reasoning_dataset gsm8k
# Or use the script (Windows)
scripts\05_evaluate.bat
# Step 0: Download data
python dataset/download_data.py --dataset gsm8k
# Step 1: Collect trajectories
python -m dataset.trajectory_collector \
--base_model "Qwen/Qwen2.5-Math-1.5B" \
--data_file dataset/gsm8k_train.jsonl \
--output_file dataset/trajectories_gsm8k.jsonl \
--max_problems 100
# Step 2: SFT training
python main.py --mode oreo_sft \
--base_model "Qwen/Qwen2.5-Math-1.5B" \
--reasoning_dataset gsm8k \
--epochs 3 --batch_size 4
# Step 3: OREO RL training
python main.py --mode oreo_rl \
--trajectory_file dataset/trajectories_gsm8k.jsonl \
--epochs 10 --batch_size 4
# Step 4: Evaluation
python main.py --mode test \
--ckpt_name oreo_policy_e10 \
--reasoning_dataset gsm8k
| Parameter | Default | Description |
|---|---|---|
--base_model | Qwen/Qwen2.5-Math-1.5B | HuggingFace model name |
--max_seq_length | 512 | Maximum token sequence length |
--policy_lr | 3e-6 | Policy model learning rate |
--value_lr | 1e-5 | Value model learning rate |
--temperature | 0.1 | Temperature for soft Bellman equation |
--gamma | 0.99 | RL discount factor |
--batch_size | 4 | Batch size (per GPU) |
--epochs | 10 | Number of training epochs |
--use_gradient_checkpointing | true | Enable gradient checkpointing (saves memory) |
--share_backbone | true | Share transformer between policy and value |
# Use these settings for small GPU:
--batch_size 2 \
--use_gradient_checkpointing true \
--max_seq_length 256 \
--precision float16 # If supported
# Using torchrun (recommended)
torchrun --nproc_per_node=2 main.py \
--distributed true \
--mode oreo_sft \
...
# Effective batch size = batch_size * num_gpus
OREO/
βββ models/OREO/ # OREO models
β βββ policy_model.py # Policy (LLM)
β βββ value_model.py # Value function
β βββ oreo_config.py # Configuration
βββ dataset/ # Data handling
β βββ download_data.py # Download datasets
β βββ trajectory_collector.py # Collect trajectories
β βββ oreo_data_provider.py # Data loading
βββ learning/ # Training logic
β βββ oreo_task.py # OREO task class
β βββ oreo_engine.py # Training loops
βββ utils/ # Utilities
β βββ oreo_losses.py # Loss functions
β βββ oreo_metrics.py # Evaluation metrics
β βββ generation_utils.py # Text generation
β βββ answer_extraction.py # Answer parsing
βββ scripts/ # Training scripts
β βββ 01_download_data.bat
β βββ 02_collect_trajectories.bat
β βββ 03_train_sft.bat
β βββ 04_train_oreo_rl.bat
β βββ 05_evaluate.bat
βββ checkpoints/ # Saved models (created automatically)
GSM8K (Grade School Math, 8.5K problems)
python dataset/download_data.py --dataset gsm8kMATH (Competition Math)
python dataset/download_data.py --dataset mathTest (Small synthetic dataset)
python dataset/download_data.py --dataset testTrajectories are stored as JSONL with format:
{
"problem": "What is 25% of 80?",
"solution": "25% = 0.25. 0.25 * 80 = 20",
"predicted_answer": "20",
"ground_truth": "20",
"is_correct": true,
"reward": 1.0
}
--share_backbone true--batch_size 2 \
--use_gradient_checkpointing true \
--max_seq_length 256 \
--max_problems 50 # For trajectory collection
--batch_size 8 \
--epochs 5 \
--max_problems 50 # Fewer trajectories
--epochs 20 \
--policy_lr 1e-6 \ # Lower learning rate
--temperature 0.05 \ # Lower temperature
--max_problems 500 # More trajectories
Solution 1: Reduce batch size
--batch_size 1 # or 2
Solution 2: Enable gradient checkpointing
--use_gradient_checkpointing true
Solution 3: Reduce sequence length
--max_seq_length 256 # or 128
Solution 4: Use smaller model
--base_model "Qwen/Qwen2-0.5B" # 500M params instead of 1.5B
Solution 1: Use multi-GPU
torchrun --nproc_per_node=2 main.py --distributed true ...
Solution 2: Reduce data size
--max_problems 50 # Fewer trajectory examples
Solution 3: Fewer epochs
--epochs 5 # Instead of 10-20
Solution 1: Train longer
--epochs 20 # More epochs
Solution 2: More trajectories
--max_problems 500 # More training data
Solution 3: Adjust hyperparameters
--policy_lr 1e-6 \ # Lower LR
--temperature 0.05 # Lower temperature
Solution: Adjust generation parameters
--temperature 0.7 \ # Try 0.5-1.0
--top_p 0.9 \ # Try 0.8-0.95
--top_k 50 # Try 20-100
Example output:
==================================================
EVALUATION RESULTS
==================================================
accuracy: 0.7500
avg_reward: 0.7500
avg_trajectory_length: 3.2
num_predictions: 100
==================================================
| Method | GSM8K Accuracy | Training Data |
|---|---|---|
| Base Model | 55-60% | - |
| SFT Only | 65-70% | Solutions |
| OREO (SFT + RL) | 75-80% | Solutions + Trajectories |
{"problem": "...", "solution": "...", "answer": "..."}
Modify oreo_data_provider.py to load your dataset
Train with --reasoning_dataset custom
--base_model "meta-llama/Llama-2-7b" \ # LLaMA
--base_model "deepseek-ai/deepseek-math-7b-instruct" # DeepSeek
Key hyperparameters to tune:
policy_lr: Try 1e-6 to 1e-5value_lr: Try 3e-6 to 3e-5temperature: Try 0.05 to 0.2gamma: Try 0.95 to 0.99Paper: Offline Reinforcement Learning for LLM Multi-Step Reasoning
Original Implementation:
Pretrained Models:
jwhj/Qwen2.5-Math-1.5B-OREOjwhj/Qwen2.5-Math-1.5B-OREO-ValueQ: Do I need to collect trajectories? A: For full OREO training (RL phase), yes. For SFT only, no.
Q: How long does training take? A: SFT: 30-60 min, RL: 1-2 hours (GTX 1060, batch_size=2, 100 problems)
Q: Can I use my own dataset? A: Yes! Create JSONL with problem/solution/answer fields.
Q: What GPU do I need? A: Minimum 6GB (GTX 1060). Recommended: 8GB+ (RTX 3060).
Q: Does it work on CPU? A: Yes, but very slow (not recommended).
Q: Can I skip SFT and train RL directly? A: Not recommended. SFT provides good initialization for RL.
All OREO components have been implemented:
You're now ready to train OREO models for multi-step reasoning! Start with the Quick Start section above.
For questions or issues, refer to:
OREO_IMPLEMENTATION_PLAN.md (detailed technical plan)Happy reasoning! π§ β¨
1 commits
Python
92.5%
Shell
7.5%