Concepts / Batch Size and Training Stability

Batch Size and Training Stability

Batch size is a tradeoff: small batches give noisy updates, and it is a hyperparameter to experiment with when training LLMs.

  • Programming

A Question Before the Code

Before looking at any code, start with the question the code answers: how many training examples should the model see before it adjusts its weights? In regular deep learning, that number is the batch size. This article answers that question first, then shows how a data loader turns a long stream of text into batches whose overlap you can predict and control.

Two ideas run through the whole article. The first is that batch size is a tradeoff, not a fixed rule. The second is that the way windows are placed inside the text, controlled by the stride, determines whether one batch repeats tokens from the last one or leaves tokens out.

Batch Size and Noisy Updates

Training on very small groups produces updates that jump around, because each group is a rough sample of the whole dataset. A small batch sees only a few examples, so the direction it suggests for changing the weights can differ quite a lot from the direction a full view of the data would suggest. Those jumps are the noise.

Training on very large groups produces smoother updates. The price is that each update costs more, and the model sees fewer updates per pass over the data. Batch size therefore sits in the middle of a tradeoff: smoothness and cost pull in opposite directions.

Rough sample of thedatasetLarger group ofsequencesUpdates jump aroundUpdates are smootherMore updates per passFewer updates perpass
How the same training pass looks when each update is computed from a small group of sequences versus a large group. Read down each column to compare the effects.

What the Data Loader Takes In

The loader is built by a function called create_dataloader_v1. It takes five arguments: the raw text, the batch size, the maximum length of each sequence, the stride, and a shuffle flag. Each argument controls a different part of the batch.

  • Raw text: the long stream of text the loader cuts into pieces
  • Batch size: how many sequences travel together in one batch
  • Maximum length: how many tokens each sequence contains
  • Stride: how far the starting point moves from one sequence to the next
  • Shuffle: whether the order of sequences is randomized

Notice that the call asks for a batch size greater than 1. Each call to next pulls one whole batch, so the inputs and targets come back as grids in which each row is one sequence. Keep that row structure in mind, because the question of overlap is really a question about which rows share tokens.

Predicting the Second Window

Before you look at what the loader returns, make a prediction. Consider a call where each sequence is four tokens long and the stride is also four. Think about where the second sequence begins relative to the first.

What do you think happens?

If the starting point moves forward by four tokens each time, does any token belong to both the first and second sequence?

  • Yes, the windows repeat their tokens

  • No, the second window begins where the first one stops

Reveal answer
Answer:

No, the second window begins where the first one stops

With a four-token window and a four-token stride, the first window covers positions 0 through 3 and the second starts at position 4. No token is shared.

If your guess was that sequences repeat, you were picturing a window that moves more slowly than its own length. That mismatch is exactly what the stride controls, and the next section makes it concrete.

Stride as the Distance Between Starts

The stride is the distance the loader moves forward between one sequence's starting token and the next sequence's starting token. The window length is set by the maximum length, and the stride is set separately. Because these two numbers are independent, the windows can sit end to end, overlap, or leave gaps.

When the stride is smaller than the maximum length, each window reuses some tokens from the window before it. When the stride equals the maximum length, the windows sit end to end. The source states the result directly: increasing the stride to 4 uses the data set fully and avoids any overlap between batches. It also notes that more overlap could lead to increased overfitting, so overlap should be chosen deliberately.

t0Window 1t1Window 1t2Window 1t3Window 1t4Windows 1 and 2t5Windows 1 and 2t6Windows 1 and 2t7Windows 1 and 2t8Windows 2 and 3t9Windows 2 and 3t10Windows 2 and 3t11Windows 2 and 3
Each cell is one token position. Windows start at positions 0, 4, and 8, so the shaded positions 4 through 11 belong to two windows each. The t labels are generic placeholders for positions.
Worked example

Finding the overlap for three stride values

The maximum length is 8 tokens. Work out which positions each window covers and whether consecutive windows overlap or leave gaps for a stride of 4, a stride of 8, and a stride of 10.

  1. 1
    Stride 4

    Window 1 covers positions 0 to 7. The next window starts 4 positions later, at position 4, so it covers 4 to 11. Positions 4 to 7 appear in both windows, so the windows overlap.

  2. 2
    Stride 8

    Window 1 covers positions 0 to 7. The next window starts at position 8 and covers 8 to 15. Every position belongs to exactly one window, so there is no overlap and no gap.

  3. 3
    Stride 10

    Window 1 covers positions 0 to 7. The next window starts at position 10 and covers 10 to 17. Positions 8 and 9 belong to neither window, so they are skipped.

Result

A stride smaller than the maximum length causes overlap, a stride equal to it gives end-to-end windows, and a stride larger than it leaves gaps. The stride alone decides which case you get.

Inputs and Targets Pair Up

The loader returns two grids, inputs and targets, and both are built from the same windows. When you compare a row of inputs with the matching row of targets, the targets are the same tokens shifted forward by one position. That is what lets the model learn to predict the next token from the tokens that came before it.

python
Output (expected)
['t0', 't1', 't2', 't3']
['t1', 't2', 't3', 't4']
Notes on the code
  • window[:-1] drops the last token, so the input row is the window without its final position
  • window[1:] drops the first token, so the target row is the same window shifted forward by one

Batch size changes how many such row pairs come back together, but it does not change the pairing inside each row. Each row is always its input window matched with its target window shifted by one.

Tracking Down an Unexpected Overlap

Suppose a batch you print contains repeated tokens from one sequence to the next, but you expected none. The useful debugging question is where the divergence starts. Start with the arguments, not the model. The flowchart below shows the order of checks.

Unexpected overlapor gapStride equal to maxlength?Windows sit end toendAdjacent windowsshare tokensConfirm stride andmax lengthBatch size onlygroups rowsStride smaller thanmax length?Stride larger: tokensskippedcheck firstyesnoyesno
Compare the stride with the maximum length first, since those two settings decide where each window starts and ends. Then confirm both values match your plan. Batch size only groups the windows into rows.
Debugging

A printed batch shows token t4 at the end of one row and at the start of the next, but the plan was no overlap. Step one: read the stride in the loader call. If it is 2 while the maximum length is 4, adjacent windows share two tokens, which explains the repeat. Step two: confirm the stride you intended was 4. If the arguments match the plan, the windows are behaving as the stride rule predicts, and the surprise lies in the expectation rather than the loader.

Mistakes Beginners Make

  • Common mistake

    Treating batch size as the setting that controls overlap

    What it looks like

    Changing the batch size from 2 to 8 and expecting the sequences in each row to share fewer tokens.

    Why it fails

    Batch size decides how many sequences travel together in one batch. It does not change where each window starts.

    Do this instead

    To change overlap, change the stride. Batch size only changes how many rows come back together.

  • Common mistake

    Assuming a stride smaller than the window length is harmless

    What it looks like

    Using a stride of 4 with a maximum length of 8 and expecting no repeated tokens.

    Why it fails

    Each window then starts before the previous window ends, so tokens are reused. The source notes that more overlap could lead to increased overfitting.

    Do this instead

    Choose the overlap deliberately. A stride equal to the maximum length uses the data fully and avoids overlap.

  • Common mistake

    Expecting smooth updates from very small batches

    Why it fails

    Each small group is a rough sample of the dataset, so the updates they produce jump around.

    Do this instead

    Expect noisier updates with small batches, and test larger sizes if the noise hurts training.

  • Common mistake

    Debugging the model before checking the loader arguments

    Why it fails

    If the windows overlap or skip tokens, the cause is usually in how the loader was called.

    Do this instead

    Check the stride and maximum length in the loader call first, then confirm they match your plan.

Practice: Predict the Windows

MEDIUM

The maximum length is 6 tokens and the stride is 3. Without running anything, write down the start positions of the first three windows, then state whether consecutive windows overlap, leave a gap, or sit end to end. Finally, say which setting you would change if you wanted no overlap at all.

Hints
  • The second window starts at position 0 plus the stride.
  • Compare the stride with the maximum length to decide between overlap, end-to-end, and gap.
  • A stride equal to the maximum length removes the overlap.

Key Takeaways

  • Batch size is a tradeoff: small batches give noisy updates that jump around, while large batches give smoother updates at a higher cost per update and fewer updates per pass.
  • The stride is the distance between one window's starting token and the next, so a stride smaller than the maximum length causes overlap, a stride equal to it gives end-to-end windows, and a larger stride leaves gaps.
  • The loader's inputs and targets are the same windows shifted by one position, and batch size changes how many row pairs return together but not the pairing within a row.
  • When batches overlap or skip tokens unexpectedly, check the stride and maximum length in the loader call before suspecting the model.

Related concepts

More concepts starting with B