"""Tools related to devices, memory, etc."""fromtypingimportAnyfrom._utilsimportdeprecatedfrom.cudaimportfree_memoryasfree_memory_originaltry:importpynvmlexceptImportError:pynvml=NoneifpynvmlisnotNone:frompynvmlimportNVMLError_LibraryNotFounddef_to_str(x):ifisinstance(x,str):returnxelifisinstance(x,bytes):returnstr(x,'utf-8')else:raiseValueError('Internal error')
[docs]@deprecated('Instead, use `delu.cuda.free_memory`.')deffree_memory(*args,**kwargs)->None:""" <DEPRECATION MESSAGE> """returnfree_memory_original(*args,**kwargs)
[docs]@deprecated('Instead, use functions from `torch.cuda`')defget_gpus_info()->dict[str,Any]:"""Get information about GPU devices: driver version, memory, utilization etc. <DEPRECATION MESSAGE> The example below shows what kind of information is returned as the result. All figures about memory are given in bytes. Returns: Information about GPU devices. Warning: The 'devices' value contains information about *all* gpus regardless of the value of ``CUDA_VISIBLE_DEVICES``. Examples: .. code-block:: print(delu.hardware.get_gpu_info()) Output example (formatted for convenience): .. code-block:: none { 'driver': '440.33.01', 'devices': [ { 'name': 'GeForce RTX 2080 Ti', 'memory_total': 11554717696, 'memory_free': 11554652160, 'memory_used': 65536, 'utilization': 0, }, { 'name': 'GeForce RTX 2080 Ti', 'memory_total': 11552096256, 'memory_free': 11552030720, 'memory_used': 65536, 'utilization': 0, }, ], } """ifpynvmlisNone:raiseRuntimeError('To use this function, install pynvml via `pip install pynvml<12.0`')try:pynvml.nvmlInit()exceptNVMLError_LibraryNotFoundaserr:raiseRuntimeError('Failed to get information about GPU memory. ''Make sure that you actually have GPU and all relevant software installed.')fromerrn_devices=pynvml.nvmlDeviceGetCount()devices=[]fordevice_idinrange(n_devices):handle=pynvml.nvmlDeviceGetHandleByIndex(device_id)memory_info=pynvml.nvmlDeviceGetMemoryInfo(handle)devices.append({'name':_to_str(pynvml.nvmlDeviceGetName(handle)),'memory_total':memory_info.total,'memory_free':memory_info.free,'memory_used':memory_info.used,'utilization':pynvml.nvmlDeviceGetUtilizationRates(handle).gpu,})return{'driver':_to_str(pynvml.nvmlSystemGetDriverVersion()),'devices':devices,}