Concepts / Batch Processing in LLM Training

Batch Processing in LLM Training

A PyTorch training run repeats a core update cycle: reset gradients, calculate loss, perform a backward pass, and update weights.

  • Programming

Why Training Happens in Small Pieces

A neural network starts out with weights that produce poor predictions. Training is the process of nudging those weights, again and again, until the predictions improve. No single nudge is enough, and the network never sees the whole dataset as one lump. Instead, the training code is a set of nested repetitions: an outer loop that goes over the entire training set, and an inner loop that works through that set in smaller chunks.

Understanding the PyTorch training workflow means understanding what happens inside those loops and why each step exists. The approach taken here starts from the smallest unit of learning, one weight update, and then builds outward. Once you can explain what a single update needs, the outer repetitions and the monitoring steps become simple additions rather than a memorized list.

The Core Update Cycle

Every weight update in a PyTorch training run follows the same core cycle, made of four operations. Each one has a specific job, and they must happen in this order for each batch.

  1. 1
    Reset the gradients left over from the previous update. PyTorch accumulates gradients unless they are reset, so this step clears them first.
  2. 2
    Run the model on the current batch of data to make predictions, then calculate the loss, which measures how far those predictions are from the targets.
  3. 3
    Perform a backward pass, which computes new loss gradients describing how each weight should change to reduce that loss.
  4. 4
    Update the model weights using those gradients.
Reset gradientsForward pass and lossBackward passUpdate weightsNext batchthenthenthenthenmore batches
What happens next in each iteration, and how does the loop return to the start for the next batch?

The four operations form one round. When the loop moves to the next batch, the cycle begins again from the reset step. The loop therefore repeats the same four-step round over and over, and each round is one weight update.

Splitting the Training Set into Batches

Rather than computing one update from the entire training set at once, the training loop processes the data in batches. Each batch is a group of training examples, and the loop iterates over the batches inside every epoch. Each pass through a batch produces one round of the core update cycle, so the number of batches determines how many weight updates happen during one complete pass through the data.

Rule

The batch count depends on two quantities: how many training examples exist and how many examples each batch holds. A larger batch size means fewer batches per pass, and a smaller batch size means more batches. Because the batch count comes from this division, you can work it out before training begins.

Batch 1examples 1 to 100Batch 2examples 101 to 200Batch 3examples 201 to 300Batch 4examples 301 to 400nextnextnext
How does one full pass over the dataset get split into sequential batch indices, and how many batches make up one epoch?
Worked example

Counting the batches in one epoch

A training set contains 400 examples and each batch holds 100 examples. How many batches make up one epoch, and how many weight updates happen in that epoch?

  1. 1
    Identify the quantities

    The training set size is 400 and the batch size is 100.

  2. 2
    Apply the division

    Number of batches = 400 divided by 100, which equals 4.

  3. 3
    Connect batches to updates

    Each batch produces one round of the core update cycle, so one epoch produces 4 weight updates.

Result

One epoch contains 4 batches, and therefore 4 weight updates.

What do you think happens?

If the batch size doubles from 100 to 200 while the training set stays at 400 examples, how many batches make up one epoch?

  • 8 batches

  • 4 batches

  • 2 batches

  • 1 batch

Reveal answer
Answer:

2 batches

The batch count is 400 divided by 200, which equals 2. A larger batch size gives fewer batches per pass, so fewer weight updates happen in each epoch.

The Nested Epoch Loop

Training usually runs for more than one epoch. The outer loop repeats the whole pass, and the inner loop repeats the batch cycle inside it. The result is a nested structure: the epoch loop sets the number of full passes, and within each pass the batch loop performs the core update cycle once per batch. Iterating over epochs is the first step of the workflow for this reason, because it defines the outermost repetition that everything else lives inside.

Learning Steps Versus Monitoring Steps

The workflow ends with two steps that are not needed for learning itself. The first prints the training and validation set losses so you can watch progress. The second generates sample text so you can inspect what the model currently produces. The source describes these as optional steps used for tracking training progress. They change nothing about the weights; they only let the person running training see how things are going.

Reset gradientsPrint lossesCompute lossGenerate sample textBackward passUpdate weights
Which steps in the training loop actually change the model's weights, and which steps only observe progress without affecting learning?
AspectLearning stepsMonitoring steps
ExamplesReset gradients, compute loss, backward pass, update weightsPrint training and validation losses, generate sample text
Effect on weightsChange the weightsChange nothing about the weights
PurposeNudge the network toward better predictionsLet the person running training watch progress
Required for learningYesNo, described as optional

Learning steps and monitoring steps in the PyTorch training workflow

Mistakes Beginners Make with Batches

  • Common mistake

    Assuming one epoch means one weight update

    What it looks like

    With 400 examples and a batch size of 100, a student expects a single update per epoch.

    Why it fails

    Each batch produces one round of the core update cycle, so the number of batches sets the number of updates in one pass.

    Do this instead

    Divide the training set size by the batch size to count the batches, then recognize that each batch gives one update.

  • Common mistake

    Skipping the gradient reset

    What it looks like

    A training loop runs the forward pass, backward pass, and update without clearing gradients between batches.

    Why it fails

    PyTorch accumulates gradients unless they are reset, so leftover gradients from the previous update carry into the next one.

    Do this instead

    Start every round of the cycle with the reset step.

  • Common mistake

    Treating monitoring steps as part of learning

    What it looks like

    A student believes that printing losses helps the weights improve.

    Why it fails

    The monitoring steps change nothing about the weights; they only let the person running training see progress.

    Do this instead

    Keep the four core operations separate from the printing and text generation steps when reasoning about what changes the model.

  • Common mistake

    Forgetting that a larger batch size produces fewer batches per pass

Tracing One Training Run

EASY

A training set has 1,000 examples and the batch size is 50. Work out how many batches make up one epoch. Then list, in order, the four core operations that run for the first batch, and state whether the monitoring steps change the weights.

Hints
  • Use the division of training set size by batch size.
  • Remember that the reset step comes first in every round of the cycle.
  • Consider what the source says the monitoring steps do to the weights.

Key Takeaways

  • A PyTorch training run repeats a core update cycle: reset gradients, calculate loss, perform a backward pass, and update weights.
  • The number of batches equals the training set size divided by the batch size, so one epoch contains that many weight updates and the count can be computed before training begins.
  • An epoch loop sets the number of full passes over the data, and a batch loop inside it performs the core update cycle once per batch.
  • Printing losses and generating sample text are optional monitoring steps that track progress and do not change the weights.

Related concepts

More concepts starting with B