Skip to content

Commit

Permalink
Revert "[misc] improve memory profiling (vllm-project#11809)"
Browse files Browse the repository at this point in the history
This reverts commit 889e662.
  • Loading branch information
gshtras committed Jan 13, 2025
1 parent 5a51290 commit 079750e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 94 deletions.
19 changes: 1 addition & 18 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import pytest
import torch
from vllm_test_utils import monitor

from vllm.config import ParallelConfig, VllmConfig, set_current_vllm_config
from vllm.utils import (FlexibleArgumentParser, PlaceholderModule,
Expand Down Expand Up @@ -309,32 +308,16 @@ def test_memory_profiling():

weights_memory_in_bytes = 128 * 1024 * 1024 * 4 # 512 MiB

def measure_current_non_torch():
free, total = torch.cuda.mem_get_info()
current_used = total - free
current_torch = torch.cuda.memory_reserved()
current_non_torch = current_used - current_torch
return current_non_torch

with memory_profiling(baseline_memory_in_bytes=baseline_memory_in_bytes,
weights_memory_in_bytes=weights_memory_in_bytes) as result, \
monitor(measure_current_non_torch) as monitored_values:
weights_memory_in_bytes=weights_memory_in_bytes) as result:
# make a memory spike, 1 GiB
spike = torch.randn(256, 1024, 1024, device='cuda', dtype=torch.float32)
del spike

# Add some extra non-torch memory 256 MiB (simulate NCCL)
handle2 = lib.cudaMalloc(256 * 1024 * 1024)

# this is an analytic value, it is exact,
# we only have 256 MiB non-torch memory increase
measured_diff = monitored_values.values[-1] - monitored_values.values[0]
assert measured_diff == 256 * 1024 * 1024

# Check that the memory usage is within 5% of the expected values
# 5% tolerance is caused by PyTorch caching allocator,
# we cannot control PyTorch's behavior of its internal buffers,
# which causes a small error (<10 MiB in practice)
non_torch_ratio = result.non_torch_increase_in_bytes / (256 * 1024 * 1024) # noqa
torch_peak_ratio = result.torch_peak_increase_in_bytes / (1024 * 1024 * 1024) # noqa
assert abs(non_torch_ratio - 1) <= 0.05
Expand Down
3 changes: 1 addition & 2 deletions tests/vllm_test_utils/vllm_test_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
"""

from .blame import BlameResult, blame
from .monitor import MonitoredValues, monitor

__all__ = ["blame", "BlameResult", "monitor", "MonitoredValues"]
__all__ = ["blame", "BlameResult"]
68 changes: 0 additions & 68 deletions tests/vllm_test_utils/vllm_test_utils/monitor.py

This file was deleted.

12 changes: 6 additions & 6 deletions vllm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2073,10 +2073,10 @@ class MemorySnapshot:
timestamp: float = 0.0

def measure(self):
self.torch_peak_in_bytes = torch.cuda.max_memory_reserved()
# torch.cuda.memory_reserved() is how many bytes
# PyTorch gets from cuda (by calling cudaMalloc, etc.)
self.torch_memory_in_bytes = torch.cuda.memory_reserved()
self.torch_peak_in_bytes = torch.cuda.memory_stats(
)["allocated_bytes.all.peak"]
self.torch_memory_in_bytes = torch.cuda.memory_stats(
)["allocated_bytes.all.current"]
self.timestamp = time.time()

def __sub__(self, other: "MemorySnapshot") -> "MemorySnapshot":
Expand Down Expand Up @@ -2153,10 +2153,10 @@ def memory_profiling(
The memory used for loading weights (a.) is directly given from the argument `weights_memory_in_bytes`.
The increase of `torch.cuda.memory_stats()["allocated_bytes.all.peak"]` after profiling gives (b.).
The increase of ``torch.cuda.memory_stats()["allocated_bytes.all.peak"]` after profiling gives (b.).
(c.) is tricky. We measure the total memory used in this GPU (`torch.cuda.mem_get_info()[1] - torch.cuda.mem_get_info()[0]`),
subtract the baseline memory, the memory used by the model weights, and diff of `torch.cuda.memory_reserved()`.
subtract the baseline memory, the memory used by the model weights, and diff of `torch.cuda.memory_stats()["allocated_bytes.all.current"]`.
""" # noqa
torch.cuda.reset_peak_memory_stats()

Expand Down

0 comments on commit 079750e

Please sign in to comment.