Search

Search papers and lessons

Accurate, Large Minibatch SGD: Training ImageNet in 1 Hour

Priya Goyal, Piotr Dollár, Ross Girshick et al. · 2017

Goyal and colleagues trained ResNet-50 on ImageNet with 8192 images per step on 256 GPUs in one hour, keeping the accuracy of the 256-image baseline, using a linear learning-rate rule and a warmup.

On this page

  • Colour = topic mix
  • Discrete math
  • Optimizers
  • Probability
  • Calculus
  • Linear algebra
  • Systems
  • ReadReady to readNot yet

TL;DR

Goyal and colleagues showed how to train an image classifier on ImageNet with very large batches of training images, 8192 per step, spread over 256 GPUs, and finish in about one hour. Their accuracy matched a baseline trained with 256 images per step. The method has two parts: scale the learning rate linearly with the batch size, and start training with a gradual warmup so the large steps do not break the early phase.

Builds on: Stochastic Gradient Descent, Data Parallelism, All-Reduce, Deep Residual Learning for Image Recognition (ResNet) · Leads to: Linear Scaling Rule, Learning Rate Warmup

Why it mattered

The paper made large-batch distributed training a standard tool. Its linear scaling rule and its warmup became default settings in many large vision and language training runs, and the rule is now taught as the first thing to try when a batch size changes. The paper also showed that the same rule transferred to detection and segmentation, using Mask R-CNN on COCO, so the gain was not limited to classification.

The practical lesson is about time. Before this work, ResNet-50 took about 29 hours on 8 GPUs at batch size 256. With batch size 8192 on 256 GPUs, the same model trained in about one hour, which made it possible to test ideas on ImageNet quickly.

The problem

Training with minibatch stochastic gradient descent updates the weights from the average gradient of a small set of examples. A larger batch gives a more accurate average gradient, and it lets many GPUs share the work, so each GPU keeps a fixed amount of computation. The catch is that a larger batch does not, by itself, give the same training result. Changing the batch size changes how the optimiser behaves, and the step size that works for one batch size may fail at another.

Before this paper, it was known informally that the learning rate should grow with the batch size, but its limits were not understood. It was also unclear how to handle the early phase of training, where the network changes quickly and a large step can make the loss spike. The paper set out to answer both questions for a batch size of 8192, which was far larger than had been tested with a matched accuracy target.

How it works

Take one update of the baseline, with batch size n=256n = 256 and learning rate η=0.1\eta = 0.1. Now suppose we want to use k=32k = 32 times as many images per step, a batch of kn=8192kn = 8192, and keep the same number of passes over the data. The method proceeds as follows.

  1. Split the batch across workers. With 8 GPUs per server and 32 images per GPU, one server holds a batch of 256. Using 256 GPUs gives 8192 images per step. Check: 256×32=8192256 \times 32 = 8192.
  2. Scale the learning rate. The linear scaling rule multiplies the learning rate by the same factor kk as the batch size. The reference learning rate is η=0.1⋅kn/256\eta = 0.1 \cdot kn / 256, so for 8192 it is 0.1×32=3.20.1 \times 32 = 3.2. Check: 3.2/0.1=32=k3.2 / 0.1 = 32 = k.
  3. Warm up in the first epochs. An epoch is one full pass over the training images. A learning rate of 3.2 at the start is too large, because the network is still changing fast. The learning rate starts at 0.1 and rises by a fixed amount at each iteration until it reaches 3.2 after 5 epochs. Check: the rise is smooth, not a jump.
  4. Keep the other settings. Momentum, weight decay and the batch-normalization batch size of 32 stay fixed. The paper keeps the batch-normalization statistics computed over 32 images per GPU, so the loss being optimised does not change with the total batch size.
  5. Train and decay. Training runs for 90 epochs. The learning rate is divided by 10 at epochs 30, 60 and 80, as in the baseline. Check: the schedule has the same shape as the baseline, scaled only by the peak rate.
  6. Aggregate gradients across GPUs. Each layer's gradient is summed across all GPUs with an All-Reduce, done while the backward pass continues on the earlier layers. This keeps the communication from waiting until the end of the step.
flowchart LR
  A["Batch 8192 split over 256 GPUs"] --> B["Forward and backward per GPU"]
  B --> C["All-reduce each layer's gradient"]
  C --> D["Update with scaled learning rate"]
  D --> E["Warmup for 5 epochs, then decay"]

The diagram shows the training loop for one step. The all-reduce runs layer by layer during the backward pass, and the learning rate follows the warmup and decay schedule.

The math

The paper writes minibatch SGD with momentum and learning rate η\eta as an update on the average gradient of a minibatch B\mathcal{B} of size nn:

wt+1=wt−η1n∑x∈B∇l(x,wt).w_{t+1} = w_t - \eta \frac{1}{n} \sum_{x \in \mathcal{B}} \nabla l(x, w_t).

Step 1 in symbols: the linear scaling rule

Take kk small steps of size nn at learning rate η\eta, and compare them with one step on the combined batch of size knkn at learning rate η^\hat{\eta}. The small steps give wt+k=wt−η1n∑j<k∑x∈Bj∇l(x,wt+j)w_{t+k} = w_t - \eta \frac{1}{n} \sum_{j<k} \sum_{x \in \mathcal{B}_j} \nabla l(x, w_{t+j}). The large step gives w^t+1=wt−η^1kn∑j<k∑x∈Bj∇l(x,wt)\hat{w}_{t+1} = w_t - \hat{\eta} \frac{1}{kn} \sum_{j<k} \sum_{x \in \mathcal{B}_j} \nabla l(x, w_t).

If the gradients did not change over the kk steps, that is ∇l(x,wt)≈∇l(x,wt+j)\nabla l(x, w_t) \approx \nabla l(x, w_{t+j}), then the two updates agree exactly when η^=kη\hat{\eta} = k \eta. The paper uses this as the rule: multiply the learning rate by kk when the batch size is multiplied by kk. The assumption is strong, and it fails early in training, which is why the warmup is needed.

Step 2 in symbols: the reference learning rate

For the ResNet-50 experiments the paper sets

η=0.1⋅kn256.\eta = 0.1 \cdot \frac{kn}{256}.

With kn=256kn = 256 this gives η=0.1\eta = 0.1, the baseline. With kn=8192kn = 8192 it gives η=3.2\eta = 3.2.

Step 3 in symbols: the gradual warmup

The warmup raises the rate linearly from ηstart=0.1\eta_{\text{start}} = 0.1 to η^=3.2\hat{\eta} = 3.2 over 5 epochs. If the rise is spread evenly over the TT iterations of those epochs, the rate increases by (3.2−0.1)/T(3.2 - 0.1)/T at each step. The paper does not state TT, but the ImageNet training set has about 1.28 million images, so one epoch of batch 8192 has about 1.28×106/8192≈1561.28 \times 10^6 / 8192 \approx 156 iterations, and five epochs have about 781. The rise per step is then about 3.1/781≈0.00403.1 / 781 \approx 0.0040.

Results

The main comparison is in Table 1 of the paper, with validation error on ImageNet and ResNet-50, averaged over 5 trials:

SettingBatch sizeLearning rateTop-1 error (%)
Baseline (8 GPUs, one server)2560.123.60 ± 0.12
No warmup81923.224.84 ± 0.37
Constant warmup81923.225.88 ± 0.56
Gradual warmup81923.223.74 ± 0.09

The gradual warmup run is within 0.14 points of the baseline, about the size of the run-to-run spread the authors measured (standard deviations of 0.09 and 0.12). The constant warmup is worse than no warmup at all, because the error spikes when the rate leaves the low phase.

Figure 1 of the paper shows validation error across batch sizes from 64 to 65536. It stays flat from 64 up to 8k, then rises, and training diverges beyond 64k with the linear rule. The paper reports about 90 percent scaling efficiency from 8 to 256 GPUs, and that one pass over the ImageNet training images took about 30 seconds on 352 GPUs. The method also transferred: ResNet-50 models pre-trained with batch sizes up to 8k gave Mask R-CNN detection and segmentation results on COCO matching the small-batch baseline.

The limits are stated plainly. The paper notes that the linear rule works up to about 8k on ImageNet, that the ImageNet-5k experiment gives a similar elbow, and that more work is needed to explain why the limit sits where it does. Results are for one architecture and one dataset family, and the authors used simulated gradient accumulation for batches of 12k and above on a single server, which they checked against true distributed runs.

Reading guide

Check yourself

QuestionThe baseline batch size is 256 with learning rate 0.1. What learning rate does the reference rule give for a batch of 2048?Show answer

The rule is η=0.1⋅kn/256\eta = 0.1 \cdot kn / 256. For kn=2048kn = 2048 the factor is 2048/256=82048 / 256 = 8, so η=0.1×8=0.8\eta = 0.1 \times 8 = 0.8.

QuestionWhy does the paper use a warmup, when the linear rule already gives the right learning rate for a batch of 8192?Show answer

The linear rule assumes that the gradients barely change over the steps it replaces. That assumption fails early in training, when the network changes quickly. A large rate of 3.2 at the start can make the loss spike. The warmup starts at 0.1 and rises to 3.2 over 5 epochs, so the large rate is used only after the network has settled.

QuestionTable 1 gives 23.60 for the baseline and 23.74 for the gradual warmup run. Is the difference larger than the authors' measured noise?Show answer

No. The difference is 23.74−23.60=0.1423.74 - 23.60 = 0.14 points. The standard deviations are 0.12 and 0.09, so the gap is about the size of the run-to-run spread. The paper reads the two as matching.

QuestionA team wants to use batch-normalization with 8192 images per step across 256 GPUs. They compute the batch statistics over all 8192 images. What does the paper say this changes?Show answer

It changes the loss being optimised. The paper keeps the batch-normalization batch fixed at 32 images per GPU, because the batch-normalization statistics depend on the size of the group they are computed over. Computing them across all 8192 images changes the function being minimised, not only the speed.

QuestionWhy does the paper overlap the all-reduce with the backward pass, instead of waiting until all gradients are ready?Show answer

Each layer's gradient is ready at a different time in the backward pass, and the gradients of one layer do not depend on the others. Starting the all-reduce as soon as a layer's gradient is ready hides most of the communication behind computation, which is needed to reach near-linear scaling across servers.

Sources

Read the full paper

Priya Goyal, Piotr Dollár, Ross Girshick et al., 2017. arXiv:1706.02677