brunvelop/ComfyUI
2
1import os2import importlib.util3from comfy.cli_args import args4 5#Can't use pytorch to get the GPU names because the cuda malloc has to be set before the first import.6def get_gpu_names():7 if os.name == 'nt':8 import ctypes9 10 # Define necessary C structures and types11 class DISPLAY_DEVICEA(ctypes.Structure):12 _fields_ = [13 ('cb', ctypes.c_ulong),14 ('DeviceName', ctypes.c_char * 32),15 ('DeviceString', ctypes.c_char * 128),16 ('StateFlags', ctypes.c_ulong),17 ('DeviceID', ctypes.c_char * 128),18 ('DeviceKey', ctypes.c_char * 128)19 ]20 21 # Load user32.dll22 user32 = ctypes.windll.user3223 24 # Call EnumDisplayDevicesA25 def enum_display_devices():26 device_info = DISPLAY_DEVICEA()27 device_info.cb = ctypes.sizeof(device_info)28 device_index = 029 gpu_names = set()30 31 while user32.EnumDisplayDevicesA(None, device_index, ctypes.byref(device_info), 0):32 device_index += 133 gpu_names.add(device_info.DeviceString.decode('utf-8'))34 return gpu_names35 return enum_display_devices()36 else:37 return set()38 39blacklist = {"GeForce GTX TITAN X", "GeForce GTX 980", "GeForce GTX 970", "GeForce GTX 960", "GeForce GTX 950", "GeForce 945M",40 "GeForce 940M", "GeForce 930M", "GeForce 920M", "GeForce 910M", "GeForce GTX 750", "GeForce GTX 745", "Quadro K620",41 "Quadro K1200", "Quadro K2200", "Quadro M500", "Quadro M520", "Quadro M600", "Quadro M620", "Quadro M1000",42 "Quadro M1200", "Quadro M2000", "Quadro M2200", "Quadro M3000", "Quadro M4000", "Quadro M5000", "Quadro M5500", "Quadro M6000",43 "GeForce MX110", "GeForce MX130", "GeForce 830M", "GeForce 840M", "GeForce GTX 850M", "GeForce GTX 860M",44 "GeForce GTX 1650", "GeForce GTX 1630"45 }46 47def cuda_malloc_supported():48 try:49 names = get_gpu_names()50 except:51 names = set()52 for x in names:53 if "NVIDIA" in x:54 for b in blacklist:55 if b in x:56 return False57 return True58 59 60if not args.cuda_malloc:61 try:62 version = ""63 torch_spec = importlib.util.find_spec("torch")64 for folder in torch_spec.submodule_search_locations:65 ver_file = os.path.join(folder, "version.py")66 if os.path.isfile(ver_file):67 spec = importlib.util.spec_from_file_location("torch_version_import", ver_file)68 module = importlib.util.module_from_spec(spec)69 spec.loader.exec_module(module)70 version = module.__version__71 if int(version[0]) >= 2: #enable by default for torch version 2.0 and up72 args.cuda_malloc = cuda_malloc_supported()73 except:74 pass75 76 77if args.cuda_malloc and not args.disable_cuda_malloc:78 env_var = os.environ.get('PYTORCH_CUDA_ALLOC_CONF', None)79 if env_var is None:80 env_var = "backend:cudaMallocAsync"81 else:82 env_var += ",backend:cudaMallocAsync"83 84 os.environ['PYTORCH_CUDA_ALLOC_CONF'] = env_var85 