NVIDIA / NVIDIA/TransformerEngine

AttnFuncWithCP can use less memory

Open
#952 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
3.5k
Forks
831
Avg merge
3d 11h
Merged PRs (30d)
65

Description

In AttnFuncWithCP.forward, up to 3 buffer (two calculating and one recving) is used simultaneously but p2p_comm_buffers will grow to cp_size which I believe is a waste. Just add buffer[i] = None and kv_input[i%2] = None after _flash_attn_forward may lower memory usage as the reference count of calculated kv is zero.

Here's some mock code:

  • No buffer release:
import torch

torch.cuda.memory._record_memory_history(max_entries=100000)

device = 'cuda:7'
cp_size = 16
streams = [torch.cuda.current_stream(), torch.cuda.Stream()]
buffers = [None for _ in range(cp_size)]
buffers[0] = torch.empty(100, 100, dtype=torch.float32, device=device)

torch.cuda.memory._dump_snapshot('dev_cuda_mem_1.pkl')
for i in range(cp_size):
    with torch.cuda.stream(streams[i % 2]):
        if i < cp_size - 1:
            buffers[i + 1] = torch.empty(100, 100, dtype=torch.float32, device=device)
            torch.cuda.memory._dump_snapshot('cuda_mem_snapshot.pkl')

        kv = buffers[i]
        # ....
        for _ in range(10000):
            buffers[i] *= buffers[i]

        # buffers[i] = None
image
  • With buffer release:
import torch

torch.cuda.memory._record_memory_history(max_entries=100000)

device = 'cuda:7'
cp_size = 16
streams = [torch.cuda.current_stream(), torch.cuda.Stream()]
buffers = [None for _ in range(cp_size)]
buffers[0] = torch.empty(100, 100, dtype=torch.float32, device=device)

torch.cuda.memory._dump_snapshot('dev_cuda_mem_1.pkl')
for i in range(cp_size):
    with torch.cuda.stream(streams[i % 2]):
        if i < cp_size - 1:
            buffers[i + 1] = torch.empty(100, 100, dtype=torch.float32, device=device)
            torch.cuda.memory._dump_snapshot('cuda_mem_snapshot.pkl')

        kv = buffers[i]
        # ....
        for _ in range(10000):
            buffers[i] *= buffers[i]

        buffers[i] = None
image

Link to PR #951

Contributor guide

Open the contributing guide

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.

Research direction

Start at AttnFuncWithCP.forward and compare the linked PR #951 with the issue's mock buffer-release examples. Check memory behavior around _flash_attn_forward, p2p_comm_buffers, and cp_size; done means the buffers can be released without changing attention behavior or causing communication errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
distributed-systems, machine-learning, performance
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.