Lightning-AI / Lightning-AI/lightning-thunder

Sharing memory pools in CUDAGraphs has non-trivial constraints for memory reuse

Open
#1,792 3 comments 0 reactions 1 assignee View on GitHub

@ali-alshaar7 is already working on this.

Since Feb 25, 2025.

cudagraphs
Dominant language
Python
Stars
1.5k
Forks
121
PR merge metrics
No merged PRs in 30d

Description

*Note*: If you have a model or program that is not supported yet but should be, please use the program coverage template.

## 🐛 Bug

In theory, [sharing memory pools across CUDAGraphs](https://github.com/Lightning-AI/lightning-thunder/pull/1790) should decrease allocated memory, but in our experiments, it doesn't.

### To Reproduce

follow the test in the above PR.

### Expected behavior

The following minimal repro using pure torch cg does show difference between setting men_pool = None and men_pool = g1.pool(), but the difference is very small.

```
# with pool sharing: 15585.885696
# without pool sharing: 15586.934272
```

```py
import torch
import litgpt
from torch.testing import make_tensor
from functools import partial

device = torch.device("cuda")

cfg = litgpt.Config.from_name("open_llama_3b", n_layer=10)
with device:
make = partial(make_tensor, low=0, high=255, device=device, dtype=torch.long, requires_grad=False)
shape = (1,) + (cfg.block_size,)
shape2 = (1,) + (cfg.block_size//2,)

x = make(shape)
y = make(shape2)
m = litgpt.GPT(cfg)

workload = m

g1 = torch.cuda.CUDAGraph()
g2 = torch.cuda.CUDAGraph()

for _ in range(3):
with torch.no_grad():
static_out_1 = workload(x)
static_out_2 = workload(y)

with torch.cuda.graph(g1):
static_out_1.copy_(workload(x))

mem_pool = None # or g1.pool() for pool sharing

with torch.cuda.graph(g2, pool=mem_pool):
static_out_2.copy_(workload(y))

# with pool sharing: 15585.885696
# without pool sharing: 15586.934272
print(torch.cuda.max_memory_allocated() / 1e6)

```

### Environment

- PyTorch Version (e.g., 1.0):
- OS (e.g., Linux):
- How you installed PyTorch (`conda`, `pip`, source):
- Build command you used (if compiling from source):
- Python version:
- CUDA/cuDNN version:
- GPU models and configuration: T4 GPU on Ligtning Studio
- Any other relevant information:

### Additional context

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.