A lightweight library designed to accelerate the process of training PyTorch models by providing a minimal

Overview

pytorch-accelerated

pytorch-accelerated is a lightweight library designed to accelerate the process of training PyTorch models by providing a minimal, but extensible training loop - encapsulated in a single Trainer object - which is flexible enough to handle the majority of use cases, and capable of utilizing different hardware options with no code changes required.

pytorch-accelerated offers a streamlined feature set, and places a huge emphasis on simplicity and transparency, to enable users to understand exactly what is going on under the hood, but without having to write and maintain the boilerplate themselves!

The key features are:

  • A simple and contained, but easily customisable, training loop, which should work out of the box in straightforward cases; behaviour can be customised using inheritance and/or callbacks.
  • Handles device placement, mixed-precision, DeepSpeed integration, multi-GPU and distributed training with no code changes.
  • Uses pure PyTorch components, with no additional modifications or wrappers, and easily interoperates with other popular libraries such as timm, transformers and torchmetrics.
  • A small, streamlined API ensures that there is a minimal learning curve for existing PyTorch users.

Significant effort has been taken to ensure that every part of the library - both internal and external components - is as clear and simple as possible, making it easy to customise, debug and understand exactly what is going on behind the scenes at each step; most of the behaviour of the trainer is contained in a single class! In the spirit of Python, nothing is hidden and everything is accessible.

pytorch-accelerated is proudly and transparently built on top of Hugging Face Accelerate, which is responsible for the movement of data between devices and launching of training configurations. When customizing the trainer, or launching training, users are encouraged to consult the Accelerate documentation to understand all available options; Accelerate provides convenient functions for operations such gathering tensors and gradient clipping, usage of which can be seen in the pytorch-accelerated examples folder!

To learn more about the motivations behind this library, along with a detailed getting started guide, check out this blog post.

Installation

pytorch-accelerated can be installed from pip using the following command:

pip install pytorch-accelerated

To make the package as slim as possible, the packages required to run the examples are not included by default. To include these packages, you can use the following command:

pip install pytorch-accelerated[examples]

Quickstart

To get started, simply import and use the pytorch-accelerated Trainer ,as demonstrated in the following snippet, and then launch training using the accelerate CLI described below.

# examples/train_mnist.py
import os

from torch import nn, optim
from torch.utils.data import random_split
from torchvision import transforms
from torchvision.datasets import MNIST

from pytorch_accelerated import Trainer

class MNISTModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.main = nn.Sequential(
            nn.Linear(in_features=784, out_features=128),
            nn.ReLU(),
            nn.Linear(in_features=128, out_features=64),
            nn.ReLU(),
            nn.Linear(in_features=64, out_features=10),
        )

    def forward(self, input):
        return self.main(input.view(input.shape[0], -1))

def main():
    dataset = MNIST(os.getcwd(), download=True, transform=transforms.ToTensor())
    train_dataset, validation_dataset, test_dataset = random_split(dataset, [50000, 5000, 5000])
    model = MNISTModel()
    optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
    loss_func = nn.CrossEntropyLoss()

    trainer = Trainer(
            model,
            loss_func=loss_func,
            optimizer=optimizer,
    )

    trainer.train(
        train_dataset=train_dataset,
        eval_dataset=validation_dataset,
        num_epochs=8,
        per_device_batch_size=32,
    )

    trainer.evaluate(
        dataset=test_dataset,
        per_device_batch_size=64,
    )
    
if __name__ == "__main__":
    main()

To launch training using the accelerate CLI , on your machine(s), run:

accelerate config --config_file accelerate_config.yaml

and answer the questions asked. This will generate a config file that will be used to properly set the default options when doing

accelerate launch --config_file accelerate_config.yaml train.py [--training-args]

Note: Using the accelerate CLI is completely optional, training can also be launched in the usual way using:

python train.py / python -m torch.distributed ...

depending on your infrastructure configuration, for users who would like to maintain a more fine-grained control over the launch command.

More complex training examples can be seen in the examples folder here.

Alternatively, if you would rather undertsand the core concepts first, this can be found in the documentation.

Usage

Who is pytorch-accelerated aimed at?

  • Users that are familiar with PyTorch but would like to avoid having to write the common training loop boilerplate to focus on the interesting parts of the training loop.
  • Users who like, and are comfortable with, selecting and creating their own models, loss functions, optimizers and datasets.
  • Users who value a simple and streamlined feature set, where the behaviour is easy to debug, understand, and reason about!

When shouldn't I use pytorch-accelerated?

  • If you are looking for an end-to-end solution, encompassing everything from loading data to inference, which helps you to select a model, optimizer or loss function, you would probably be better suited to fastai. pytorch-accelerated focuses only on the training process, with all other concerns being left to the responsibility of the user.
  • If you would like to write the entire training loop yourself, just without all of the device management headaches, you would probably be best suited to using Accelerate directly! Whilst it is possible to customize every part of the Trainer, the training loop is fundamentally broken up into a number of different methods that you would have to override. But, before you go, is writing those for loops really important enough to warrant starting from scratch again 😉 .
  • If you are working on a custom, highly complex, use case which does not fit the patterns of usual training loops and want to squeeze out every last bit of performance on your chosen hardware, you are probably best off sticking with vanilla PyTorch; any high-level API becomes an overhead in highly specialized cases!

Acknowledgements

Many aspects behind the design and features of pytorch-accelerated were greatly inspired by a number of excellent libraries and frameworks such as fastai, timm, PyTorch-lightning and Hugging Face Accelerate. Each of these tools have made an enormous impact on both this library and the machine learning community, and their influence can not be stated enough!

pytorch-accelerated has taken only inspiration from these tools, and all of the functionality contained has been implemented from scratch in a way that benefits this library. The only exceptions to this are some of the scripts in the examples folder in which existing resources were taken and modified in order to showcase the features of pytorch-accelerated; these cases are clearly marked, with acknowledgement being given to the original authors.

Comments
  • Do we need to set mixed-precision explicitly or is it handled if tensor cores available?

    Do we need to set mixed-precision explicitly or is it handled if tensor cores available?

    I following your awesome guide on timm: https://towardsdatascience.com/getting-started-with-pytorch-image-models-timm-a-practitioners-guide-4e77b4bf9055.

    I am running training on an A100-based VM which should support mixed-precision training. Does Trainer from PyTorch Accelerated take care of that automatically?

    opened by sayakpaul 6
  • ERROR: No matching distribution found for pytorch-accelerated

    ERROR: No matching distribution found for pytorch-accelerated

    I'm just trying to install the package using the pip command and I get the following errors:

    ERROR: Could not find a version that satisfies the requirement pytorch-accelerated (from versions: none)
    ERROR: No matching distribution found for pytorch-accelerated
    

    Am I missing something?

    P.S. I've already installed the requirements including accelerate and tqdm

    opened by phosseini 4
  • Can pytorch-accelerated be used with pytorch-lightning callbacks and loggers?

    Can pytorch-accelerated be used with pytorch-lightning callbacks and loggers?

    I'm interested in this package for its support of methods like EMA that don't seem to have made it into Lightning yet, but don't want to lost my current experiment tracking setup etc.

    opened by GeorgePearse 3
  • Do you know about Lightning Lite ?

    Do you know about Lightning Lite ?

    Hey @Chris-hughes10,

    Awesome work there !

    Did you know about Lightning Lite in PyTorch Lightning ? Here are the docs : https://pytorch-lightning.readthedocs.io/en/latest/starter/lightning_lite.html

    lightning_lite

    opened by tchaton 1
  • Refactor loss tracking

    Refactor loss tracking

    • Create private methods in trainer to handle loss tracking, removing duplication
    • Move loss gathering to the end of each epoch, as opposed to after each batch
    • Add tests for loss tracker
    opened by Chris-hughes10 0
  • Add limit batches context manager

    Add limit batches context manager

    Add a context manager which can be used to limit the number of training and evaluation batches used without having to manually add the callback. This is done by setting an environment variable.

    opened by Chris-hughes10 0
  • Refactor batch unpacking

    Refactor batch unpacking

    • Refactor batch unpacking to explicitly assign the first two items as xb and yb. This will enable more flexibility in what is returned by a dataloader
    opened by Chris-hughes10 0
  • Enables distributed evaluation on uneven inputs

    Enables distributed evaluation on uneven inputs

    Adds functionality to enable distributed evaluation on uneven samples. Previously, this was handled by adding extra samples to the dataset, this behaviour is now disabled by default.

    opened by Chris-hughes10 0
Releases(v0.1.40)
  • v0.1.40(Nov 17, 2022)

    What's Changed

    • Add option to execute callbacks during ModelEma evaluation loop by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/41

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.39...v0.1.40

    Source code(tar.gz)
    Source code(zip)
  • v0.1.39(Oct 14, 2022)

    What's Changed

    • Improve gathering to automatically pad tensors across processes
    • Add get_model method in Trainer by @bepuca in https://github.com/Chris-hughes10/pytorch-accelerated/pull/39

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.38...v0.1.39

    Source code(tar.gz)
    Source code(zip)
  • v0.1.38(Sep 7, 2022)

    What's Changed

    • update worker init function by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/37
    • Separate out decay function in model EMA for easier override by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/38

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.37...v0.1.38

    Source code(tar.gz)
    Source code(zip)
  • v0.1.37(Aug 24, 2022)

  • v0.1.36(Aug 22, 2022)

    What's Changed

    • Improve logging for SaveBestModelCallback by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/35
    • Add sync batchnorm callback by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/34
    • Add Ema model callback by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/36

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.35...v0.1.36

    Source code(tar.gz)
    Source code(zip)
  • v0.1.35(Jul 9, 2022)

    What's Changed

    • Update Custom sampler handling by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/33

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.34...v0.1.35

    Source code(tar.gz)
    Source code(zip)
  • v0.1.34(Jun 29, 2022)

  • v0.1.33(Jun 29, 2022)

  • v0.1.32(Jun 29, 2022)

    What's Changed

    • Add limit batches context manager by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/32

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.31...v0.1.32

    Source code(tar.gz)
    Source code(zip)
  • v0.1.31(Jun 22, 2022)

  • v0.1.30(Jun 22, 2022)

    What's Changed

    • Add Limit batches callback (beta version) by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/31

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.29...v0.1.30

    Source code(tar.gz)
    Source code(zip)
  • v0.1.29(Jun 17, 2022)

    What's Changed

    • Improve grad accumulation by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/30 Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.28...v0.1.29
    Source code(tar.gz)
    Source code(zip)
  • v0.1.28(May 31, 2022)

    What's Changed

    • Add local process first decorator by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/27
    • Update fp16 arg to mixed precision by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/28
    • Update accelerate version to 0.8.0 by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/29

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.27...v0.1.28

    Source code(tar.gz)
    Source code(zip)
  • v0.1.27(May 24, 2022)

  • v0.1.26(Apr 28, 2022)

    What's Changed

    • Add process decorators for distributed training by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/25
    • Update accelerate version by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/26

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.25...v0.1.26

    Source code(tar.gz)
    Source code(zip)
  • v0.1.25(Apr 25, 2022)

    What's Changed

    • Add handling for multi boolean tensors by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/23
    • Refactor batch unpacking by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/24

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.24...v0.1.25

    Source code(tar.gz)
    Source code(zip)
  • v0.1.24(Apr 20, 2022)

  • v0.1.23(Apr 17, 2022)

    What's Changed

    • Add schedulers by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/22
    • Add a better way of getting default callbacks
    • Update project license to Apache-2.0

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.22...v0.1.23

    Source code(tar.gz)
    Source code(zip)
  • v0.1.22(Feb 23, 2022)

    What's Changed

    • Add operations to placeholders by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/17
    • Add clarification for LR schedulers in the docs by @bepuca in https://github.com/Chris-hughes10/pytorch-accelerated/pull/16
    • Add specialised trainer to work with timm schedulers

    New Contributors

    • @bepuca made their first contribution in https://github.com/Chris-hughes10/pytorch-accelerated/pull/16

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.21...v0.1.22

    Source code(tar.gz)
    Source code(zip)
  • v0.1.21(Jan 27, 2022)

  • v0.1.20(Jan 19, 2022)

    What's Changed

    • Added an example to the docs for a callback that saves predictions during evaluation by @alexhock10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/13
    • Create run config for standalone evaluation runs by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/14

    New Contributors

    • @alexhock10 made their first contribution in https://github.com/Chris-hughes10/pytorch-accelerated/pull/13

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.9...v0.1.20

    Source code(tar.gz)
    Source code(zip)
  • v0.1.9(Dec 31, 2021)

    What's Changed

    • Freezing exploration by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/11
    • Add gather method by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/12

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.8...v0.1.9

    Source code(tar.gz)
    Source code(zip)
  • v0.1.8(Dec 11, 2021)

    What's Changed

    • Changes to facilitate AzureML example by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/10

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.7...v0.1.8

    Source code(tar.gz)
    Source code(zip)
  • v0.1.7(Nov 30, 2021)

    What's Changed

    • Update early stopping by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/9
    • Remove torch dependency (covered by accelerate)

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.6...v0.1.7

    Source code(tar.gz)
    Source code(zip)
  • v0.1.6(Nov 26, 2021)

  • v0.1.5(Nov 24, 2021)

    What's Changed

    • Add intersphinx to docs by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/7
    • Update device handling by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/8

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.4...v0.1.5

    Source code(tar.gz)
    Source code(zip)
  • v0.1.4(Nov 17, 2021)

    Update the package documentation

    What's Changed

    • Get docs to build properly by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/5
    • Get docs to build properly by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/6

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.3...v0.1.4

    Source code(tar.gz)
    Source code(zip)
  • v0.1.3(Nov 13, 2021)

    Initial release

    What's Changed

    • Add gradient clipping to trainer by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/2
    • Prepare pypi workflow by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/3

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/commits/v0.1.0

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.0...v0.1.2

    What's Changed

    • Add sphinx docs by @Chris-hughes10 in https://github.com/Chris-hughes10/pytorch-accelerated/pull/4

    Full Changelog: https://github.com/Chris-hughes10/pytorch-accelerated/compare/v0.1.2...v0.1.3

    Source code(tar.gz)
    Source code(zip)
Owner
Chris Hughes
Chris Hughes
PyTorch common framework to accelerate network implementation, training and validation

pytorch-framework PyTorch common framework to accelerate network implementation, training and validation. This framework is inspired by works from MML

Dongliang Cao 3 Dec 19, 2022
NVIDIA Merlin is an open source library providing end-to-end GPU-accelerated recommender systems, from feature engineering and preprocessing to training deep learning models and running inference in production.

NVIDIA Merlin NVIDIA Merlin is an open source library designed to accelerate recommender systems on NVIDIA’s GPUs. It enables data scientists, machine

null 419 Jan 3, 2023
Accelerate Neural Net Training by Progressively Freezing Layers

FreezeOut A simple technique to accelerate neural net training by progressively freezing layers. This repository contains code for the extended abstra

Andy Brock 203 Jun 19, 2022
🏎️ Accelerate training and inference of 🤗 Transformers with easy to use hardware optimization tools

Hugging Face Optimum ?? Optimum is an extension of ?? Transformers, providing a set of performance optimization tools enabling maximum efficiency to t

Hugging Face 842 Dec 30, 2022
banditml is a lightweight contextual bandit & reinforcement learning library designed to be used in production Python services.

banditml is a lightweight contextual bandit & reinforcement learning library designed to be used in production Python services. This library is developed by Bandit ML and ex-authors of Facebook's applied reinforcement learning platform, Reagent.

Bandit ML 51 Dec 22, 2022
Aiming at the common training datsets split, spectrum preprocessing, wavelength select and calibration models algorithm involved in the spectral analysis process

Aiming at the common training datsets split, spectrum preprocessing, wavelength select and calibration models algorithm involved in the spectral analysis process, a complete algorithm library is established, which is named opensa (openspectrum analysis).

Fu Pengyou 50 Jan 7, 2023
StudioGAN is a Pytorch library providing implementations of representative Generative Adversarial Networks (GANs) for conditional/unconditional image generation.

StudioGAN is a Pytorch library providing implementations of representative Generative Adversarial Networks (GANs) for conditional/unconditional image generation.

null 3k Jan 8, 2023
(CVPR2021) ClassSR: A General Framework to Accelerate Super-Resolution Networks by Data Characteristic

ClassSR (CVPR2021) ClassSR: A General Framework to Accelerate Super-Resolution Networks by Data Characteristic Paper Authors: Xiangtao Kong, Hengyuan

Xiangtao Kong 308 Jan 5, 2023
Pacman-AI - AI project designed by UC Berkeley. Designed reflex and minimax agents for the game Pacman.

Pacman AI Jussi Doherty CAP 4601 - Introduction to Artificial Intelligence - Fall 2020 Python version 3.0+ Source of this project This repo contains a

Jussi Doherty 1 Jan 3, 2022
Repository providing a wide range of self-supervised pretrained models for computer vision tasks.

Hierarchical Pretraining: Research Repository This is a research repository for reproducing the results from the project "Self-supervised pretraining

Colorado Reed 53 Nov 9, 2022
Lightweight mmm - Lightweight (Bayesian) Media Mix Model

Lightweight (Bayesian) Media Mix Model This is not an official Google product. L

Google 342 Jan 3, 2023
ManimML is a project focused on providing animations and visualizations of common machine learning concepts with the Manim Community Library.

ManimML ManimML is a project focused on providing animations and visualizations of common machine learning concepts with the Manim Community Library.

null 259 Jan 4, 2023
LWCC: A LightWeight Crowd Counting library for Python that includes several pretrained state-of-the-art models.

LWCC: A LightWeight Crowd Counting library for Python LWCC is a lightweight crowd counting framework for Python. It wraps four state-of-the-art models

Matija Teršek 39 Dec 28, 2022
A minimal implementation of face-detection models using flask, gunicorn, nginx, docker, and docker-compose

Face-Detection-flask-gunicorn-nginx-docker This is a simple implementation of dockerized face-detection restful-API implemented with flask, Nginx, and

Pooya-Mohammadi 30 Dec 17, 2022
The lightweight PyTorch wrapper for high-performance AI research. Scale your models, not the boilerplate.

The lightweight PyTorch wrapper for high-performance AI research. Scale your models, not the boilerplate. Website • Key Features • How To Use • Docs •

Pytorch Lightning 21.1k Jan 1, 2023
The lightweight PyTorch wrapper for high-performance AI research. Scale your models, not the boilerplate.

The lightweight PyTorch wrapper for high-performance AI research. Scale your models, not the boilerplate. Website • Key Features • How To Use • Docs •

Pytorch Lightning 11.9k Feb 13, 2021
The lightweight PyTorch wrapper for high-performance AI research. Scale your models, not the boilerplate.

The lightweight PyTorch wrapper for high-performance AI research. Scale your models, not the boilerplate. Website • Key Features • How To Use • Docs •

Pytorch Lightning 21.1k Jan 8, 2023
Reinforcement learning library(framework) designed for PyTorch, implements DQN, DDPG, A2C, PPO, SAC, MADDPG, A3C, APEX, IMPALA ...

Automatic, Readable, Reusable, Extendable Machin is a reinforcement library designed for pytorch. Build status Platform Status Linux Windows Supported

Iffi 348 Dec 24, 2022
Pytorch library for end-to-end transformer models training and serving

Pytorch library for end-to-end transformer models training and serving

Mikhail Grankin 768 Jan 1, 2023