Reinforcement Learning from Human Feedback
local graph
Chapter 18 — Reinforcement Learning from Human Feedback
Section titled “Chapter 18 — Reinforcement Learning from Human Feedback”In this chapter, we’ll dive into Reinforcement Learning from Human Feedback (RLHF), a powerful technique for aligning LLMs with human preferences. RLHF combines reinforcement learning with human feedback to fine-tune language models. It aims to align the model’s outputs with human preferences, improving the quality and safety of generated text.
RLHF differs from standard supervised fine-tuning by optimizing for human preferences rather than predefined correct answers. While supervised learning minimizes loss against labeled examples, RLHF creates a reward model from human comparisons between model outputs and then uses this reward function (typically with proximal policy optimization (PPO)) to update the model’s policy. The process typically employs a divergence penalty to prevent excessive drift from the initial model distribution.
The key benefits of RLHF are as follows:
- Improved alignment of models with human values and preferences
- Enhanced control over model outputs
- Reduction of harmful or biased content
- Ability to optimize for specific task performance
By the end of this chapter, you’ll be able to implement RLHF techniques to improve the alignment and output quality of your LLMs.
In this chapter, we’ll be covering the following topics:
- Components of RLHF systems
- Scaling RLHF
- Limitations of RLHF in language modeling
- Applications of RLHF
Components of RLHF systems
Section titled “Components of RLHF systems”A typical RLHF system for LLMs consists of three main components:
- Base language model: The pre-trained LLM to be fine-tuned
- Reward model: A model trained on human preferences to provide feedback
- Policy optimization: The process of updating the base model using the reward signal
The base language model serves as the starting point. This is the general-purpose large language model that has already undergone extensive pre-training on large-scale corpora using self-supervised objectives such as next-token prediction. At this stage, the model is capable of generating coherent language and demonstrating broad linguistic competence. However, it lacks alignment with human preferences, task-specific objectives, or context-dependent behavior expected in real-world deployment. This pre-trained model is the substrate upon which subsequent tuning is performed. Its architecture, training regime, and scaling have already been well-documented in literature, and since RLHF builds upon it without altering its fundamental structure, further detailing it is unnecessary here.
Instead, let us focus on the reward model and the policy optimization component, which work together to guide and reshape the output distribution of the base model based on human-aligned criteria. These two parts introduce the core mechanisms of feedback-driven adaptation and reinforcement tuning and will be examined in the following sections.
Reward model
Section titled “Reward model”Let’s implement a basic structure for the reward model:
import torchfrom transformers import AutoModelForCausalLM, AutoTokenizerclass RLHFSystem: def __init__(self, base_model_name, reward_model_name): self.base_model = AutoModelForCausalLM.from_pretrained( base_model_name) self.reward_model = \ AutoModelForSequenceClassification.from_pretrained( reward_model_name ) self.tokenizer = AutoTokenizer.from_pretrained( base_model_name) def generate_text(self, prompt): inputs = self.tokenizer(prompt, return_tensors="pt") outputs = self.base_model.generate(inputs, max_length=100) return self.tokenizer.decode(outputs[0], skip_special_tokens=True) def get_reward(self, text): inputs = self.tokenizer(text, return_tensors="pt") with torch.no_grad(): outputs = self.reward_model(inputs) return outputs.logits.item()This class sets up the basic structure for an RLHF system, including the base language model and the reward model. The generate_text method produces text from a given prompt, while get_reward estimates the reward for a given text using the reward model.
The reward model is central to the RLHF process, as it translates human preferences into a learnable signal. Trained on datasets consisting of human comparisons between model outputs—where evaluators choose the better of two responses—it learns to predict how a human might rate any given response. During the reinforcement learning phase, this reward model serves as an automated proxy for human judgment, allowing the base model to receive immediate feedback on thousands of generated outputs. The policy model (the language model being optimized) then learns to maximize these predicted reward scores through techniques such as PPO, gradually shifting its behavior toward generating responses that better align with human preferences while maintaining coherence and capabilities through divergence constraints. This creates a powerful feedback loop that enables continuous alignment with human values, something that would be impossible with static supervised datasets.
Here’s a simple implementation of reward model training:
from torch.utils.data import DataLoader, Datasetfrom transformers import Trainer, TrainingArgumentsclass FeedbackDataset(Dataset): def __init__(self, texts, labels): self.texts = texts self.labels = labels def __len__(self): return len(self.texts) def __getitem__(self, idx): return {"text": self.texts[idx], "label": self.labels[idx]} def train_reward_model(model, tokenizer, texts, labels): dataset = FeedbackDataset(texts, labels) def tokenize_function(examples): return tokenizer(examples["text"], padding="max_length", truncation=True) tokenized_dataset = dataset.map(tokenize_function, batched=True) training_args = TrainingArguments( output_dir="./results", num_train_epochs=3, per_device_train_batch_size=8, learning_rate=2e-5, ) trainer = Trainer( model=model, args=training_args, train_dataset=tokenized_dataset, ) trainer.train() return modelThis code sets up a dataset of human feedback and trains the reward model using the Hugging Face Trainer API. The reward model learns to predict human preferences based on the provided labels.
Policy optimization
Section titled “Policy optimization”Policy optimization is the process of updating the base language model using the rewards from the reward model. A common approach is PPO, which strikes a balance between ease of implementation, sample efficiency, and reliable performance. The term “proximal” in PPO refers to its key innovation: limiting how much the policy can change in each training step to prevent harmful large updates. It does this by using a “clipped” objective function that discourages updates that would move the policy too far from its previous version. PPO has become especially popular in AI alignment and RLHF because it’s more stable than other policy gradient methods – it helps with avoiding the problem where model updates become too aggressive and destroy previously learned good behaviors. When used in language models, PPO helps gradually shift the model’s outputs to better match human preferences while maintaining coherent and fluent text generation.
Here’s a simplified implementation of PPO for LLMs:
def ppo_step( base_model, reward_model, optimizer, prompt, num_iterations=5): for _ in range(num_iterations): # Generate text outputs = base_model.generate(prompt, max_length=100, return_dict_in_generate=True, output_scores=True ) generated_text = tokenizer.decode( outputs.sequences[0], skip_special_tokens=True ) # Get reward reward = reward_model(generated_text) # Compute policy loss log_probs = outputs.scores[0].log_softmax(dim=-1) policy_loss = -log_probs * reward # Update model optimizer.zero_grad() policy_loss.mean().backward() optimizer.step() return base_modelThis function performs a single step of PPO, generating text, computing rewards, and updating the base model’s parameters to maximize the expected reward. Keep in mind that this PPO code is illustrative; actual implementations may require more around rewards and safety checks.
Direct preference optimization (DPO) is another approach in RLHF that focuses on aligning models with human preferences by directly optimizing for preferred outcomes. Unlike traditional RL methods, which often rely on reward models to guide learning, DPO simplifies the process by using pairs of preferred and dispreferred outputs to adjust the model’s behavior. This method enhances efficiency and effectiveness in training models so that they generate outputs that align more closely with human expectations.
DPO might be preferred over PPO when computational efficiency and implementation simplicity are priorities. This is because DPO eliminates the need for separate reward model training and complex reinforcement learning optimization loops. It offers a more streamlined approach by directly updating policy parameters from preference data, which can be particularly valuable in scenarios with limited resources or when PPO training exhibits instability or reward hacking. DPO can also make better use of limited human preference datasets without the intermediate step of reward modeling. Additionally, it provides a cleaner experimental setup for studying how preferences directly impact model behavior without the confounding factors introduced by separate reward models and reinforcement learning optimization.
Here’s a short code example demonstrating how to implement DPO using Python:
from transformers import AutoModelForCausalLM, AutoTokenizerfrom trl import DPOTrainer# Load a pre-trained language model and tokenizermodel_name = "gpt2"model = AutoModelForCausalLM.from_pretrained(model_name)tokenizer = AutoTokenizer.from_pretrained(model_name)# Define the dataset containing human preference pairs# Each entry in the dataset is a tuple (prompt, preferred_completion, dispreferred_completion)dataset = [ ("Prompt 1", "Preferred Completion 1", "Dispreferred Completion 1"), ("Prompt 2", "Preferred Completion 2", "Dispreferred Completion 2"), # Add more data as needed]# Initialize the DPO Trainertrainer = DPOTrainer( model=model, tokenizer=tokenizer, dataset=dataset, beta=0.1 # Hyperparameter controlling the strength of preference optimization)# Train the model using DPOtrainer.train()# Save the fine-tuned modelmodel.save_pretrained("fine-tuned-model")tokenizer.save_pretrained("fine-tuned-model")This code snippet demonstrates how to set up and train a language model using DPO, allowing it to better align with human feedback by directly optimizing for preferred completions.
Having discussed PPO and DPO, next, we’ll examine scaling strategies for RLHF regarding large-scale models.
Scaling RLHF
Section titled “Scaling RLHF”Scaling RLHF to large models presents challenges due to computational requirements. Here are some strategies that can be implemented:
- Distributed training: This involves partitioning the training workload across multiple devices – typically GPUs or TPUs – by employing data parallelism, model parallelism, or pipeline parallelism. In data parallelism, the same model is replicated across devices, and each replica processes a different mini-batch of data. Gradients are averaged and synchronized after each step. On the other hand, model parallelism splits the model itself across multiple devices, enabling the training of architectures that are too large to fit on a single device. Finally, pipeline parallelism further divides the model into sequential stages across devices, which are then trained in a pipelined fashion to improve throughput. Frameworks such as DeepSpeed and Megatron-LM provide infrastructure for managing these complex parallelization schemes and optimizing communication overheads.
- Gradient checkpointing: This reduces memory usage by selectively storing only a subset of intermediate activations during the forward pass. During backpropagation, the missing activations are recomputed as needed, trading compute for memory. This technique is particularly useful when training large transformer models, where memory consumption due to storing full activation histories becomes prohibitive. Popular libraries such as PyTorch’s
torch.utils.checkpointor TensorFlow’s recomputation wrappers make it possible to apply this technique without having to rewrite model architectures. - Mixed-precision training: This utilizes 16-bit floating-point (FP16 or BF16) formats instead of the standard 32-bit (FP32) for most computations. This reduces memory footprint and increases throughput due to faster arithmetic and lower memory bandwidth usage. To maintain model accuracy and numerical stability, a master copy of weights is maintained in FP32, and dynamic loss scaling is often used to prevent underflow in gradients. Libraries such as NVIDIA’s Apex or native support in PyTorch and TensorFlow enable automatic mixed-precision training. This method is especially effective on modern hardware such as NVIDIA’s Tensor Cores or Google’s TPUs, which are optimized for low-precision computation.
Figure 19*.1* summarizes these strategies:

Figure 19.1 – Strategies for scaling RLHF
Here’s an example of how to implement gradient checkpointing:
from transformers import GPT2LMHeadModeldef enable_gradient_checkpointing(model): if hasattr(model, "gradient_checkpointing_enable"): model.gradient_checkpointing_enable() else: model.base_model.gradient_checkpointing_enable() return modelbase_model = GPT2LMHeadModel.from_pretrained("gpt2-large")base_model = enable_gradient_checkpointing(base_model)This function enables gradient checkpointing for the model, which can significantly reduce memory usage during training, allowing for larger batch sizes or model sizes.
Limitations of RLHF in language modeling
Section titled “Limitations of RLHF in language modeling”While RLHF is powerful, it faces several challenges:
- Reward hacking: Models may exploit loopholes in the reward function
- Limited feedback: Human feedback may not cover all possible scenarios
- Suboptimal local optima: The optimization process may get stuck in suboptimal solutions
- Scaling issues: Obtaining high-quality human feedback at scale is challenging
To address reward hacking, consider implementing a constrained optimization approach:
def constrained_ppo_step( base_model, reward_model, constraint_model, optimizer, prompt, constraint_threshold=0.5): outputs = base_model.generate(prompt, max_length=100, return_dict_in_generate=True, output_scores=True ) generated_text = tokenizer.decode( outputs.sequences[0], skip_special_tokens=True ) reward = reward_model(generated_text) constraint_value = constraint_model(generated_text) if constraint_value > constraint_threshold: return base_model # Skip update if constraint is violated # Compute and apply policy update (similar to previous ppo_step) # ... return base_modelThis function adds a constraint check before updating the model, helping to prevent reward hacking by ensuring the generated text meets certain criteria.
This method modifies the standard training flow by evaluating a generated output not just for reward alignment but also for compliance with an external constraint model. The process begins with a response being generated from the base model using a given prompt. The resulting text is passed through both a reward model and a constraint model. The reward model assigns a scalar reward value based on its alignment with desired behaviors or objectives. In parallel, the constraint model evaluates whether the output satisfies specified limitations, such as avoiding harmful content, staying within factual bounds, or respecting legal or ethical filters.
The constraint model returns a scalar value that quantifies the degree of constraint violation. This value is compared against a predefined threshold. If the value exceeds the threshold, indicating that the output violates the constraint, the training step is aborted for this sample. No gradient is calculated, and the model parameters remain unchanged. This selective update mechanism ensures that only outputs that both align with human preferences and satisfy safety or policy constraints contribute to learning. This design decouples the constraint signal from the reward function, maintaining clear boundaries between learning objectives and constraint enforcement. As a result, it preserves the integrity of both components and makes the system more interpretable and modular.
Applications of RLHF
Section titled “Applications of RLHF”RLHF can be applied to various LLM tasks, including the following:
- Open-ended text generation
- Dialogue systems
- Content moderation
- Summarization
- Code generation
Here’s an example of applying RLHF to a summarization task:
def rlhf_summarization( base_model, reward_model, text, num_iterations=5): prompt = f"Summarize the following text:\n{text}\n\nSummary:" for _ in range(num_iterations): summary = base_model.generate(prompt, max_length=100) reward = reward_model(summary) # Update base_model using PPO or another RL algorithm # ... return summary# Example usagelong_text = "..." # Long text to summarizesummary = rlhf_summarization(base_model, reward_model, long_text)print(summary)This function applies RLHF to the task of text summarization, iteratively improving the summary based on rewards from the reward model.
The key steps involve generating a summary using a base model, receiving feedback from a reward model, and updating the base model iteratively to improve the summarization over time.
Here’s a breakdown of how summarization works in this code:
- Prompt construction: The function takes text as input and creates a prompt that asks the model to summarize that text. This is done by formatting the input text into a string that includes a directive to summarize the content. An example of such a prompt is
Summarize the following text:\n{text}\n\nSummary:. This prompt is sent to the base model so that a summary can be generated. - Base model summary generation: The
base_model.generatefunction is used to generate a summary from the prompt. The generated summary is limited to a maximum length of 100 tokens (max_length=100). The summary is based on the input text and is the first attempt at summarization. - Reward model feedback: After the base model generates a summary, the reward model evaluates the quality of the summary. The reward model is a separate model that measures how well the generated summary aligns with desired qualities (such as being accurate, concise, or coherent). The reward function assigns a score to the summary, which reflects its quality based on the model’s internal criteria.
- Iterative process: This process of generating a summary and receiving feedback is repeated for
num_iterationstimes (in this case, five times by default). Each iteration involves generating a new summary, receiving feedback from the reward model, and potentially updating the base model to improve the summary in future iterations. - Model update (placeholder): The placeholder comment,
# Update base_model using PPO or another RL algorithm, indicates that after each iteration, the base model should be updated using a reinforcement learning algorithm, such as PPO. This update will adjust the parameters of the base model to generate better summaries based on feedback from the reward model. However, the actual code for model updating isn’t provided here and would typically involve reinforcement learning techniques to fine-tune the base model based on the rewards it receives. - Final output: After completing the specified number of iterations, the function returns the final summary generated by the base model. This summary is expected to be the result of multiple improvements made based on the feedback that’s received from the reward model during the iterative process.
Summary
Section titled “Summary”RLHF is a powerful technique used by many frontier model providers, such as OpenAI and Anthropic, in fine-tuning pre-trained models. This chapter discussed some basic ideas behind this pattern. RLHF still has its limitations since humans are involved in the process of training a reward model, and as such, it doesn’t scale well. Recently, some more generic reinforcement learning without human feedback has been tested by companies such as DeepSeek. However, this is beyond the scope of this book. You can refer to the following research paper by DeepSeek for more information: https://arxiv.org/pdf/2501.12948.
As we move forward, we’ll explore advanced prompt engineering techniques for LLMs. In the next chapter, we’ll delve into sophisticated methods for guiding LLM behavior and outputs through carefully crafted prompts, building on the alignment techniques we’ve discussed here. These advanced prompting strategies will enable you to leverage the full potential of your LLMs while maintaining fine-grained control over their outputs.
Get This Book's PDF Version and Exclusive Extras
|