---
title: "The N Implementation Details of RLHF with PPO"
description: "A from-scratch PyTorch reproduction of OpenAI's 2019 lm-human-preferences RLHF codebase closely matches the original learning curves on stylistic tasks like sentiment and descriptiveness."
url: "https://swe.quest/articles/the-n-implementation-details-of-rlhf-with-ppo-ed05a4d20cc991ad"
canonical: "https://huggingface.co/blog/the_n_implementation_details_of_rlhf_with_ppo"
source: "Hugging Face"
published: "2023-10-24"
difficulty: "hard"
kind: "Research"
reading_minutes: 24
tags: ["model-training","llms","python","machine-learning"]
---

# The N Implementation Details of RLHF with PPO

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2023-10-24, 24 min read, Research. Original: [https://huggingface.co/blog/the\_n\_implementation\_details\_of\_rlhf\_with\_ppo](https://huggingface.co/blog/the_n_implementation_details_of_rlhf_with_ppo)

## Summary

A from-scratch PyTorch reproduction of OpenAI's 2019 lm-human-preferences RLHF codebase closely matches the original learning curves on stylistic tasks like sentiment and descriptiveness. Along the way it catalogs the implementation details that matter for faithful replication: reward model and value head scoring the concatenated query and response, a dedicated out-of-vocabulary padding token, position-index adjustment for padding, fixed-length sampling that ignores EOS, learning-rate annealing, per-process seeds, reward normalization, rejection sampling and reward whitening. It also documents a TensorFlow vs PyTorch Adam numerical difference that causes overly aggressive updates, and how different base models behave when reward labels come from gpt2-large. Practical notes show how to replicate each behaviour with Hugging Face transformers, along with the hardware constraints of running the original TF1 code.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)
- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)

## Prerequisites

- PPO reinforcement learning ([easy posts on Model training](https://swe.quest/tags/model-training.md?difficulty=easy))
- transformer language models
- Hugging Face transformers
- reward modeling

## Keywords

RLHF, PPO, lm-human-preferences, Hugging Face transformers, GPT-2, PyTorch, TensorFlow, Adam

## Related posts

- [StackLLaMA: A hands-on guide to train LLaMA with RLHF](https://swe.quest/articles/stackllama-a-hands-on-guide-to-train-llama-with-rlhf-9d42cc2a0ae4620b.md): Hugging Face · 2023-04-05 · Hard · 14 min read · Model training, LLMs, Machine learning, Python
  A 7B LLaMA model is trained to answer Stack Exchange questions with the full RLHF pipeline: supervised fine-tuning on packed Stack Exchange text, a pairwise reward model trained on upvote-derived preference scores, and PPO with a KL penalty against a frozen reference model.
- [Putting RL back in RLHF](https://swe.quest/articles/putting-rl-back-in-rlhf-dbdbc5d62840e65d.md): Hugging Face · 2024-06-12 · Hard · 11 min read · Model training, LLMs, Python
  RLOO (REINFORCE Leave-One-Out) is now available as a trainer in Hugging Face TRL.
- [Fine-tune Llama 2 with DPO](https://swe.quest/articles/fine-tune-llama-2-with-dpo-68d2e917aae1caca.md): Hugging Face · 2023-08-08 · Hard · 7 min read · Model training, LLMs, Python
  Direct Preference Optimization (DPO) replaces the reward-model and RL steps of RLHF with a simple binary cross-entropy loss over preference pairs, using a frozen reference model to provide an implicit reward.
- [Toward RL on mixed-precision, mixed-accelerator samplers](https://swe.quest/articles/toward-rl-on-mixed-precision-mixed-accelerator-samplers-1c0dc5519077aec1.md): Modal · 2026-10-13 · Hard · 17 min read · Model training, Inference & serving, LLMs, Hardware, Cost optimization
  RL training of Qwen3.6-35B-A3B on agentic coding stays stable when samplers run on six accelerator types (A100 through B300, RTX PRO 6000) in NVFP4, FP8 and BF16 with their own kernels, feeding a BF16 trainer on B200.
- [vLLM V0 to V1: Correctness Before Corrections in RL](https://swe.quest/articles/vllm-v0-to-v1-correctness-before-corrections-in-rl-4dc68bef7cff8909.md): Hugging Face · 2026-05-06 · Hard · 7 min read · Model training, Inference & serving, LLMs, Debugging
  Migrating PipelineRL's rollout generation from vLLM V0 (0.8.5) to V1 (0.18.1) caused training metrics (clip rate, KL, entropy, reward) to diverge.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
