Skip to content
induwara.lk
induwara.lkAI · Fine-tuning

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.

By Induwara AshinsanaUpdated Jul 4, 2026
Effective batch size
Formula cited · HF Trainer
Popular setups

per_device_train_batch_size, what fits in VRAM.

gradient_accumulation_steps.

World size, data-parallel devices.

warmup_ratio × total steps = warmup steps.

Effective batch size
32
m × g × w = 4 × 8 × 1
Micro-batches / step
8
Optimizer steps / epoch
1,563
Total optimizer steps
4,689
Warmup steps
141
Tokens / optimizer step
65,536
Total tokens seen
307,298,304

Step-by-step

QuantityFormulaValue
Effective batch (B)m × g × w32
Micro-batches / stepg × w8
Steps / epochceil(D / B)1,563
Total steps (S)steps/epoch × E4,689
Warmup stepsround(S × ratio)141
Tokens / stepB × L65,536
Total tokensS × B × L307,298,304

Cross-check: the naive token count D × L × E is 307,200,000. Using HF Trainer's internal dataloader-floor method the run is 4,686 optimizer steps, within a few of the 4,689 headline, the difference being how the final partial batch of each epoch is counted.

Paste into your trainer

TrainingArguments(
    per_device_train_batch_size=4,
    gradient_accumulation_steps=8,
    num_train_epochs=3,
    warmup_ratio=0.03,
)  # effective batch = 4 × 8 = 32

Effective-batch and warmup formulas follow the Hugging Face TrainingArguments reference and the gradient-accumulation guide. Every figure is integer arithmetic computed in your browser, no model, no upload, no API call.

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

Forward, 2×GPU QLoRA, effective batch 64

  1. Inputs: m=4, g=8, w=2, D=50,000, E=3, L=1,024, warmup 0.03
  2. Effective batch B = 4 × 8 × 2 = 64
  3. Micro-batches / step = g × w = 8 × 2 = 16
  4. Steps / epoch = ceil(50,000 / 64) = ceil(781.25) = 782
  5. Total steps S = 782 × 3 = 2,346
  6. Warmup = round(2,346 × 0.03) = round(70.38) = 70
  7. Tokens / step = 64 × 1,024 = 65,536
  8. Total tokens = 2,346 × 65,536 = 153,747,456 (naive 153,600,000)

Reverse, solve accumulation for batch 128 on one GPU

  1. Inputs: target B* = 128, m = 2, w = 1
  2. g = ceil(128 / (2 × 1)) = ceil(64) = 64
  3. Achieved batch = 2 × 64 × 1 = 128, exact, no overshoot

Edge case, reverse target that is not divisible

  1. Inputs: target B* = 100, m = 3, w = 1
  2. g = ceil(100 / 3) = ceil(33.33) = 34
  3. Achieved batch = 3 × 34 = 102
  4. Overshoot = 102 − 100 = 2 (you cannot accumulate a fraction of a step)

Frequently asked questions

Sources & references

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.