Migrating to BOINC AI Accelerate
This tutorial will detail how to easily convert existing PyTorch code to use ๐ Accelerate! Youโll see that by just changing a few lines of code, ๐ Accelerate can perform its magic and get you on your way toward running your code on distributed systems with ease!
The base training loop
To begin, write out a very basic PyTorch training loop.
We are under the presumption that training_dataloader
, model
, optimizer
, scheduler
, and loss_function
have been defined beforehand.
Copied
Add in ๐ Accelerate
Copied
Setting the right device
Copied
Preparing your objects
Copied
These objects are returned in the same order they were sent in. By default when using device_placement=True
, all of the objects that can be sent to the right device will be. If you need to work with data that isnโt passed to [~Accelerator.prepare] but should be on the active device, you should pass in the device
you made earlier.
Accelerate will only prepare objects that inherit from their respective PyTorch classes (such as torch.optim.Optimizer
).
Modifying the training loop
Copied
With that, your training loop is now ready to use ๐ Accelerate!
The finished code
Below is the final version of the converted code:
Copied
More Resources
Last updated