| 1 |
<!--Copyright 2023 The HuggingFace Team. All rights reserved. |
| 2 |
|
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 |
the License. You may obtain a copy of the License at |
| 5 |
|
| 6 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
|
| 8 |
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 |
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 |
specific language governing permissions and limitations under the License. |
| 11 |
|
| 12 |
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be |
| 13 |
rendered properly in your Markdown viewer. |
| 14 |
--> |
| 15 |
|
| 16 |
# Using Local SGD with Accelerate |
| 17 |
|
| 18 |
Local SGD is a technique for distributed training where gradients are not synchronized every step. Thus, each process updates its own version of the model weights and after a given number of steps these weights are synchronized by averaging across all processes. This improves communication efficiency and can lead to substantial training speed up especially when a computer lacks a faster interconnect such as NVLink. |
| 19 |
Unlike gradient accumulation (where improving communication efficiency requires increasing the effective batch size), Local SGD does not require changing a batch size or a learning rate / schedule. However, if necessary, Local SGD can be combined with gradient accumulation as well. |
| 20 |
|
| 21 |
In this tutorial you will see how to quickly setup Local SGD Accelerate. Compared to a standard Accelerate setup, this requires only two extra lines of code. |
| 22 |
|
| 23 |
This example will use a very simplistic PyTorch training loop that performs gradient accumulation every two batches: |
| 24 |
|
| 25 |
```python |
| 26 |
device = "cuda" |
| 27 |
model.to(device) |
| 28 |
|
| 29 |
gradient_accumulation_steps = 2 |
| 30 |
|
| 31 |
for index, batch in enumerate(training_dataloader): |
| 32 |
inputs, targets = batch |
| 33 |
inputs = inputs.to(device) |
| 34 |
targets = targets.to(device) |
| 35 |
outputs = model(inputs) |
| 36 |
loss = loss_function(outputs, targets) |
| 37 |
loss = loss / gradient_accumulation_steps |
| 38 |
loss.backward() |
| 39 |
if (index + 1) % gradient_accumulation_steps == 0: |
| 40 |
optimizer.step() |
| 41 |
scheduler.step() |
| 42 |
optimizer.zero_grad() |
| 43 |
``` |
| 44 |
|
| 45 |
## Converting it to Accelerate |
| 46 |
|
| 47 |
First the code shown earlier will be converted to use Accelerate with neither a LocalSGD or a gradient accumulation helper: |
| 48 |
|
| 49 |
```diff |
| 50 |
+ from accelerate import Accelerator |
| 51 |
+ accelerator = Accelerator() |
| 52 |
|
| 53 |
+ model, optimizer, training_dataloader, scheduler = accelerator.prepare( |
| 54 |
+ model, optimizer, training_dataloader, scheduler |
| 55 |
+ ) |
| 56 |
|
| 57 |
for index, batch in enumerate(training_dataloader): |
| 58 |
inputs, targets = batch |
| 59 |
- inputs = inputs.to(device) |
| 60 |
- targets = targets.to(device) |
| 61 |
outputs = model(inputs) |
| 62 |
loss = loss_function(outputs, targets) |
| 63 |
loss = loss / gradient_accumulation_steps |
| 64 |
+ accelerator.backward(loss) |
| 65 |
if (index+1) % gradient_accumulation_steps == 0: |
| 66 |
optimizer.step() |
| 67 |
scheduler.step() |
| 68 |
``` |
| 69 |
|
| 70 |
## Letting Accelerate handle model synchronization |
| 71 |
|
| 72 |
All that is left now is to let Accelerate handle model parameter synchronization **and** the gradient accumulation for us. For simplicity let us assume we need to synchronize every 8 steps. This is |
| 73 |
achieved by adding one `with LocalSGD` statement and one call `local_sgd.step()` after every optimizer step: |
| 74 |
|
| 75 |
```diff |
| 76 |
+local_sgd_steps=8 |
| 77 |
|
| 78 |
+with LocalSGD(accelerator=accelerator, model=model, local_sgd_steps=8, enabled=True) as local_sgd: |
| 79 |
for batch in training_dataloader: |
| 80 |
with accelerator.accumulate(model): |
| 81 |
inputs, targets = batch |
| 82 |
outputs = model(inputs) |
| 83 |
loss = loss_function(outputs, targets) |
| 84 |
accelerator.backward(loss) |
| 85 |
optimizer.step() |
| 86 |
scheduler.step() |
| 87 |
optimizer.zero_grad() |
| 88 |
+ local_sgd.step() |
| 89 |
``` |
| 90 |
|
| 91 |
Under the hood, the Local SGD code **disables** automatic gradient synchronization (but accumulation still works as expected!). Instead it averages model parameters every `local_sgd_steps` steps (as well as at the end of the training loop). |
| 92 |
|
| 93 |
## Limitations |
| 94 |
|
| 95 |
The current implementation works only with basic multi-GPU (or multi-CPU) training without, e.g., [DeepSpeed.](https://github.com/deepspeedai/DeepSpeed). |
| 96 |
|
| 97 |
## References |
| 98 |
|
| 99 |
Although we are not aware of the true origins of this simple approach, the idea of local SGD is quite old and goes |
| 100 |
back to at least: |
| 101 |
|
| 102 |
Zhang, J., De Sa, C., Mitliagkas, I., & Ré, C. (2016). [Parallel SGD: When does averaging help?. arXiv preprint |
| 103 |
arXiv:1606.07365.](https://huggingface.co/papers/1606.07365) |
| 104 |
|
| 105 |
We credit the term Local SGD to the following paper (but there might be earlier references we are not aware of). |
| 106 |
|
| 107 |
Stich, Sebastian Urban. ["Local SGD Converges Fast and Communicates Little." ICLR 2019-International Conference on |
| 108 |
Learning Representations. No. CONF. 2019.](https://huggingface.co/papers/1805.09767) |