Effective Batch Size & Gradient Accumulation Calculator
Find the effective (global) batch size of an LLM fine-tune from your per-device batch, gradient accumulation steps, and GPU count — then get optimizer steps, warmup steps, and tokens seen. Reverse mode solves the accumulation steps for a target batch. Runs in your browser, formula cited.
How it works
Every figure is integer arithmetic — no model is downloaded or run. The calculator uses the same definition the Hugging Face Trainer applies internally. Let m be the per-device batch size, g the gradient accumulation steps, w the world size (number of GPUs), D the dataset size, E the epochs, L the sequence length, and r the warmup ratio.
1. Effective (global) batch size
B = m × g × w. Gradient accumulation runs g forward/backward passes on each device before a single synchronized optimizer update, so it simulates a larger batch without extra memory. Data parallelism across w GPUs multiplies it again. (Hugging Face, TrainingArguments.)
2. Optimizer steps per epoch
With the last partial batch kept and padded, steps/epoch = ceil(D / B); with drop last on it is floor(D / B). Total optimizer steps are S = steps/epoch × E, rounded to an integer for fractional epochs. HF's Trainer floor-divides the dataloader length by the accumulation steps, which can shift the count by a step or two at each epoch boundary — the tool shows both so the gap is explicit rather than hidden.
3. Warmup steps
warmup = round(S × r). This mirrors how the Trainer derives warmup_steps from warmup_ratio when you do not set an absolute step count — useful for a linear or cosine schedule that ramps the learning rate over the first few percent of training.
4. Tokens seen
Tokens per optimizer step are B × L, and the run total is S × B × L. That sits a little above the naive D × L × E because the padded final batch of each epoch adds tokens; dropping the last batch removes the gap.
5. Reverse mode
Given a target effective batch B*, the accumulation steps are g = ceil(B* / (m × w)). When B* is not divisible by m × w the achieved batch overshoots — the tool reports the real batch and how far above target it lands, because you can only accumulate a whole number of steps.
Worked examples
Frequently asked questions
Sources & references
- Hugging Face — TrainingArguments (per_device_train_batch_size, gradient_accumulation_steps, warmup_ratio)
- Hugging Face — Efficient training on a single GPU: gradient accumulation
- PyTorch — Performance tuning guide (gradient accumulation pattern)
The effective-batch, optimizer-step, and warmup formulas were last cross-checked against the Hugging Face Trainer documentation on 2026-07-04. The page is reviewed when the Trainer batch or scheduler semantics change.
Related tools
Comments & feedback
Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.
Found a bug, edge case, or want another training-config field added?
Email me at [email protected] — most fixes ship within 24 hours.