# Safe-RLHF

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

- Category: Training & Fine-Tuning / RLHF & Alignment
- Language: Python
- Licence: Apache-2.0
- Repository: https://github.com/PKU-Alignment/safe-rlhf
- Canonical page: https://ai-tldr.dev/tools/safe-rlhf/

## In short

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.

1. Clone and create the environment

   Clone the repository and build the conda environment from the provided recipe file.

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

2. Run supervised fine-tuning

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

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

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

```bash
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
```

4. Chat with the trained model

   Launch the interactive CLI demo to test your model.

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

## 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
