Files
hair/local_test/bench2.py
T
xslandCursor e7b62f2b2e perf(接口2): 稳定混跑耗时至12s内 —— ComfyUI插队 + CLIP挪CPU + 提示词全局统一
问题:接口2 与接口3/5 乱序调用时耗时抖动(最差 15~22s)。两个根因:
1. GPU 24G 常驻 21.4G,Flux-2(3.9G) 无法完全驻留显存,每次采样动态换页,
   速度随空闲显存波动(2s~8s);
2. ComfyUI 单队列 FIFO,接口2 排在接口3/5 批量任务后面。

改动:
- hairline/comfyui.py: run() 新增 front 参数,/prompt 带 "front": true 插队到队列最前;
  redraw.py 透传;service.py 接口2 三处调用(女重绘 + 男有/无遮罩)传 front=True,
  接口3/5 仍走普通队列。
- add_hair.json / 0716add-hair-api.json: 节点61 CLIPLoader device default→cpu。
  qwen CLIP(4G) 不再占显存(文本条件缓存常年命中),ComfyUI 显存 8.8G→4.5G,
  Flux-2 完全驻留,采样稳定 ~3-5s。代价:换 prompt 后首次请求 CPU 编码 ~11s(一次性)。
- 提示词全局统一为「填充遮罩区域的头发,皮肤加一点磨皮,再加一点美颜」:
  app.py 4处默认值、service.py _REDRAW_PROMPT、redraw.py _DEFAULT_PROMPT、
  4个工作流节点60内置文案、测试页(test_interface2/3/7/12/12_final)、local_test。
  任何两个不同 prompt 交替提交都会打爆 CLIP 编码缓存(--cache-classic 只存最近一次),
  之前测试页旧文案与服务端不一致导致交替测试每次 +11s。
- app.py: 接口7 /api/v1/hair/grow-v2 下线(业务弃用;add_hair2.json 的 Klein-9b
  会把常驻 Klein-4b 挤出显存)。保留 stub 返回 1007 明确报错,避免裸 404。

实测(1024 档):接口2女 8.5~10s、接口2男 ~5s、接口3 ~7-10s,交替混跑无尖刺。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-23 01:01:48 +08:00

70 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Steps sweep + resolution test on the working 9B fp8-fast config."""
import io, time
import requests
import numpy as np
from PIL import Image, ImageFilter
import app as A
COMFY = "http://127.0.0.1:8188"
MODEL = "flux2.0/flux-2-klein-9b-fp8.safetensors"
DTYPE = "fp8_e4m3fn_fast"
def upload(scale=1.0):
image = Image.open("用来重绘.jpg").convert("RGB")
mask_img = Image.open("用来重绘.png").convert("RGBA")
if scale != 1.0:
w, h = image.size
w, h = int(w * scale) // 8 * 8, int(h * scale) // 8 * 8
image = image.resize((w, h), Image.LANCZOS)
mask_data = np.max(np.array(mask_img), axis=2)
m = Image.fromarray(mask_data, mode="L")
if m.size != image.size:
m = m.resize(image.size, Image.LANCZOS)
m = m.filter(ImageFilter.GaussianBlur(radius=4))
alpha = Image.eval(m, lambda x: 255 - x)
r, g, b = image.split()
rgba = Image.merge("RGBA", (r, g, b, alpha))
buf = io.BytesIO(); rgba.save(buf, format="PNG"); buf.seek(0)
up = requests.post(f"{COMFY}/upload/image",
files={"image": ("hair_input.png", buf, "image/png")}).json()
return up["name"], image.size
def run(fname, steps):
wf = A.build_workflow(fname, "填充遮罩区域的头发,皮肤加一点磨皮,再加一点美颜")
wf["16"]["inputs"]["unet_name"] = MODEL
wf["16"]["inputs"]["weight_dtype"] = DTYPE
wf["1"]["inputs"]["steps"] = steps
pid = requests.post(f"{COMFY}/prompt", json={"prompt": wf}).json()["prompt_id"]
dl = time.time() + 120
while time.time() < dl:
time.sleep(0.1)
h = requests.get(f"{COMFY}/history/{pid}").json()
if pid in h and "17" in h[pid].get("outputs", {}):
ts = {m[0]: m[1].get("timestamp") for m in h[pid]["status"]["messages"]}
return (ts["execution_success"] - ts["execution_start"]) / 1000.0
return float("nan")
print("=== 步数扫描 (9B fp8-fast, 原分辨率 1024x775) ===", flush=True)
fname, sz = upload(1.0)
run(fname, 6) # warmup
res = {}
for s in [2, 3, 4, 6, 8]:
t = min(run(fname, s), run(fname, s))
res[s] = t
print(f" steps={s}: {t:.2f}s", flush=True)
# derive per-step cost & fixed overhead via two points
per = (res[8] - res[2]) / 6
fixed = res[2] - per * 2
print(f" -> 每步 ~{per:.3f}s, 固定开销(VAE/编码/colormatch/加载) ~{fixed:.2f}s", flush=True)
print("\n=== 分辨率影响 (steps=4) ===", flush=True)
for scale in [1.0, 0.75, 0.6]:
fn, s2 = upload(scale)
run(fn, 4) # warmup
t = min(run(fn, 4), run(fn, 4))
print(f" {s2[0]}x{s2[1]} ({s2[0]*s2[1]/1e6:.2f}MP): {t:.2f}s", flush=True)