[Admin maintenance] Support new ZeroGPU hardware

#3
by multimodalart HF Staff - opened
app.py CHANGED
@@ -1,8 +1,130 @@
1
  import gradio as gr
2
- import spaces
3
  import os
4
- import shutil
5
  os.environ['SPCONV_ALGO'] = 'native'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  from huggingface_hub import hf_hub_download
7
 
8
  from app_utils import (
 
1
  import gradio as gr
 
2
  import os
 
3
  os.environ['SPCONV_ALGO'] = 'native'
4
+ os.environ['ATTN_BACKEND'] = 'xformers'
5
+ os.environ['SPARSE_ATTN_BACKEND'] = 'xformers'
6
+
7
+ import spaces
8
+
9
+ # Build the CUDA extensions that used to ship as torch-2.4-ABI prebuilt wheels
10
+ # (nvdiffrast, diff_gaussian_rasterization) and the torch-2.4-cu121-only
11
+ # torch-scatter, all from source against the actually-installed torch.
12
+ import subprocess, sys, tempfile, ctypes
13
+
14
+ CUDA_HOME = "/cuda-image/usr/local/cuda-13.0"
15
+ CUDA_LIBDIR = os.path.join(CUDA_HOME, "lib64")
16
+
17
+
18
+ @spaces.GPU(duration=600)
19
+ def _first_gpu_setup():
20
+ need = {}
21
+ for name, modname in [
22
+ ("nvdiffrast", "nvdiffrast"),
23
+ ("diff_gaussian_rasterization", "diff_gaussian_rasterization"),
24
+ ("torch_scatter", "torch_scatter"),
25
+ ("detectron2", "detectron2"),
26
+ ]:
27
+ try:
28
+ __import__(modname)
29
+ except ImportError:
30
+ need[name] = True
31
+ if not need:
32
+ print("CUDA extensions already present.")
33
+ return
34
+
35
+ if not os.path.exists(os.path.join(CUDA_HOME, "bin", "nvcc")):
36
+ raise RuntimeError(f"nvcc not at {CUDA_HOME}/bin/nvcc; update CUDA_HOME.")
37
+
38
+ patch_dir = tempfile.mkdtemp(prefix="torch_cuda_patch_")
39
+ with open(os.path.join(patch_dir, "sitecustomize.py"), "w") as f:
40
+ f.write(
41
+ "try:\n"
42
+ " import torch.utils.cpp_extension as _c\n"
43
+ " _c._check_cuda_version = lambda *a, **k: None\n"
44
+ "except Exception:\n"
45
+ " pass\n"
46
+ )
47
+
48
+ env = os.environ.copy()
49
+ env["CUDA_HOME"] = CUDA_HOME
50
+ env["CUDA_PATH"] = CUDA_HOME
51
+ env["PATH"] = os.path.join(CUDA_HOME, "bin") + os.pathsep + env.get("PATH", "")
52
+ env["PYTHONPATH"] = patch_dir + os.pathsep + env.get("PYTHONPATH", "")
53
+ env["TORCH_CUDA_ARCH_LIST"] = "12.0"
54
+
55
+ subprocess.check_call(
56
+ [sys.executable, "-m", "pip", "install", "--no-deps", "setuptools", "wheel", "ninja"],
57
+ )
58
+
59
+ if "nvdiffrast" in need:
60
+ subprocess.check_call(
61
+ [sys.executable, "-m", "pip", "install", "--no-build-isolation",
62
+ "git+https://github.com/NVlabs/nvdiffrast/"],
63
+ env=env,
64
+ )
65
+ if "diff_gaussian_rasterization" in need:
66
+ mip_dir = tempfile.mkdtemp(prefix="mip_splatting_")
67
+ subprocess.check_call(
68
+ ["git", "clone", "--recursive", "--depth=1",
69
+ "https://github.com/autonomousvision/mip-splatting.git", mip_dir],
70
+ )
71
+ subprocess.check_call(
72
+ [sys.executable, "-m", "pip", "install", "--no-build-isolation",
73
+ os.path.join(mip_dir, "submodules", "diff-gaussian-rasterization")],
74
+ env=env,
75
+ )
76
+ if "torch_scatter" in need:
77
+ # Prebuilt wheel for torch 2.10.0 + cu128 (matches the torch wheel's
78
+ # CUDA), avoids the CUDA-version mismatch you get when building against
79
+ # the container's CUDA 13 toolkit.
80
+ subprocess.check_call(
81
+ [sys.executable, "-m", "pip", "install", "--no-deps",
82
+ "torch-scatter",
83
+ "-f", "https://data.pyg.org/whl/torch-2.10.0+cu128.html"],
84
+ )
85
+ if "detectron2" in need:
86
+ subprocess.check_call(
87
+ [sys.executable, "-m", "pip", "install", "--no-build-isolation",
88
+ "git+https://github.com/facebookresearch/detectron2.git"],
89
+ env=env,
90
+ )
91
+
92
+
93
+ _first_gpu_setup()
94
+ ctypes.CDLL(os.path.join(CUDA_LIBDIR, "libcudart.so.13"), mode=ctypes.RTLD_GLOBAL)
95
+ os.environ["LD_LIBRARY_PATH"] = CUDA_LIBDIR + os.pathsep + os.environ.get("LD_LIBRARY_PATH", "")
96
+
97
+ # torch 2.6 flipped torch.load weights_only default to True; the bbox_gen
98
+ # partfield checkpoint pickles yacs.config.CfgNode and other non-tensor
99
+ # objects. Trusted upstream checkpoint — fall back to weights_only=False.
100
+ import torch as _torch
101
+ _orig_load = _torch.load
102
+ _torch.load = lambda *a, **k: _orig_load(*a, **{**k, "weights_only": k.get("weights_only", False)})
103
+
104
+ # xformers on Blackwell (sm_120) has no operator for fp32 memory_efficient_attention:
105
+ # FA3 / Cutlass cap at compute capability 9.0, FA2 only supports fp16/bf16. DINOv2
106
+ # (loaded via torch.hub) calls xops.memory_efficient_attention with fp32 → no
107
+ # dispatch. Route through torch SDPA, which handles sm_120 fp32 natively.
108
+ import xformers.ops as _xops
109
+ import torch.nn.functional as _F
110
+
111
+ def _mea_via_sdpa(q, k, v, attn_bias=None, p=0.0, scale=None, op=None):
112
+ q = q.transpose(1, 2); k = k.transpose(1, 2); v = v.transpose(1, 2)
113
+ attn_mask = None
114
+ if attn_bias is not None:
115
+ if hasattr(attn_bias, "materialize"):
116
+ try:
117
+ attn_mask = attn_bias.materialize((q.shape[-2], k.shape[-2]), device=q.device, dtype=q.dtype)
118
+ except Exception:
119
+ attn_mask = None
120
+ elif isinstance(attn_bias, _torch.Tensor):
121
+ attn_mask = attn_bias
122
+ out = _F.scaled_dot_product_attention(q, k, v, attn_mask=attn_mask, dropout_p=p, scale=scale)
123
+ return out.transpose(1, 2).contiguous()
124
+
125
+ _xops.memory_efficient_attention = _mea_via_sdpa
126
+
127
+ import shutil
128
  from huggingface_hub import hf_hub_download
129
 
130
  from app_utils import (
modules/bbox_gen/models/autogressive_bbox_gen.py CHANGED
@@ -105,7 +105,7 @@ class BboxGen(nn.Module):
105
  self.decoder: BBoxOPT = AutoModelForCausalLM.from_config(
106
  self.decoder_config,
107
  torch_dtype=torch.bfloat16,
108
- attn_implementation="flash_attention_2"
109
  )
110
  else:
111
  self.decoder: BBoxOPT = AutoModelForCausalLM.from_config(
 
105
  self.decoder: BBoxOPT = AutoModelForCausalLM.from_config(
106
  self.decoder_config,
107
  torch_dtype=torch.bfloat16,
108
+ attn_implementation="sdpa"
109
  )
110
  else:
111
  self.decoder: BBoxOPT = AutoModelForCausalLM.from_config(
requirements.txt CHANGED
@@ -1,7 +1,7 @@
1
- --extra-index-url https://download.pytorch.org/whl/cu121
2
-
3
- torch==2.4.0
4
- torchvision==0.19.0
5
  pillow==10.4.0
6
  imageio==2.36.1
7
  imageio-ffmpeg==0.5.1
@@ -17,10 +17,9 @@ pyvista==0.44.2
17
  pymeshfix==0.17.0
18
  igraph==0.11.8
19
  git+https://github.com/EasternJournalist/utils3d.git@9a4eb15e4021b67b12c460c7057d642626897ec8
20
- xformers==0.0.27.post2
21
  spconv-cu120==2.3.6
22
  transformers==4.50.3
23
- pydantic==2.10.6
24
  diffusers==0.32.0
25
  lightning==2.2
26
  mesh2sdf
@@ -35,10 +34,3 @@ timm
35
  h5py
36
  boto3
37
  git+https://github.com/facebookresearch/segment-anything.git
38
- git+https://github.com/facebookresearch/detectron2.git
39
- --find-links https://data.pyg.org/whl/torch-2.4.0+cu121.html
40
- torch-scatter
41
-
42
- https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.0.post2/flash_attn-2.7.0.post2+cu12torch2.4cxx11abiFALSE-cp310-cp310-linux_x86_64.whl
43
- https://huggingface.co/spaces/JeffreyXiang/TRELLIS/resolve/main/wheels/diff_gaussian_rasterization-0.0.0-cp310-cp310-linux_x86_64.whl?download=true
44
- https://huggingface.co/spaces/JeffreyXiang/TRELLIS/resolve/main/wheels/nvdiffrast-0.3.3-cp310-cp310-linux_x86_64.whl?download=true
 
1
+ torch==2.10.0
2
+ torchvision==0.25.0
3
+ einops
4
+ psutil
5
  pillow==10.4.0
6
  imageio==2.36.1
7
  imageio-ffmpeg==0.5.1
 
17
  pymeshfix==0.17.0
18
  igraph==0.11.8
19
  git+https://github.com/EasternJournalist/utils3d.git@9a4eb15e4021b67b12c460c7057d642626897ec8
20
+ xformers
21
  spconv-cu120==2.3.6
22
  transformers==4.50.3
 
23
  diffusers==0.32.0
24
  lightning==2.2
25
  mesh2sdf
 
34
  h5py
35
  boto3
36
  git+https://github.com/facebookresearch/segment-anything.git