๐ค Algorithms#
Under construction ๐ง
Included in the Composer library is a suite of algorithmic speedup algorithms. These modify the basic training procedure, and are intended to be composed together to easily create a complex and hopefully more efficient training routine. While other libraries may have implementations of some of these, the implementations in Composer are specifically written to be combined with other methods.
Below is a brief overview of the algorithms currently in Composer. For more detailed information about each algorithm, see the method cards, also linked in the table. Each algorithm has a functional implementation intended for use with your own training loop, and an implementation intended for use with Composerโs trainer.
Name |
tldr |
functional |
---|---|---|
Uses channels last memory format (NHWC) |
|
|
Combines pairs of examples in non-overlapping regions and mixes labels |
|
|
Computes running average of model weights. |
||
Removes columns and rows from the image for augmentation and efficiency. |
|
|
Scale the learning rate schedule by a factor |
||
SAM optimizer measures sharpness of optimization space |
||
Replace attention with AliBi |
|
|
Progressively increase sequence length. |
|
|
Blends pairs of examples and labels |
|
|
Replaces eligible layers with Squeeze-Excite layers |
|
|
Replaces a specified layer with a stochastic verion that randomly drops the layer or samples during training |
|
|
Factorize GEMMs into smaller GEMMs |
|
|
Use smaller samples to compute batchnorm |
|
|
Drops examples with small loss contributions. |
|
|
Increases the input image size during training |
|
|
Applies a series of random augmentations |
|
|
Applies blur before pooling or downsampling |
|
|
Progressively freezes layers during training. |
|
|
Smooths the labels with a uniform prior |
|
|
Image-perserving data augmentations |
|
|
Randomly erases rectangular blocks from the image. |
|
Functional API#
The simplest way to use Composerโs algorithms is through the functional API. Composerโs algorithms can be grouped into three broad classes:
data augmentations add additional transforms to the training data.
model surgery algorithms modify the network architecture.
training loop modifications change various properties of the training loop.
Data augmentations can be inserted either into the dataloader as a transform, or after a
batch has been loaded depending on what the augmentation acts on. Here is an example of using
RandAugment
with the functional API
import torch
from torchvision import datasets, transforms
from composer import functional as cf
c10_transforms = transforms.Compose([cf.randaugment(), # <---- Add RandAugment
transforms.ToTensor(),
transforms.Normalize(mean, std)])
dataset = datasets.CIFAR10('../data',
train=True,
download=True,
transform=c10_transforms)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=1024)
Other data augmentations, such as CutMix
act on a batch of inputs. These can be
inserted in the training loop after a batch is loaded from the dataloader as follows:
from composer import functional as CF
cutmix_alpha = 1
num_classes = 10
for batch_idx, (data, target) in enumerate(dataloader):
### Insert CutMix here ###
data = CF.cutmix(data, target, cutmix_alpha, num_classes)
### ------------------ ###
optimizer.zero_grad()
output = model(data)
loss = loss(output, target)
loss.backward()
optimizer.step()
Model surgery algorithms make direct modifications to the network itself. Functionally,
these can be called as follows, using BlurPool
as an example
import torchvision.models as models
from composer import functional as cf
model = models.resnet18()
cf.apply_blurpool(model)
Each method card has a section describing how to use these methods in your own trainer loop.
Composer Trainer#
To make full use of Composer, we recommend using our algorithms and trainer together. Using algorithms with the trainer is simple, just pass a list of the algorithms you want to run as the algorithms argument when initializing the trainer. Composer will automatically run each algorithm at the appropriate time during training, as well as handle any collisions and reorderings needed.
Here is an example of how to call trainer with a few algorithms:
from composer import Trainer
from composer.algorithms.blurpool import BlurPool
from composer.algorithms.channels_last import ChannelsLast
channels_last = ChannelsLast()
blurpool = BlurPool(replace_convs=True,
replace_maxpools=True,
blur_first=True)
trainer = Trainer(model=model,
train_dataloader=train_dataloader,
eval_dataloader=test_dataloader,
max_duration='90ep',
device='gpu',
algorithms=[channels_last, blurpool],
validate_every_n_epochs=-1,
seed=42)
Custom algorithms#
To implement a custom algorithm, it is necessary to first understand how Composer uses events to know where in the training loop to run an algorithm, and how algorithms can modify the state used for subsequent computations.