| 1 |
<!--Copyright 2022 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 |
# Comparing performance across distributed setups |
| 17 |
|
| 18 |
Evaluating and comparing the performance from different setups can be quite tricky if you don't know what to look for. |
| 19 |
For example, you cannot run the same script with the same batch size across TPU, multi-GPU, and single-GPU with Accelerate |
| 20 |
and expect your results to line up. |
| 21 |
|
| 22 |
But why? |
| 23 |
|
| 24 |
There are three reasons for this that this tutorial will cover: |
| 25 |
|
| 26 |
1. **Setting the right seeds** |
| 27 |
2. **Observed Batch Sizes** |
| 28 |
3. **Learning Rates** |
| 29 |
|
| 30 |
## Setting the Seed |
| 31 |
|
| 32 |
While this issue has not come up as much, make sure to use [`utils.set_seed`] to fully set the seed in all distributed cases so training will be reproducible: |
| 33 |
|
| 34 |
```python |
| 35 |
from accelerate.utils import set_seed |
| 36 |
|
| 37 |
set_seed(42) |
| 38 |
``` |
| 39 |
|
| 40 |
Why is this important? Under the hood this will set **5** different seed settings: |
| 41 |
|
| 42 |
```python |
| 43 |
random.seed(seed) |
| 44 |
np.random.seed(seed) |
| 45 |
torch.manual_seed(seed) |
| 46 |
torch.cuda.manual_seed_all(seed) # or torch.xpu.manual_seed_all, etc |
| 47 |
# ^^ safe to call this function even if cuda is not available |
| 48 |
if is_torch_xla_available(): |
| 49 |
xm.set_rng_state(seed) |
| 50 |
``` |
| 51 |
|
| 52 |
The random state, numpy's state, torch, torch's device state, and if TPUs are available torch_xla's cuda state. |
| 53 |
|
| 54 |
## Observed Batch Sizes |
| 55 |
|
| 56 |
When training with Accelerate, the batch size passed to the dataloader is the **batch size per GPU**. What this entails is |
| 57 |
a batch size of 64 on two GPUs is truly a batch size of 128. As a result, when testing on a single GPU this needs to be accounted for, |
| 58 |
as well as similarly for TPUs. |
| 59 |
|
| 60 |
The below table can be used as a quick reference to try out different batch sizes: |
| 61 |
|
| 62 |
<Tip> |
| 63 |
|
| 64 |
In this example, there are two GPUs for "Multi-GPU" and a TPU pod with 8 workers |
| 65 |
|
| 66 |
</Tip> |
| 67 |
|
| 68 |
| Single GPU Batch Size | Multi-GPU Equivalent Batch Size | TPU Equivalent Batch Size | |
| 69 |
|-----------------------|---------------------------------|---------------------------| |
| 70 |
| 256 | 128 | 32 | |
| 71 |
| 128 | 64 | 16 | |
| 72 |
| 64 | 32 | 8 | |
| 73 |
| 32 | 16 | 4 | |
| 74 |
|
| 75 |
## Learning Rates |
| 76 |
|
| 77 |
As noted in multiple sources[[1](https://aws.amazon.com/blogs/machine-learning/scalable-multi-node-deep-learning-training-using-gpus-in-the-aws-cloud/)][[2](https://docs.nvidia.com/clara/clara-train-sdk/pt/model.html#classification-models-multi-gpu-training)], the learning rate should be scaled *linearly* based on the number of devices present. The below |
| 78 |
snippet shows doing so with Accelerate: |
| 79 |
|
| 80 |
<Tip> |
| 81 |
|
| 82 |
Since users can have their own learning rate schedulers defined, we leave this up to the user to decide if they wish to scale their |
| 83 |
learning rate or not. |
| 84 |
|
| 85 |
</Tip> |
| 86 |
|
| 87 |
```python |
| 88 |
learning_rate = 1e-3 |
| 89 |
accelerator = Accelerator() |
| 90 |
learning_rate *= accelerator.num_processes |
| 91 |
|
| 92 |
optimizer = AdamW(params=model.parameters(), lr=learning_rate) |
| 93 |
``` |
| 94 |
|
| 95 |
You will also find that `accelerate` will step the learning rate based on the number of processes being trained on. This is because |
| 96 |
of the observed batch size noted earlier. So in the case of 2 GPUs, the learning rate will be stepped twice as often as a single GPU |
| 97 |
to account for the batch size being twice as large (if no changes to the batch size on the single GPU instance are made). |
| 98 |
|
| 99 |
## Gradient Accumulation and Mixed Precision |
| 100 |
|
| 101 |
When using gradient accumulation and mixed precision, due to how gradient averaging works (accumulation) and the precision loss (mixed precision), |
| 102 |
some degradation in performance is expected. This will be explicitly seen when comparing the batch-wise loss between different compute |
| 103 |
setups. However, the overall loss, metric, and general performance at the end of training should be _roughly_ the same. |