[wip] [doc] Performance and Scalability notes #9824
Labels
Performance
WIP
Label your PR/Issue with WIP for some long outstanding Issues/PRs that are work in progress
Let's start another doc. I think it works the best to work on these as an issue and not a PR since anybody can read these easily, rather than reading a markdown.
As in the other similar work-in-progress-doc, let me write the bulk of it out and then you can ask questions / make requests and clarifications.
Performance and Scalability: How To Fit a Bigger Model and Train It Faster
Quick notes:
This section gives brief ideas on how to make training faster and support bigger models. Later sections will expand, demonstrate and elucidate each of these.
Faster Training
HW:
SW:
Bigger Models
HW:
SW:
Hardware
Multi-GPU Connectivity
If you use multiple GPUs the way cards are inter-connected can have a huge impact on the total training time.
If the GPUs are on the same physical node, you can run:
and it will tell you how the GPUs are inter-connected.
On a machine with dual-GPU and which are connected with NVLink, you will most likely see something like:
on a different machine w/o NVLink we may see:
The report includes this Legend:
So the first report
NV2
tells us the GPUs are interconnected with 2 NVLinks, and the second reportPHB
we have a typical consumer-level PCIe+Bridge setup.Check what type of connectivity you have on your setup. Some of these will make the communication between cards faster (e.g. NVLink), others slower (e.g. PHB).
Depending on the type of scalability solution used, the connectivity speed could have a major or a minor impact. If the GPUs need to sync rarely, as in DDP, the impact of a slower connection will be less significant. If the GPUs need to send messages to each other often, as in ZeRO-DP, then faster connectivity becomes super important to achieve faster training.
NVlink
NVLink is a wire-based serial multi-lane near-range communications link developed by Nvidia.
Each new generation provides a faster bandwidth, e.g. here is a quote from Nvidia Ampere GA102 GPU Architecture:
So the higher
X
you get in the report ofNVX
in the output ofnvidia-smi topo -m
the better. The generation will depend on your GPU architecture.Let's compare the execution of a gpt2 language model training over a small sample of wikitext.
The results are:
You can see that NVLink completes the training ~23% faster.
In the second benchmark we use
NCCL_P2P_DISABLE=1
to tell the GPUs not to use NVLink.Here is the full benchmark code and outputs:
Hardware: 2x TITAN RTX 24GB each + NVlink with 2 NVLinks (
NV2
innvidia-smi topo -m
)Software:
pytorch-1.8-to-be
+cuda-11.0
/transformers==4.3.0.dev0
Software
Anatomy of Model's Memory
The components on GPU memory are the following:
forward
vsbackward
Execution SpeedFor convolutions and linear layers there are 2x flops in the backward compared to the forward, which generally translates into ~2x slower (sometimes more, because sizes in the backward tend to be more awkward). Activations are usually bandwidth-limited, and it’s typical for an activation to have to read more data in the backward than in the forward (e.g. activation forward reads once, writes once, activation backward reads twice, gradOutput and output of the forward, and writes once, gradInput).
fp16
AMP = Automatic Mixed Precision
If we look at what's happening with FP16 training (mixed precision) we have:
So the saving only happen for the forward activations saved for the backward computation, and there is a slight overhead because the gradients are properly stored both in half and full precision. (This is probably over-simplified but I think it's enough to explain what follows.)
Now let's look at a simple text-classification fine-tuning on 2 GPUs (I'm giving the command for reference):
Since the only savings we get are in the model activations saved for the backward passed, it's logical that the bigger those activations are, the bigger the saving will be. If we try different batch sizes, I indeed get (this is with nvidia-smi so not completely reliable as said above but it will be a fair comparison):
So there is only a real memory saving if we train at a high batch size (and it's not half) and at batch sizes lower than 8, you actually get a bigger memory footprint (because of the overhead mentioned above). The gain for FP16 training is that in each of those cases, the training with the flag
--fp16
is twice as fast, which does require every tensor to have every dimension be a multiple of 8 (so if your batch size is not a multiple of 8, you won't get that speed-up, and the scriptfinetune_trainer.py
does not pad the tensors to a sequence length that is a multiple of 8).TL;DR: FP16 with apex or AMP will only give you some memory savings with a reasonably high batch size.
Some amazing tutorials to read on mixed precision:
fp16 caching
pytorch
autocast
which performs AMP include a caching feature, which speed things up by caching fp16-converted values. Here is the full description from this comment:Autocast maintains a cache of the FP16 casts of model params (leaves). This helps streamline parameter reuse: if the same FP32 param is used in several different FP16list ops, like several matmuls, instead of re-casting the param to FP16 on entering each matmul, the cast will occur on the first matmul, the casted FP16 copy will be cached, and for all later matmuls the FP16 copy will be reused. The cache is maintained only within a particular outermost autocast context. When you exit the autocast context the cache is dropped. For recommended usage, in which autocast wraps the forward pass, and then you exit the context before calling backward(), this means the cache only lasts the duration of the forward pass each iteration, and will be rebuilt next iteration. (The cache of FP16-casted copies MUST be rebuilt each iteration. The FP32 params get updated by the optimizer, so the FP16 copies must be recreated, otherwise the FP16 values will be stale.)
DP vs DDP
DistributedDataParallel
(DDP) is typically faster thanDataParallel
(DP), but it is not always the case:Here are the main differences in the inter-GPU communication overhead between the two modes:
DDP:
backward
, once the local gradients are ready, they are then averaged across all processesDP:
For each batch:
forward
and sends output from each gpu to gpu 0, computes lossbackward
The only communication DDP performs per batch is sending gradients, whereas DP does 5 different data exchanges per batch.
DP copies data within the process via python threads, whereas DDP copies data via torch.distributed.
Under DP gpu 0 performs a lot more work than the rest of the gpus, thus resulting in under-utilization of gpus.
You can use DDP across multiple machines, but this is not the case with DP.
There are other differences between DP and DDP but they aren't relevant to this discussion.
If you want to go really deep into understanding these 2 modes, this article is highly recommended, as it has great diagrams, includes multiple benchmarks and profiler outputs on various hardware, explains all the nuances that you may need to know.
Let's look at an actual benchmark:
Analysis:
Here DP is ~10% slower than DDP w/ NVlink, but ~15% faster than DDP w/o NVlink
The real difference will depend on how much data each GPU needs to sync with the others - the more there is to sync, the more a slow link will slow down the total runtime.
Here is the full benchmark code and outputs:
NCCL_P2P_DISABLE=1
was used to disable the NVLink feature on the corresponding benchmark.Hardware: 2x TITAN RTX 24GB each + NVlink with 2 NVLinks (
NV2
innvidia-smi topo -m
)Software:
pytorch-1.8-to-be
+cuda-11.0
/transformers==4.3.0.dev0
Batch Sizes
The best performance is achieved when the tensor's batch size dimension is a multiple of 8. It's the final batch size of the tensor that gets passed to the GPU to calculate something that's important.
Examples:
chunks=3
is used, you want the batch size to be 24 (or a higher multiple of 8). Because if you use a batch size of 16, you will end up with 3 micro-batches of size 6,5,5.There is no harm in using smaller batch sizes and at times one can hardly squeeze a batch size of 1 before getting OOM, it just won't be as fast as it can be.
DataLoader
One of the important requirements to reach great training speed is the ability to feed the GPU at the maximum speed it can handle. By default everything happens in the main process and it might not be able to read the data from disk fast enough, and thus create a bottleneck, leading to GPU under-utilization.
DataLoader(
pin_memory=True, ...)
which ensures that the data gets preloaded into the pinned memory on CPU and typically leads to much faster transfers from CPU to GPU memory.DataLoader(
num_workers=4, ...)
- spawn several workers to pre-load data faster - during training watch the GPU utilization stats and if it's far from 100% experiment with raising the number of workers. Of course, the problem could be elsewhere so a very big number of workers won't necessarily lead to a better performance.Faster optimizer
pytorch-nightly introduced
torch.optim._multi_tensor
which should significantly speed up the optimizers for situations with lots of small feature tensors. It should eventually become the default, but if you want to experiment with it sooner and don't mind using the bleed-edge, see: #9965Credits
It'd be difficult to track and record every contribution, so in order to keep things practical I will try to keep track of major contributors. And I have a huge gratitude to everybody who has ever asked or answered a question on forums/issues/slacks/SO/etc., parts or summaries of which were integrated into this article. Thank you!
The major contributors:
The text was updated successfully, but these errors were encountered: