You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
defget_max_memory(max_memory: Optional[Dict[Union[int, str], Union[int, str]]] =None):
""" Get the maximum memory available if nothing is passed, converts string to int otherwise. """importpsutilifmax_memoryisNone:
ifnot (torch.cuda.is_available() oris_npu_available() oris_xpu_available()):
max_memory= {}
else:
# Make sure CUDA is initialized on each GPU to have the right memory info.ifis_npu_available():
foriinrange(torch.npu.device_count()):
_=torch.tensor(0, device=torch.device("npu", i))
max_memory= {i: torch.npu.mem_get_info(i)[0] foriinrange(torch.npu.device_count())}
elifis_xpu_available():
foriinrange(torch.xpu.device_count()):
_=torch.tensor(0, device=torch.device("xpu", i))
max_memory= {i: torch.xpu.max_memory_allocated(i) foriinrange(torch.xpu.device_count())}
else:
foriinrange(torch.cuda.device_count()):
_=torch.tensor([0], device=i)
max_memory= {i: torch.cuda.mem_get_info(i)[0] foriinrange(torch.cuda.device_count())}
# allocate everything in the mps device as the RAM is sharedifis_mps_available():
max_memory["mps"] =psutil.virtual_memory().availableelse:
max_memory["cpu"] =psutil.virtual_memory().availablereturnmax_memoryforkeyinmax_memory:
ifisinstance(max_memory[key], str):
max_memory[key] =convert_file_size_to_int(max_memory[key])
# Need to sort the device by type to make sure that we allocate the gpu first.# As gpu/npu/xpu are represented by int, we need to sort them first.gpu_devices= [kforkinmax_memory.keys() ifisinstance(k, int)]
gpu_devices.sort()
# check if gpu/npu/xpu devices are available and if not, throw a warningifis_npu_available():
num_devices=torch.npu.device_count()
elifis_xpu_available():
num_devices=torch.xpu.device_count()
else:
num_devices=torch.cuda.device_count()
fordeviceingpu_devices:
ifdevice>=num_devicesordevice<0:
logger.warning(f"Device {device} is not available, available devices are {list(range(num_devices))}")
# Add the other devices in the preset order if they are availableall_devices=gpu_devices+ [kforkin ["mps", "cpu", "disk"] ifkinmax_memory.keys()]
# Raise an error if a device is not recognizedforkinmax_memory.keys():
ifknotinall_devices:
raiseValueError(
f"Device {k} is not recognized, available devices are integers(for GPU/XPU), 'mps', 'cpu' and 'disk'"
)
max_memory= {k: max_memory[k] forkinall_devices}
returnmax_memory
The text was updated successfully, but these errors were encountered:
Accelarate 库中具有npu相关的实现,但其调用中依赖了
torch_npu.npu.mem_get_info
来获取当前显存占用情况,而这个当前版本的torch_npu
不支持。是否有一些替代的函数可以实现类似mem_get_info
函数的功能?相关代码(来自accelerate==0.28.0)
The text was updated successfully, but these errors were encountered: