CocoBro commited on
Commit
43bf2c1
·
1 Parent(s): caea508

fix load gpu

Browse files
Files changed (1) hide show
  1. app.py +16 -3
app.py CHANGED
@@ -174,7 +174,10 @@ def patch_paths_in_exp_config(exp_cfg: Dict[str, Any], repo_root: Path, qwen_roo
174
  # 学长说的就是这里:所有费资源的操作(加载+推理)都要放在这里面
175
  @spaces.GPU
176
  def run_edit(audio_file, caption, num_steps, guidance_scale, guidance_rescale, seed):
177
- # 延迟导入,防止全局污染
 
 
 
178
  import torch
179
  import hydra
180
  from omegaconf import OmegaConf
@@ -230,8 +233,18 @@ def run_edit(audio_file, caption, num_steps, guidance_scale, guidance_rescale, s
230
  logger.info("Moving model to CUDA (FP16)...")
231
 
232
  # 这一步将模型送入显卡
233
- model = model.to(device, dtype=torch.float16).eval()
234
-
 
 
 
 
 
 
 
 
 
 
235
  # Scheduler
236
  try:
237
  scheduler = noise_schedulers.DDIMScheduler.from_pretrained(
 
174
  # 学长说的就是这里:所有费资源的操作(加载+推理)都要放在这里面
175
  @spaces.GPU
176
  def run_edit(audio_file, caption, num_steps, guidance_scale, guidance_rescale, seed):
177
+
178
+
179
+
180
+
181
  import torch
182
  import hydra
183
  from omegaconf import OmegaConf
 
233
  logger.info("Moving model to CUDA (FP16)...")
234
 
235
  # 这一步将模型送入显卡
236
+ def safe_move_model(m, dev):
237
+ logger.info("🛡️ Moving model layer by layer to avoid RAM spike...")
238
+ for name, child in m.named_children():
239
+ # 逐层搬运:CPU内存释放一点 -> GPU显存增加一点
240
+ child.to(dev, dtype=torch.float16)
241
+ logger.info(f"Moving {name} to GPU...")
242
+ m.to(dev, dtype=torch.float16)
243
+ return m
244
+
245
+ model = safe_move_model(model, device)
246
+ model.eval()
247
+ logger.info("Model is moved to CUDA.")
248
  # Scheduler
249
  try:
250
  scheduler = noise_schedulers.DDIMScheduler.from_pretrained(