cuio commited on
Commit
78cbf2f
·
verified ·
1 Parent(s): 7b2bc28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -23
app.py CHANGED
@@ -15,7 +15,7 @@
15
  """URSA TI2V application."""
16
 
17
  # =========================
18
- # ✅ 改 1:环境变量必须最先设置(早于 torch / spaces / diffnext)
19
  # =========================
20
  import os
21
 
@@ -25,11 +25,13 @@ if os.environ.get("HF_TOKEN"):
25
  os.environ["HF_HUB_ENABLE_HUGGINGFACE_TRANSFER"] = "1"
26
  os.environ["HF_HUB_DOWNLOAD_TIMEOUT"] = "300"
27
  os.environ["TOKENIZERS_PARALLELISM"] = "true"
28
- # 必须在 CUDA 初始化前设置,否则 expandable_segments 不生效
29
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
 
 
30
 
31
  # =========================
32
- # ✅ 改 2:import spaces 必须早于 torch(ZeroGPU 硬性要求)
33
  # =========================
34
  import spaces
35
 
@@ -41,14 +43,77 @@ import numpy as np
41
  import PIL.Image
42
  import torch
43
 
44
- from diffnext.pipelines import URSAPipeline
45
- from diffnext.utils import export_to_image, export_to_video
46
-
47
  # =========================
48
- # 🔧 修复 Qwen3Config 缺失 rope_theta
 
 
 
 
 
 
 
 
 
49
  # =========================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  # =========================
 
 
 
 
 
 
 
 
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  # =========================
53
  def _patch_diffnext_qwen3():
54
  try:
@@ -60,28 +125,20 @@ def _patch_diffnext_qwen3():
60
 
61
  @classmethod
62
  def patched_from_config(cls, config):
63
- # rope_theta
64
  if not hasattr(config, "rope_theta"):
65
  rp = getattr(config, "rope_parameters", None)
66
  if isinstance(rp, dict) and "rope_theta" in rp:
67
  config.rope_theta = rp["rope_theta"]
68
  else:
69
  config.rope_theta = float(1e6)
70
-
71
- # max_position_embeddings
72
  if not hasattr(config, "max_position_embeddings"):
73
- config.max_position_embeddings = int(
74
- getattr(config, "max_seq_len", 2048)
75
- )
76
-
77
- # ✅ 正确:只传 config,不要传 cls
78
  return orig_from_config(config)
79
 
80
  qwen3.Qwen3RotaryEmbedding.from_config = patched_from_config
81
 
82
 
83
  _patch_diffnext_qwen3()
84
- # =========================
85
 
86
 
87
  def parse_args():
@@ -107,8 +164,7 @@ def crop_image(image, target_h, target_w):
107
 
108
 
109
  # =========================
110
- # ✅ 改 3:模型加载放在【模块顶层】,不能放进 if __name__ == "__main__"
111
- # ZeroGPU 只在模块级启用 CUDA 模拟并打包权重
112
  # =========================
113
  args = parse_args()
114
 
@@ -122,9 +178,9 @@ pipe = URSAPipeline.from_pretrained(args.model, **model_args).to(device)
122
 
123
 
124
  # =========================
125
- # ✅ 改 4:给真正跑推理的函数加 @spaces.GPU
126
  # =========================
127
- @spaces.GPU(duration=60)
128
  def generate_image(
129
  prompt,
130
  negative_prompt,
@@ -141,7 +197,7 @@ def generate_image(
141
  return [export_to_image(image, quality=95) for image in images] + [seed]
142
 
143
 
144
- @spaces.GPU(duration=180)
145
  def generate_video(
146
  prompt,
147
  negative_prompt,
@@ -258,6 +314,6 @@ if __name__ == "__main__":
258
  ],
259
  outputs=[result, seed],
260
  )
261
- # ✅ 改 5:加 queue(default_concurrency_limit=1),避免多请求抢同一张卡
262
  app.__exit__(), app.queue(default_concurrency_limit=1).launch(
263
- share=False, ssr_mode=False, max_threads=1)
 
15
  """URSA TI2V application."""
16
 
17
  # =========================
18
+ # ✅ 第 1 步:环境变量必须最先设置(早于 torch / spaces / diffnext)
19
  # =========================
20
  import os
21
 
 
25
  os.environ["HF_HUB_ENABLE_HUGGINGFACE_TRANSFER"] = "1"
26
  os.environ["HF_HUB_DOWNLOAD_TIMEOUT"] = "300"
27
  os.environ["TOKENIZERS_PARALLELISM"] = "true"
28
+ # 必须在 CUDA 初始化 / torch 导入前设置
29
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
30
+ # 关掉 TorchDynamo(torch 在 import 时读取该环境变量)
31
+ os.environ["TORCH_COMPILE_DISABLE"] = "1"
32
 
33
  # =========================
34
+ # ✅ 第 2 步:import spaces 必须早于 torch(ZeroGPU 硬性要求)
35
  # =========================
36
  import spaces
37
 
 
43
  import PIL.Image
44
  import torch
45
 
 
 
 
46
  # =========================
47
+ # 🔧 第 3 步:把 torch.compile 变成恒等变换
48
+ # diffnext 在【模块导入时】就用 @torch.compile 装饰了 RoPE 实现:
49
+ # - FlexRotaryEmbedding.PEFunc.interleaved_impl / partitioned_impl
50
+ # - RotaryEmbed3D.PEFunc.call_impl
51
+ # - FlexAttn.get_attn_func() 运行期调用 torch.compile(flex_attention)
52
+ # 该编译是惰性的,直到首次真实推理(进入 @spaces.GPU、真卡挂载后)才真正触发,
53
+ # 此时 TorchInductor 用 CUDA event 给 Triton kernel 做 autotune 微基准,
54
+ # 在 ZeroGPU 的 fork worker 里 event 计时失效 →
55
+ # RuntimeError: Both events must be completed before calculating elapsed time
56
+ # ZeroGPU 官方明确不支持 torch.compile(每任务新起进程,JIT 无法复用)。
57
  # =========================
58
+ try:
59
+ torch._dynamo.config.disable = True
60
+ except Exception:
61
+ pass
62
+
63
+
64
+ def _eager_compile(model=None, **kwargs):
65
+ """@torch.compile(...) / torch.compile(model) 都直接返回原对象。"""
66
+ if model is not None:
67
+ return model
68
+ return lambda fn: fn
69
+
70
+
71
+ torch.compile = _eager_compile
72
+
73
+ from diffnext.pipelines import URSAPipeline # noqa: E402 必须在 patch 之后
74
+ from diffnext.utils import export_to_image, export_to_video # noqa: E402
75
+
76
+
77
+ # =========================
78
+ # 🔧 第 4 步:兜底 —— 直接把被编译的 RoPE 方法换成纯 eager 实现
79
+ # (即使第 3 步失效,这一步也能保证不会走 Inductor)
80
  # =========================
81
+ def _patch_rope_eager():
82
+ from diffnext.models import embeddings as emb
83
+
84
+ def interleaved_impl(self, x, w):
85
+ return w[..., 0].mul(x[..., 0]).add_(w[..., 1] * x[..., 1]).flatten(3)
86
+
87
+ def partitioned_impl(self, x, w):
88
+ return w[..., 0].mul(x[:, :, :, 0]).add_(w[..., 1] * x[:, :, :, 1]).flatten(3)
89
 
90
+ def call_impl_3d(self, x, w):
91
+ return w[..., 0].mul(x[..., 0]).add_(w[..., 1] * x[..., 1]).flatten(3)
92
+
93
+ patched = []
94
+ for cls_name, meth, fn in (
95
+ ("FlexRotaryEmbedding", "interleaved_impl", interleaved_impl),
96
+ ("FlexRotaryEmbedding", "partitioned_impl", partitioned_impl),
97
+ ("RotaryEmbed3D", "call_impl", call_impl_3d),
98
+ ):
99
+ cls = getattr(emb, cls_name, None)
100
+ if cls is not None and hasattr(cls, "PEFunc"):
101
+ setattr(cls.PEFunc, meth, fn)
102
+ patched.append(f"{cls_name}.PEFunc.{meth}")
103
+ return patched
104
+
105
+
106
+ try:
107
+ _ROPE_PATCHED = _patch_rope_eager()
108
+ except Exception as e: # 不因打补丁失败而阻塞启动
109
+ _ROPE_PATCHED = ["FAILED: %r" % (e,)]
110
+
111
+ print("[patch] torch.compile -> identity:", torch.compile is _eager_compile, flush=True)
112
+ print("[patch] rope eager:", _ROPE_PATCHED, flush=True)
113
+
114
+
115
+ # =========================
116
+ # 🔧 第 5 步:修复 Qwen3Config 缺失 rope_theta
117
  # =========================
118
  def _patch_diffnext_qwen3():
119
  try:
 
125
 
126
  @classmethod
127
  def patched_from_config(cls, config):
 
128
  if not hasattr(config, "rope_theta"):
129
  rp = getattr(config, "rope_parameters", None)
130
  if isinstance(rp, dict) and "rope_theta" in rp:
131
  config.rope_theta = rp["rope_theta"]
132
  else:
133
  config.rope_theta = float(1e6)
 
 
134
  if not hasattr(config, "max_position_embeddings"):
135
+ config.max_position_embeddings = int(getattr(config, "max_seq_len", 2048))
 
 
 
 
136
  return orig_from_config(config)
137
 
138
  qwen3.Qwen3RotaryEmbedding.from_config = patched_from_config
139
 
140
 
141
  _patch_diffnext_qwen3()
 
142
 
143
 
144
  def parse_args():
 
164
 
165
 
166
  # =========================
167
+ # ✅ 第 6 步:模型加载放在【模块顶层】(ZeroGPU 硬性要求)
 
168
  # =========================
169
  args = parse_args()
170
 
 
178
 
179
 
180
  # =========================
181
+ # ✅ 第 7 步:真正跑推理的函数加 @spaces.GPU
182
  # =========================
183
+ @spaces.GPU(duration=90)
184
  def generate_image(
185
  prompt,
186
  negative_prompt,
 
197
  return [export_to_image(image, quality=95) for image in images] + [seed]
198
 
199
 
200
+ @spaces.GPU(duration=300)
201
  def generate_video(
202
  prompt,
203
  negative_prompt,
 
314
  ],
315
  outputs=[result, seed],
316
  )
317
+ # 限制并发,避免多个请求抢同一张卡
318
  app.__exit__(), app.queue(default_concurrency_limit=1).launch(
319
+ share=False, ssr_mode=False, max_threads=1)