multimodalart HF Staff commited on
Commit
443fdae
·
verified ·
1 Parent(s): 13989b8

Make torch.xpu shim catch-all (no-op for any attribute) to survive further internal probes

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -11,17 +11,24 @@ import torch
11
  # versions unconditionally reference `torch.xpu.empty_cache` at import time
12
  # (in diffusers.utils.torch_utils), which raises AttributeError. Stub it out.
13
  if not hasattr(torch, "xpu"):
14
- import types
15
- torch.xpu = types.SimpleNamespace(
16
- empty_cache=lambda: None,
17
- is_available=lambda: False,
18
- synchronize=lambda *a, **k: None,
19
- device_count=lambda: 0,
20
- manual_seed=lambda *a, **k: None,
21
- reset_peak_memory_stats=lambda *a, **k: None,
22
- max_memory_allocated=lambda *a, **k: 0,
23
- current_device=lambda: 0,
24
- )
 
 
 
 
 
 
 
25
 
26
  from huggingface_hub import snapshot_download
27
  from huggingface_hub.utils import GatedRepoError, RepositoryNotFoundError, RevisionNotFoundError
 
11
  # versions unconditionally reference `torch.xpu.empty_cache` at import time
12
  # (in diffusers.utils.torch_utils), which raises AttributeError. Stub it out.
13
  if not hasattr(torch, "xpu"):
14
+ class _XPUStub:
15
+ """Stand-in for the `torch.xpu` submodule, absent from this build.
16
+
17
+ Several libraries (diffusers, and even torch's own `torch/random.py`)
18
+ unconditionally reach for `torch.xpu.<attr>` once `hasattr(torch,
19
+ "xpu")` is True, without gating every call behind `is_available()`.
20
+ Rather than chase each attribute one by one, any unknown attribute
21
+ resolves to a harmless no-op that returns 0 (falsy, and safe to use
22
+ in comparisons/indexing like `torch.xpu.current_device()`).
23
+ """
24
+
25
+ def is_available(self):
26
+ return False
27
+
28
+ def __getattr__(self, _name):
29
+ return lambda *args, **kwargs: 0
30
+
31
+ torch.xpu = _XPUStub()
32
 
33
  from huggingface_hub import snapshot_download
34
  from huggingface_hub.utils import GatedRepoError, RepositoryNotFoundError, RevisionNotFoundError