update
This commit is contained in:
@@ -51,7 +51,7 @@ class InpaintRequest(ImageInput):
|
||||
strength: float = Field(0.8, ge=0.0, le=1.0)
|
||||
negative_prompt: Optional[str] = Field("", description="负向 prompt")
|
||||
mask_b64: Optional[str] = Field(None, description="mask PNG base64(可选)")
|
||||
max_side: int = Field(1024, ge=128, le=2048)
|
||||
max_side: int = Field(1024, ge=128, le=8192, description="推理时长边上限;不超过则按输入全分辨率推理后再还原")
|
||||
guidance_scale: Optional[float] = Field(None, ge=0.0, le=200.0, description="扩散 CFG;不传则各后端默认")
|
||||
num_inference_steps: Optional[int] = Field(None, ge=1, le=150, description="采样步数;不传则各后端默认")
|
||||
|
||||
|
||||
@@ -63,13 +63,14 @@ def make_flux_fill_predictor(
|
||||
|
||||
if device == "cuda":
|
||||
try:
|
||||
pipe.enable_model_cpu_offload()
|
||||
pipe.enable_vae_tiling()
|
||||
except Exception:
|
||||
try:
|
||||
pipe.enable_vae_tiling()
|
||||
except Exception:
|
||||
pass
|
||||
pipe.to("cuda")
|
||||
pass
|
||||
try:
|
||||
pipe.enable_attention_slicing()
|
||||
except Exception:
|
||||
pass
|
||||
pipe.to("cuda")
|
||||
else:
|
||||
pipe.to(device)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user