█

AI/TLDR

Buy me a coffee
Buy me a coffee

Safe-RLHF

Train LLMs that stay helpful and harmless with separate reward and cost signals

RLHF & AlignmentOpen source
Language
Python
License
Apache-2.0
$git clone https://github.com/PKU-Alignment/safe-rlhf.git
IN SHORT

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.

bashbash
git clone https://github.com/PKU-Alignment/safe-rlhf.git
cd safe-rlhf
conda env create --file conda-recipe.yaml

Run supervised fine-tuning

Start the pipeline by fine-tuning a base model. Point it at any Hugging Face model name or local checkpoint.

bashbash
bash scripts/sft.sh \
    --model_name_or_path <your-model-name-or-checkpoint-path> \
    --output_dir output/sft

Train 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.

bashbash
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-lag

Chat with the trained model

Launch the interactive CLI demo to test your model.

bashbash
python3 -m safe_rlhf.serve.cli --model_name_or_path output/ppo-lag

Commands 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.

ToolStarsWhat it does
Heretic★ 44kA 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.5kAn open reproduction of the DeepSeek-R1 reasoning pipeline, with scripts for GRPO training and reasoning-data generation.
verl★ 24.4kVolcano Engine's RL post-training framework (HybridFlow) for building GRPO, PPO, and other RL pipelines on top of FSDP, Megatron, and vLLM.
TRL★ 19.9kHugging Face's post-training library with trainers for SFT, reward modeling, DPO, PPO, and GRPO to align language models with preferences.
Agent Lightning★ 19kAn open-source trainer from Microsoft that improves AI agents built with any framework using reinforcement learning, prompt optimization, and supervised fine-tuning.
ART★ 10.9kOpenPipe's Agent Reinforcement Trainer for post-training LLM agents on multi-step tasks using GRPO and rule- or judge-based rewards.
OpenRLHF★ 10.2kA Ray- and vLLM-based RLHF framework that scales PPO, GRPO, and REINFORCE++ training to models with 70B+ parameters.
Safe-RLHF★ 1.6kTrain LLMs that stay helpful and harmless with separate reward and cost signals