[Admin maintenance] Support new ZeroGPU hardware

#4
by multimodalart HF Staff - opened
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🦀
4
  colorFrom: red
5
  colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 4.32.1
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
4
  colorFrom: red
5
  colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 5.49.1
8
  app_file: app.py
9
  pinned: false
10
  license: mit
app.py CHANGED
@@ -1,18 +1,115 @@
1
  import os
2
- import shlex
3
  import subprocess
 
4
 
5
- import gradio as gr
6
- import numpy as np
7
  import spaces
8
  import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  from diffusers import DiffusionPipeline
10
 
11
- subprocess.run(
12
- shlex.split(
13
- "pip install wheel/diff_gaussian_rasterization-0.0.0-cp310-cp310-linux_x86_64.whl"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  )
15
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  TMP_DIR = "/tmp"
18
  os.makedirs(TMP_DIR, exist_ok=True)
 
1
  import os
2
+ import sys
3
  import subprocess
4
+ import tempfile
5
 
 
 
6
  import spaces
7
  import torch
8
+ import ctypes
9
+
10
+ # Monkey-patch xformers.ops.memory_efficient_attention to fall back to
11
+ # torch SDPA on Blackwell (sm_120). On the new ZeroGPU GPUs, neither
12
+ # the FA3 nor the cutlass xformers kernel supports compute capability
13
+ # 12.0, so any call into xformers' MEA raises NotImplementedError.
14
+ # imagedream's mv_unet imports xformers.ops directly, so we have to
15
+ # patch it before that import happens.
16
+ import xformers
17
+ import xformers.ops as _xops
18
+
19
+
20
+ def _xformers_mea_sdpa(query, key, value, attn_bias=None, p=0.0, scale=None,
21
+ op=None, **kwargs):
22
+ # xformers MEA accepts (B, M, K) or (B, M, H, K). Torch SDPA wants
23
+ # (B, H, M, K). Reshape appropriately and reverse on output.
24
+ if query.dim() == 3:
25
+ # Single-head: add an H=1 axis.
26
+ q = query.unsqueeze(1)
27
+ k = key.unsqueeze(1)
28
+ v = value.unsqueeze(1)
29
+ squeeze_out = True
30
+ else:
31
+ # (B, M, H, K) -> (B, H, M, K)
32
+ q = query.transpose(1, 2)
33
+ k = key.transpose(1, 2)
34
+ v = value.transpose(1, 2)
35
+ squeeze_out = False
36
+ attn_mask = attn_bias
37
+ if hasattr(attn_mask, "materialize"):
38
+ try:
39
+ attn_mask = attn_mask.materialize(
40
+ shape=(q.shape[0], q.shape[1], q.shape[2], k.shape[2]),
41
+ dtype=q.dtype,
42
+ device=q.device,
43
+ )
44
+ except Exception:
45
+ attn_mask = None
46
+ out = torch.nn.functional.scaled_dot_product_attention(
47
+ q, k, v, attn_mask=attn_mask, dropout_p=p, scale=scale,
48
+ )
49
+ if squeeze_out:
50
+ return out.squeeze(1)
51
+ return out.transpose(1, 2)
52
+
53
+
54
+ _xops.memory_efficient_attention = _xformers_mea_sdpa
55
+ xformers.ops.memory_efficient_attention = _xformers_mea_sdpa
56
+
57
+ import gradio as gr
58
+ import numpy as np
59
  from diffusers import DiffusionPipeline
60
 
61
+
62
+ CUDA_HOME = "/cuda-image/usr/local/cuda-13.0"
63
+ CUDA_LIBDIR = os.path.join(CUDA_HOME, "lib64")
64
+
65
+
66
+ @spaces.GPU(duration=600)
67
+ def _first_gpu_setup():
68
+ try:
69
+ import diff_gaussian_rasterization # noqa
70
+ return
71
+ except ImportError:
72
+ pass
73
+
74
+ patch_dir = tempfile.mkdtemp(prefix="torch_cuda_patch_")
75
+ with open(os.path.join(patch_dir, "sitecustomize.py"), "w") as f:
76
+ f.write(
77
+ "try:\n"
78
+ " import torch.utils.cpp_extension as _c\n"
79
+ " _c._check_cuda_version = lambda *a, **k: None\n"
80
+ "except Exception:\n"
81
+ " pass\n"
82
+ )
83
+
84
+ env = os.environ.copy()
85
+ env["CUDA_HOME"] = CUDA_HOME
86
+ env["CUDA_PATH"] = CUDA_HOME
87
+ env["PATH"] = os.path.join(CUDA_HOME, "bin") + os.pathsep + env.get("PATH", "")
88
+ env["PYTHONPATH"] = patch_dir + os.pathsep + env.get("PYTHONPATH", "")
89
+ env["TORCH_CUDA_ARCH_LIST"] = "12.0"
90
+
91
+ subprocess.check_call(
92
+ [sys.executable, "-m", "pip", "install", "--no-deps",
93
+ "setuptools", "wheel", "ninja", "packaging"],
94
  )
95
+
96
+ # The PyPI release of `diff-gaussian-rasterization` is the original
97
+ # graphdeco-inria version; the LGM model uses that one.
98
+ subprocess.check_call(
99
+ [sys.executable, "-m", "pip", "install",
100
+ "--no-build-isolation", "--no-deps",
101
+ "git+https://github.com/graphdeco-inria/diff-gaussian-rasterization.git"],
102
+ env=env,
103
+ )
104
+
105
+
106
+ _first_gpu_setup()
107
+ try:
108
+ ctypes.CDLL(os.path.join(CUDA_LIBDIR, "libcudart.so.13"), mode=ctypes.RTLD_GLOBAL)
109
+ os.environ["LD_LIBRARY_PATH"] = CUDA_LIBDIR + os.pathsep + os.environ.get("LD_LIBRARY_PATH", "")
110
+ except OSError:
111
+ pass
112
+
113
 
114
  TMP_DIR = "/tmp"
115
  os.makedirs(TMP_DIR, exist_ok=True)
requirements.txt CHANGED
@@ -1,4 +1,3 @@
1
- wheel
2
  numpy
3
  tyro
4
  diffusers
@@ -27,8 +26,5 @@ kiui >= 0.2.3
27
  xatlas
28
  roma
29
  plyfile
30
- torch == 2.2.0
31
- torchvision == 0.17.0
32
- torchaudio == 2.2.0
33
  xformers
34
  ushlex
 
 
1
  numpy
2
  tyro
3
  diffusers
 
26
  xatlas
27
  roma
28
  plyfile
 
 
 
29
  xformers
30
  ushlex
wheel/diff_gaussian_rasterization-0.0.0-cp310-cp310-linux_x86_64.whl DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:957eac9a321f881c46e8d8a9206bd5f253b64685f5cc35f07d943b908ef2263f
3
- size 3134028