Skip to main content

TRUMPY: Tracing and Reverse Understanding Memory in Pytorch

Project description

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!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trumpy-0.1.2.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

trumpy-0.1.2-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file trumpy-0.1.2.tar.gz.

File metadata

  • Download URL: trumpy-0.1.2.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.13

File hashes

Hashes for trumpy-0.1.2.tar.gz
Algorithm Hash digest
SHA256 1b34afbe29ce2ec2b2b41849f88fba239055d953487ea93c2903e408e9ab050d
MD5 820c4642f871b658c27459016291709e
BLAKE2b-256 d34af30527de5c05b729de59c8413a33f002a20a46bef39c1c9d58cbd08e5a61

See more details on using hashes here.

File details

Details for the file trumpy-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: trumpy-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.13

File hashes

Hashes for trumpy-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1b351fd43e19916c3b948bfdf27257f06010f8fa4e732200883609d2d966f9cc
MD5 b4616f8516abc8bb7056c52432688ea1
BLAKE2b-256 59d538bf17377b901be21f2eda7cdc93357948cc7b9e919208ce36d2616f0041

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page