Skip to main content
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.11, and works cross-platform (Ubuntu, Windows, macOS).

Continuous integration

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

Install

Stable release from PyPI:

pip install gradient-accumulator

Or from source:

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

Getting started

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

What?

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 then accumulated to produce the full batch gradient.

Note that how we implemented gradient accumulation is slightly different from this illustration, as our design does not require having the entire batch in CPU memory. More information on what goes under the hood can be seen in the documentations.

Why?

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) overrides the train_step method of a given Model, whereas approach (2) wraps the optimizer. (1) is only compatible with single-GPU usage, whereas (2) also supports distributed training (multi-GPU).

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.
  • Multi-GPU distributed training using generic optimizer wrapper.

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

Applications

  • Helland et al., Segmentation of glioblastomas in early post-operative multi-modal MRI with deep neural networks, arXiv, 2023, https://arxiv.org/abs/2304.08881
  • Bouget et al., Raidionics: an open software for pre- and postoperative central nervous system tumor segmentation and standardized reporting, arXiv, 2023, https://arxiv.org/abs/2305.14351
  • Pedersen et al., H2G-Net: A multi-resolution refinement approach for segmentation of breast cancer region in gigapixel histopathological images, Frontiers in Medicine, 2022, https://doi.org/10.3389/fmed.2022.971873

Acknowledgements

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 Read The Docs team which offer free documentation hosting!

How to cite?

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

@software{andre_pedersen_2023_7905351,
  author       = {André Pedersen and Tor-Arne Schmidt Nordmo and Javier Pérez de Frutos and David Bouget},
  title        = {andreped/GradientAccumulator: v0.5.0},
  month        = may,
  year         = 2023,
  publisher    = {Zenodo},
  version      = {v0.5.0},
  doi          = {10.5281/zenodo.7905351},
  url          = {https://doi.org/10.5281/zenodo.7905351}
}

Metadata

Release files for gradient-accumulator 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gradient-accumulator 0.5.2
File Size Uploaded
gradient-accumulator-0.5.2.tar.gz 15.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gradient-accumulator 0.5.2
File Interpreter ABI Platform
gradient_accumulator-0.5.2-py3-none-any.whl Python 3 none any Details

Total release size: 30.7 kB

Release files / gradient-accumulator-0.5.2.tar.gz

Download URL gradient-accumulator-0.5.2.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e12ae18a8a8235d449a60b28284979aa2686977e512ac60ad6e69565fcf84bae
BLAKE2b-256 checksum
How to use checksums
6aeec7a96c88ae21eef0a2a7f0a14ff5d312d98a6963cfd0bb3e560ca9422dfe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / gradient_accumulator-0.5.2-py3-none-any.whl

Download URL gradient_accumulator-0.5.2-py3-none-any.whl
Size 15.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6034449eb9253de51dd252b990986dfe54d57a3c5577bc2f8396956b8cd9fd5a
BLAKE2b-256 checksum
How to use checksums
d4df58521829805223ba1a3925fddb96fbe63be93b5b0ee00a1bfccc8f5bd391
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release history Release notifications | RSS feed

This release

0.5.2 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page