Skip to content

Remove background - onnx-community/BEN2-ONNX - bug when using webGPU #1272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 of 5 tasks
pikilipita opened this issue Apr 4, 2025 · 9 comments
Open
1 of 5 tasks
Labels
bug Something isn't working

Comments

@pikilipita
Copy link

System Info

transformers 3.4.1
macOs 14.7.4
Chrome 136

Environment/Platform

  • Website/web-app
  • Browser extension
  • Server-side (e.g., Node.js, Deno, Bun)
  • Desktop app (e.g., Electron)
  • Other (e.g., VSCode extension)

Description

While using onnx-community/BEN2-ONNX to remove background, the wasm version result is fine, but the webGPU result adds a 'ghost' on the mask:

Image

the original image of this example be found here, but the ghosting issue is happening on all images I tested.
https://unsplash.com/photos/woman-standing-near-pink-concrete-wall-during-daytime-TT-ROxWj9nA

tested on transformers 3.4.1 with those settings:

var segmenter = await pipeline("background-removal", "onnx-community/BEN2-ONNX", { device:"webgpu" });

Reproduction

to reproduce, use those settings with any input image:
var segmenter = await pipeline("background-removal", "onnx-community/BEN2-ONNX", { device:"webgpu" });

@pikilipita pikilipita added the bug Something isn't working label Apr 4, 2025
@xenova
Copy link
Collaborator

xenova commented Apr 15, 2025

cc @fs-eire in case you have any ideas.

@prathikr
Copy link

Looking into the issue...

@prathikr
Copy link

Hi @pikilipita do you have a full index.html that reproduces this issue? Something that generates the binary image and displays it on the page?

@pikilipita
Copy link
Author

@prathikr
yes, I just made one for you:
https://pikimov.com/tr-test/

@prathikr
Copy link

Hi @pikilipita, thank you for your patience. @fs-eire and I looked at your model and noticed a peculiar pattern. In both the multifieldcrossatt and dec_blk modules there is a softmax that feeds to a cast-to-fp32 followed by a cast-to-fp16:
Image
If I adjust it to follow the pattern cast-to-fp32 --> softmax --> cast-to-fp16 then the model works:
Image

Can you please double check how the multifieldcrossatt and dec_blk modules are implemented to see if the second pattern can be followed?

@pikilipita
Copy link
Author

Hello @prathikr , thank you for looking into the issue,
Where would you like me to look for modules? Not sure I can help, I'm just using recommended transformers pipeline code, I'm not an LLM expert at all.

@prathikr
Copy link

@xenova I see you uploaded the BEN2 model to HF model hub. Do you know where the model code is to check why the cast nodes are being exported incorrectly?

@xenova
Copy link
Collaborator

xenova commented May 13, 2025

It's using the float16 quantization script: https://github.com/huggingface/transformers.js/blob/main/scripts/float16.py, which is adapted from a bunch of other scripts.

I'm pretty certain the bug is from there, for when there is already an existing cast node in the graph.

Would greatly appreciate help in fixing it @prathikr if you have time.

@prathikr
Copy link

prathikr commented May 13, 2025

@xenova do you use BEN2_Base.onnx as input to this script? I can see the softmax --> cast to fp32 --> cast to fp16 is in the base model so this script may not be the issue. It is likely an issue in the original model implementation. I have a version of onnx-community/BEN2-ONNX with the swapped nodes and the script that swaps the nodes. I can share both via email if that would be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants