What is Safe-RLHF?
Safe-RLHF (codenamed Beaver) is an open-source RLHF framework from the PKU-Alignment team at Peking University. It sits in the RLHF & Alignment category of the Training & Fine-Tuning tools and is open-source, written in Python and licensed Apache-2.0. To get started: clone and create the environment.
Overview
Safe-RLHF (codenamed Beaver) is an open-source RLHF framework from the PKU-Alignment team at Peking University. It covers the full alignment pipeline for popular pre-trained models like LLaMA, OPT, and Baichuan: supervised fine-tuning (SFT), reward and cost model training, standard RLHF, and the team's Safe RLHF method.
What sets it apart is how it handles safety. Instead of folding everything into a single reward, it trains a separate reward model for helpfulness and a separate cost model for harm, then optimizes the policy to maximize reward while keeping cost under a constraint. The result is a model that aims to be both helpful and harmless rather than trading one for the other.
It is built for alignment researchers and ML engineers who want a reproducible pipeline. The project also ships a large human-labeled preference dataset (PKU-SafeRLHF) and pre-trained Beaver-7B, reward, and cost checkpoints, so you can reproduce results or start from existing weights.
What it does
- Supports the full pipeline: SFT, reward and cost model training, RLHF, and Safe RLHF, all in one framework
- Separates helpfulness (reward) from safety (cost) so the policy is optimized under an explicit safety constraint
- Works with popular pre-trained models including LLaMA, OPT, and Baichuan
- Ships the PKU-SafeRLHF human-labeled preference dataset (up to 1M pairs of helpful and harmless preferences)
- Provides pre-trained Beaver-7B, reward, and cost checkpoints on Hugging Face
- Built on DeepSpeed, with multi-scale safety evaluation via BIG-bench and GPT-4 evaluation
Getting started
Safe-RLHF is installed from source via a conda environment, then run as a sequence of training scripts ending with an interactive CLI demo. The commands below come straight from the project's README.
Clone and create the environment
Clone the repository and build the conda environment from the provided recipe file.
git clone https://github.com/PKU-Alignment/safe-rlhf.git
cd safe-rlhf
conda env create --file conda-recipe.yamlRun supervised fine-tuning
Start the pipeline by fine-tuning a base model. Point it at any Hugging Face model name or local checkpoint.
bash scripts/sft.sh \
--model_name_or_path <your-model-name-or-checkpoint-path> \
--output_dir output/sftTrain reward and cost models, then run Safe RLHF
Train the helpfulness reward model and the safety cost model from the SFT checkpoint, then optimize the policy under the safety constraint with PPO-Lagrangian.
bash scripts/reward-model.sh --model_name_or_path output/sft --output_dir output/rm
bash scripts/cost-model.sh --model_name_or_path output/sft --output_dir output/cm
bash scripts/ppo-lag.sh \
--actor_model_name_or_path output/sft \
--reward_model_name_or_path output/rm \
--cost_model_name_or_path output/cm \
--output_dir output/ppo-lagChat with the trained model
Launch the interactive CLI demo to test your model.
python3 -m safe_rlhf.serve.cli --model_name_or_path output/ppo-lagCommands and code are distilled from the project's own documentation — always check the official repo for the latest.
When to use it
- Reproducing constrained alignment research where safety must be enforced as a separate constraint, not blended into one reward
- Fine-tuning an open base model (LLaMA, OPT, Baichuan) into a helpful and harmless chat assistant
- Training and evaluating reward and cost models on human preference data for safety-aware ranking
- Benchmarking a model's safety with the included BIG-bench and GPT-4 evaluation tooling
How Safe-RLHF compares
Safe-RLHF alongside other open-source rlhf & alignment tools AI/TLDR tracks, ranked by GitHub stars.
| Tool | Stars | What it does |
|---|---|---|
| Heretic | ★ 44k | A command-line tool that removes refusal behaviour from transformer language models by directional ablation, using an Optuna parameter search that co-minimizes refusals and KL divergence from the original model. |
| Open-R1 | ★ 26.5k | An open reproduction of the DeepSeek-R1 reasoning pipeline, with scripts for GRPO training and reasoning-data generation. |
| verl | ★ 24.4k | Volcano Engine's RL post-training framework (HybridFlow) for building GRPO, PPO, and other RL pipelines on top of FSDP, Megatron, and vLLM. |
| TRL | ★ 19.9k | Hugging Face's post-training library with trainers for SFT, reward modeling, DPO, PPO, and GRPO to align language models with preferences. |
| Agent Lightning | ★ 19k | An open-source trainer from Microsoft that improves AI agents built with any framework using reinforcement learning, prompt optimization, and supervised fine-tuning. |
| ART | ★ 10.9k | OpenPipe's Agent Reinforcement Trainer for post-training LLM agents on multi-step tasks using GRPO and rule- or judge-based rewards. |
| OpenRLHF | ★ 10.2k | A Ray- and vLLM-based RLHF framework that scales PPO, GRPO, and REINFORCE++ training to models with 70B+ parameters. |
| Safe-RLHF | ★ 1.6k | Train LLMs that stay helpful and harmless with separate reward and cost signals |

