bann commited on
Commit
97a97db
·
1 Parent(s): 109c1c7

perf(zerogpu): pre-load model pipeline on CPU at startup and increase GPU timeout quota

Browse files
Files changed (1) hide show
  1. app.py +10 -12
app.py CHANGED
@@ -404,7 +404,7 @@ def resolve_single_lora(
404
  return None, "None", 0.0
405
 
406
 
407
- def load_pipeline(model_repo: str = DEFAULT_MODEL_REPO):
408
  global PIPE, CURRENT_MODEL_REPO, LOAD_ERROR, LOADED_IN
409
 
410
  if PIPE is not None and CURRENT_MODEL_REPO == model_repo:
@@ -441,7 +441,7 @@ def load_pipeline(model_repo: str = DEFAULT_MODEL_REPO):
441
  PIPE = pipe
442
  CURRENT_MODEL_REPO = model_repo
443
  LOADED_IN = time.time() - started
444
- print(f"[wan] ready in {LOADED_IN:.0f}s", flush=True)
445
  except Exception as error:
446
  traceback.print_exc()
447
  LOAD_ERROR = f"**Loading `{model_repo}` failed**: `{type(error).__name__}: {error}`"
@@ -477,17 +477,10 @@ def _fit_keyframe(image_input, target_width: int, target_height: int) -> Image.I
477
 
478
  def get_duration(*args, **kwargs):
479
  try:
480
- # _generate_video_gpu receives: (prompt, negative_prompt, image_input, height, width, num_frames, steps, ...)
481
- height = kwargs.get("height", args[3] if len(args) > 3 else 480)
482
- width = kwargs.get("width", args[4] if len(args) > 4 else 832)
483
- num_frames = kwargs.get("num_frames", args[5] if len(args) > 5 else 81)
484
  steps = kwargs.get("steps", args[6] if len(args) > 6 else 20)
485
- pixels_per_frame = int(height) * int(width)
486
- total_tokens = (pixels_per_frame / 256) * (int(num_frames) / 4)
487
- estimated = int(int(steps) * (total_tokens * 0.00012) + 20)
488
- return max(60, min(MAX_GPU_DURATION, estimated))
489
  except Exception:
490
- return 180
491
 
492
 
493
  @spaces.GPU(duration=get_duration, size=GPU_SIZE)
@@ -504,7 +497,7 @@ def _generate_video_gpu(
504
  lora_configs: list[tuple[str, float]],
505
  model_repo: str,
506
  ):
507
- pipe = load_pipeline(model_repo)
508
  pipe.to("cuda")
509
 
510
  active_lora_names = []
@@ -1059,4 +1052,9 @@ with gr.Blocks(css=custom_css, title="Wan 2.2 Video Studio") as app:
1059
  )
1060
 
1061
  if __name__ == "__main__":
 
 
 
 
 
1062
  app.launch(allowed_paths=[OUTPUT_DIR])
 
404
  return None, "None", 0.0
405
 
406
 
407
+ def get_or_load_pipeline(model_repo: str = DEFAULT_MODEL_REPO):
408
  global PIPE, CURRENT_MODEL_REPO, LOAD_ERROR, LOADED_IN
409
 
410
  if PIPE is not None and CURRENT_MODEL_REPO == model_repo:
 
441
  PIPE = pipe
442
  CURRENT_MODEL_REPO = model_repo
443
  LOADED_IN = time.time() - started
444
+ print(f"[wan] ready in {LOADED_IN:.0f}s on CPU", flush=True)
445
  except Exception as error:
446
  traceback.print_exc()
447
  LOAD_ERROR = f"**Loading `{model_repo}` failed**: `{type(error).__name__}: {error}`"
 
477
 
478
  def get_duration(*args, **kwargs):
479
  try:
 
 
 
 
480
  steps = kwargs.get("steps", args[6] if len(args) > 6 else 20)
481
+ return max(180, min(MAX_GPU_DURATION, int(steps) * 8 + 60))
 
 
 
482
  except Exception:
483
+ return 240
484
 
485
 
486
  @spaces.GPU(duration=get_duration, size=GPU_SIZE)
 
497
  lora_configs: list[tuple[str, float]],
498
  model_repo: str,
499
  ):
500
+ pipe = get_or_load_pipeline(model_repo)
501
  pipe.to("cuda")
502
 
503
  active_lora_names = []
 
1052
  )
1053
 
1054
  if __name__ == "__main__":
1055
+ try:
1056
+ print("[wan] pre-initializing default pipeline on CPU...", flush=True)
1057
+ get_or_load_pipeline(DEFAULT_MODEL_REPO)
1058
+ except Exception as err:
1059
+ print(f"[wan] initial preload deferred to first call: {err}", flush=True)
1060
  app.launch(allowed_paths=[OUTPUT_DIR])