Compute the effective batch from per device batch, accum steps, and GPU count
Effective batch is per_device_train_batch_size times gradient_accumulation_steps times world_size. With 2, 8, and 4 GPUs that is 64 examples per optimizer update.
Imagine four bakery teams each baking trays of cookies. Each team's oven only fits two trays at a time, so they bake two trays per oven round. They keep collecting cookies into a shared pile and only deliver the pile to the storefront after eight oven rounds. That means each team contributes 2 trays per round times 8 rounds equals 16 trays before delivery. Four teams doing this in parallel deliver 4 times 16 equals 64 trays per delivery cycle. The optimizer is the storefront and one delivery cycle is one update. The accumulation steps are the oven rounds, and the GPUs are the bakery teams.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Effective batch size is the most-misunderstood number in distributed fine-tuning. It sits at the intersection of three different configuration knobs, and missing any of them produces a number off by a factor that silently degrades convergence.
The formula itself is simple: multiply the per-device micro-batch by the number of accumulation steps and by the number of data-parallel ranks. The full effective batch is the number of training examples whose gradients are averaged together before each optimizer step, and it is the number that determines training dynamics.
This deep dive walks through the three axes that contribute, the worked example from the question, the practical implications for learning-rate tuning, and the subtle traps around loss reduction and variable-length sequences that can break the arithmetic in ways that are not obvious until you profile a misbehaving run.
The three axes that combine to produce effective batch
Effective batch is the product of three independent factors, each corresponding to a different way of distributing work.
Per-device batch size is the chunk one GPU processes in a single forward and backward pass. It is bounded by activation memory: more examples per pass means more activations cached for backward, which is the dominant memory term in transformer training. On an 80GB H100 with a 7B model at 2048 sequence length, the per-device batch typically tops out around 4 to 8 without gradient checkpointing.
Gradient accumulation steps is the number of these per-device passes that are summed in the gradient buffers before the optimizer steps. Each pass adds its gradient to the running sum in-place; no extra memory is needed beyond the gradient buffers themselves. After the configured number of passes, the optimizer steps once on the accumulated gradient and the buffers are zeroed. This is the memory versus throughput axis: doubling accumulation steps doubles the effective batch without increasing memory, at the cost of doubling the number of forward and backward passes per optimizer step.
World size is the number of data-parallel ranks. Each rank holds a complete copy of the model and runs its own forward-backward passes on its own slice of data. Gradients are averaged across ranks via an all-reduce before each optimizer step, so every rank contributes its examples to the effective batch. This is the spatial axis: more ranks means more examples processed in parallel, with linear scaling subject to interconnect bandwidth.
These three axes multiply because they are orthogonal. World size 4 with per-device batch 2 and accumulation steps 8 means 4 ranks each process 2 examples per pass and accumulate 8 passes before stepping, for a total of 64 examples per optimizer update.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Hugging Face Trainer logs effective_train_batch_size at startup as per_device times accum times world for exactly this reason.
- Axolotl's YAML configs expose all three knobs explicitly so users can compute the effective batch before launching long Llama 4 fine-tuning runs.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy does tensor parallel not multiply the effective batch the way data parallel does?
Trace what each parallelism shards. Data parallel replicates the model and shards the batch, so each rank contributes to the effective batch; tensor parallel shards the model itself and replicates the batch, so the same batch is processed once across all ranks.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Forgetting to multiply by world_size when computing effective batch. The optimizer sees the combined gradient from all data-parallel ranks, not just one rank's contribution.
60 second bullets to scan on the way to the call.
The three-factor formula: per_device times accum times world_size
Why data parallel multiplies the effective batch but tensor parallel does not
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.