Unload latent upscale model on OOM

This commit is contained in:
2026-09-03 20:30:58 +00:00
parent 9dc3c405a6
commit fbb5f799cd
+24 -8
View File
@@ -1,4 +1,5 @@
from functools import lru_cache from functools import lru_cache
import gc
import glob import glob
import math import math
import os import os
@@ -489,15 +490,21 @@ def _upscale_video_model_core(video, param):
model = load_upscale_model(model_name, dev, precision) model = load_upscale_model(model_name, dev, precision)
norm_mean, norm_std = _make_norm_tensors(dev, compute_dtype) norm_mean, norm_std = _make_norm_tensors(dev, compute_dtype)
with torch.inference_mode(): try:
s = s.sub(norm_mean).div(norm_std) with torch.inference_mode():
out = model(s, scale=eff, target_size=(t, h_out, w_out)) s = s.sub(norm_mean).div(norm_std)
del s out = model(s, scale=eff, target_size=(t, h_out, w_out))
out = out.mul(norm_std).add(norm_mean) del s
out = out.mul(norm_std).add(norm_mean)
out = out.to(device="cpu", dtype=orig_dtype) out = out.to(device="cpu", dtype=orig_dtype)
unload_upscale_model(model_name, dev, precision) return out, h_out, w_out
return out, h_out, w_out finally:
unload_upscale_model(model_name, dev, precision)
try:
gc.collect()
except Exception:
pass
def upscale_video_model(video, param): def upscale_video_model(video, param):
@@ -509,6 +516,15 @@ def upscale_video_model(video, param):
smaller = _shrink_model_tile_param(param) smaller = _shrink_model_tile_param(param)
if smaller is None: if smaller is None:
raise raise
try:
gc.collect()
except Exception:
pass
if torch.cuda.is_available():
try:
torch.cuda.empty_cache()
except Exception:
pass
return upscale_video_model(video, smaller) return upscale_video_model(video, smaller)