1 of 48

CS 263:

Advanced NLP

Saadia Gabriel

Lecture 6

2 of 48

Announcements

  • Homework #2 will be out later tonight and will have two parts:
  • Part 1 (due 2/18): zero- and few-shot prompting for machine translation
  • Part 2 (due 2/27): review an in-class paper presentation, then review 3 of your peers’ reviews.
  • Guidelines have been added for the mid-project report, proposals will be checked by Friday

3 of 48

Last time…

What is the best approach for continuing training to improve general model performance on aspects like safety or usability?

4 of 48

Today

We will explore various sampling or search based approaches for constructing actual text sequences from our language model’s token probability distributions

fun?

5 of 48

Quiz #2

https://forms.gle/ZUcdgV2aWjmsPCvs7

6 of 48

So far we’ve mostly talked about training models…

The following slides are partially adapted from Graham Neubig’s and Amanda Bertsch’s slides for CMU 11-664/763

7 of 48

How do we sample from our trained models?

8 of 48

Introduction to Inference Algorithms

Our model:

How do we select each yj from this distribution?

At each time step, select the most likely token under the model’s probability distribution:

Greedy decoding:

9 of 48

Issues with Greedy Decoding

This ignores the long-tail of the model’s distribution, and leads to generic outputs:

10 of 48

Other Sampling Methods

Ancestral sampling:

Random based on underlying probabilities

Now we can end up with very improbable sequences

11 of 48

Other Sampling Methods

Top-k:

At each time step you sample from the renormalized distribution from the top k set

12 of 48

Other Sampling Methods

Nucleus (or top-p sampling):

Here top-k with k=3 would cover most of the probability mass

Many likely options are discarded under top-k

13 of 48

Distribution Temperature

14 of 48

Distribution Temperature

Scale the logits by a hyperparameter T > 0

What happens if T is large (e.g 1.5)?

What happens if T is small (e.g .2)?

https://medium.com/@imisri1/how-to-set-sampling-temperature-for-gpt-models-762887df1fac

15 of 48

HuggingFace Model Generation

Prompt: We were sitting in CS263 at UCLA…

Continuation: , learning about the basics of computer science…

Continuation (k=10): , a class on computer security, where I…

Continuation (p=.9): in 1989, where we learned…

Continuation (k=10, T=2.0): in January of 21 when I had what I considered to be…

16 of 48

Issues with sampling-based methods

While this is mostly likely at each timestep, it may not generate an optimal sequence.

But is it always true that a “good” token selection at timestep t will lead to a “good” token selection at timestep t+1, t+2, etc?

17 of 48

Beam Search

The basic idea is that we explore more options for our partial sequence before making a decision about which tokens to keep.

This is a breadth-first search:

18 of 48

Beam Search

19 of 48

Beam Search

20 of 48

Beam Search

21 of 48

Beam Search

22 of 48

Beam Search

23 of 48

Beam Search

24 of 48

Beam Search

Why?

25 of 48

Beam Search Summary

26 of 48

Beam Search Variants

27 of 48

Diverse Beam Search

We can modify this step

28 of 48

Diverse Beam Search

29 of 48

Diverse Beam Search

30 of 48

Diverse Beam Search

31 of 48

Diverse Beam Search

32 of 48

Diverse Beam Search

33 of 48

Stochastic Beam Search

(Kool et al., 2019)

Improves diversity by adding randomness to selection process.

Gumbel Distribution

(often used to model the distribution for sample extremes (min, max))

34 of 48

Stochastic Beam Search

(Kool et al., 2019)

35 of 48

Stochastic Beam Search

(Kool et al., 2019)

36 of 48

Selecting a Beam Size

37 of 48

Trade-offs of Inference Algorithms

Greedy

Pros: Fast and deterministic

Cons: Can be suboptimal (particularly repetitive outputs)

Sampling (top-k, top-p)

Pros: More creative and diverse outputs

Cons: Can introduce too much variance and incoherent or unrealistic outputs

Beam Search

Pros: Explores possible subsequences, improving coherence and overall quality

Cons: Computationally expensive and suffers from diversity issues

38 of 48

Advanced Inference Methods

39 of 48

Constrained Decoding

Fudge (Yang and Klein, 2021)

40 of 48

Constrained Decoding

Fudge (Yang and Klein, 2021)

41 of 48

Constrained Decoding

Fudge (Yang and Klein, 2021)

42 of 48

Contrastive Decoding

Li et al. (2023)

43 of 48

Contrastive Decoding

Li et al. (2023)

44 of 48

Self-correcting Decoding

Madaan et al. (2023)

45 of 48

What is test-time scaling?

Instead of retraining the model, improve task performance through additional inference-time compute (e.g. generating additional tokens to explain step-by-step process of problem-solving)

46 of 48

Chain-of-thought prompting

(Wei et al., 2022)

47 of 48

Implicit Chain-of-thought

(Kojima et al., 2022)

48 of 48

Next time:

Evaluation!