# Alignment Handbook

> Hugging Face recipes and scripts for the full SFT-then-preference-alignment pipeline

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

## In short

Alignment Handbook is Hugging Face recipes and scripts for the full SFT-then-preference-alignment pipeline. It walks through the whole pipeline used to produce models like Zephyr-7B: continued pretraining, supervised fine-tuning (SFT) for chat, and preference alignment with DPO or ORPO. 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.

## Overview

The Alignment Handbook is a collection of training recipes and scripts from Hugging Face for building aligned chat models. It walks through the whole pipeline used to produce models like Zephyr-7B: continued pretraining, supervised fine-tuning (SFT) for chat, and preference alignment with DPO or ORPO.

It is aimed at ML engineers and researchers who want to reproduce known aligned models or train their own on custom datasets. Each training run is described by a single YAML recipe file that holds all the parameters, so you change configs rather than rewriting code.

Within the fine-tuning and RLHF/alignment space, it sits one level above raw training libraries: the scripts are thin wrappers over the Hugging Face stack, and they support full-weight distributed training with DeepSpeed ZeRO-3 as well as parameter-efficient LoRA/QLoRA.

## What it does

- Covers the full pipeline: continued pretraining, SFT, reward modeling, rejection sampling, DPO, and ORPO
- Reproducible recipes as YAML files (e.g. Zephyr-7B, SmolLM, StarChat2) that hold every parameter for a run
- Ready-made scripts for SFT (sft.py) and preference alignment (dpo.py) launched with accelerate
- Supports full-weight distributed training via DeepSpeed ZeRO-3, plus LoRA/QLoRA for parameter-efficient tuning
- Instructions and formatting guidance for fine-tuning chat models on your own datasets
- Built on the Hugging Face ecosystem with documented dataset and model collections

## Getting started

Set up a Python environment with the pinned dependencies, then launch a training run from one of the provided recipe YAML files. The example below reproduces the Zephyr-7B-beta pipeline.

1. Create a virtual environment and install dependencies

   Use uv to create an environment, install the pinned PyTorch build, then install the handbook package and Flash Attention 2.

```bash
uv venv handbook --python 3.11 && source handbook/bin/activate && uv pip install --upgrade pip
uv pip install torch==2.6.0 --index-url https://download.pytorch.org/whl/cu126
uv pip install .
uv pip install "flash-attn==2.7.4.post1" --no-build-isolation
```

2. Log in to Hugging Face

   Authenticate so you can pull base models and datasets and push your trained model.

```bash
huggingface-cli login
```

3. Run supervised fine-tuning (SFT)

   Launch the SFT script with a recipe config, using the ZeRO-3 accelerate config for distributed training.

```bash
ACCELERATE_LOG_LEVEL=info accelerate launch --config_file recipes/accelerate_configs/zero3.yaml scripts/sft.py --config recipes/zephyr-7b-beta/sft/config_full.yaml
```

4. Align with DPO

   Take the SFT model and align it to preferences using direct preference optimization.

```bash
ACCELERATE_LOG_LEVEL=info accelerate launch --config_file recipes/accelerate_configs/zero3.yaml scripts/dpo.py --config recipes/zephyr-7b-beta/dpo/config_full.yaml
```

## When to use it

- Reproduce a published aligned chat model such as Zephyr-7B from its recipe
- Fine-tune and align an open base model on your own instruction and preference datasets
- Compare alignment methods (DPO vs. KTO vs. IPO, or ORPO) using the included recipes
- Adapt a model to a new language or domain through continued pretraining, then SFT and DPO
