fffiloni commited on
Commit
c603a37
·
verified ·
1 Parent(s): 70eda8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -16
app.py CHANGED
@@ -82,6 +82,7 @@ import torch
82
  # Krea remote code compiles torch.nn.attention.flex_attention.
83
  # We no-op torch.compile globally for this compatibility Space.
84
  _ORIG_TORCH_COMPILE = getattr(torch, "compile", None)
 
85
 
86
 
87
  def _asf_zerogpu_compile_bypass(fn=None, *args, **kwargs):
@@ -92,20 +93,32 @@ def _asf_zerogpu_compile_bypass(fn=None, *args, **kwargs):
92
  torch.compile(fn, ...)
93
  @torch.compile(...)
94
  def fn(...): ...
 
 
 
95
  """
 
 
96
  if fn is None:
97
  def decorator(real_fn):
98
  return _asf_zerogpu_compile_bypass(real_fn, *args, **kwargs)
99
 
100
  return decorator
101
 
102
- name = getattr(fn, "__name__", repr(fn))
103
- module = getattr(fn, "__module__", "")
 
 
 
 
 
 
 
 
 
 
 
104
 
105
- print(
106
- f"[ASF] ZeroGPU compatibility: bypassing torch.compile for {module}.{name}",
107
- flush=True,
108
- )
109
  return fn
110
 
111
 
@@ -328,6 +341,7 @@ def health():
328
  }
329
 
330
 
 
331
  def warmup_model():
332
  """
333
  Manual warm-up button.
@@ -365,18 +379,25 @@ def _gpu_duration(prompt, num_blocks, num_inference_steps, seed):
365
  steps = 4
366
 
367
  # Model is loaded at app startup.
368
- # Duration only covers generation.
369
- # Aggressive ZeroGPU reservation:
370
- # 1x4 -> 30s
371
- # 3x4 -> 51s
372
- # 6x4 -> 87s
373
- # 9x4 -> 123s
374
- # 9x8 -> 180s cap
375
- return min(180, max(30, 15 + blocks * steps * 3))
 
376
 
377
 
378
  @_spaces_gpu(duration=_gpu_duration, size="xlarge")
379
- def generate(prompt, num_blocks, num_inference_steps, seed):
 
 
 
 
 
 
380
  pipe = _load_pipeline()
381
 
382
  if pipe is None:
@@ -414,9 +435,19 @@ def generate(prompt, num_blocks, num_inference_steps, seed):
414
  generator = torch.Generator(device="cpu").manual_seed(seed)
415
 
416
  try:
417
- for block_idx in range(num_blocks):
 
 
 
 
 
418
  _log(f"Block {block_idx + 1}/{num_blocks}")
419
 
 
 
 
 
 
420
  state = pipe(
421
  state,
422
  prompt=[prompt],
@@ -442,10 +473,14 @@ def generate(prompt, num_blocks, num_inference_steps, seed):
442
  if not frames:
443
  raise RuntimeError("No frames were generated.")
444
 
 
 
445
  output_path = f"/tmp/krea_output_{int(time.time())}.mp4"
446
  export_to_video(frames, output_path, fps=24)
447
 
 
448
  _log(f"Saved video to {output_path}")
 
449
  return output_path
450
 
451
 
 
82
  # Krea remote code compiles torch.nn.attention.flex_attention.
83
  # We no-op torch.compile globally for this compatibility Space.
84
  _ORIG_TORCH_COMPILE = getattr(torch, "compile", None)
85
+ _ASF_COMPILE_BYPASS_ANNOUNCED = False
86
 
87
 
88
  def _asf_zerogpu_compile_bypass(fn=None, *args, **kwargs):
 
93
  torch.compile(fn, ...)
94
  @torch.compile(...)
95
  def fn(...): ...
96
+
97
+ Default behavior is quiet because FlexAttention may call this repeatedly
98
+ during generation.
99
  """
100
+ global _ASF_COMPILE_BYPASS_ANNOUNCED
101
+
102
  if fn is None:
103
  def decorator(real_fn):
104
  return _asf_zerogpu_compile_bypass(real_fn, *args, **kwargs)
105
 
106
  return decorator
107
 
108
+ if os.environ.get("ASF_VERBOSE_COMPILE_BYPASS", "0") == "1":
109
+ name = getattr(fn, "__name__", repr(fn))
110
+ module = getattr(fn, "__module__", "")
111
+ print(
112
+ f"[ASF] ZeroGPU compatibility: bypassing torch.compile for {module}.{name}",
113
+ flush=True,
114
+ )
115
+ elif not _ASF_COMPILE_BYPASS_ANNOUNCED:
116
+ print(
117
+ "[ASF] ZeroGPU compatibility: torch.compile bypass is active.",
118
+ flush=True,
119
+ )
120
+ _ASF_COMPILE_BYPASS_ANNOUNCED = True
121
 
 
 
 
 
122
  return fn
123
 
124
 
 
341
  }
342
 
343
 
344
+ @_spaces_gpu(duration=120, size="xlarge")
345
  def warmup_model():
346
  """
347
  Manual warm-up button.
 
379
  steps = 4
380
 
381
  # Model is loaded at app startup.
382
+ # Observed: 9 blocks × 4 steps ≈ <75s.
383
+ # Keep a small safety buffer without over-reserving ZeroGPU.
384
+ #
385
+ # 1×4 -> 30s
386
+ # 3×4 -> 44s
387
+ # 6×4 -> 68s
388
+ # 9×4 -> 92s
389
+ # 9×8 -> 150s cap
390
+ return min(150, max(30, int(20 + blocks * steps * 2)))
391
 
392
 
393
  @_spaces_gpu(duration=_gpu_duration, size="xlarge")
394
+ def generate(
395
+ prompt,
396
+ num_blocks,
397
+ num_inference_steps,
398
+ seed,
399
+ progress=gr.Progress(track_tqdm=True),
400
+ ):
401
  pipe = _load_pipeline()
402
 
403
  if pipe is None:
 
435
  generator = torch.Generator(device="cpu").manual_seed(seed)
436
 
437
  try:
438
+ progress(0, desc="Preparing generation")
439
+
440
+ for block_idx in progress.tqdm(
441
+ range(num_blocks),
442
+ desc="Generating video blocks",
443
+ ):
444
  _log(f"Block {block_idx + 1}/{num_blocks}")
445
 
446
+ progress(
447
+ block_idx / max(1, num_blocks),
448
+ desc=f"Generating block {block_idx + 1}/{num_blocks}",
449
+ )
450
+
451
  state = pipe(
452
  state,
453
  prompt=[prompt],
 
473
  if not frames:
474
  raise RuntimeError("No frames were generated.")
475
 
476
+ progress(0.95, desc="Exporting video")
477
+
478
  output_path = f"/tmp/krea_output_{int(time.time())}.mp4"
479
  export_to_video(frames, output_path, fps=24)
480
 
481
+ progress(1.0, desc="Done")
482
  _log(f"Saved video to {output_path}")
483
+
484
  return output_path
485
 
486