Coder Social home page Coder Social logo

mhoibo / gradientaccumulator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from andreped/gradientaccumulator

0.0 0.0 0.0 5.29 MB

:dart: Accumulated Gradients for TensorFlow 2

Home Page: https://gradientaccumulator.readthedocs.io/

License: MIT License

Python 75.16% Makefile 0.91% Jupyter Notebook 23.93%

gradientaccumulator's Introduction

gradient-accumulator

GradientAccumulator

Seemless gradient accumulation for TensorFlow 2

Pip Downloads PyPI version License DOI

GradientAccumulator was developed by SINTEF Health due to the lack of an easy-to-use method for gradient accumulation in TensorFlow 2.

The package is available on PyPI and is compatible with and have been tested against TensorFlow 2.2-2.12 and Python 3.6-3.12, and works cross-platform (Ubuntu, Windows, macOS).

Build Type Status
Code coverage codecov
Documentations Documentation Status
Unit tests CI

Stable release from PyPI:

pip install gradient-accumulator

Or from source:

pip install git+https://github.com/andreped/GradientAccumulator

A simple example to add gradient accumulation to an existing model is by:

from gradient_accumulator import GradientAccumulateModel
from tensorflow.keras.models import Model

model = Model(...)
model = GradientAccumulateModel(accum_steps=4, inputs=model.input, outputs=model.output)

Then simply use the model as you normally would!

In practice, using gradient accumulation with a custom pipeline might require some extra overhead and tricks to get working.

For more information, see documentations which are hosted at gradientaccumulator.readthedocs.io

Gradient accumulation (GA) enables reduced GPU memory consumption through dividing a batch into smaller reduced batches, and performing gradient computation either in a distributing setting across multiple GPUs or sequentially on the same GPU. When the full batch is processed, the gradients are the accumulated to produce the full batch gradient.

In TensorFlow 2, there did not exist a plug-and-play method to use gradient accumulation with any custom pipeline. Hence, we have implemented two generic TF2-compatible approaches:

Method Usage
GradientAccumulateModel model = GradientAccumulateModel(accum_steps=4, inputs=model.input, outputs=model.output)
GradientAccumulateOptimizer opt = GradientAccumulateOptimizer(accum_steps=4, optimizer=tf.keras.optimizers.SGD(1e-2))

Both approaches control how frequently the weigths are updated, but in their own way. Approach (1) is for single-GPU only, whereas (2) supports both single-GPU and distributed training (multi-GPU). However, note that (2) is not yet working as intended. Hence, use (1) for most applications.

Our implementations enable theoretically infinitely large batch size, with identical memory consumption as for a regular mini batch. If a single GPU is used, this comes at the cost of increased training runtime. Multiple GPUs could be used to improve runtime performance.

Technique Usage
Batch Normalization layer = AccumBatchNormalization(accum_steps=4)
Adaptive Gradient Clipping model = GradientAccumulateModel(accum_steps=4, agc=True, inputs=model.input, outputs=model.output)
Mixed precision model = GradientAccumulateModel(accum_steps=4, mixed_precision=True, inputs=model.input, outputs=model.output)
  • As batch normalization (BN) is not natively compatible with GA, we have implemented a custom BN layer which can be used as a drop-in replacement.
  • Support for adaptive gradient clipping has been added as an alternative to BN.
  • Mixed precision can also be utilized on both GPUs and TPUs.

For more information on usage, supported techniques, and examples, refer to the documentations.

The gradient accumulator model wrapper is based on the implementation presented in this thread on stack overflow. The adaptive gradient clipping method is based on the implementation by @sayakpaul. The optimizer wrapper is derived from the implementation by @fsx950223 and @stefan-falk.

The documentations hosted here was made possible by the incredible ReadTheDocs team which offer free documentation hosting!

If you used this package or found the project relevant in your research, please, include the following citation:

@software{andre_pedersen_2023_7890319,
  author       = {André Pedersen and Javier Pérez de Frutos and David Bouget},
  title        = {andreped/GradientAccumulator: v0.4.2},
  month        = may,
  year         = 2023,
  publisher    = {Zenodo},
  version      = {v0.4.2},
  doi          = {10.5281/zenodo.7890319},
  url          = {https://doi.org/10.5281/zenodo.7890319}
}

gradientaccumulator's People

Contributors

andreped avatar dbouget avatar jpdefrutos avatar rschmirler avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.