Skip to main content

Super-lightweight line-by-line memory profiler for numerical Python code. See where those allocations are coming from! Supports NumPy, PyTorch, and more to come.

Project description

MemoryLane 💾🛣️

by Peter Sharpe


A super-lightweight line-by-line memory profiler for numerical Python code. See where those pesky allocations are coming from!

  • Supports PyTorch CUDA memory measurement, and more to come.
  • Minimal dependencies (just Rich + your favorite numerical library)

Installation

pip install memorylane[torch]  # For PyTorch support

Usage

To use MemoryLane, just import it and decorate your function with @profile:

import torch
from memorylane import profile

@profile
def my_function():
    x = torch.randn(5120, 5120, device="cuda")
    x = x @ x
    x = x.relu()
    x = x.mean()
    return x

my_function()

This will print your line-by-line memory usage:

<style> .r1 {color: #800080; text-decoration-color: #800080; font-weight: bold} .r2 {font-weight: bold} .r3 {color: #008080; text-decoration-color: #008080; font-weight: bold} .r4 {color: #5f8787; text-decoration-color: #5f8787} .r5 {color: #5f8787; text-decoration-color: #5f8787; font-weight: bold} .r6 {color: #008000; text-decoration-color: #008000; font-weight: bold} .r7 {color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822} .r8 {color: #ff4689; text-decoration-color: #ff4689; background-color: #272822} .r9 {color: #ae81ff; text-decoration-color: #ae81ff; background-color: #272822} .r10 {color: #e6db74; text-decoration-color: #e6db74; background-color: #272822} .r11 {color: #7f7f7f; text-decoration-color: #7f7f7f} .r12 {color: #800000; text-decoration-color: #800000; font-weight: bold} .r13 {color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822} body { color: #000000; background-color: #ffffff; } /* Disable wrapping in
 and  blocks */
pre, code {
    white-space: pre;
    overflow-x: auto;
}
</style>


    
 ━━━━━━ MemoryLane: Line-by-Line Memory Profiler ━━━━━━
 Tracing 'my_function' (file: /home/psharpe/GitHub/memorylane/examples/make_report/make_reports.py:9):
 Mem:    100 MB | ΔMem:    100 MB | Peak:    100 MB | ΔPeak:    100 MB | make_reports.py:11   |     x = torch.randn(5120, 5120, device="cuda")
 Mem:    108 MB | ΔMem:      8 MB | Peak:    208 MB | ΔPeak:    108 MB | make_reports.py:12   |     x = x @ x
 Mem:    108 MB | ΔMem:      0 MB | Peak:    208 MB | ΔPeak:      0 MB | make_reports.py:13   |     x = x.relu()
 Mem:      8 MB | ΔMem:   -100 MB | Peak:    208 MB | ΔPeak:      0 MB | make_reports.py:14   |     x = x.mean()
 Mem:      8 MB | ΔMem:      0 MB | Peak:    208 MB | ΔPeak:      0 MB | make_reports.py:15   |     return x

Features

  • For complicated functions, filter the report to only show lines with non-negligible changes in memory usage: @profile(only_show_significant=True)
  • When used from terminal, the printouts like make_reports.py:11 become clickable links that will take you directly to the offending line in your code
  • Profiling of multiple functions, including nested ones (these will be shown with indentation, to allow you to see where the allocations are coming from)
  • Report generation in HTML and text formats
  • (Work in progress) Support for measuring memory usage of:
    • PyTorch CPU operations
    • NumPy operations
    • JAX operations
    • Python whole-process memory usage
    • ...and more!

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

memorylane-0.1.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

memorylane-0.1.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file memorylane-0.1.0.tar.gz.

File metadata

  • Download URL: memorylane-0.1.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.4

File hashes

Hashes for memorylane-0.1.0.tar.gz
Algorithm Hash digest
SHA256 800e463b9f6a3b8972324a72134667a865845774a789abdac8d8bc5e3bcb701f
MD5 1777d060de296030caa0de24feee35af
BLAKE2b-256 a4b4afdb49fbc321f767f3cf58ab955765ab74d81609f3f66bb3c6d08d7c213b

See more details on using hashes here.

File details

Details for the file memorylane-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for memorylane-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1bb6e9b58113a3f568170d9e704d2532641e6c916585774320b97e0db71c8d68
MD5 9d908af689006a95eecc782e976b9a9c
BLAKE2b-256 a9ae4dc8fd8320b4a28791522795f7a70af4b5465211029020b9a5539289e653

See more details on using hashes here.

Supported by

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