This commit is contained in:
2026-05-15 00:46:37 +08:00
parent b4c7697e8a
commit f64e624a39
8 changed files with 190 additions and 100 deletions
+20 -17
View File
@@ -78,10 +78,8 @@ def _enable_memory_opts(pipe, device: str) -> None:
pipe.enable_vae_tiling()
except Exception:
pass
try:
pipe.enable_model_cpu_offload()
except Exception:
pass
# 不使用 enable_model_cpu_offload:与 transformers 5.x 下 CLIPTextModel 等子模块的 accelerate hook
# 组合易触发 `'_hf_hook'` AttributeError;整管已 .to(cuda) 时以 slicing/tiling 省显存即可。
def _align_size(orig_w: int, orig_h: int, max_side: int) -> tuple[int, int]:
@@ -123,8 +121,7 @@ def _make_sdxl_inpaint_predictor(
use_safetensors=True,
).to(device)
# 省显存设置(尽量不改变输出语义
# 注意:CPU offload 会明显变慢,但能显著降低显存占用。
# 省显存attention slicing / VAE slicing tiling(不再 enable_model_cpu_offload,见 _enable_memory_opts 注释
if device == "cuda":
try:
pipe.enable_attention_slicing()
@@ -138,10 +135,6 @@ def _make_sdxl_inpaint_predictor(
pipe.enable_vae_tiling()
except Exception:
pass
try:
pipe.enable_model_cpu_offload()
except Exception:
pass
def _predict(
image: Image.Image,
@@ -244,10 +237,7 @@ def _make_controlnet_predictor(_: UnifiedInpaintConfig):
pipe.enable_vae_tiling()
except Exception:
pass
try:
pipe.enable_model_cpu_offload()
except Exception:
pipe.to(device)
pipe.to(device)
else:
pipe.to(device)
@@ -361,9 +351,22 @@ def _make_lama_predictor(cfg: UnifiedInpaintConfig):
max_side: int = 1024,
**_kwargs,
) -> Image.Image:
# big-lama:仅 (RGB, mask);无文本条件。
_ = (strength, guidance_scale, num_inference_steps, max_side)
return lama(image, mask)
# big-lama:仅 (RGB, mask);无文本条件。与 SDXL/FLUX 一致:可按 max_side 降采样推理后再还原到输入分辨率。
_ = (strength, guidance_scale, num_inference_steps)
image = image.convert("RGB")
mask = mask.convert("L")
orig_w, orig_h = image.size
run_w, run_h = _align_size(orig_w, orig_h, max_side=max_side)
if (run_w, run_h) != (orig_w, orig_h):
image_run = image.resize((run_w, run_h), Image.BICUBIC)
mask_run = mask.resize((run_w, run_h), Image.NEAREST)
else:
image_run = image
mask_run = mask
out = lama(image_run, mask_run)
if out.size != (orig_w, orig_h):
out = out.resize((orig_w, orig_h), resample=Image.BICUBIC)
return out
return _predict