Skip to content

Add GPU-backed BiRefNet General tracer#38

Merged
jasonmadigan merged 3 commits into
tracefinity:mainfrom
noobydp:gpu-birefnet-general
May 11, 2026
Merged

Add GPU-backed BiRefNet General tracer#38
jasonmadigan merged 3 commits into
tracefinity:mainfrom
noobydp:gpu-birefnet-general

Conversation

@noobydp

@noobydp noobydp commented May 3, 2026

Copy link
Copy Markdown
Contributor

This PR adds BiRefNet General as a GPU-backed local tracing option so higher-quality background removal can run locally without forcing every model to load at startup. It also wires rembg/ONNX Runtime provider selection and Windows CUDA DLL discovery so NVIDIA setups can use CUDA when available while retaining CPU fallback.

Code and PR drafted by Codex

Summary

  • add BiRefNet General as an additional local tracer
  • route rembg-based local tracers through ONNX Runtime GPU provider selection
  • add Windows NVIDIA wheel DLL path setup for CUDA/cuDNN runtime discovery
  • lazy-load tracers instead of preloading every configured model at startup

Verification

  • backend compileall: python -m compileall app
  • frontend build:
    pm run build
  • CUDA smoke test: BiRefNet General rembg session reports ['CUDAExecutionProvider', 'CPUExecutionProvider'] and completes inference

@noobydp
noobydp marked this pull request as ready for review May 3, 2026 15:23
Comment thread backend/requirements.txt Outdated
rembg>=2.0.50 # birefnet-lite, isnet, paper detection
onnxruntime>=1.17.0 # rembg runtime
rembg>=2.0.75 # birefnet, isnet, paper detection
onnxruntime-gpu[cuda,cudnn]>=1.23.0 # rembg runtime with CUDA provider

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't make GPU the default. onnxruntime-gpu[cuda,cudnn] only ships wheels for Linux x86_64 / Windows, breaks pip install on Mac, and pulls a few GB of NVIDIA libs even when no GPU. I reckon GPU users can opt in via a separate requirements-gpu.txt (or extras)

Comment thread backend/app/config.py Outdated
@noobydp

noobydp commented May 5, 2026 via email

Copy link
Copy Markdown
Contributor Author

@noobydp

noobydp commented May 10, 2026

Copy link
Copy Markdown
Contributor Author

Updated the change to make gpu usage opt-in

logger.warning("failed to preload ONNX Runtime CUDA DLLs: %s", exc)


def get_onnx_providers(require_gpu: bool = False):

@jasonmadigan jasonmadigan May 11, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require_gpu is never passed True by any caller -- ai_tracer.py and image_processor.py both call get_onnx_providers() with no args. Either wire it up for birefnet-general loads where GPU is expected, or remove it.

@@ -0,0 +1,105 @@
from __future__ import annotations

@jasonmadigan jasonmadigan May 11, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has zero test coverage. test_tracer_config.py only exercises Settings.available_tracers. At minimum, need tests for: invalid TRACEFINITY_ONNX_PROVIDER raises ValueError, cpu mode returns ["CPUExecutionProvider"], auto mode branches correctly.

Comment thread backend/app/api/routes.py
for _tid in settings.available_tracers:
_get_tracer(_tid)

polygon_scaler = PolygonScaler()

@jasonmadigan jasonmadigan May 11, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The eager preload loop is gone, so a broken tracer config (bad model name, missing weights) will only surface on first user request. Consider a lightweight validation step at startup -- e.g. check configured tracer names against _REMBG_MODELS keys without actually loading models.

- cuda: require CUDAExecutionProvider
- cpu: CPUExecutionProvider only
"""
import onnxruntime as ort

@jasonmadigan jasonmadigan May 11, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_preload_onnxruntime_cuda_dlls above catches ImportError gracefully when onnxruntime is missing, but this bare import will crash. Should be consistent -- either both handle it or both let it propagate.

@@ -151,12 +153,16 @@ def _load_local_model(self):
label = self._LOCAL_MODEL_LABELS.get(name, name)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: docstring on _load_local_model (line 149) says "at startup" but loading is now lazy (preload loop removed from routes.py).

@jasonmadigan

Copy link
Copy Markdown
Collaborator

thanks @noobydp just a few comments then I think we're in good shape to merge.

@jasonmadigan

Copy link
Copy Markdown
Collaborator

all that stuff is pretty nit-picky tbh, just approving. thanks a lot @noobydp

@jasonmadigan
jasonmadigan merged commit 29ce60b into tracefinity:main May 11, 2026
1 check passed
@noobydp
noobydp deleted the gpu-birefnet-general branch May 12, 2026 03:58
shanetinklenberg pushed a commit to shanetinklenberg/tracefinity that referenced this pull request Jun 28, 2026
Add GPU-backed BiRefNet General tracer
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.

2 participants