ShVaSa Blog
Tech & AI • 6 min read

I Ran the Full RLHF Pipeline on a Free GPU, Then Pitted PPO Against DPO

Published on 2026-11-13 by Sharath Japa, ShVaSa Lab Lead
aitechdeep-learningmultimodal
Discuss on r/shvasa

Executive Summary

The transition from Supervised Fine-Tuning (SFT) to Reinforcement Learning from Human Feedback (RLHF) represents a critical inflection point in aligning Large Language Models (LLMs) with complex human preferences. While Direct Preference Optimization (DPO) has gained traction for its simplicity, bypassing the explicit reward modeling and policy optimization loop of Proximal Policy Optimization (PPO), the full RLHF pipeline remains the gold standard for robustness in high-stakes alignment tasks. This analysis dissects the engineering realities of running the complete RLHF stack—SFT, Reward Modeling, and PPO—on constrained hardware, specifically contrasting the memory-intensive actor-critic architecture of PPO against the offline, batch-optimized nature of DPO. The core challenge lies not merely in algorithmic convergence but in the system-level bottlenecks of GPU memory bandwidth, gradient checkpointing efficiency, and the stability of the KL-divergence penalty term. We demonstrate that while DPO offers a streamlined implementation path, PPO provides finer-grained control over the policy space, provided the infrastructure can handle the synchronous communication overhead between the actor and critic models.

Implementation Architecture

The full RLHF pipeline consists of three distinct phases, each requiring specific hardware optimizations. The first phase, SFT, is a standard next-token prediction task. The second, Reward Modeling (RM), involves training a separate model (often sharing the SFT backbone) to score pairs of responses. The third, PPO, is the most complex, involving an actor model generating responses, a critic model estimating value, and a reward model providing the scalar signal.

For a realistic implementation on a single GPU or a small cluster, memory management is paramount. We utilize transformers for model loading and trl (Transformer Reinforcement Learning) for the PPO loop. The key engineering decision is the use of 8-bit or 4-bit quantization for the critic and reward models to free up VRAM for the actor's activation gradients.

Below is a Python implementation snippet demonstrating the initialization of the PPO pipeline with critical hyperparameters for stability. This code highlights the necessity of configuring the ppo_config to balance the KL penalty, which prevents the policy from deviating too far from the reference SFT model.

PYTHON
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from trl import PPOTrainer, PPOConfig, AutoModelForCausalLMWithValueHead
from datasets import load_dataset

# 1. Load Models and Tokenizer
model_name = "facebook/opt-350m" # Small model for demonstration
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLMWithValueHead.from_pretrained(model_name)
ref_model = AutoModelForCausalLM.from_pretrained(model_name)

# 2. Configure PPO Hyperparameters
# Key: kl_coef controls the divergence from the reference model
# Key: batch_size must fit within GPU memory
ppo_config = PPOConfig(
    model_name=model_name,
    learning_rate=1.41e-5,
    log_with="tensorboard",
    batch_size=4,
    mini_batch_size=1,
    gradient_accumulation_steps=4,
    ppo_epochs=4,
    seed=42,
    optimize_cuda_cache=True,
    kl_coef=0.02, # Critical: too high stifles learning, too low causes mode collapse
)

# 3. Initialize Trainer
# Note: In a full pipeline, you would load the reward model here
# reward_model = AutoModelForSequenceClassification.from_pretrained("reward_model_path")

ppo_trainer = PPOTrainer(
    config=ppo_config,
    model=model,
    ref_model=ref_model,
    tokenizer=tokenizer,
    # dataset=your_dataset,
    # reward_model=reward_model,
)

print("PPO Pipeline Initialized. Ready for training loop.")

ShVaSa Intuition

The fundamental divergence between PPO and DPO lies in how they handle the optimization objective. PPO is an online, on-policy algorithm. It generates data, evaluates it, and updates the policy in a loop. This creates a dynamic feedback system where the data distribution shifts with every update. The intuition here is that the model must learn to navigate a moving target, using the critic to estimate the value of states and the reward model to provide sparse feedback. The bottleneck is often the "advantage estimation" step, where the variance of the reward signal can destabilize training.

DPO, conversely, reframes RLHF as a classification problem. It removes the explicit reward model and critic, directly optimizing the policy to maximize the probability of preferred responses over rejected ones, subject to a KL constraint. The intuition is that if you have a good dataset of preference pairs, you don't need to learn a separate reward function; you can learn the policy directly. This reduces the system complexity significantly, eliminating the need for synchronous actor-critic communication.

However, the trade-off is data efficiency and stability. PPO can adapt to noisy reward signals by leveraging the critic's value function, whereas DPO is sensitive to the quality of the preference dataset. If the preference data is noisy, DPO can overfit to artifacts in the data. PPO, with its larger parameter space (actor + critic), has more capacity to generalize, but at the cost of higher computational overhead and memory usage.

System Bottlenecks and Optimization

When running this pipeline on free or limited GPU resources, several bottlenecks emerge:

  1. Memory Bandwidth: The actor and critic models are loaded into VRAM simultaneously during PPO training. For a 7B parameter model, this requires significant VRAM. Quantization (e.g., using bitsandbytes) is essential but introduces compute overhead.
  2. Gradient Checkpointing: To save memory, gradient checkpointing is used, which trades compute for memory. This is critical for fitting large models into a single GPU.
  3. Scheduling Overhead: In PPO, the actor generates sequences, which are then passed to the reward and critic models. This sequential dependency creates idle time for the GPU. Using asynchronous data loading and prefetching can mitigate this.
  4. KL Divergence Control: The KL penalty term is crucial. If the policy deviates too much from the SFT model, the generated text may become incoherent. Tuning the kl_coef is an art, requiring careful monitoring of the KL divergence metric during training.

Architecture Diagram

The following Mermaid diagram illustrates the data flow and component interactions in the PPO-based RLHF pipeline, highlighting the synchronous nature of the actor-critic loop.

graph TD
    subgraph "Data Preparation"
        A[Raw Text Data] --> B[SFT Training]
        B --> C[SFT Model]
        C --> D[Preference Data Generation]
        D --> E[Human/Model Labeling]
        E --> F[Preference Dataset]
    end

    subgraph "Reward Modeling"
        C --> G[Reward Model Training]
        G --> H[Reward Model]
    end

    subgraph "PPO Loop"
        I[Actor Model] --> J[Generate Response]
        J --> K[Reward Model]
        K --> L[Reward Score]
        J --> M[Critic Model]
        M --> N[Value Estimate]
        L & N --> O[Advantage Calculation]
        O --> P[Policy Update]
        P --> I
        C -.-> Q[Reference Model]
        Q --> R[KL Penalty]
        R --> P
    end

    F --> D
    H --> K

This architecture underscores the complexity of PPO: it requires maintaining multiple model instances (Actor, Critic, Reference, Reward) and coordinating their interactions in real-time. DPO simplifies this by removing the Critic and Reward Model from the training loop, relying solely on the Actor and the pre-computed preference data. For engineers constrained by hardware, DPO offers a pragmatic path to alignment, but for those seeking maximum performance and control, the full PPO pipeline remains the superior, albeit more demanding, choice.


Grounding & Source Attribution

Author Avatar

Sharath Japa, ShVaSa Lab Lead

Systems & AI Infrastructure Researcher, DGX Station homelab lead. Engineering RL environments, edge silicon inference, and local reasoning model distillation. Verified Profiles: Google Scholar • Google Patents • LinkedIn • GitHub (@sharathjapa) • Medium • r/shvasa.


Canonical Source: https://shvasa.us/posts/i-ran-the-full-rlhf-pipeline-on-a-free-gpu-then-pitted-ppo-against-dpo

Advertisement

Related Research & Architectural Blueprints