Skip to content

fix(birefnet): load with trust_remote_code and disable fp16 on MPS#255

Open
eaglstun wants to merge 1 commit into
nikopueringer:mainfrom
eaglstun:fix/birefnet-trust-remote-code
Open

fix(birefnet): load with trust_remote_code and disable fp16 on MPS#255
eaglstun wants to merge 1 commit into
nikopueringer:mainfrom
eaglstun:fix/birefnet-trust-remote-code

Conversation

@eaglstun

@eaglstun eaglstun commented Jun 6, 2026

Copy link
Copy Markdown

BiRefNet ships a custom architecture (birefnet.py) in its HuggingFace repo, so AutoModelForImageSegmentation.from_pretrained cannot load the weights without executing that code. With trust_remote_code=False it raises "contains custom code which must be executed", making BiRefNet alpha-hint generation completely non-functional. Load with trust_remote_code=True — the officially documented way to load ZhengPeng7/BiRefNet; the code is fetched locally by snapshot_download just above.

Also gate half precision to CUDA only. fp16 is unstable on Apple's MPS backend (BiRefNet's swin attention emits NaNs), and the model weights and the input tensor in process() were keyed off the same module-level flag, so they could silently disagree on dtype. The decision is now stored on the instance (self.use_half) and used in both places.

Adds tests/test_birefnet.py (model load mocked — no network/GPU/weights): asserts trust_remote_code=True and that half() is applied only on CUDA. Both fail on the prior code and pass with the fix.

Closes #230

What does this change?

How was it tested?

Checklist

  • uv run pytest passes
  • uv run ruff check passes
  • uv run ruff format --check passes

BiRefNet ships a custom architecture (birefnet.py) in its HuggingFace repo, so
AutoModelForImageSegmentation.from_pretrained cannot load the weights without
executing that code. With trust_remote_code=False it raises "contains custom
code which must be executed", making BiRefNet alpha-hint generation completely
non-functional. Load with trust_remote_code=True — the officially documented
way to load ZhengPeng7/BiRefNet; the code is fetched locally by snapshot_download
just above.

Also gate half precision to CUDA only. fp16 is unstable on Apple's MPS backend
(BiRefNet's swin attention emits NaNs), and the model weights and the input
tensor in process() were keyed off the same module-level flag, so they could
silently disagree on dtype. The decision is now stored on the instance
(self.use_half) and used in both places.

Adds tests/test_birefnet.py (model load mocked — no network/GPU/weights):
asserts trust_remote_code=True and that half() is applied only on CUDA. Both
fail on the prior code and pass with the fix.

Closes nikopueringer#230
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

[Bug]: ERROR text is incorrect when trying to run BiRefNet General-Lite

1 participant