Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -28,22 +28,86 @@ except ImportError:
|
|
| 28 |
spaces = _SpacesShim()
|
| 29 |
|
| 30 |
|
| 31 |
-
# === CPU MODE OVERRIDE ===
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
return self.float()
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
# === END CPU MODE OVERRIDE ===
|
| 48 |
|
| 49 |
from diffusers import AutoencoderKL, DDIMScheduler
|
|
|
|
| 28 |
spaces = _SpacesShim()
|
| 29 |
|
| 30 |
|
| 31 |
+
# === CPU MODE OVERRIDE (comprehensive) ===
|
| 32 |
+
import functools
|
| 33 |
+
|
| 34 |
+
if not torch.cuda.is_available():
|
| 35 |
+
# 1. Tensor.cuda() -> noop
|
| 36 |
+
_orig_tensor_cuda = torch.Tensor.cuda
|
| 37 |
+
def _safe_tensor_cuda(self, *a, **kw):
|
| 38 |
+
return self
|
| 39 |
+
torch.Tensor.cuda = _safe_tensor_cuda
|
| 40 |
+
|
| 41 |
+
# 2. Tensor.half() -> float() on CPU
|
| 42 |
+
_orig_half = torch.Tensor.half
|
| 43 |
+
def _safe_half(self, *a, **kw):
|
| 44 |
return self.float()
|
| 45 |
+
torch.Tensor.half = _safe_half
|
| 46 |
+
|
| 47 |
+
# 3. Module.cuda() -> noop
|
| 48 |
+
_orig_module_cuda = torch.nn.Module.cuda
|
| 49 |
+
def _safe_module_cuda(self, *a, **kw):
|
| 50 |
+
return self
|
| 51 |
+
torch.nn.Module.cuda = _safe_module_cuda
|
| 52 |
+
|
| 53 |
+
# 4. Module.to() -> force cpu
|
| 54 |
+
_orig_module_to = torch.nn.Module.to
|
| 55 |
+
def _safe_module_to(self, *args, **kwargs):
|
| 56 |
+
# Replace any "cuda" device with "cpu"
|
| 57 |
+
new_args = []
|
| 58 |
+
for a in args:
|
| 59 |
+
if isinstance(a, (str,)) and "cuda" in a:
|
| 60 |
+
new_args.append("cpu")
|
| 61 |
+
elif isinstance(a, torch.device) and a.type == "cuda":
|
| 62 |
+
new_args.append(torch.device("cpu"))
|
| 63 |
+
elif a == torch.float16:
|
| 64 |
+
new_args.append(torch.float32)
|
| 65 |
+
else:
|
| 66 |
+
new_args.append(a)
|
| 67 |
+
if "device" in kwargs:
|
| 68 |
+
d = kwargs["device"]
|
| 69 |
+
if isinstance(d, str) and "cuda" in d:
|
| 70 |
+
kwargs["device"] = "cpu"
|
| 71 |
+
elif isinstance(d, torch.device) and d.type == "cuda":
|
| 72 |
+
kwargs["device"] = torch.device("cpu")
|
| 73 |
+
if "dtype" in kwargs and kwargs["dtype"] == torch.float16:
|
| 74 |
+
kwargs["dtype"] = torch.float32
|
| 75 |
+
return _orig_module_to(self, *new_args, **kwargs)
|
| 76 |
+
torch.nn.Module.to = _safe_module_to
|
| 77 |
+
|
| 78 |
+
# 5. Tensor.to() -> force cpu
|
| 79 |
+
_orig_tensor_to = torch.Tensor.to
|
| 80 |
+
def _safe_tensor_to(self, *args, **kwargs):
|
| 81 |
+
new_args = []
|
| 82 |
+
for a in args:
|
| 83 |
+
if isinstance(a, (str,)) and "cuda" in a:
|
| 84 |
+
new_args.append("cpu")
|
| 85 |
+
elif isinstance(a, torch.device) and a.type == "cuda":
|
| 86 |
+
new_args.append(torch.device("cpu"))
|
| 87 |
+
elif a == torch.float16:
|
| 88 |
+
new_args.append(torch.float32)
|
| 89 |
+
else:
|
| 90 |
+
new_args.append(a)
|
| 91 |
+
if "device" in kwargs:
|
| 92 |
+
d = kwargs["device"]
|
| 93 |
+
if isinstance(d, str) and "cuda" in d:
|
| 94 |
+
kwargs["device"] = "cpu"
|
| 95 |
+
elif isinstance(d, torch.device) and d.type == "cuda":
|
| 96 |
+
kwargs["device"] = torch.device("cpu")
|
| 97 |
+
if "dtype" in kwargs and kwargs["dtype"] == torch.float16:
|
| 98 |
+
kwargs["dtype"] = torch.float32
|
| 99 |
+
return _orig_tensor_to(self, *new_args, **kwargs)
|
| 100 |
+
torch.Tensor.to = _safe_tensor_to
|
| 101 |
+
|
| 102 |
+
# 6. torch.load -> force map_location=cpu
|
| 103 |
+
_orig_load = torch.load
|
| 104 |
+
@functools.wraps(_orig_load)
|
| 105 |
+
def _safe_load(*args, **kwargs):
|
| 106 |
+
kwargs["map_location"] = "cpu"
|
| 107 |
+
return _orig_load(*args, **kwargs)
|
| 108 |
+
torch.load = _safe_load
|
| 109 |
+
|
| 110 |
+
print("[CPU OVERRIDE] All CUDA calls redirected to CPU", flush=True)
|
| 111 |
# === END CPU MODE OVERRIDE ===
|
| 112 |
|
| 113 |
from diffusers import AutoencoderKL, DDIMScheduler
|