Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update modeling.py by adding try-catch section to skip the unavailable devices #2681

Merged
merged 2 commits into from
May 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions src/accelerate/utils/modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,27 +803,40 @@ def get_max_memory(max_memory: Optional[Dict[Union[int, str], Union[int, str]]]
import psutil

if max_memory is None:
if not (torch.cuda.is_available() or is_npu_available() or is_mlu_available() or is_xpu_available()):
max_memory = {}

else:
# Make sure CUDA is initialized on each GPU to have the right memory info.
if is_npu_available():
for i in range(torch.npu.device_count()):
max_memory = {}
# Make sure CUDA is initialized on each GPU to have the right memory info.
if is_npu_available():
for i in range(torch.npu.device_count()):
try:
_ = torch.tensor(0, device=torch.device("npu", i))
max_memory = {i: torch.npu.mem_get_info(i)[0] for i in range(torch.npu.device_count())}
elif is_mlu_available():
for i in range(torch.mlu.device_count()):
max_memory.append({i: torch.npu.mem_get_info(i)[0]})
except Exception:
logger.warning(f"Device {i} seems unavailable, Proceeding to check subsequent devices.")
MeVeryHandsome marked this conversation as resolved.
Show resolved Hide resolved
continue
elif is_mlu_available():
for i in range(torch.mlu.device_count()):
try:
_ = torch.tensor(0, device=torch.device("mlu", i))
max_memory = {i: torch.mlu.mem_get_info(i)[0] for i in range(torch.mlu.device_count())}
elif is_xpu_available():
for i in range(torch.xpu.device_count()):
max_memory.append({i: torch.mlu.mem_get_info(i)[0]})
except Exception:
logger.warning(f"Device {i} seems unavailable, Proceeding to check subsequent devices.")
MeVeryHandsome marked this conversation as resolved.
Show resolved Hide resolved
continue
elif is_xpu_available():
for i in range(torch.xpu.device_count()):
try:
_ = torch.tensor(0, device=torch.device("xpu", i))
max_memory = {i: torch.xpu.max_memory_allocated(i) for i in range(torch.xpu.device_count())}
else:
for i in range(torch.cuda.device_count()):
max_memory.append({i: torch.xpu.max_memory_allocated(i)})
except Exception:
logger.warning(f"Device {i} seems unavailable, Proceeding to check subsequent devices.")
MeVeryHandsome marked this conversation as resolved.
Show resolved Hide resolved
continue
else:
for i in range(torch.cuda.device_count()):
try:
_ = torch.tensor([0], device=i)
max_memory = {i: torch.cuda.mem_get_info(i)[0] for i in range(torch.cuda.device_count())}
max_memory.append({i: torch.cuda.mem_get_info(i)[0]})
except Exception:
logger.warning(f"Device {i} seems unavailable, Proceeding to check subsequent devices.")
MeVeryHandsome marked this conversation as resolved.
Show resolved Hide resolved
continue
# allocate everything in the mps device as the RAM is shared
if is_mps_available():
max_memory["mps"] = psutil.virtual_memory().available
Expand Down
Loading