strategist922 / composer

library of algorithms to speed up neural network training

Home Page:http://docs.mosaicml.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool


A PyTorch Library for Efficient Neural Network Training

Train Faster, Reduce Cost, Get Better Models

[Website] - [Getting Started] - [Docs] - [Methods] - [We're Hiring!]

PyPi Version PyPi Package Version PyPi Downloads Documentation Chat @ Slack License


👋 Welcome

Composer is a library written in PyTorch that enables you to train neural networks faster, at lower cost, and to higher accuracy. We've implemented more than two dozen speed-up methods that can be applied to your training loop in just a few lines of code, or used with our built-in Trainer. We continually integrate the latest state-of-the-art in efficient neural network training.

Composer features:

  • 20+ methods for speeding up training networks for computer vision and language modeling. Don't waste hours trying to reproduce research papers when Composer has done the work for you.
  • An easy-to-use trainer that has been written to be as performant as possible and integrates best practices for efficient training.
  • Functional forms of all of our speedup methods that allow you to integrate them into your existing training loop.
  • Strong, reproducible baselines to get you started as quickly as possible.

Benefits

With no additional tuning, you can apply our methods to:

  • Train ResNet-50 on ImageNet to the standard 76.6% top-one accuracy for $40 in 1.2 hours (with vanilla PyTorch: $116 in 3.5 hours) on AWS.
  • Train a GPT-2 125M to a standard perplexity of 24.11 for $145 in 4.5 hours (with vanilla PyTorch: $255 in 7.8 hours) on AWS.

🚀 Quickstart

💾 Installation

Composer is available with Pip:

pip install mosaicml

Alternatively, install Composer with Conda:

conda install -c mosaicml mosaicml

🚌 Usage

You can use Composer's speedup methods in two ways:

  • Through a standalone Functional API (similar to torch.nn.functional) that allows you to integrate them into your existing training code.
  • Using Composer's built-in Trainer, which is designed to be performant and automatically takes care of many of the low-level details of using speedup methods.

Example: Functional API Open In Colab

Integrate our speed-up methods into your training loop with just a few lines of code, and see the results. Here we easily apply BlurPool and SqueezeExcite:

import composer.functional as cf
from torchvision import models

my_model = models.resnet18()

# add blurpool and squeeze excite layers
my_model = cf.apply_blurpool(my_model)
my_model = cf.apply_squeeze_excite(my_model)

# your own training code starts here

For more examples, see the Composer Functional API Colab notebook and Functional API guide.

Example: Trainer Open In Colab

For the best experience and the most efficient possible training, we recommend using Composer's built-in trainer, which automatically takes care of the low-level details of using speedup methods and provides useful abstractions that facilitate rapid experimentation.

from torch.utils.data import DataLoader
from torchvision import datasets, transforms

from composer import Trainer
from composer.algorithms import BlurPool, ChannelsLast, CutMix, LabelSmoothing
from composer.models import MNIST_Classifier

transform = transforms.Compose([transforms.ToTensor()])
train_dataset = datasets.MNIST("data", download=True, train=True, transform=transform)
eval_dataset = datasets.MNIST("data", download=True, train=False, transform=transform)
train_dataloader = DataLoader(train_dataset, batch_size=128)
eval_dataloader = DataLoader(eval_dataset, batch_size=128)

trainer = Trainer(
    model=MNIST_Classifier(num_classes=10),
    train_dataloader=train_dataloader,
    eval_dataloader=eval_dataloader,
    max_duration="2ep",
    algorithms=[
        BlurPool(replace_convs=True, replace_maxpools=True, blur_first=True),
        ChannelsLast(),
        CutMix(num_classes=10),
        LabelSmoothing(smoothing=0.1),
    ]
)
trainer.fit()

Composer's built-in trainer makes it easy to add multiple speedup methods in a single line of code! Trying out new methods or combinations of methods is as easy as changing a single list. As we continually implement more methods, they will be easy for you to add to your code.

For concrete examples of methods in Composer, here are some (see here for all) speedup methods currently in Composer:

Name Attribution tl;dr Example Benchmark Speed Up*
Alibi Press et al, 2021 Replace attention with AliBi. GPT-2 1.5x
BlurPool Zhang, 2019 Applies an anti-aliasing filter before every downsampling operation. ResNet-101 1.2x
ChannelsLast PyTorch Uses channels last memory format (NHWC). ResNet-101 1.5x
CutOut DeVries et al, 2017 Randomly erases rectangular blocks from the image. ResNet-101 1.2x
LabelSmoothing Szegedy et al, 2015 Smooths the labels with a uniform prior ResNet-101 1.5x
MixUp Zhang et al, 2017 Blends pairs of examples and labels. ResNet-101 1.5x
RandAugment Cubuk et al, 2020 Applies a series of random augmentations to each image. ResNet-101 1.3x
SAM Foret et al, 2021 An optimization strategy that seeks flatter minima. ResNet-101 1.4x
SeqLengthWarmup Li et al, 2021 Progressively increase sequence length. GPT-2 1.2x
Stochastic Depth Huang et al, 2016 Replaces a specified layer with a stochastic version that randomly drops the layer or samples during training ResNet-101 1.1x

* = time-to-train to the same quality as the baseline.

🛠 Building Speedup Recipes

Given two methods that speed up training by 1.5x each, do they combine to provide a 2.25x (1.5x * 1.5x) speedup? Not necessarily. They may optimize the same part of the training process and lead to diminishing returns, or they may even interact in ways that prove detrimental. Determining which methods to compose together isn't as simple as assembling a set of methods that perform best individually.

We have come up with compositions of methods that work especially well together through rigorous exploration of the design space of recipes and research on the science behind composition. The MosaicML Explorer contains all of the data we have collected so far on composition, and it highlights the compositions of methods that are pareto-optimal - that provide the best possible tradeoffs between training time or cost and the quality of the trained model. Whether you want to reach the same quality faster or get better quality within your current budget, Explorer can help you decide which speedup methods to use. We update this data regularly as we add new methods and develop better recipes.

As an example, here are two performant recipes, one for ResNet-101 on ImageNet, and the other for GPT-2 on OpenWebText, on 8xA100s:

ResNet-101

Name Functional tl;dr Benchmark Speed Up
Blur Pool cf.apply_blurpool Applies an anti-aliasing filter before every downsampling operation. ResNet-101 1.2x
Channels Last cf.apply_
channels_last
Uses channels last memory format (NHWC). ResNet-101 1.5x
Label Smoothing cf.smooth_labels Smooths the labels with a uniform prior. ResNet-101 1.5x
MixUp CF.mixup_batch Blends pairs of examples and labels. ResNet-101 1.5x
Progressive Resizing cf.resize_batch Increases the input image size during training. ResNet-101 1.3x
SAM N/A SAM optimizer measures sharpness of optimization space. ResNet-101 1.5x
Composition N/A Cheapest: $49 @ 78.1% Acc ResNet-101 3.5x

GPT-2

Name Functional tl;dr Benchmark Speed Up
Alibi cf.apply_alibi Replace attention with AliBi. GPT-2 1.6x
Seq Length Warmup cf.set_batch_
sequence_length
Progressively increase sequence length. GPT-2 1.5x
Composition N/A Cheapest: $145 @ 24.11 PPL GPT-2 1.7x

⚙️ What benchmarks does Composer support?

Composer uses a benchmark as a term to denote a particular model trained on a particular dataset in a standardized, reproducible way. A benchmark is a specific model trained for a task, where a task = dataset + loss function + metric.

We support computer vision and natural language processing use cases, such as (but not limited to) the following. New benchmarks will be added regularly, as will compatibility with existing libraries.

Model Dataset Loss Task Evaluation Metrics
Computer Vision
ResNet Family CIFAR-10 Cross Entropy Image Classification Classification Accuracy
ResNet Family ImageNet Cross Entropy Image Classification Classification Accuracy
EfficientNet Family ImageNet Cross Entropy Image Classification Classification Accuracy
UNet BraTS Dice Loss Image Segmentation Dice Coefficient
DeepLab v3 ADE20K Cross Entropy Image Segmentation mIoU
Natural Language Processing
BERT Family {Wikipedia & BooksCorpus, C4} Cross Entropy Masked Language Modeling GLUE
GPT Family {OpenWebText, C4} Cross Entropy Language Modeling
Perplexity

🤔 Why should I use Composer?

The compute required to train a state-of-the-art machine learning model is doubling every 6 months, putting these capabilities further and further out of reach for the broader community with each passing day. Composer addresses this challenge by focusing on training efficiency: it contains cutting-edge speedup methods that modify the training algorithm to reduce the time and cost necessary to train deep learning models. When you use Composer, you can rest assured that you are training efficiently. We have combed the literature, done the science, and built industrial-grade implementations to ensure this is the case.

Even after these speedup methods are implemented, assembling them together into recipes is nontrivial. We designed Composer with the right abstractions to composing (and creating new) speedup methods.

Specifically, Composer's efficiency methods use two-way callbacks from (Howard et al, 2020) to modify the entire training state at particular events in the training loop to effect speed-ups. We handle collisions between methods, the proper order of execution for algorithms, and more.

Through this, our methods can modify:

  • data inputs for batches (data augmentations, sequence length warmup, skipping examples, etc.)
  • neural network architecture (pruning, model surgery, etc.)
  • loss function (label smoothing, MixUp, CutMix, etc.)
  • optimizer (Sharpness Aware Minimization)
  • training dynamics (layer freezing, selective backprop, etc.)

Easily add your own methods or callbacks to instrument any part of the training loop.

🧐 Why shouldn’t I use Composer?

  • Composer is mostly optimized for computer vision (CV) and natural language processing (NLP) use cases, including custom models and custom datasets. We strongly encourage exploration on integrating our algorithms into new domains, such as reinforcement learning. Feel free to join our Slack and discuss!
  • Composer currently supports NVIDIA GPUs. We are adding support for additional hardware platforms, and you should expect more soon.
  • Composer is an active and ongoing project. Since Composer is still in alpha, our API may not be stable. We recommend pegging your work to a Composer version, and we will respond quickly to issues posted to this repository.

📚 Learn More

Here's some resources actively maintained by the Composer community to help you get started:

Resource Details
Getting started with our Trainer An interactive Colab Notebook aimed at teaching users about our Trainer
Getting started with our Functional API An interactive Colab Notebook aimed at teaching users about our Functional API
Building Speedup Methods An interactive Colab Notebook aimed at teaching users about building speedup methods on top of Composer
Training BERTs with Composer An interactive Colab Notebook aimed at helping users learn how to train BERT models with Composer!
We're Hiring! Join us! 🤩

If you have any questions, please feel free to reach out to us on Twitter, email, or our Community Slack!

💫 Contributors

Composer is part of the broader Machine Learning community, and we welcome any contributions, pull requests, or issues!

To start contributing, see our Contributing page.

✍️ Citation

@misc{mosaicml2022composer,
    author = {The Mosaic ML Team},
    title = {composer},
    year = {2021},
    howpublished = {\url{https://github.com/mosaicml/composer/}},
}

About

library of algorithms to speed up neural network training

http://docs.mosaicml.com

License:Other


Languages

Language:Python 59.8%Language:Jupyter Notebook 39.6%Language:Shell 0.2%Language:Dockerfile 0.2%Language:Makefile 0.1%