ImageRemBG (BiRefNet) crashes with "expected scalar type Float but found Half"
- Dominant language
- Python
- Stars
- 133k
- Forks
- 15.7k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 158
Description
### Custom Node Testing
- [ ] I have tried disabling custom nodes and the issue persists (see [how to disable custom nodes](https://docs.comfy.org/troubleshooting/custom-node-issues#step-1%3A-test-with-all-custom-nodes-disabled) if you need help)
### Expected Behavior
The BiRefNet background removal node should process the image and return a mask,. It's a core node with no unusual settings.
### Actual Behavior
The node fails every single run with:
RuntimeError: expected scalar type Float but found Half
It dies inside the shifted-window attention of birefnet.py at the "attn @ v" line. Full traceback is in the debug logs section. The same graph works fine if I start ComfyUI with --fp32-text-enc, so it only affects the fp16 path.
### Steps to Reproduce
1. Fresh ComfyUI 0.32.0, default launch flags (no fp32/fp8/bf16 text encoder override).
2. Minimal graph: LoadImage -> ImageRemBG (birefnet model) -> PreviewImage. No custom nodes involved in the graph.
3. Queue it. It crashes every time.
### Debug Logs
```powershell
[INFO] Requested to load BiRefNet
[INFO] Model BiRefNet prepared for dynamic VRAM loading. 419MB Staged. 0 patches attached. Force pre-loaded 268 weights: 4216 KB.
[ERROR] !!! Exception during processing !!! expected scalar type Float but found Half
[ERROR] Traceback (most recent call last):
File "D:\FOREX\...\ComfyUI\execution.py", line 545, in execute
...
File "D:\FOREX\...\comfy_extras\nodes_bg_removal.py", line 48, in execute
mask = bg_removal_model.encode_image(image)
File "D:\FOREX\...\comfy\bg_removal_model.py", line 54, in encode_image
out = self.model(pixel_values=pixel_values)
File "D:\FOREX\...\comfy\background_removal\birefnet.py", line 572, in forward
scaled_preds = self.forward_ori(pixel_values)
... (middle frames) ...
File "D:\FOREX\...\comfy\background_removal\birefnet.py", line 119, in forward
x = (attn @ v).transpose(1, 2).reshape(B_, N, C)
~~~~~^~~
RuntimeError: expected scalar type Float but found Half
```
### Other
I traced the cause. In comfy/background_removal/birefnet.py line 261, the shifted-window mask is created without a dtype,
so it's fp32:
img_mask = torch.zeros((1, Hp, Wp, 1), device=x.device)
Line 277 builds attn_mask from it and never casts it to x.dtype. Since bg_removal_model.py takes text_encoder_dtype (which defaults to fp16), the model runs fp16, line 113 promotes attn to fp32 when adding the fp32 mask, and line 119 then does fp32 @ fp16 -> the crash.
The original BiRefNet repo fixed this already - its swin_v1.py ends the mask construction with .to(x.dtype) and also casts the mask inside WindowAttention:
https://github.com/ZhengPeng7/BiRefNet/blob/main/models/backbones/swin_v1.py
ComfyUI's copy just predates that fix. I added .to(x.dtype) at line 277 locally and the node works fine now, so that's the fix.
Setup: Windows portable, ComfyUI 0.32.0, frontend 1.48.7, Python 3.12.10, PyTorch 2.9.1+cu130, RTX 4070 Ti SUPER 16GB.
Contributor guide
Research direction
Start in comfy/background_removal/birefnet.py around the shifted-window mask construction at lines 261-277 and inspect how it reaches the attention operation at line 119. Reproduce the minimal LoadImage ImageRemBG PreviewImage graph with default launch flags, then verify that BiRefNet returns a mask on the fp16 path without the dtype error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 90/100