[Admin maintenance] Support new ZeroGPU hardware

#7
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.20.1
8
  python_version: 3.10.13
9
  app_file: app.py
10
  pinned: false
 
4
  colorFrom: red
5
  colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 5.49.1
8
  python_version: 3.10.13
9
  app_file: app.py
10
  pinned: false
app.py CHANGED
@@ -1,11 +1,135 @@
1
  import os
 
2
  import shlex
3
  import subprocess
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import tyro
5
  import imageio
6
  import numpy as np
7
  import tqdm
8
- import torch
9
  import torch.nn as nn
10
  import torch.nn.functional as F
11
  import torchvision.transforms.functional as TF
@@ -13,13 +137,9 @@ from safetensors.torch import load_file
13
  import rembg
14
  import gradio as gr
15
 
16
- import spaces
17
-
18
- # download checkpoints
19
  from huggingface_hub import hf_hub_download
20
- ckpt_path = hf_hub_download(repo_id="ashawkey/LGM", filename="model_fp16_fixrot.safetensors")
21
 
22
- subprocess.run(shlex.split("pip install wheel/diff_gaussian_rasterization-0.0.0-cp310-cp310-linux_x86_64.whl"))
23
 
24
  import kiui
25
  from kiui.op import recenter
@@ -34,7 +154,6 @@ IMAGENET_DEFAULT_STD = (0.229, 0.224, 0.225)
34
  GRADIO_VIDEO_PATH = 'gradio_output.mp4'
35
  GRADIO_PLY_PATH = 'gradio_output.ply'
36
 
37
- # opt = tyro.cli(AllConfigs)
38
  opt = Options(
39
  input_size=256,
40
  up_channels=(1024, 1024, 512, 256, 128), # one more decoder
@@ -79,7 +198,6 @@ pipe_text = MVDreamPipeline.from_pretrained(
79
  'ashawkey/mvdream-sd2.1-diffusers', # remote weights
80
  torch_dtype=torch.float16,
81
  trust_remote_code=True,
82
- # local_files_only=True,
83
  )
84
  pipe_text = pipe_text.to(device)
85
 
@@ -87,7 +205,6 @@ pipe_image = MVDreamPipeline.from_pretrained(
87
  "ashawkey/imagedream-ipmv-diffusers", # remote weights
88
  torch_dtype=torch.float16,
89
  trust_remote_code=True,
90
- # local_files_only=True,
91
  )
92
  pipe_image = pipe_image.to(device)
93
 
@@ -256,7 +373,7 @@ with block:
256
  inputs=[input_image],
257
  outputs=[output_image, output_video, output_file],
258
  fn=lambda x: process(input_image=x, prompt=''),
259
- cache_examples=True,
260
  label='Image-to-3D Examples'
261
  )
262
 
@@ -273,7 +390,7 @@ with block:
273
  inputs=[input_text],
274
  outputs=[output_image, output_video, output_file],
275
  fn=lambda x: process(input_image=None, prompt=x),
276
- cache_examples=True,
277
  label='Text-to-3D Examples'
278
  )
279
 
 
1
  import os
2
+ import sys
3
  import shlex
4
  import subprocess
5
+ import tempfile
6
+ import ctypes
7
+
8
+ import spaces
9
+ import torch
10
+
11
+
12
+ # ---------------------------------------------------------------------------
13
+ # Blackwell (sm_120) shim: xformers' FA3/FA2/CutlassF kernels in the prebuilt
14
+ # torch-2.10/2.11 wheel all reject compute capability 12.0, so any call into
15
+ # `xformers.ops.memory_efficient_attention(...)` raises NotImplementedError.
16
+ # The LGM stack calls MEA directly in two places:
17
+ # - core/attention.py: 4D shape (B, M, H, K) (the dino-style Attention class)
18
+ # - mvdream/mv_unet.py: 3D shape (B*H, M, K) (the cross-attention block)
19
+ # Route both through torch SDPA. Must be installed BEFORE the imports that
20
+ # pull in core/attention.py and mvdream/mv_unet.py.
21
+ # ---------------------------------------------------------------------------
22
+ import xformers
23
+ import xformers.ops as _xops
24
+
25
+
26
+ def _xformers_mea_sdpa(query, key, value, attn_bias=None, p=0.0, scale=None,
27
+ op=None, **kwargs):
28
+ if query.dim() == 3:
29
+ # (B, M, K) -> single-head; add an H=1 axis.
30
+ q = query.unsqueeze(1)
31
+ k = key.unsqueeze(1)
32
+ v = value.unsqueeze(1)
33
+ squeeze_out = True
34
+ else:
35
+ # (B, M, H, K) -> (B, H, M, K)
36
+ q = query.transpose(1, 2)
37
+ k = key.transpose(1, 2)
38
+ v = value.transpose(1, 2)
39
+ squeeze_out = False
40
+ attn_mask = attn_bias
41
+ if hasattr(attn_mask, "materialize"):
42
+ try:
43
+ attn_mask = attn_mask.materialize(
44
+ shape=(q.shape[0], q.shape[1], q.shape[2], k.shape[2]),
45
+ dtype=q.dtype,
46
+ device=q.device,
47
+ )
48
+ except Exception:
49
+ attn_mask = None
50
+ out = torch.nn.functional.scaled_dot_product_attention(
51
+ q, k, v, attn_mask=attn_mask, dropout_p=p, scale=scale,
52
+ )
53
+ if squeeze_out:
54
+ return out.squeeze(1)
55
+ return out.transpose(1, 2)
56
+
57
+
58
+ _xops.memory_efficient_attention = _xformers_mea_sdpa
59
+ xformers.ops.memory_efficient_attention = _xformers_mea_sdpa
60
+
61
+
62
+ # ---------------------------------------------------------------------------
63
+ # Build `diff_gaussian_rasterization` from source against torch 2.10/2.11/cu128
64
+ # on the first GPU call. The vendored wheel/diff_gaussian_rasterization-...whl
65
+ # in this Space was built against torch 2.4 (`libcudart.so.11.0`) and won't
66
+ # load on Blackwell. We build the original graphdeco-inria fork (what
67
+ # core/gs.py imports).
68
+ # ---------------------------------------------------------------------------
69
+ CUDA_HOME = "/cuda-image/usr/local/cuda-13.0"
70
+ CUDA_LIBDIR = os.path.join(CUDA_HOME, "lib64")
71
+
72
+
73
+ @spaces.GPU(duration=600)
74
+ def _first_gpu_setup():
75
+ try:
76
+ import diff_gaussian_rasterization # noqa: F401
77
+ return
78
+ except ImportError:
79
+ pass
80
+
81
+ patch_dir = tempfile.mkdtemp(prefix="torch_cuda_patch_")
82
+ with open(os.path.join(patch_dir, "sitecustomize.py"), "w") as f:
83
+ f.write(
84
+ "try:\n"
85
+ " import torch.utils.cpp_extension as _c\n"
86
+ " _c._check_cuda_version = lambda *a, **k: None\n"
87
+ "except Exception:\n"
88
+ " pass\n"
89
+ )
90
+
91
+ env = os.environ.copy()
92
+ env["CUDA_HOME"] = CUDA_HOME
93
+ env["CUDA_PATH"] = CUDA_HOME
94
+ env["PATH"] = os.path.join(CUDA_HOME, "bin") + os.pathsep + env.get("PATH", "")
95
+ env["PYTHONPATH"] = patch_dir + os.pathsep + env.get("PYTHONPATH", "")
96
+ env["TORCH_CUDA_ARCH_LIST"] = "12.0"
97
+
98
+ subprocess.check_call(
99
+ [sys.executable, "-m", "pip", "install", "--no-deps",
100
+ "setuptools", "wheel", "ninja", "packaging"],
101
+ )
102
+
103
+ # Build the vendored `diff-gaussian-rasterization/` source in this repo.
104
+ # This Space ships a custom fork that returns 4 outputs (image, radii,
105
+ # depth, alpha); the upstream graphdeco-inria release returns only 2 and
106
+ # would crash `core/gs.py` at unpack time. The vendored tree is what
107
+ # LGM's training/inference code was written against.
108
+ vendored_dgr = os.path.join(os.path.dirname(os.path.abspath(__file__)),
109
+ "diff-gaussian-rasterization")
110
+ subprocess.check_call(
111
+ [sys.executable, "-m", "pip", "install",
112
+ "--no-build-isolation", "--no-deps",
113
+ vendored_dgr],
114
+ env=env,
115
+ )
116
+
117
+
118
+ _first_gpu_setup()
119
+ try:
120
+ ctypes.CDLL(os.path.join(CUDA_LIBDIR, "libcudart.so.13"), mode=ctypes.RTLD_GLOBAL)
121
+ os.environ["LD_LIBRARY_PATH"] = CUDA_LIBDIR + os.pathsep + os.environ.get("LD_LIBRARY_PATH", "")
122
+ except OSError:
123
+ pass
124
+
125
+
126
+ # ---------------------------------------------------------------------------
127
+ # Now the usual app imports.
128
+ # ---------------------------------------------------------------------------
129
  import tyro
130
  import imageio
131
  import numpy as np
132
  import tqdm
 
133
  import torch.nn as nn
134
  import torch.nn.functional as F
135
  import torchvision.transforms.functional as TF
 
137
  import rembg
138
  import gradio as gr
139
 
 
 
 
140
  from huggingface_hub import hf_hub_download
 
141
 
142
+ ckpt_path = hf_hub_download(repo_id="ashawkey/LGM", filename="model_fp16_fixrot.safetensors")
143
 
144
  import kiui
145
  from kiui.op import recenter
 
154
  GRADIO_VIDEO_PATH = 'gradio_output.mp4'
155
  GRADIO_PLY_PATH = 'gradio_output.ply'
156
 
 
157
  opt = Options(
158
  input_size=256,
159
  up_channels=(1024, 1024, 512, 256, 128), # one more decoder
 
198
  'ashawkey/mvdream-sd2.1-diffusers', # remote weights
199
  torch_dtype=torch.float16,
200
  trust_remote_code=True,
 
201
  )
202
  pipe_text = pipe_text.to(device)
203
 
 
205
  "ashawkey/imagedream-ipmv-diffusers", # remote weights
206
  torch_dtype=torch.float16,
207
  trust_remote_code=True,
 
208
  )
209
  pipe_image = pipe_image.to(device)
210
 
 
373
  inputs=[input_image],
374
  outputs=[output_image, output_video, output_file],
375
  fn=lambda x: process(input_image=x, prompt=''),
376
+ cache_examples=False,
377
  label='Image-to-3D Examples'
378
  )
379
 
 
390
  inputs=[input_text],
391
  outputs=[output_image, output_video, output_file],
392
  fn=lambda x: process(input_image=None, prompt=x),
393
+ cache_examples=False,
394
  label='Text-to-3D Examples'
395
  )
396
 
requirements.txt CHANGED
@@ -1,4 +1,3 @@
1
- torch==2.4.0
2
  xformers
3
 
4
  numpy
 
 
1
  xformers
2
 
3
  numpy
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:42bf718442ba764469170abc09d99a70b7c1d891dc290f2e1247db09c95a0e88
3
- size 3021758