Skip to content

Commit 83bca4c

Browse files
rharish101rohitgr7awaelchli
authored
Prioritize Previously-Used GPUs During Auto-Selection (#10485)
Co-authored-by: Rohit Gupta <[email protected]> Co-authored-by: Adrian Wälchli <[email protected]>
1 parent 475d7d0 commit 83bca4c

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

pytorch_lightning/tuner/auto_gpu_select.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,19 @@ def pick_single_gpu(exclude_gpus: List[int]) -> int:
4545
RuntimeError:
4646
If you try to allocate a GPU, when no GPUs are available.
4747
"""
48+
previously_used_gpus = []
49+
unused_gpus = []
4850
for i in range(torch.cuda.device_count()):
4951
if i in exclude_gpus:
5052
continue
53+
54+
if torch.cuda.memory_reserved(f"cuda:{i}") > 0:
55+
previously_used_gpus.append(i)
56+
else:
57+
unused_gpus.append(i)
58+
59+
# Prioritize previously used GPUs
60+
for i in previously_used_gpus + unused_gpus:
5161
# Try to allocate on device:
5262
device = torch.device(f"cuda:{i}")
5363
try:

0 commit comments

Comments
 (0)