Skip to content

Commit 747b068

Browse files
authored
[Hardware] Replace memory related torch.cuda APIs (#37031)
Signed-off-by: Kunshang Ji <jikunshang95@gmail.com>
1 parent 122f75d commit 747b068

8 files changed

Lines changed: 20 additions & 17 deletions

File tree

benchmarks/attention_benchmarks/runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,8 @@ def _run_single_benchmark(
418418
mem_stats = {}
419419
if config.profile_memory:
420420
mem_stats = {
421-
"allocated_mb": torch.cuda.memory_allocated(device) / 1024**2,
422-
"reserved_mb": torch.cuda.memory_reserved(device) / 1024**2,
421+
"allocated_mb": torch.accelerator.memory_allocated(device) / 1024**2,
422+
"reserved_mb": torch.accelerator.memory_reserved(device) / 1024**2,
423423
}
424424

425425
return times, mem_stats

benchmarks/benchmark_topk_topp.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,16 @@ def create_logits(
9595
def measure_memory() -> tuple[int, int]:
9696
"""Return (allocated, reserved) memory in bytes."""
9797
torch.accelerator.synchronize()
98-
return torch.cuda.memory_allocated(), torch.cuda.max_memory_allocated()
98+
return (
99+
torch.accelerator.memory_allocated(),
100+
torch.accelerator.max_memory_allocated(),
101+
)
99102

100103

101104
def reset_memory_stats():
102105
"""Reset peak memory statistics."""
103106
reset_buffer_cache()
104-
torch.cuda.reset_peak_memory_stats()
107+
torch.accelerator.reset_peak_memory_stats()
105108
torch.accelerator.empty_cache()
106109
gc.collect()
107110

tests/test_regression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_gc():
5555
# The memory allocated for model and KV cache should be released.
5656
# The memory allocated for PyTorch and others should be less than 50MB.
5757
# Usually, it's around 10MB.
58-
allocated = torch.cuda.memory_allocated()
58+
allocated = torch.accelerator.memory_allocated()
5959
assert allocated < 50 * 1024 * 1024
6060

6161

tests/utils_/test_mem_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_memory_profiling():
2929
def measure_current_non_torch():
3030
free, total = torch.cuda.mem_get_info()
3131
current_used = total - free
32-
current_torch = torch.cuda.memory_reserved()
32+
current_torch = torch.accelerator.memory_reserved()
3333
current_non_torch = current_used - current_torch
3434
return current_non_torch
3535

tools/pre_commit/check_torch_cuda.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# Regex: match `torch.cuda.xxx` but allow `torch.accelerator.xxx`
99
# --------------------------------------------------------------------------- #
1010
_TORCH_CUDA_PATTERNS = [
11-
r"\btorch\.cuda\.(empty_cache|synchronize|device_count|current_device|set_device|device\()\b",
11+
r"\btorch\.cuda\.(empty_cache|synchronize|device_count|current_device|memory_reserved|memory_allocated|max_memory_allocated|max_memory_reserved|reset_peak_memory_stats|memory_stats|set_device|device\()\b",
1212
r"\bwith\storch\.cuda\.device\b",
1313
]
1414

vllm/model_executor/model_loader/base_loader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def load_model(
6464
# Log peak GPU memory after loading weights. This is needed
6565
# to have test coverage on peak memory for online quantization.
6666
if current_platform.is_cuda():
67-
peak_memory = torch.cuda.max_memory_allocated()
67+
peak_memory = torch.accelerator.max_memory_allocated()
6868
logger.debug_once(
6969
"Peak GPU memory after loading weights: %s GiB",
7070
format_gib(peak_memory),

vllm/utils/mem_utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ def measure(self) -> None:
9393
device = self.device_
9494

9595
# we measure the torch peak memory usage via allocated_bytes,
96-
# rather than `torch.cuda.memory_reserved()` .
97-
# After `torch.cuda.reset_peak_memory_stats()`,
98-
# `torch.cuda.memory_reserved()` will keep growing, and only shrink
96+
# rather than `torch.accelerator.memory_reserved()` .
97+
# After `torch.accelerator.reset_peak_memory_stats()`,
98+
# `torch.accelerator.memory_reserved()` will keep growing, and only shrink
9999
# when we call `torch.accelerator.empty_cache()` or OOM happens.
100-
self.torch_peak = current_platform.memory_stats(device).get(
100+
self.torch_peak = torch.accelerator.memory_stats(device).get(
101101
"allocated_bytes.all.peak", 0
102102
)
103103

@@ -123,10 +123,10 @@ def measure(self) -> None:
123123

124124
self.cuda_memory = self.total_memory - self.free_memory
125125

126-
# torch.cuda.memory_reserved() is how many bytes
126+
# torch.accelerator.memory_reserved() is how many bytes
127127
# PyTorch gets from cuda (by calling cudaMalloc, etc.)
128128
# this is used to measure the non-torch memory usage
129-
self.torch_memory = current_platform.memory_reserved(device)
129+
self.torch_memory = torch.accelerator.memory_reserved(device)
130130

131131
self.non_torch_memory = self.cuda_memory - self.torch_memory
132132
self.timestamp = time.time()
@@ -243,15 +243,15 @@ def memory_profiling(
243243
The memory used for loading weights (a.) is directly given from the
244244
argument `weights_memory`.
245245
246-
The increase of `torch.cuda.memory_stats()["allocated_bytes.all.peak"]`
246+
The increase of `torch.accelerator.memory_stats()["allocated_bytes.all.peak"]`
247247
during profiling gives (b.).
248248
249249
The increase of `non_torch_memory` from creating the current vLLM instance
250250
until after profiling to get (c.).
251251
"""
252252
gc.collect()
253253
torch.accelerator.empty_cache()
254-
current_platform.reset_peak_memory_stats(baseline_snapshot.device_)
254+
torch.accelerator.reset_peak_memory_stats(baseline_snapshot.device_)
255255

256256
result = MemoryProfilingResult(
257257
before_create=baseline_snapshot,

vllm/v1/worker/gpu_worker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def determine_available_memory(self) -> int:
387387
) as profile_result:
388388
self.model_runner.profile_run()
389389

390-
profile_torch_peak = current_platform.memory_stats(self.device).get(
390+
profile_torch_peak = torch.accelerator.memory_stats(self.device).get(
391391
"allocated_bytes.all.peak", 0
392392
)
393393

0 commit comments

Comments
 (0)