1 of 67

CSCI-SHU 376: Natural Language Processing

Hua Shen

2026-10-14

Fall 2026

Lecture 11: RLHF & LLM Decoding

2 of 67

Today’s Plan

  • RLHF
  • LLM Decoding Overview
  • Sampling
  • Controllable Generation

​

​

3 of 67

Limitations of Instruction finetuning

  • It is expensive to collect ground-truth data for tasks
  • Some tasks like open-ended creative generation have no right answer
    • E.g., write a story about a lion
  • Language modelling penalizes all token-level mistakes equally, but some are worse than others
  • Can we try to satisfy human preferences?

​

4 of 67

Reinforcement Learning from Human Feedback (RLHF)

5 of 67

Reinforcement Learning from Human Feedback (RLHF)

  • Instruction tuning first
  • Then maximize reward

​

6 of 67

7 of 67

Recap: SFT Recap

Training:

<prompter> How do you make tea?

<assistant> Remove the tea bag and enjoy.

Inference:

<prompter> What country won the most gold medals in 2008 Olympics?

<assistant>

​

  • SFT = next-token prediction on instruction–response pairs�
  • Special tokens define speaker roles (prompter / assistant)
  • Serve as “cold start” in RLHF
  • After SFT, we get a reference model (policy)

Question: Can we get a better/optimal policy?

8 of 67

What is an “Optimal Policy”?

A simple formula:

Key question: how to get reward for each LLM generation?

9 of 67

P A R T 0 2

10 of 67

Comparison Data

P A R T 0 2

Challenge: What is a good measure of human preference?

​

Answer: Use pairwise comparison data — humans can more reliably judge which of two responses is better than assign absolute scores.

Data: (x, y_win, y_lost)

Reward model: R(x, y_win) > R(x, y_lost)

​

Question: Why is pairwise data a desired form?

11 of 67

Long before LLM: ELO Rating

  • Elo rating is widely adopted in chess, online games (e.g., League of Legends), etc…

​

  • Elo is an online learning algorithm

​

  • In preference optimization, however, we often seek a global estimation of latent rewards from all pairwise data.

12 of 67

Bradley-Terry (BT) model

Preference Distribution :

Parametrize + + maximum likelihood

Frame the problem as a binary classification we have the negative log-likelihood loss:

Tips: normalize the rewards for low variance

13 of 67

RLHF

During the RL phase, the learned reward function is used to provide feedback to the language model. The optimization is formulated as:

Due to the discrete nature of language generation, this objective is not differentiable and is typically optimized with reinforcement learning.

14 of 67

P A R T 0 2

15 of 67

Preparation:RL concepts

A common RL trajectory:

S0, A0, R1, S1, A1, R2, S2, A2, R3…

Start from initial state S0

Take action A0

Get Reward R1

Go to state S1

16 of 67

What is Agent and Environment in RL?

Agent:

Input: State St

Output: Action At

Usually formalized as policy: π(a | s)

​

Environment:

Input: current State St, Action At

Output: next State St+1, Reward

Usually formalized as: p(s’,r | s,a)

​

​

17 of 67

Example: Blackjack/21 points

You are initially given one card. You may take more cards, one at a time if you like. Stop anytime before lose.

  • jack = queen = king = 10; ace = 11; No joker included
  • You get 0 return if go over 21;
  • Return sum of your cards, otherwise.

​

Goal: close to 21 without going over 21.

​

18 of 67

Blackjack: Reward, State, Action

Let X be sum of your card,

Reward: [0, 21]

0 for non-terminal state

X if X <= 21, or 0 if X > 21, in terminal state.

State: X

Action: {0, 1}

0 for “don’t take more”, 1

Stop if choose 0 or X exceed 21.

​

19 of 67

Blackjack: Possible trajectories

τ1 : S0 = 7, A0 = 1, R1 = 0, S1 = 12, A1 =1, R2 = 0, S2 = 20, A2 = 0, R3 = 20

τ2: S0 = 11, A0 = 1, R1 = 0, S1 = 15, A1 =1, R2 = 0, S2 = 25, A2 = 0, R3 = 0

Finally, which one is better?

Return value G ()

G1 = 0 + 0 + 20 = 20 😃

G2 = 0 + 0 + 0 = 0 😭

Episodic: only gives (useful) reward at end

p.s. Go, Chess, and LLM usually holds Episodic

​

20 of 67

Modeling Reward: Policy, Value, and Action-Value

Policy: π(a | s)

  • Given I got 20 points, I should “stop”
  • I want to take action according to this state

Value: vπ(s) = Eπ[ Gt | St = s ]

  • Eπ [Rt +1 + 𝛾Rt+2 + …. | St = s ], For episodic, R: 0, 0, 0, ..., x
  • I want to know how good/bad my state is

Action Value: qπ(s,a) := Eπ[ Gt| St = s, At = a]

  • I want to know how good if I take this action at this state

21 of 67

Policy Gradient/REINFORCE algorithm

RL: Policy: π(a | s)

DRL: Policy Network

  • Can be just MLP
  • Input: a state vector
  • Output: logit over all actions

​

After softmax, it becomes “probability”

22 of 67

Policy Gradient/REINFORCE algorithm

Recall that RL trajectory:

S0, A0, R1, S1, A1, R2, S2, A2, R3…

Now At = argmax (πӨ(a| St)), all a in Action space

We can then do Gradient Ascent to raise Return G:

​

23 of 67

Policy Gradient/REINFORCE algorithm

By Policy Gradient Theorem, the gradient can be written like RHS

Thus, Deep learning training can be carried out

24 of 67

Policy Gradient/REINFORCE algorithm

25 of 67

Simplified LLM

current state:

Sentence input

LLM

(include tokenizer)

Logit output

(Over vocab)

Probability output

(Over vocab)

26 of 67

RL formalization of LLM problem

Episodic RL problem:

- initial state S0 = prompt

- action space A = vocabulary

- action At = token in vocabulary

- state St = prompt + all response tokens generated so far

- policy πӨ(a| St) = LLM output prob

- episode ends when <|endoftext|> token is sampled

Reward model would assign rφ(x,y)

Try to maximize Ex~D,y~πθ [ rφ(x,y) ]

P A R T 0 1

27 of 67

Reinforcement Learning (RL) Summary

  • The field of reinforcement learning has studied these problems for many years
  • Circa 2013: resurgence of interest in RL applied to deep learning in game playing
  • New area: Applying RL to modern LMs

​

28 of 67

Reinforcement Learning (RL)

 

29 of 67

RL: Language Generation

  • State: a prompt and tokens-generated so far
  • Action: generate a token
  • Policy: generator (e.g., Language Model)
  • Environment: append token
  • Reward: how good is the current token?

-> decomposed from reward on the full sequence

30 of 67

RL: One step Language Generation

  • State: a prompt
  • Action: generate a full response
  • Policy: generator (e.g., Language Model)
  • Reward: reward on the full sequence

31 of 67

Reinforcement Learning (RL)

  • The task criteria / preference is directly optimized via the reward
  • Data is generated by the model, and a reward tells us how to use data for training
  • Model generations are in the training loop

32 of 67

  • Human-in-the-loop is expensive!
  • Instead of directly asking humans for preference, model their preferences as a separate NLP problem

​

    • Rule-based rewards

​

    • Model-based rewards

​

How do we get rewards?

33 of 67

  • A verifiable property of output
  • Example: reward for solving a math problem

Rule-based rewards

34 of 67

  • A verifiable property of output
  • Example: write a program that passes test cases

Rule-based rewards

35 of 67

Model-based rewards

  • Model R(x, y) that scores output sequences
  • Example: classify whether the output is “helpful”

​

36 of 67

Model-based rewards: Preference

  • Human judgments are noisy and miscalibrated!
  • Instead of directly asking for ratings, ask for pairwise comparisons that are more reliable

​

37 of 67

  • Human judgments are noisy and miscalibrated!
  • Instead of directly asking for ratings, ask for pairwise comparisons that are more reliable

​

Model-based rewards : Preference

38 of 67

Evaluating reward models

39 of 67

Optimizing for human preferences

 

40 of 67

Optimizing for human preferences

  • How do we actually change our LM parameters to maximize this?

​

  • Policy gradient methods in RL give us tools for estimating and optimizing this objection

​

41 of 67

RLHF provides additional gains

42 of 67

RLHF Summary

 

43 of 67

Instruct GPT: scaling up RLHF to many tasks

44 of 67

Instruct GPT: scaling up RLHF to many tasks

  • Labeler collected tasks

​

45 of 67

Instruct GPT: scaling up RLHF to many tasks

46 of 67

Today’s Plan

  • RLHF
  • LLM Decoding Overview
  • Sampling
  • Controllable Generation

​

​

​

47 of 67

What is inside LLM?

  • A model defines a conditional probability distribution

​

​

48 of 67

LMs are locally normalized

  • It could be the sequence starts with low probability tokens, but have high overall probability
  • Therefore, it is hard to do inference with global constraints

​

49 of 67

Probability distribution -> Hallucination

  • Model generally assigns non-zero probability to any (incorrect) outputs

​

50 of 67

Our goal: Get “Good” Outputs

  • A “good” output given a probability distribution
  • Changing the decoding algorithms

​

51 of 67

Today’s Plan

  • LLM Decoding Overview
  • Sampling
  • Controllable Generation

​

​

​

52 of 67

Recap: Greedy Decoding

  • Greedy Decoding: Compute argmax (over entire vocab) at every step

53 of 67

Recap: Beam Search

  • At every step, keep track of the k most probable partial translations
  • Score of each hypothesis = log probability of sequence so far

​

​

  • Not guaranteed to be optimal, but more efficient than exhaustive search

54 of 67

Highest probability always best?

  • Outputs with low probability tend to be worse

​

​

​

​

  • Difference between top outputs is unclear…

55 of 67

Highest probability always best?

  • Many outputs are meaningful!

56 of 67

Ancestral Sampling

  • Exactly samples from model distribution!

​

57 of 67

Ancestral Sampling

  • Long-tail problem
  • Even if each individual token in the long-tail has low probability, these small probabilities add up…

​

58 of 67

Top-K Sampling

  • Only sample from the most probable <k> tokens

59 of 67

Top-p Sampling

  • Also called nucleus sampling
  • Only sample from the top <p> probability mass
  • Ignore the long-tails

60 of 67

Epsilon Sampling

 

61 of 67

Contrastive Decoding

  • Smaller models make different mistakes

​

  • Choose outputs that the “expert” finds much more likely than the “amateur”

62 of 67

Today’s Plan

  • LLM Decoding Overview
  • Sampling
  • Controllable Generation

​

​

​

63 of 67

Different types of Constraints

  • Low-level constraints: structured output, length etc

63

64 of 67

Different types of Constraints

  • High-level constraints: semantic, avoid hallucination etc

64

65 of 67

Prompting is not enough!

65

66 of 67

Constrained decoding: Manipulate logits

  • Set P(“climb” | X, y) = 0?

​

66

67 of 67

Constrained decoding: Rejection Sampling

  • Generate a lot of samples, then reject

​

67