diff --git a/modules/devices.py b/modules/devices.py index f00143d50..f69d19f53 100644 --- a/modules/devices.py +++ b/modules/devices.py @@ -507,11 +507,15 @@ def test_for_nans(x, where): raise NansException(message) +def normalize_device(device): + if torch.device(device).type in {"cpu", "mps", "meta"}: + return torch.device(device) + if torch.device(device).index is None: + return torch.device(str(device), index=0) + return torch.device(device) + + def same_device(d1, d2): if d1.type != d2.type: return False - if d1.type == "cuda" and d1.index is None: - d1 = torch.device("cuda", index=0) - if d2.type == "cuda" and d2.index is None: - d2 = torch.device("cuda", index=0) - return d1 == d2 + return normalize_device(d1) == normalize_device(d2) diff --git a/modules/sd_models.py b/modules/sd_models.py index 538e8b047..2a5478be9 100644 --- a/modules/sd_models.py +++ b/modules/sd_models.py @@ -803,7 +803,7 @@ def apply_balanced_offload(sd_model): return module def pre_forward(self, module, *args, **kwargs): - if normalize_device(module.device) != normalize_device(devices.device): + if devices.normalize_device(module.device) != devices.normalize_device(devices.device): device_index = torch.device(devices.device).index if device_index is None: device_index = 0 @@ -853,14 +853,6 @@ def apply_balanced_offload(sd_model): return sd_model -def normalize_device(device): - if torch.device(device).type in {"cpu", "mps", "meta"}: - return torch.device(device) - if torch.device(device).index is None: - return torch.device(str(device) + ":0") - return torch.device(device) - - def move_model(model, device=None, force=False): if model is None or device is None: return @@ -900,7 +892,7 @@ def move_model(model, device=None, force=False): shared.log.error(f'Model move execution device: device={device} {e}') if getattr(model, 'has_accelerate', False) and not force: return - if hasattr(model, "device") and normalize_device(model.device) == normalize_device(device): + if hasattr(model, "device") and devices.normalize_device(model.device) == devices.normalize_device(device): return try: try: