Merge origin/master: bf16 precision, chunked attention, seg-reuse, mimalloc purge

Adopts master as the standard for all overlapping work. Master's landed
optimizations supersede server-edition's own fp16 experiment:

- Unified --precision fp32|bf16 knob (bgfilter/precision.py) driving all
  three models: ViTMatte weight cast + both BiRefNets via autocast, with a
  hardware gate (falls back to fp32 off native-bf16 CPUs) and an
  AutocastCPU fp32 shim for torchvision deform_conv2d.
- Query-chunked ViTMatte global attention (bgfilter/attn_chunk.py), exact
  and bitwise-identical, caps the N^2 spike (~19 -> ~4 GB at 2048).
- Cross-check HR-matting forward reused as the seg mask (birefnet backend
  only), skipping the primary seg model; MIMALLOC_PURGE_DELAY=0.
- inference_mode and the detect_background_color removal converge with
  server-edition's earlier equivalents.

Conflict resolution (favoring master, preserving server-only features):
- vitmatte_infer/segmentation: dropped server's device-derived fp16 for
  master's precision path, kept resolve_model_source (local weights).
- service.py: cross-check SegmentationSettings now passes precision so the
  HTTP service honors bf16 like the CLI's _second_opinion does.

Verified on CPU: default fp32 pipeline loads 2 models (seg-reuse active)
and bf16 path runs (autocast + deform_conv2d shim) — both exit 0.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-06 16:02:44 +08:00
14 changed files with 316 additions and 24 deletions
+1
View File
@@ -11,6 +11,7 @@
- **两条管线**(`segmentation.enabled` 切换):无分割模型的 Chroma + ViTMatte;或单个语义分割模型(默认 birefnet,可切 anime-seg,用 `--seg-backend`)定 trimap 拓扑,ViTMatte 细化。
- **背景色泛化**:`screen_color` 先验(如 `#CFEFFF`);`null` 时自动从图像边框探测平背景色(探测不到干净平背景会报错),不再写死绿幕。
- **已移除**:染绿修复(recolor)、发丝补救(dropout-fill)、BiRefNet+anime-seg 结合方案——它们只是给“生图即染色/断裂”的坏素材擦屁股;源头改用干净背景生图后不再需要。
- **新增(2026-07)**:交叉模型否决(`cross_check`,BiRefNet_HR-matting 第二意见 min 融合,清发丝缝隙残蓝,见 `docs/hair_gap_artifacts.md`);其前向默认兼任 seg mask(`reuse_as_seg`,净模型数回到 2);`--precision bf16` 一个旋钮加速全部三类模型;ViTMatte 全局 attention 默认按 query 分块(输出逐位不变,2048² 内存峰值 25→11 GB)。
下文其余章节描述绿幕基线流程,仍然有效。
+8
View File
@@ -89,6 +89,14 @@ min 融合;B2 的意见锐化(remap 0.5→0.95)被否——数字更低(0.673 vs
**已落地(2026-07-04,默认开启)。** 新增 `cross_check` 设置块 +
`--cross-check/--no-cross-check`;pipeline 在 clean_alpha 后插入
`cross_check_alpha`(alpha_post.py),第二意见加载复用 BiRefNetSegmenter。
**成本已消化(2026-07-06)。** 当年"+1 个模型 +1 次推理"的账已改写:
`cross_check.reuse_as_seg`(默认开)让 HR-matting 前向兼任 seg mask,
**净模型数回到 2 个**(trimap 99.8% 不变,先导 Outputs/segreuse/);
`--precision bf16` 经 autocast + deform_conv2d shim 覆盖 BiRefNet
(35s→26s @2048);ViTMatte 全局 attention 分块(逐位等价)把内存峰值
从 25.2GB 压到 11.4GB(2048×2048 bf16,9700X)。CPU 上开 cross-check
的常规图约 33s/张(bf16)。
验证:开启时 TestImage3 输出与评审原型逐字节一致;`--no-cross-check` 与
旧 baseline 逐字节一致;FixImage1(粉底)脸部无损、发缘粉残留被清,
唯一副作用是羽化权重外溢到暗豁免区边缘(弓弦零星像素最多 -0.5,中位