Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Workflow:Huggingface Trl PPO RLHF Training

From Leeroopedia


Knowledge Sources
Domains LLMs, Reinforcement_Learning, RLHF
Last Updated 2026-02-06 16:00 GMT

Overview

End-to-end process for training language models using Proximal Policy Optimization (PPO) with a learned reward model, implementing the classic RLHF pipeline of policy, value, reward, and reference models.

Description

This workflow implements the full Reinforcement Learning from Human Feedback (RLHF) pipeline using PPO, the algorithm used to train InstructGPT and early ChatGPT models. It requires four models operating together: a policy model (the model being trained), a reference model (frozen copy for KL regularization), a reward model (trained separately on preference data), and a value model (critic that estimates expected reward). The policy generates completions, the reward model scores them, and the value model provides baselines for variance reduction. PPO then updates the policy using clipped surrogate objectives. This is currently in TRL's experimental module.

Usage

Execute this workflow when you have a trained reward model and want to apply the full RLHF pipeline with explicit value function estimation. PPO provides more stable training than simpler policy gradient methods at the cost of additional model complexity (four models in memory). Consider using GRPO or RLOO instead for a simpler setup that does not require a value model.

Execution Steps

Step 1: Environment and Argument Configuration

Configure the PPO training run including all four model paths, dataset source, and PPO-specific hyperparameters. PPO requires careful tuning of the balance between exploration and exploitation.

Key considerations:

  • total_episodes defines the total number of training episodes (prompts processed)
  • num_ppo_epochs controls how many passes over each batch of experience
  • num_mini_batches splits each batch into mini-batches for gradient updates
  • missing_eos_penalty penalizes completions that do not end with an EOS token
  • local_rollout_forward_batch_size controls the batch size during generation rollouts

Step 2: Tokenizer and Model Loading

Load all four models required for the PPO pipeline. Each model has a specific role and architecture. The tokenizer must be shared and properly configured with padding tokens.

What is loaded:

  • Policy model: AutoModelForCausalLM from the SFT checkpoint, the model being trained
  • Reference model: AutoModelForCausalLM from the same SFT checkpoint, frozen throughout training (set to None if using PEFT)
  • Reward model: AutoModelForSequenceClassification from a separately trained reward model checkpoint
  • Value model: AutoModelForSequenceClassification initialized from the reward model checkpoint, trained alongside the policy

Key considerations:

  • The tokenizer needs an explicit pad_token if not present (typically set to EOS token)
  • The value model is initialized from the reward model but has separate trainable parameters
  • With PEFT/LoRA on the policy, the reference model can be omitted (base model serves as reference)
  • All models should use the same dtype (bfloat16 recommended)

Step 3: Prompt Dataset Loading and Preparation

Load and pre-tokenize the prompt dataset. PPO requires pre-tokenized prompts for efficient rollout generation. The dataset is filtered and validated before training.

Key considerations:

  • Pre-tokenize prompts using the shared tokenizer
  • Filter examples that exceed the maximum prompt length
  • Validate that prompts do not end with the EOS token (to allow generation)
  • Use PartialState context manager for distributed-friendly dataset processing
  • Split into train and evaluation sets

Step 4: PPO Trainer Initialization

Create the PPOTrainer with all four models, the tokenized dataset, and training configuration. The trainer orchestrates the complex interaction between generation, reward scoring, value estimation, and policy updates.

Key considerations:

  • The trainer manages GPU memory for all four models simultaneously
  • Generation parameters (temperature, top_k) control completion diversity
  • response_length limits the maximum generated response length
  • stop_token can be set to "eos" to stop generation at EOS tokens

Step 5: PPO Training Loop

The training loop executes the full PPO algorithm: generate completions, compute rewards and values, calculate advantages, and update both the policy and value model.

What happens per training iteration:

  • Rollout phase: Policy generates completions for a batch of prompts
  • Scoring phase: Reward model scores each prompt-completion pair
  • Value estimation: Value model estimates expected returns at each token position
  • Advantage computation: GAE (Generalized Advantage Estimation) computes token-level advantages
  • Policy update: Clipped surrogate objective prevents overly large policy changes
  • Value update: Value model is updated to better predict rewards

Key considerations:

  • KL divergence from the reference model regularizes training
  • The clipping mechanism prevents catastrophic policy updates
  • Multiple PPO epochs on each batch improve sample efficiency
  • Monitor policy_loss, value_loss, kl, and reward/mean for training health

Step 6: Model Saving and Evaluation

Save the trained policy model and optionally generate sample completions for qualitative evaluation.

Key considerations:

  • Only the policy model (and its LoRA adapters) is saved; other models are discarded
  • Use generate_completions to produce sample outputs for human evaluation
  • Push to HuggingFace Hub for deployment
  • The trained model should show improved performance on the reward model's criteria

Execution Diagram

GitHub URL

Workflow Repository