import importlib.metadata import torch import logging from contextlib import contextmanager logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') log = logging.getLogger(__name__) def check_diffusers_version(): try: version = importlib.metadata.version('diffusers') required_version = '0.31.0' if version < required_version: raise AssertionError(f"diffusers version {version} is installed, but version {required_version} or higher is required.") except importlib.metadata.PackageNotFoundError: raise AssertionError("diffusers is not installed.") def print_memory(device): memory = torch.cuda.memory_allocated(device) / 1024**3 max_memory = torch.cuda.max_memory_allocated(device) / 1024**3 max_reserved = torch.cuda.max_memory_reserved(device) / 1024**3 log.info(f"Allocated memory: {memory=:.3f} GB") log.info(f"Max allocated memory: {max_memory=:.3f} GB") log.info(f"Max reserved memory: {max_reserved=:.3f} GB") #memory_summary = torch.cuda.memory_summary(device=device, abbreviated=False) #log.info(f"Memory Summary:\n{memory_summary}") def get_module_memory_mb(module): memory = 0 for param in module.parameters(): if param.data is not None: memory += param.nelement() * param.element_size() return memory / (1024 * 1024) # Convert to MB def apply_lora(model, device_to=None): to_load = [] for n, m in model.model.named_modules(): params = [] skip = False for name, param in m.named_parameters(recurse=False): params.append(name) for name, param in m.named_parameters(recurse=True): if name not in params: skip = True # skip random weights in non leaf modules break if not skip and (hasattr(m, "comfy_cast_weights") or len(params) > 0): to_load.append((n, m, params)) to_load.sort(reverse=True) for x in to_load: n = x[0] m = x[1] params = x[2] if hasattr(m, "comfy_patched_weights"): if m.comfy_patched_weights == True: continue for param in params: model.patch_weight_to_device("{}.{}".format(n, param), device_to=device_to) m.comfy_patched_weights = True model.current_weight_patches_uuid = model.patches_uuid return model