fix(birefnet): load with trust_remote_code and disable fp16 on MPS#255
Open
eaglstun wants to merge 1 commit into
Open
fix(birefnet): load with trust_remote_code and disable fp16 on MPS#255eaglstun wants to merge 1 commit into
eaglstun wants to merge 1 commit into
Conversation
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>
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.
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 pytestpassesuv run ruff checkpassesuv run ruff format --checkpasses