StaticExposure commited on
Commit
38bfc8d
·
verified ·
1 Parent(s): 32011ef

Prefetch + cache remapped style LoRA outside the GPU window; raise GPU duration to 120s

Browse files
Files changed (1) hide show
  1. app.py +34 -19
app.py CHANGED
@@ -159,7 +159,7 @@ def handle_speed_mode(speed_mode):
159
  else:
160
  return gr.update(value="Quality mode selected - 45 steps for best quality"), 45, 3.5
161
 
162
- @spaces.GPU(duration=70)
163
  def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, negative_prompt=""):
164
  pipe.to("cuda")
165
  generator = torch.Generator(device="cuda").manual_seed(seed)
@@ -178,31 +178,42 @@ def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scal
178
 
179
  return image
180
 
181
- @spaces.GPU(duration=70)
182
 
183
- def _load_style_lora(pipe, lora_path, weight_name=None):
184
- """Load a style LoRA, converting ai-toolkit / ComfyUI key names to diffusers names."""
 
 
185
  import os
186
  from safetensors.torch import load_file
187
  from huggingface_hub import hf_hub_download, list_repo_files
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  try:
189
- if os.path.isfile(str(lora_path)):
190
- local = str(lora_path)
191
- else:
192
- wn = weight_name
193
- if not wn:
194
- cands = [f for f in list_repo_files(lora_path) if f.endswith(".safetensors")]
195
- if not cands:
196
- raise FileNotFoundError("no .safetensors in " + str(lora_path))
197
- wn = cands[0]
198
- local = hf_hub_download(lora_path, wn)
199
- sd = load_file(local)
200
- if any(k.startswith("diffusion_model.") for k in sd):
201
- sd = {("transformer." + k[len("diffusion_model."):]): v for k, v in sd.items()}
202
- print("[patch] remapped diffusion_model.* -> transformer.* for", lora_path)
203
  pipe.load_lora_weights(sd, adapter_name="style")
204
  except Exception as e:
205
- print("[patch] remap loader failed (" + repr(e) + "), falling back to default loader")
206
  pipe.load_lora_weights(lora_path, weight_name=weight_name, low_cpu_mem_usage=True, adapter_name="style")
207
 
208
  def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, aspect_ratio, lora_scale, speed_mode, progress=gr.Progress(track_tqdm=True)):
@@ -351,6 +362,10 @@ def add_custom_lora(custom_lora):
351
  if custom_lora:
352
  try:
353
  title, repo, path, trigger_word, image = check_custom_model(custom_lora)
 
 
 
 
354
  print(f"Loaded custom LoRA: {repo}")
355
  card = f'''
356
  <div class="custom_lora_card">
 
159
  else:
160
  return gr.update(value="Quality mode selected - 45 steps for best quality"), 45, 3.5
161
 
162
+ @spaces.GPU(duration=120)
163
  def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, negative_prompt=""):
164
  pipe.to("cuda")
165
  generator = torch.Generator(device="cuda").manual_seed(seed)
 
178
 
179
  return image
180
 
181
+ @spaces.GPU(duration=120)
182
 
183
+ _STYLE_SD_CACHE = {}
184
+
185
+ def _prefetch_style_lora(lora_path, weight_name=None):
186
+ """Download + key-remap a style LoRA OUTSIDE the GPU window, cache the state dict."""
187
  import os
188
  from safetensors.torch import load_file
189
  from huggingface_hub import hf_hub_download, list_repo_files
190
+ key = (str(lora_path), str(weight_name))
191
+ if key in _STYLE_SD_CACHE:
192
+ return _STYLE_SD_CACHE[key]
193
+ if os.path.isfile(str(lora_path)):
194
+ local = str(lora_path)
195
+ else:
196
+ wn = weight_name
197
+ if not wn:
198
+ cands = [f for f in list_repo_files(lora_path) if f.endswith(".safetensors")]
199
+ if not cands:
200
+ raise FileNotFoundError("no .safetensors in " + str(lora_path))
201
+ wn = cands[0]
202
+ local = hf_hub_download(lora_path, wn)
203
+ sd = load_file(local)
204
+ if any(k.startswith("diffusion_model.") for k in sd):
205
+ sd = {("transformer." + k[len("diffusion_model."):]): v for k, v in sd.items()}
206
+ print("[patch] remapped diffusion_model.* -> transformer.* for", lora_path)
207
+ _STYLE_SD_CACHE[key] = sd
208
+ print("[patch] prefetched style LoRA", lora_path, "tensors:", len(sd))
209
+ return sd
210
+
211
+ def _load_style_lora(pipe, lora_path, weight_name=None):
212
  try:
213
+ sd = _prefetch_style_lora(lora_path, weight_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  pipe.load_lora_weights(sd, adapter_name="style")
215
  except Exception as e:
216
+ print("[patch] remap loader failed (" + repr(e) + "), falling back")
217
  pipe.load_lora_weights(lora_path, weight_name=weight_name, low_cpu_mem_usage=True, adapter_name="style")
218
 
219
  def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, aspect_ratio, lora_scale, speed_mode, progress=gr.Progress(track_tqdm=True)):
 
362
  if custom_lora:
363
  try:
364
  title, repo, path, trigger_word, image = check_custom_model(custom_lora)
365
+ try:
366
+ _prefetch_style_lora(repo, path)
367
+ except Exception as _e:
368
+ print("[patch] prefetch failed:", repr(_e))
369
  print(f"Loaded custom LoRA: {repo}")
370
  card = f'''
371
  <div class="custom_lora_card">