Repository navigation
SUBJECT-GPU: the subject and scene AI masks run on the graphics cards - #1929
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.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/ai-masks.md,docs/parity-checklist.md, manual pagesai/masks-aiandai/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/stagingand from this branch, on the same machine (RTX 5070 + RTX 4070 SUPER, 32-thread CPU).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_benchon 10 photos, each model alone:Memory per card
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.rsdoesn'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_BUSYruns 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_benchalready shows both cards working (0.55 s per photo).How it was checked
the_subject_model_on_a_graphics_card_matches_the_cpuandthe_scene_model_on_a_graphics_card_matches_the_cpurun 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.Manual: ai/masks-ai, ai/models
🤖 Generated with Claude Code