Skip to main content

TRUMPY: Tracing and Reverse Understanding Memory in Pytorch

Analyze how much memory is used for calculating the backward propagation (excluding parameters and gradients). This function works by tracing the computation graph used for backward propagation.

The fascinating thing is that it works in both GPU and CPU. Therefore, you can first run the code in CPU to estimate the memory usage, which will then be a decent estimation of memory consumption in GPU!

Install

pip install trumpy or clone this repository and build it yourself.

Usage

The core function is just one-line: from trumpy import memory_for_backward; saved_memory = memory_for_backward(net, loss). You just pass the network and the calculated loss to this function, and it will give you how much memory (in bytes) is needed for backward propagation.

Example usage is in tests/test.py.

import torch
from torchvision.models.resnet import resnet50

# define device and models
device = torch.device('cpu')
net = resnet50().to(device)
input = torch.rand(16, 3, 224, 224).to(device)

# if gpu is available, record current memory
if device != torch.device('cpu'):
    current_memory = torch.cuda.memory_allocated()

# define loss function
loss = net(input).sum() # loss is the starting point of the backward computation graph
from trumpy import memory_for_backward
# calculate the memory used for backward
saved_memory = memory_for_backward(net, loss)
print(f'estimated memory usage for backward related tensors in {device}: {saved_memory / 1024 ** 3} GB')

# check how much memory pytorch holds
if device != torch.device('cpu'):
    import gc
    gc.collect()
    saved_memory = torch.cuda.memory_allocated() - current_memory
    print(f'ground-truth memory usage for backward related tensors in gpu: {saved_memory / 1024 ** 3} GB')

You should see something like:

estimated memory usage for backward related tensors in cpu: 1.2881765365600586 GB

After turning on the GPU (setting device = torch.device('cuda:0')):

estimated memory usage for backward related tensors in cuda:0: 1.2881765365600586 GB
ground-truth memory usage for backward related tensors in gpu: 1.28438138961792 GB

You see that the estimated memory usage in CPU is very similar with the actual memory usage in GPU!

Metadata

Release files for trumpy 0.1.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 trumpy 0.1.2
File Size Uploaded Provenance
trumpy-0.1.2.tar.gz 3.8 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 8.4 kB

Release files / trumpy-0.1.2.tar.gz

Download URL trumpy-0.1.2.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
1b34afbe29ce2ec2b2b41849f88fba239055d953487ea93c2903e408e9ab050d
BLAKE2b-256 checksum
How to use checksums
d34af30527de5c05b729de59c8413a33f002a20a46bef39c1c9d58cbd08e5a61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.13

Release files / trumpy-0.1.2-py3-none-any.whl

Download URL trumpy-0.1.2-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1b351fd43e19916c3b948bfdf27257f06010f8fa4e732200883609d2d966f9cc
BLAKE2b-256 checksum
How to use checksums
59d538bf17377b901be21f2eda7cdc93357948cc7b9e919208ce36d2616f0041
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.13

Security for trumpy 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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