HomeVideos

How are large language models trained?

Now Playing

How are large language models trained?

Transcript

227 segments

0:06

You might have heard people say things large language models are

0:10

trained on massive amounts of text,

0:12

or large language models predict the next token in a sequence.

0:16

But what does any of that actually mean.

0:19

How were large language models trained in practice.

0:22

In this video, we'll walk through the steps

0:24

it takes to train large language models

0:26

that behave as helpful assistants,

0:28

follow instructions, use tools, and just generally respond

0:32

with the right kind of vibe.

0:33

Step one is pre-training.

0:35

During this phase, the model is trained on a task

0:38

called next token prediction.

0:41

This essentially means the model is given a sequence of text,

0:44

and its task is to predict what comes next.

0:47

One of the advantages of model pre-training

0:49

is that it's a type of self-supervised learning,

0:52

meaning you don't need to go through the effort of labeling

0:55

the whole training data set.

0:57

Instead, you take a lot of data from publicly available sources,

1:02

and you create the pre-training data

1:04

set by chunking this data into inputs and outputs.

1:08

For example, if this document about houseplants

1:11

was part of your pre-training data set,

1:13

you could chunk it into a data set that looked something

1:16

like this.

1:18

The model's training task is to correctly predict

1:20

the next word in the sequence.

1:23

In order to do this effectively across a diverse set of inputs,

1:27

the LLM implicitly learns patterns about language,

1:31

writing styles, and even some world

1:33

knowledge how the monstera is an easy to care for houseplant.

1:38

While the labeling strategy isn't super complicated,

1:41

what is complicated about pre-training

1:43

is doing this at scale.

1:45

This phase of LLM training is a massive engineering challenge

1:50

because models are too large for a single chip.

1:53

Engineers must use complex parallelism

1:56

to split the model and data across thousands

1:59

of interconnected GPUs.

2:00

They also need to implement strategies for automated

2:03

recovery systems to handle crashes, and massive pipelines

2:07

to pre-process the petabytes of training data.

2:11

But if you do all of that successfully,

2:13

you have a model that can take in a prompt

2:16

and use its vast world knowledge,

2:18

understanding of language and sense of how different

2:20

kinds of documents work to produce a response.

2:24

Now, unless you're a developer who actually trains LLMs,

2:28

it's unlikely you've interacted directly with a model that's

2:31

only gone through pre-training.

2:33

Even though pre-trained models, particularly

2:35

the more recent and sophisticated ones,

2:38

are fairly capable, they can also

2:40

be unpredictable and unaligned.

2:43

So there's still a lot of work that

2:45

needs to happen to get the LLM to be ready for use

2:48

in a production environment.

2:50

And that is where model post-training comes in.

2:53

Post-training is the phase where we refine model quality

2:56

and focus on alignment so that the generated answers are safe,

3:00

well formatted, helpful, more accurate, and pleasant

3:03

to engage with.

3:05

Often post training involves two different training strategies

3:09

supervised fine tuning or SFT and reinforcement learning

3:13

or RL.

3:15

An SFT we train the model on gold standard interactions

3:18

that demonstrate the behavior we want it to learn.

3:21

During RL, the model generates many answers

3:24

and a separate reward model grades them.

3:28

The reward model gives points for helpful, safe, and accurate

3:31

responses.

3:32

Pushing the model towards the behaviors

3:33

we want and penalizes responses we don't want.

3:37

Let's dive into both of these in more detail.

3:39

So let's say you wanted the model

3:41

to learn how to create a friendly, conversational

3:43

response to a question, like a helpful assistant.

3:46

To do this, you might create a data set

3:48

of prompts formatted as questions

3:50

how do I make gluten free muffins.

3:53

And the corresponding answers that demonstrate

3:55

the helpful assistant style.

3:58

During SFT, the model is fed the prompt, it produces a response

4:02

and then the loss is measured between the predicted response

4:05

and the gold standard response, which is the label,

4:08

and that's used to update the model's weights.

4:11

This is basically classic ML model training,

4:14

and by showing the model these ideal examples,

4:16

it learns how to produce responses in line

4:18

with the behavior we care about.

4:21

You might follow a similar process

4:22

for teaching the model how to use tools,

4:24

creating a data set of prompts that require tools,

4:27

and the syntax of the corresponding tool call.

4:30

Or you could use SFT for improving the model's ability

4:33

to solve math problems, or really any other capability you

4:36

want the model to improve on.

4:38

Now, SFT is a good starting place,

4:40

but it teaches the model to memorize what good looks like

4:43

and doesn't always generalize to New,

4:46

unseen variations of a task.

4:48

To do that, we need a mechanism for the model

4:50

to practice producing good responses

4:52

and receiving feedback, iterating

4:55

and improving over time.

4:56

And that is where RL comes in.

4:59

In RL, you have the model generate responses

5:02

to prompts that are salient to the task you

5:04

want the model to learn.

5:06

The model's responses are scored by a separate model called

5:09

the reward model, and based on the scores,

5:12

the LLM updates its weights and improves over time.

5:16

How you train this reward model is out of scope for now,

5:18

but there are resources in the description below that

5:21

provide some more details.

5:23

One way of thinking about the differences between SFT and RL

5:26

is that SFT sets the floor.

5:29

It ensures that the model is at least competent.

5:32

It understands the basic format of a list, a code block,

5:34

or an answer to a question RL raises the ceiling.

5:38

SFT can only ever be as good as the curated training data,

5:42

but RL allows the model to surpass this data

5:45

by exploring millions of different ways

5:47

to answer a prompt and keeping only the ones that

5:49

get the highest scores.

5:51

The model can find more efficient or clearer ways

5:53

of responding to prompts than what was in the original SFT

5:56

training set.

5:58

In practice, the balance between SFT and RL

6:00

is still an open question, and there's no standard ratio

6:04

of how much to do either step.

6:06

But while the exact training recipe

6:08

will differ between different state of the art models,

6:11

the high level components are the same.

6:13

And at least for now, that means some combination

6:15

of SFT followed by RL.

6:19

So far, we've talked through the core components

6:21

of how LLMs are trained.

6:23

But there's one missing piece.

6:25

How do we measure any of this.

6:27

Anytime we train a model, we need

6:29

to have some way of quantifying how good the model is

6:32

at the task we care about.

6:34

Are the experiments we're running

6:35

actually improving the model's ability

6:37

or just wasting valuable compute.

6:40

Measuring success in pre-training

6:42

is fairly straightforward.

6:44

All we care about is whether the model is getting better

6:47

at predicting the next word.

6:49

But while pre-training focuses on how well does

6:52

the model predict the next word.

6:54

Post training focuses on questions of alignment,

6:57

how well does the model follow instructions and behave

7:00

like a safe, friendly, and helpful assistant.

7:03

And all of that can be a little harder

7:05

to measure for closed domain problems like math or coding.

7:09

You might test the model's abilities

7:11

on a data set of problems, and check if the model produced

7:14

a correct answer.

7:15

This is the score you track towards

7:17

to see if SFT and RL are actually

7:20

improving the model's skills.

7:21

The more questions in your test data

7:23

set that the model gets right, the better it's

7:25

doing for measuring things like is the model being

7:28

collaborative or insightful or helpful,

7:31

or any other task that's hard to quantify.

7:34

You first need to have a clear definition

7:35

of what those terms mean and what would

7:37

constitute a good response.

7:40

For example, here's how you might define what it means

7:42

for a model to be helpful.

7:44

When you have that defined, you can

7:46

use this criteria to evaluate the model's response.

7:49

First, you create a test set of relevant prompts.

7:53

In the case of helpfulness, pretty much any prompt

7:55

could be considered relevant because we always

7:57

want a model to respond in a helpful way.

8:00

But for measuring something like how funny is this model,

8:03

you'd want to make sure that your test set is made of prompts

8:05

where you actually expect the model to respond

8:08

in a humorous way.

8:09

You're not going to get a great signal on how good

8:11

the model is if the test prompts have nothing to do with humor.

8:15

Once you have this test set of prompts,

8:16

you pass it to the model you're evaluating

8:18

and you get back responses.

8:20

You then score those responses against the criteria

8:23

that you defined earlier.

8:25

In practice, you probably wouldn't do this completely

8:27

manually.

8:28

Instead, you would scale the process using a second LLM that

8:31

works as a judge and does the scoring for you

8:34

and have humans spot check the results.

8:37

This judge model, known as an auto rater,

8:40

evaluates the LLMs responses to a prompt

8:42

and scores it according to your criteria.

8:45

As you try out different data sets and training strategies,

8:48

you'll use this evaluation to help you determine

8:51

if you're on the right path and if the model is indeed

8:53

getting better over time.

8:55

You still definitely want to spot check the results

8:58

and do more robust testing before deploying the model

9:00

in production.

9:01

But using an auto trader with a set of relevant

9:03

prompts and defined criteria gives you

9:05

a number that quantifies the model's ability

9:08

and can be computed relatively quickly.

9:10

So you can track progress through post training.

9:13

Putting all of this together, we start with module pre-training,

9:16

where we train LLMs to do next token prediction

9:20

on large amounts of text.

9:22

This initial training phase equips the model

9:25

with a broad understanding of patterns, features,

9:28

and relationships within the data.

9:31

Then we take this pre-trained model

9:33

and we apply post-training techniques,

9:36

where we refine the quality, consistency,

9:38

and alignment of the model so that it responds

9:41

as a conversational, pleasant assistant with improved

9:44

skills in areas such as reasoning or using tools.

9:48

A lot of data experiments and compute later

9:50

and we have a trained large language model.

9:54

Let me know in the comments what you want to learn about next.

9:57

I'll read the research papers so you don't have to.

Interactive Summary

The video details the two main phases of training Large Language Models (LLMs): pre-training and post-training. Pre-training focuses on 'next token prediction' across massive text datasets, enabling the model to implicitly learn language patterns and world knowledge through self-supervised learning, despite being a major engineering challenge. Post-training refines the model's quality and aligns it to be a helpful, safe, and pleasant assistant, primarily utilizing Supervised Fine-Tuning (SFT) and Reinforcement Learning (RL). SFT establishes a baseline of desired behaviors from 'gold standard' examples, while RL allows the model to iterate and improve by generating responses that are graded by a separate reward model, thus enhancing generalization. Evaluation of success in post-training, especially for subjective qualities, involves defining clear criteria, using relevant test prompts, and often employing a second LLM, an 'auto-rater,' for scoring, with human oversight.

Suggested questions

5 ready-made prompts