Upload lora-scripts/sd-scripts/library/device_utils.py with huggingface_hub
Browse files
lora-scripts/sd-scripts/library/device_utils.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import functools
|
| 2 |
+
import gc
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
try:
|
| 7 |
+
HAS_CUDA = torch.cuda.is_available()
|
| 8 |
+
except Exception:
|
| 9 |
+
HAS_CUDA = False
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
HAS_MPS = torch.backends.mps.is_available()
|
| 13 |
+
except Exception:
|
| 14 |
+
HAS_MPS = False
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
import intel_extension_for_pytorch as ipex # noqa
|
| 18 |
+
|
| 19 |
+
HAS_XPU = torch.xpu.is_available()
|
| 20 |
+
except Exception:
|
| 21 |
+
HAS_XPU = False
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def clean_memory():
|
| 25 |
+
gc.collect()
|
| 26 |
+
if HAS_CUDA:
|
| 27 |
+
torch.cuda.empty_cache()
|
| 28 |
+
if HAS_XPU:
|
| 29 |
+
torch.xpu.empty_cache()
|
| 30 |
+
if HAS_MPS:
|
| 31 |
+
torch.mps.empty_cache()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def clean_memory_on_device(device: torch.device):
|
| 35 |
+
r"""
|
| 36 |
+
Clean memory on the specified device, will be called from training scripts.
|
| 37 |
+
"""
|
| 38 |
+
gc.collect()
|
| 39 |
+
|
| 40 |
+
# device may "cuda" or "cuda:0", so we need to check the type of device
|
| 41 |
+
if device.type == "cuda":
|
| 42 |
+
torch.cuda.empty_cache()
|
| 43 |
+
if device.type == "xpu":
|
| 44 |
+
torch.xpu.empty_cache()
|
| 45 |
+
if device.type == "mps":
|
| 46 |
+
torch.mps.empty_cache()
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@functools.lru_cache(maxsize=None)
|
| 50 |
+
def get_preferred_device() -> torch.device:
|
| 51 |
+
r"""
|
| 52 |
+
Do not call this function from training scripts. Use accelerator.device instead.
|
| 53 |
+
"""
|
| 54 |
+
if HAS_CUDA:
|
| 55 |
+
device = torch.device("cuda")
|
| 56 |
+
elif HAS_XPU:
|
| 57 |
+
device = torch.device("xpu")
|
| 58 |
+
elif HAS_MPS:
|
| 59 |
+
device = torch.device("mps")
|
| 60 |
+
else:
|
| 61 |
+
device = torch.device("cpu")
|
| 62 |
+
print(f"get_preferred_device() -> {device}")
|
| 63 |
+
return device
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def init_ipex():
|
| 67 |
+
"""
|
| 68 |
+
Apply IPEX to CUDA hijacks using `library.ipex.ipex_init`.
|
| 69 |
+
|
| 70 |
+
This function should run right after importing torch and before doing anything else.
|
| 71 |
+
|
| 72 |
+
If IPEX is not available, this function does nothing.
|
| 73 |
+
"""
|
| 74 |
+
try:
|
| 75 |
+
if HAS_XPU:
|
| 76 |
+
from library.ipex import ipex_init
|
| 77 |
+
|
| 78 |
+
is_initialized, error_message = ipex_init()
|
| 79 |
+
if not is_initialized:
|
| 80 |
+
print("failed to initialize ipex:", error_message)
|
| 81 |
+
else:
|
| 82 |
+
return
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print("failed to initialize ipex:", e)
|