|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
Production-ready GRPO training example for online RL. |
|
|
|
|
|
GRPO (Group Relative Policy Optimization) is an online RL method that |
|
|
optimizes relative to group performance. Best for tasks with automatic |
|
|
reward signals like code execution or math verification. |
|
|
|
|
|
Usage with hf_jobs MCP tool: |
|
|
hf_jobs("uv", { |
|
|
"script": '''<paste this entire file>''', |
|
|
"flavor": "a10g-large", |
|
|
"timeout": "4h", |
|
|
"secrets": {"HF_TOKEN": "$HF_TOKEN"}, |
|
|
}) |
|
|
|
|
|
Or submit the script content directly inline without saving to a file. |
|
|
|
|
|
Note: For most GRPO use cases, the TRL maintained script is recommended: |
|
|
https://raw.githubusercontent.com/huggingface/trl/main/examples/scripts/grpo.py |
|
|
""" |
|
|
|
|
|
import trackio |
|
|
from datasets import load_dataset |
|
|
from trl import GRPOTrainer, GRPOConfig |
|
|
|
|
|
|
|
|
trackio.init( |
|
|
project="qwen-grpo-math", |
|
|
space_id="username/trackio", |
|
|
config={ |
|
|
"model": "Qwen/Qwen2.5-0.5B-Instruct", |
|
|
"dataset": "trl-lib/math_shepherd", |
|
|
"method": "GRPO", |
|
|
} |
|
|
) |
|
|
|
|
|
|
|
|
dataset = load_dataset("trl-lib/math_shepherd", split="train") |
|
|
print(f"β
Dataset loaded: {len(dataset)} prompts") |
|
|
|
|
|
|
|
|
config = GRPOConfig( |
|
|
|
|
|
output_dir="qwen-grpo-math", |
|
|
push_to_hub=True, |
|
|
hub_model_id="username/qwen-grpo-math", |
|
|
hub_strategy="every_save", |
|
|
|
|
|
|
|
|
num_train_epochs=1, |
|
|
per_device_train_batch_size=4, |
|
|
gradient_accumulation_steps=4, |
|
|
learning_rate=1e-6, |
|
|
|
|
|
|
|
|
logging_steps=10, |
|
|
save_strategy="steps", |
|
|
save_steps=100, |
|
|
save_total_limit=2, |
|
|
|
|
|
|
|
|
warmup_ratio=0.1, |
|
|
lr_scheduler_type="cosine", |
|
|
|
|
|
|
|
|
report_to="trackio", |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
trainer = GRPOTrainer( |
|
|
model="Qwen/Qwen2.5-0.5B-Instruct", |
|
|
train_dataset=dataset, |
|
|
args=config, |
|
|
) |
|
|
|
|
|
print("π Starting GRPO training...") |
|
|
trainer.train() |
|
|
|
|
|
print("πΎ Pushing to Hub...") |
|
|
trainer.push_to_hub() |
|
|
|
|
|
|
|
|
trackio.finish() |
|
|
|
|
|
print("β
Complete! Model at: https://huggingface.co/username/qwen-grpo-math") |
|
|
print("π View metrics at: https://huggingface.co/spaces/username/trackio") |
|
|
|