Concepts / Creating Training Batches for Language Models

Creating Training Batches for Language Models

When a GPT-like language model is trained on many independent documents or books, those texts are usually concatenated into one long stream of tokens.

  • Programming

Why Fixed Vocabularies Run Out

Every tokenizer works from a fixed vocabulary, a table that assigns each known token an integer ID. Real text is open-ended, though. Sooner or later a tokenizer meets a word that was never part of the material it was built from. A new product name, a rare dialect form, or a misspelling can all show up at the moment of use. A tokenizer that only knows its vocabulary has no answer for these words unless the vocabulary and the tokenizer are extended.

This article follows one idea through the whole process. The vocabulary can be extended with special context tokens, which handle unfamiliar words and also make clear where one unrelated piece of text stops and another begins. Those tokens then feed into the step that turns raw sequences into a training batch, which is handled by a custom collate function.

Key point

Keep this question in mind as you read: if you were handed a set of sequences of different lengths and told to form a batch from them, what would you need to do to them first, and what would you need to tell the training process about them?

Adding Special Tokens to the Vocabulary

An ordinary vocabulary is built from the words and fragments found in the training data, and each one receives an ID. A special context token is an additional entry that does not come from the text itself. Its purpose is to carry a meaning that ordinary words cannot express, such as 'this word was unknown' or 'a new document starts here'.

Adding such an entry means giving it its own ID, placed alongside the existing ones. Once that is done, the tokenizer can emit the special token exactly like any other vocabulary member. The special tokens are not a separate mechanism; they are ordinary entries that happen to carry a meaning the training text itself cannot supply.

The lesson to keep from the illustration is that the special tokens are ordinary vocabulary members once they have been added. The specific numbers 783 and 784 are not general facts. A vocabulary that already holds a different number of entries would assign different IDs to the same two tokens.

Handling Unknown Words with the Unk Token

The first special token, <|unk|>, is the tokenizer's fallback for unfamiliar words. The rule is direct: when the tokenizer encounters a word that is not part of the vocabulary, it emits an <|unk|> token instead. The tokenizer does not stop, and it does not discard the text at that point. It substitutes the marker and keeps going, so tokenization of the rest of the text can continue.

Because the marker occupies one position in the token sequence, the model still receives a signal that something was there. The original word cannot be recovered from the token itself. The model knows an unknown word occurred at that position, but not which word it was.

Marking Document Boundaries with the Endoftext Token

The second special token, <|endoftext|>, solves a different problem. When a GPT-like language model is trained on many independent documents or books, those texts are usually concatenated into one long stream of tokens. Without any marker, the model would see one sentence flow straight into an unrelated one. The <|endoftext|> token tells the model that the surrounding material, although joined together for training, does not belong to one continuous narrative.

The material notes that it is common to insert an <|endoftext|> token before each document that follows a previous text source. The same token can be read as the close of one document and the opening of the next, so the boundary is visible from either side of it.

The illustration above is a generated example that follows the described arrangement: an endoftext token precedes each document that follows an earlier text source. Document A has no token before it because nothing precedes it in the stream.

From Raw Sequences to a Training Batch

Training usually happens on groups of examples at once rather than one example at a time. That group is called a batch. Before a batch can be fed to a model, its contents must be arranged into a consistent form. The step that does this work is the custom collate function, which takes raw sequences and turns them into something a training run can use.

Consider a small batch of three sequences whose lengths are 5, 3, and 4 tokens. These lengths are invented for this illustration and are not taken from the material. Before the collate function does anything, the three sequences cannot sit together as a uniform block, because they have different lengths.

What the Collate Function Is Responsible For

The material presents three responsibilities together as the work that turns raw sequences into a training batch. Each one answers a different need: making the sequences fit a common shape, supplying the target values the model is trained to predict, and marking which positions are only filler so they do not influence training.

Tracing the Three-Sequence Batch

Worked example

Collating three sequences of lengths 5, 3, and 4

Three sequences have lengths 5, 3, and 4 tokens. Trace how the batch changes through padding and masking. The chosen common length of 5 and the filler symbol are illustrative choices; the material does not specify them.

  1. 1
    Start with raw sequences

    The sequences hold 5, 3, and 4 real tokens. Together they contain 12 real token positions. They cannot form a rectangular block yet because their lengths differ.

  2. 2
    Pad to a uniform length

    Choose 5 as the common length, the longest sequence's length in this example. The 3-token sequence gains 2 filler positions and the 4-token sequence gains 1. The batch is now three rows of 5 positions, giving 15 positions in total.

  3. 3
    Create target token IDs

    Targets are created for the batch, as the collate function's second responsibility. The material does not specify how; the target layout is not shown here in detail.

  4. 4
    Mask the padding positions

    The 3 filler positions are flagged so they do not affect training. The 12 real positions remain unmasked. Nothing is deleted, so the batch keeps all 15 positions.

Result

The batch is a 3 by 5 rectangle with 12 real positions and 3 masked filler positions. The rectangle makes the batch usable as one block, and the mask tells training which positions to ignore.

RowPosition 0Position 1Position 2Position 3Position 4
Sequence 1 (5 tokens)realrealrealrealreal
Sequence 2 (3 tokens)realrealrealmasked fillermasked filler
Sequence 3 (4 tokens)realrealrealrealmasked filler

The padded batch for the three-sequence example, with real positions and masked filler positions marked. Labels are illustrative.

Common Mistakes When Preparing Batches

  • Common mistake

    Treating the unk token as a recovered copy of the missing word

    What it looks like

    A product name not in the vocabulary becomes an unk token, and the reader assumes the model still knows the name.

    Why it fails

    The unk token only marks that an unknown word occurred at that position. The original word cannot be recovered from the token itself.

    Do this instead

    Read the unk token as a placeholder signal for an unfamiliar word, not as the word.

  • Common mistake

    Joining documents without boundary markers

    What it looks like

    Three independent books are concatenated into one stream with no separator between them.

    Why it fails

    The model would see one passage flow straight into an unrelated one, with no signal that a new text begins.

    Do this instead

    Insert an endoftext token before each document that follows a previous text source.

  • Common mistake

    Assuming padding removes positions from the batch

    What it looks like

    A 3-token sequence padded to 5 is assumed to have only 3 positions left.

    Why it fails

    The material states that masking marks positions rather than removing them. The filler stays in the batch.

    Do this instead

    Keep the padded positions in the batch and mark them so training ignores them.

  • Common mistake

    Believing the collate function's three jobs can be done in any order without changing the batch

What the Material Leaves Unspecified

Several details are outside what the material establishes. It does not give the procedure for creating target token IDs, so that step should be learned from a fuller description rather than inferred here. It does not name the filler value used for padding or explain how the common length is chosen. It does not give the specific IDs that a given tokenizer will assign to its special tokens, because those depend on how many entries the existing vocabulary already holds.

What the material does specify is clear: the unk token replaces a missing word so tokenization can continue, the endoftext token separates unrelated text, the collate function pads, creates targets, and masks padding, and masking marks positions rather than removing them.

Practice: Predicting the Batch State

EASY

Two sequences have lengths 6 and 4 tokens. Using the same illustrative choice of the longest length as the common length, how many filler positions are added in total, and how many real positions does the batch contain? Then state where an endoftext token would be placed if these two sequences came from independent documents, and explain why.

Hints
  • Pad the 4-token sequence up to the common length of 6.
  • Real positions are the original token counts added together, before any padding.
  • An endoftext token goes before a document that follows a previous text source.

What do you think happens?

After padding and masking, does the batch keep the filler positions or delete them?

  • It deletes them

  • It keeps them and marks them

Reveal answer
Answer:

It keeps them and marks them

The material states that masking marks positions rather than removing them. The padded positions remain in the batch so it keeps its rectangular shape, and the mask tells training to ignore them.

Key Takeaways

  • A tokenizer's fixed vocabulary can be extended with special context tokens that carry meanings ordinary words cannot express, and each special token receives its own ID.
  • The unk token replaces any word missing from the vocabulary so tokenization can continue, but it does not preserve the original word.
  • The endoftext token marks the boundary between unrelated text sources, so a model is not led to read one document as a continuation of another.
  • The custom collate function pads sequences to a uniform length, creates target token IDs (procedure not specified in the material), and masks padding tokens by marking positions rather than removing them.

Related concepts

More concepts starting with C