daKhosa commited on
Commit
41f5b1e
·
1 Parent(s): 1bd58d5

Avoid xformers Dynamo import in LTX app

Browse files
Files changed (1) hide show
  1. app.py +39 -62
app.py CHANGED
@@ -8,9 +8,6 @@ import sys
8
  import tempfile
9
  from pathlib import Path
10
 
11
- os.environ["TORCH_COMPILE_DISABLE"] = "1"
12
- os.environ["TORCHDYNAMO_DISABLE"] = "1"
13
-
14
  import gradio as gr
15
  import numpy as np
16
  import spaces
@@ -43,8 +40,6 @@ def _run(cmd, cwd=None, check=True):
43
 
44
 
45
  def install_ltx_runtime():
46
- _run([sys.executable, "-m", "pip", "install", "xformers==0.0.32.post2", "--no-build-isolation"], check=False)
47
-
48
  if not (LTX_REPO_DIR / ".git").exists():
49
  LTX_REPO_DIR.parent.mkdir(parents=True, exist_ok=True)
50
  if LTX_REPO_DIR.exists():
@@ -86,24 +81,6 @@ from ltx_pipelines.utils.args import ImageConditioningInput
86
  from ltx_pipelines.utils.media_io import encode_video
87
 
88
 
89
- try:
90
- from ltx_core.model.transformer import attention as _attn_mod
91
- from xformers.ops import memory_efficient_attention as _memory_efficient_attention
92
-
93
- _attn_mod.memory_efficient_attention = _memory_efficient_attention
94
- print("[ATTN] xformers memory_efficient_attention enabled")
95
- except Exception as exc:
96
- print(f"[ATTN] xformers patch failed: {type(exc).__name__}: {exc}")
97
-
98
- try:
99
- from xformers.ops.fmha import _set_use_fa3
100
-
101
- _set_use_fa3(False)
102
- print("[ATTN] xformers FA3 dispatch disabled")
103
- except Exception as exc:
104
- print(f"[ATTN] FA3 disable failed: {type(exc).__name__}: {exc}")
105
-
106
-
107
  _SAFETENSORS_DTYPE_MAP = {
108
  "F64": torch.float64,
109
  "F32": torch.float32,
@@ -282,7 +259,6 @@ def update_resolution(image, high_res):
282
 
283
 
284
  @spaces.GPU
285
- @torch.inference_mode()
286
  def generate_video(
287
  input_image,
288
  prompt,
@@ -296,45 +272,46 @@ def generate_video(
296
  ):
297
  current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
298
  try:
299
- if torch.cuda.is_available():
300
- torch.cuda.reset_peak_memory_stats()
301
- log_memory("start")
302
-
303
- frame_rate = DEFAULT_FRAME_RATE
304
- num_frames = int(float(duration) * frame_rate) + 1
305
- num_frames = ((num_frames - 1 + 7) // 8) * 8 + 1
306
- height = int(height)
307
- width = int(width)
308
-
309
- print(f"[generate] {width}x{height}, frames={num_frames}, seed={current_seed}")
310
-
311
- images = []
312
- if input_image is not None:
313
- OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
314
- input_path = OUTPUT_DIR / f"input_{current_seed}.png"
315
- input_image.save(input_path)
316
- images = [ImageConditioningInput(path=str(input_path), frame_idx=0, strength=1.0)]
317
-
318
- tiling_config = TilingConfig.default()
319
- chunks = get_video_chunks_number(num_frames, tiling_config)
320
- log_memory("before pipeline")
321
-
322
- video, audio = pipeline(
323
- prompt=prompt,
324
- seed=current_seed,
325
- height=height,
326
- width=width,
327
- num_frames=num_frames,
328
- frame_rate=frame_rate,
329
- images=images,
330
- tiling_config=tiling_config,
331
- enhance_prompt=bool(enhance_prompt),
332
- )
 
333
 
334
- output_path = tempfile.mktemp(suffix=".mp4")
335
- encode_video(video=video, fps=frame_rate, audio=audio, output_path=output_path, video_chunks_number=chunks)
336
- log_memory("after encode")
337
- return output_path, current_seed
338
  except Exception as exc:
339
  import traceback
340
 
 
8
  import tempfile
9
  from pathlib import Path
10
 
 
 
 
11
  import gradio as gr
12
  import numpy as np
13
  import spaces
 
40
 
41
 
42
  def install_ltx_runtime():
 
 
43
  if not (LTX_REPO_DIR / ".git").exists():
44
  LTX_REPO_DIR.parent.mkdir(parents=True, exist_ok=True)
45
  if LTX_REPO_DIR.exists():
 
81
  from ltx_pipelines.utils.media_io import encode_video
82
 
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  _SAFETENSORS_DTYPE_MAP = {
85
  "F64": torch.float64,
86
  "F32": torch.float32,
 
259
 
260
 
261
  @spaces.GPU
 
262
  def generate_video(
263
  input_image,
264
  prompt,
 
272
  ):
273
  current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
274
  try:
275
+ with torch.no_grad():
276
+ if torch.cuda.is_available():
277
+ torch.cuda.reset_peak_memory_stats()
278
+ log_memory("start")
279
+
280
+ frame_rate = DEFAULT_FRAME_RATE
281
+ num_frames = int(float(duration) * frame_rate) + 1
282
+ num_frames = ((num_frames - 1 + 7) // 8) * 8 + 1
283
+ height = int(height)
284
+ width = int(width)
285
+
286
+ print(f"[generate] {width}x{height}, frames={num_frames}, seed={current_seed}")
287
+
288
+ images = []
289
+ if input_image is not None:
290
+ OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
291
+ input_path = OUTPUT_DIR / f"input_{current_seed}.png"
292
+ input_image.save(input_path)
293
+ images = [ImageConditioningInput(path=str(input_path), frame_idx=0, strength=1.0)]
294
+
295
+ tiling_config = TilingConfig.default()
296
+ chunks = get_video_chunks_number(num_frames, tiling_config)
297
+ log_memory("before pipeline")
298
+
299
+ video, audio = pipeline(
300
+ prompt=prompt,
301
+ seed=current_seed,
302
+ height=height,
303
+ width=width,
304
+ num_frames=num_frames,
305
+ frame_rate=frame_rate,
306
+ images=images,
307
+ tiling_config=tiling_config,
308
+ enhance_prompt=bool(enhance_prompt),
309
+ )
310
 
311
+ output_path = tempfile.mktemp(suffix=".mp4")
312
+ encode_video(video=video, fps=frame_rate, audio=audio, output_path=output_path, video_chunks_number=chunks)
313
+ log_memory("after encode")
314
+ return output_path, current_seed
315
  except Exception as exc:
316
  import traceback
317