用户可通过该项目学习如何从零实现 Group Relative Policy Optimization (GRPO),它是一个清晰的教育示例,展示了现代语言模型对齐技术,帮助理解LLM对齐原理与实践。【此简介由AI生成】
| Files | Last commit | Last update |
|---|---|---|
| 1 year ago | ||
| 1 year ago | ||
| 1 year ago | ||
| 1 year ago | ||
| 1 year ago | ||
| 1 year ago | ||
| 1 year ago |
A minimal example for GRPO from Scratch
This repository accompanies a Medium blog post: Hands-On LLM Alignment: Coding GRPO from Scratch, Step by Step, explaining how to implement Group Relative Policy Optimization (GRPO) from scratch. The implementation provides a clear, educational example of modern alignment techniques for language models.
If you find any issues with the code or have suggestions for improvements, please feel free to raise a GitHub issue or open a pull request.
Setup
1. Install Dependencies
Install all packages except flash-attn, then all packages (flash-attn is weird)
git clone https://github.com/baicenxiao/hands-on-grpo-from-scratch.git
cd hands-on-grpo-from-scratch
uv sync --no-install-package flash-attn
uv sync
2. Download GSM8K Dataset
Download GSM8K JSONL files from the official repository:
# from project root
git clone https://github.com/openai/grade-school-math.git
3. Download Model Weights
Download Qwen2.5-Math-1.5B weights to a local directory:
# Ensure you're authenticated first:
# huggingface-cli login
huggingface-cli download Qwen/Qwen2.5-Math-1.5B \
--local-dir ./Qwen2.5-Math-1.5B \
--local-dir-use-symlinks False
4. Run baseline before GRPO
uv run src/math_baseline.py
5. Run GRPO finetuning
# You need to log into your wandb account for logging
uv run src/train_grpo.py sample_config.yaml
References
The environment setup and dependency management follows the approach used in Stanford CS336 Assignment 5: Alignment, providing a robust foundation for machine learning experimentation.
Papers
- GRPO Paper: DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models - The original GRPO paper from DeepSeek-AI.
Libraries
- Hugging Face TRL: Transformer Reinforcement Learning - A library for training transformer language models with reinforcement learning, including RLHF and alignment techniques. We do not directly use TRL, but it is an important reference.
Introduction
用户可通过该项目学习如何从零实现 Group Relative Policy Optimization (GRPO),它是一个清晰的教育示例,展示了现代语言模型对齐技术,帮助理解LLM对齐原理与实践。【此简介由AI生成】
Customize your domain