Skip to main content

Chunk and Checkpoint Memory Optimisation

CI

Reduce peak memory when training models in PyTorch which require batched operations internally, such as Swin Transformers.

TLDR:

from chunkcheck import chunk_and_checkpoint

...

# There is a really large batch size along dimension 0. `chunk_and_checkpoint`
# substantially reduces peak memory usage. Adjust `chunk_size` to achieve your
# preferred time vs memory tradeoff.
y = chunk_and_checkpoint(f, x1, x2, ..., chunk_size=4, batch_dim=0)

...

Installation

pip install chunkcheck

Usage

chunkcheck exports one function: chunk_and_checkpoint. It can be fruitfully used to reduce the peak memory requirement of a programme written using PyTorch when the following hold:

  • You have one or more input torch.Tensors (x1, x2, ...) whose first dimension is a "batch" dimension of equal size.
  • You wish to compute f(x1, x2, ...), where f applies the same operation to each "batch" in (x1, x2, ...).
  • The memory required during intermediate computations in f is large compared to the memory required to store (x1, x2, ...) and the output of f(x1, x2, ...). A canonical example of this kind of function is an MLP with large hidden dimension(s).

Instead of calling f(x1, x2, ...), call chunk_and_checkpoint(f, x1, x2, ..., chunk_size=chunk_size), for some int chunk_size. Doing this should substantially reduce peak memory, and increase the computation time by only a small amount for a well-chosen chunk_size. chunk_and_checkpoint will reduce peak memory further than torch.utils.checkpoint.checkpoint ("activation checkpointing"), the exact amount depends on chunk_size.

See the docstring for chunk_and_checkpoint for more information. For a more detailed explanation of why this works, and some usage case studies, see our note on arXiv (TODO: write this and link to it).

Development

Clone the repo and cd into the repository. Then create a virtual environment, enter it, and install all dependencies:

uv venv
source .venv/bin/activate
uv sync

Running the tests:

pytest -v

Metadata

Release files for chunkcheck 0.1.1

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

Source distribution (sdist)

Source distribution for chunkcheck 0.1.1
File Size Uploaded
chunkcheck-0.1.1.tar.gz 3.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chunkcheck 0.1.1
File Interpreter ABI Platform
chunkcheck-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 6.4 kB

Release files / chunkcheck-0.1.1.tar.gz

Download URL chunkcheck-0.1.1.tar.gz
Size 3.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e72289972e10482df84f4e9c3191fbedca27a566da9e934abd660109f0985851
BLAKE2b-256 checksum
How to use checksums
095de4a4d060b2374a3804262f19c8b86d44b012064258fdf209b81b47f56cf6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / chunkcheck-0.1.1-py3-none-any.whl

Download URL chunkcheck-0.1.1-py3-none-any.whl
Size 3.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b32dc9614bdfb5298bc98d5f8814e0cc8790417c5d99bb07499e7a4ff718a7ef
BLAKE2b-256 checksum
How to use checksums
1db763fbb29a575f4b80d60e325cd9f3cbef6acde3340c1123a8a50b3d77b59c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

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