Skip to content

SUBJECT-GPU: the subject and scene AI masks run on the graphics cards - #1929

Merged
emmertex merged 1 commit into
emmertex:stagingfrom
RAD-Films:pr/subject-gpu
Dec 5, 2026
Merged

emmertex merged 1 commit into
emmertex:stagingfrom
RAD-Films:pr/subject-gpu

Conversation

@OneSlips

@OneSlips OneSlips commented Dec 5, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1896

What changed

The Subject / Background model (BiRefNet_lite) and the EoMT scene model ran on the CPU everywhere but macOS, because candle reaches the GPU through Metal only. They now run on wgpu (DirectX 12, Vulkan or Metal) with the app's own kernels, like CLIP and the face models. Nothing extra to install.

  • crates/gpu/src/birefnet.rs + wgsl/birefnet.wgsl: the network's layers as wgpu kernels, shared by BiRefNet_lite and EoMT-S.
  • segment::subject::runner: one model per graphics card, the second card first so the editing card stays with the photo on screen. Each photo goes to a free card. Out of memory on a card passes that photo on and keeps the card; any other error takes the card out. With no card left, the candle model runs as before, including staging's Metal-to-CPU fallback. The scene model shares the runner.
  • Each log line names where the model ran and its weight size.
  • examples/subject_bench.rs: runs a folder of photos on the CPU and on each card, and reports speed, agreement with the CPU and memory per card.
  • Docs: docs/ai-masks.md, docs/parity-checklist.md, manual pages ai/masks-ai and ai/models, whats-new and changelog entries.

Results

Measured on 60 Canon R5 C CR3s from a real wedding: a preset with a Subject mask, JPEG export (quality 90, long edge 6000). Both builds are release builds from origin/staging and from this branch, on the same machine (RTX 5070 + RTX 4070 SUPER, 32-thread CPU).

staging this PR
Export of 60 photos 1274 s (2.8 photos/min) 344 s (10.5 photos/min)
Subject model per photo median 14.2 s, CPU median 2.2 s, RTX 4070 SUPER
Scene model per photo median 1.1 s, CPU median 1.8 s, on the card the export also renders on
4070 SUPER busy during export 24 % 89 %
4070 SUPER memory, peak / median 11,848 MiB / 8.5 GB 11,841 MiB / 10.0 GB

On the card the scene model is slower during an export, because it waits behind the export render on the same card. On its own it is faster: 0.5 to 0.6 s per photo against 1.3 s on the CPU (below).

subject_bench on 10 photos, each model alone:

per photo agreement with CPU
CPU (candle) 9.7 s
RTX 4070 SUPER 1.71 s matte IoU min 0.99999, mean abs difference ≤ 1e-6
RTX 5070 0.78 s matte IoU min 0.99999, mean abs difference ≤ 1e-6
Both cards together 0.55 s wall
Scene model, 5070 / 4070 SUPER / CPU 0.53 / 0.60 / 1.27 s class map identical

Memory per card

  • Subject model: 170 MiB of weights stay loaded. A 1024 px photo run uses up to 1,620 MiB more, freed when the run ends.
  • Scene model: 89 MiB of weights and 141 MiB per photo.

The export peak above (about 11.8 GB) is the export render. It is the same on staging. During the export, this PR raises the median by about 1.5 GB, which is the model's working memory.

The models use their own wgpu device per card, as CLIP and the face models do. The render's budget.rs doesn't count this memory yet. On a card with little free memory a run can fail with out of memory; the runner then passes that photo to another card or to the CPU. Bringing the AI models under the card budget is a follow-up.

Limits

  • auto_mask::SUBJECT_BUSY runs one Subject matte at a time, so in the app the photos still take turns on one card. The runner uses both cards once that gate allows two runs at once; subject_bench already shows both cards working (0.55 s per photo).

How it was checked

  • the_subject_model_on_a_graphics_card_matches_the_cpu and the_scene_model_on_a_graphics_card_matches_the_cpu run the real models on each card and compare with candle. They also check that the memory figures are reported. They skip when there is no card or no model.
  • cargo test -p lcc-engine --features sam --lib segment::: 49 passed. cargo test -p lcc-gpu -p lcc-segment: passed.
  • cargo xtask ci --quick --base origin/staging: fmt, clippy, docs, notes, parity and brand all pass. One engine test fails, tests_face_recognize::a_trained_person_and_an_untrained_one_are_scored_the_same_way. It arrived with #1886: train face prototypes from the people you named #1888 (merged into staging on 4 Dec) and tests face recognition, which this PR doesn't touch.
  • The before/after export was run once per build on the same 60 photos.

Manual: ai/masks-ai, ai/models

🤖 Generated with Claude Code

The Subject / Background model (BiRefNet_lite) and the EoMT scene model ran on the CPU everywhere but
macOS (candle uses the GPU through Metal only). They now run on wgpu (DirectX 12, Vulkan, Metal) with
the app's own kernels (crates/gpu/src/birefnet.rs, wgsl/birefnet.wgsl), like CLIP and the face models,
one model per graphics card, second card first. A card that cannot load or run the model is left out;
with no card left the candle model runs as before, including its Metal-to-CPU fallback.

Checked: matte IoU against the CPU 0.99999 to 1.00000 on 20 real CR3s, scene class map identical
(subject_bench). In a 60-photo R5 C CR3 export with a Subject mask the model took a median 2.2 s per
photo on an RTX 4070 SUPER instead of 14.2 s on the CPU, and the export 344 s instead of 1274 s.
Memory per card: 170 MiB of subject weights stay loaded and a photo run takes up to 1,620 MiB more,
freed when it ends (scene: 89 MiB and 141 MiB), shown by subject_bench. The render's budget.rs does
not count them yet; that is a follow-up.

Manual: ai/masks-ai, ai/models

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@emmertex
emmertex merged commit 6d280a4 into emmertex:staging Dec 5, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Subject and scene AI masks run only on the CPU outside macOS

2 participants