[Admin maintenance] Support new ZeroGPU hardware

#12
by multimodalart HF Staff - opened
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🌟
4
  colorFrom: purple
5
  colorTo: red
6
  sdk: gradio
7
- sdk_version: 5.0.1
8
  app_file: app.py
9
  pinned: false
10
  short_description: Video Super-Resolution with Text-to-Video Model
 
4
  colorFrom: purple
5
  colorTo: red
6
  sdk: gradio
7
+ sdk_version: 5.49.1
8
  app_file: app.py
9
  pinned: false
10
  short_description: Video Super-Resolution with Text-to-Video Model
app.py CHANGED
@@ -1,6 +1,18 @@
1
- import spaces
2
  import os
 
 
 
 
 
 
 
3
  import gradio as gr
 
 
 
 
 
 
4
  from video_super_resolution.scripts.inference_sr import STAR_sr
5
 
6
  # Example video and prompt pairs
@@ -11,7 +23,7 @@ examples = [
11
  ]
12
 
13
  # Define a GPU-decorated function for enhancement
14
- @spaces.GPU()
15
  def enhance_with_gpu(input_video, input_text, upscale, max_chunk_len, chunk_size):
16
  """在每次调用时创建新的 STAR_sr 实例,确保参数正确传递"""
17
  star = STAR_sr(
 
 
1
  import os
2
+ # Disable torch's NVML-based CUDA check — NVML isn't accessible in ZeroGPU
3
+ # sandbox and torch >=2.10 hits an INTERNAL ASSERT (CUDACachingAllocator
4
+ # NVML_SUCCESS == r) when the caching allocator tries to enumerate devices.
5
+ os.environ.setdefault("PYTORCH_NVML_BASED_CUDA_CHECK", "0")
6
+ os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "backend:cudaMallocAsync")
7
+
8
+ import spaces
9
  import gradio as gr
10
+
11
+ # Step 8: weights_only flipped to True in torch 2.6+, old checkpoint needs False.
12
+ import torch
13
+ _orig_load = torch.load
14
+ torch.load = lambda *a, **k: _orig_load(*a, **{**k, "weights_only": k.get("weights_only", False)})
15
+
16
  from video_super_resolution.scripts.inference_sr import STAR_sr
17
 
18
  # Example video and prompt pairs
 
23
  ]
24
 
25
  # Define a GPU-decorated function for enhancement
26
+ @spaces.GPU(duration=100)
27
  def enhance_with_gpu(input_video, input_text, upscale, max_chunk_len, chunk_size):
28
  """在每次调用时创建新的 STAR_sr 实例,确保参数正确传递"""
29
  star = STAR_sr(
requirements.txt CHANGED
@@ -1,16 +1,14 @@
1
- torch==2.0.1
2
- torchvision==0.15.2
3
- torchaudio==2.0.2
4
  opencv-python==4.10.0.84
5
  easydict==1.13
6
  einops==0.8.0
7
  open-clip-torch==2.20.0
8
- xformers==0.0.21
9
  fairscale==0.4.13
10
  torchsde==0.2.6
11
- pytorch-lightning==2.0.1
12
- diffusers==0.30.0
13
- huggingface_hub==0.23.3
14
- gradio==4.41.0
15
- numpy==1.24
16
- tqdm
 
 
 
 
 
 
1
  opencv-python==4.10.0.84
2
  easydict==1.13
3
  einops==0.8.0
4
  open-clip-torch==2.20.0
 
5
  fairscale==0.4.13
6
  torchsde==0.2.6
7
+ pytorch-lightning
8
+ diffusers>=0.30
9
+ huggingface_hub
10
+ gradio==5.49.1
11
+ numpy
12
+ tqdm
13
+ soundfile
14
+ requests
video_to_video/modules/unet_v2v.py CHANGED
@@ -7,8 +7,9 @@ from abc import abstractmethod
7
  import torch
8
  import torch.nn as nn
9
  import torch.nn.functional as F
10
- import xformers
11
- import xformers.ops
 
12
  from einops import rearrange
13
  from fairscale.nn.checkpoint import checkpoint_wrapper
14
  from timm.models.vision_transformer import Mlp
@@ -162,36 +163,19 @@ class MemoryEfficientCrossAttention(nn.Module):
162
  v = self.to_v(context)
163
 
164
  b, _, _ = q.shape
165
- q, k, v = map(
166
- lambda t: t.unsqueeze(3).reshape(b, t.shape[
167
- 1], self.heads, self.dim_head).permute(0, 2, 1, 3).reshape(
168
- b * self.heads, t.shape[1], self.dim_head).contiguous(),
169
- (q, k, v),
170
- )
171
-
172
- # actually compute the attention, what we cannot get enough of.
173
- if q.shape[0] > self.max_bs:
174
- q_list = torch.chunk(q, q.shape[0] // self.max_bs, dim=0)
175
- k_list = torch.chunk(k, k.shape[0] // self.max_bs, dim=0)
176
- v_list = torch.chunk(v, v.shape[0] // self.max_bs, dim=0)
177
- out_list = []
178
- for q_1, k_1, v_1 in zip(q_list, k_list, v_list):
179
- out = xformers.ops.memory_efficient_attention(
180
- q_1, k_1, v_1, attn_bias=None, op=self.attention_op)
181
- out_list.append(out)
182
- out = torch.cat(out_list, dim=0)
183
- else:
184
- out = xformers.ops.memory_efficient_attention(
185
- q, k, v, attn_bias=None, op=self.attention_op)
186
 
187
  if exists(mask):
188
  raise NotImplementedError
189
- out = (
190
- out.unsqueeze(0).reshape(
191
- b, self.heads, out.shape[1],
192
- self.dim_head).permute(0, 2, 1,
193
- 3).reshape(b, out.shape[1],
194
- self.heads * self.dim_head))
195
  return self.to_out(out)
196
 
197
 
 
7
  import torch
8
  import torch.nn as nn
9
  import torch.nn.functional as F
10
+ # xformers removed: prebuilt wheel has no kernel that runs on Blackwell sm_120.
11
+ # All memory_efficient_attention call sites in this file have been replaced
12
+ # with torch.nn.functional.scaled_dot_product_attention (fused FA backend).
13
  from einops import rearrange
14
  from fairscale.nn.checkpoint import checkpoint_wrapper
15
  from timm.models.vision_transformer import Mlp
 
163
  v = self.to_v(context)
164
 
165
  b, _, _ = q.shape
166
+ # Reshape to (B, H, M, K) for torch SDPA (Flash/Mem-efficient backends).
167
+ # xformers MEA prebuilt wheel has no kernel that runs on Blackwell sm_120;
168
+ # SDPA gives the same fused FA path natively in torch on this hardware.
169
+ q = q.reshape(b, q.shape[1], self.heads, self.dim_head).permute(0, 2, 1, 3)
170
+ k = k.reshape(b, k.shape[1], self.heads, self.dim_head).permute(0, 2, 1, 3)
171
+ v = v.reshape(b, v.shape[1], self.heads, self.dim_head).permute(0, 2, 1, 3)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
 
173
  if exists(mask):
174
  raise NotImplementedError
175
+
176
+ out = torch.nn.functional.scaled_dot_product_attention(q, k, v)
177
+ # (B, H, M, K) -> (B, M, H*K)
178
+ out = out.permute(0, 2, 1, 3).reshape(b, out.shape[2], self.heads * self.dim_head)
 
 
179
  return self.to_out(out)
180
 
181
 
video_to_video/video_to_video_model.py CHANGED
@@ -14,19 +14,25 @@ from video_to_video.diffusion.schedules_sdedit import noise_schedule
14
  from video_to_video.utils.logger import get_logger
15
 
16
  from diffusers import AutoencoderKLTemporalDecoder
17
- import requests
 
18
 
19
  def download_model(url, model_path):
20
- if not os.path.exists(os.path.join(model_path, 'model.pt')):
21
- print(f"Model not found at {model_path}, downloading...")
22
- response = requests.get(url, stream=True)
23
- with open(os.path.join(model_path, 'model.pt'), 'wb') as f:
24
- for chunk in response.iter_content(chunk_size=1024):
25
- if chunk:
26
- f.write(chunk)
27
- print(f"Model downloaded to {model_path}")
28
- else:
29
  print(f"Model found at {model_path}, skipping download.")
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
 
32
  logger = get_logger()
 
14
  from video_to_video.utils.logger import get_logger
15
 
16
  from diffusers import AutoencoderKLTemporalDecoder
17
+ from huggingface_hub import hf_hub_download
18
+ import shutil
19
 
20
  def download_model(url, model_path):
21
+ target = os.path.join(model_path, 'model.pt')
22
+ if os.path.exists(target) and os.path.getsize(target) > 1_000_000_000:
 
 
 
 
 
 
 
23
  print(f"Model found at {model_path}, skipping download.")
24
+ return
25
+ # Remove partial/corrupt file if present
26
+ if os.path.exists(target):
27
+ os.remove(target)
28
+ os.makedirs(model_path, exist_ok=True)
29
+ print(f"Model not found at {model_path}, downloading via hf_hub_download...")
30
+ cached = hf_hub_download(
31
+ repo_id="SherryX/STAR",
32
+ filename="I2VGen-XL-based/heavy_deg.pt",
33
+ )
34
+ shutil.copy(cached, target)
35
+ print(f"Model downloaded to {target}")
36
 
37
 
38
  logger = get_logger()