Recent findings suggest that this tutorial assumes you have a basic understanding of PyTorch and how to train a simple model. It will showcase training on multiple GPUs through a process called Distributed Data Parallelism (DDP) through three different levels of increasing abstraction:.
Take some very basic PyTorch training code below, which sets up and trains a model on MNIST based on the official MNIST example.
We define the training device (cuda):.
Build some PyTorch DataLoaders:.
Move the model to the CUDA device:.
Before finally creating a simplistic training and evaluation loop that performs one full iteration over the dataset and calculates the test accuracy:.
Typically from here, one could either throw all of this into a python script or run it on a Jupyter Notebook.
Recent findings suggest that however, how would you then get this script to run on say two GPUs or on multiple machines if these resources are available, which could improve training speed through distributed training? Just doing python myscript.py will only ever run the script using a single GPU. This is where torch.distributed comes into play.
As the name implies, torch.distributed is meant to work on distributed setups. This can include multi-node, where you have a number of machines each with a single GPU, or multi-gpu where a single system has multiple GPUs, or some combination of both.
To convert our above code to work within a distributed setup, a few setup configurations must first be defined, detailed in the Getting Started with DDP Tutorial.
Industry observers note that first a setup and a cleanup function must be declared. This will open up a processing group that all of the compute processes can communicate through. (Related: Industry News: Average Retirement Savings by Age: Are Y...)
Recent findings suggest that note: for this section of the tutorial it should be assumed these are sent in python script files. Later on a launcher using Accelerate will be discussed that removes this necessity. (Related: Is Buy Now Pay Later (BNPL) Worth It? The Hidden Costs...)
The last piece of the puzzle is how do I send my data and model to another GPU?
It has been revealed that this is where the DistributedDataParallel module comes into play. It will copy your model onto each GPU, and when loss.backward() is called the backpropagation is performed and the resulting gradients across all these copies of the model will be averaged/reduced. This ensures each device has the same weights post the optimizer step.
Below is an example of our training setup, refactored as a function, with this capability:.
Note: Here rank is the overall rank of the current GPU compared to all the other GPUs available, meaning they have a rank of 0 -> n-1.
The optimizer needs to be declared based on the model on the specific device (so ddp_model and not model) for all of the gradients to properly be calculated.
According to recent reports, lastly, to run the script PyTorch has a convenient torchrun command line module that can help. Just pass in the number of nodes it should use as well as the script to run and you are set:.
The above will run the training script on two GPUs that live on a single machine and this is the barebones for performing only distributed training with PyTorch.
Now let's talk about Accelerate, a library aimed to make this process more seameless and also help with a few best practices.
Accelerate is a library designed to allow you to perform what we just did above, without needing to modify your code greatly. On top of this, the data pipeline innate to Accelerate can also improve performance to your code as well.
First, let's wrap all of the above code we just performed into a single function, to help us visualize the difference:.
Next let's talk about how Accelerate can help. There's a few concerns with the above code:.
Recent findings suggest that accelerate helps this through the Accelerator class. Through it, the code remains much the same except for three lines of code when comparing a single node to multinode, as shown below:.
It has been revealed that with this your PyTorch training loop is now setup to be ran on any distributed setup thanks to the Accelerator object. This code can then still be rolled out through the torchrun CLI or through Accelerate's own CLI interface, accelerate launch.
As a result its now trivialized to perform distributed training with Accelerate and keeping as much of the barebones PyTorch code the same as possible.
Earlier it was mentioned that Accelerate also makes the DataLoaders more efficient. This is through custom Samplers that can send parts of the batches automatically to different devices during training allowing for a single copy of the data to be known at one time, rather than four at once into memory depending on the configuration. Along with this, there is only a single full copy of the original dataset in memory total. Subsets of this dataset are split between all of the nodes that are utilized for training, allowing for much larger datasets to be trained on a single instance without an explosion in memory utilized.
Industry observers note that earlier it was mentioned you can start distributed code directly out of your Jupyter Notebook. This comes from Accelerate's notebook_launcher utility, which allows for starting multi-gpu training based on code inside of a Jupyter Notebook.
To use it is as trivial as importing the launcher:.
According to recent reports, and passing the training function we declared earlier, any arguments to be passed, and the number of processes to use (such as 8 on a TPU, or 2 for two GPUs). Both of the above training functions can be ran, but do note that after you start a single launch, the instance needs to be restarted before spawning another.
Finally, we arrive at the highest level of API -- the Hugging Face Trainer.
This wraps as much training as possible while still being able to train on distributed systems without the user needing to do anything at all.
First we need to import the Trainer:.
Industry observers note that then we define some TrainingArguments to control all the usual hyper-parameters. The trainer also works through dictionaries, so a custom collate function needs to be made.
Finally, we subclass the trainer and write our own compute_loss.
Afterwards, this code will also work on a distributed setup without any training code needing to be written whatsoever!
Similarly to the above examples with the notebook_launcher, this can be done again here by throwing it all into a training function:.
To learn more about PyTorch Distributed Data Parallelism, check out the documentation here.
To learn more about 🤗 Accelerate, check out the documentation here.
To learn more about 🤗 Transformers, check out the documentation here.