huggingface / huggingface/candle

Bert: Attention mask is required for embedding calculation on BGE model

Open
#1,798 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
21k
Forks
1.8k
Avg merge
16h 42m
Merged PRs (30d)
25

Description

Attention mask, as already reported here https://github.com/huggingface/candle/issues/1552 is not available.

However, that issue was closed because bert is used only for computing embeddings.

But the attention mask is required for proper calculation of many sentences at once, otherwise, results will not be correct.

I created a repository that reproduces it. https://github.com/lucasavila00/candlerepro

Main Logs: https://github.com/lucasavila00/candlerepro/blob/main/log.log

Cheking the logs of the main function shows us that if we process one item at a time, we get the right results.
Also, it shows that my implementation for batching (basically copied from the examples) adds the right padding.
And it shows that the batched implementation, if using just one item, is also correct.

However, if 2 items are used, the paddings will affect results due to missing attention mask.

The results were also checked against this Python implementation: https://github.com/lucasavila00/candlerepro/blob/main/test.ipynb

A picture of the notebook, if github can't render it:
![image](https://github.com/huggingface/candle/assets/12750442/a8e2333c-c1b1-45c9-9801-ecf8d02405e4)

It shows all values match, however the tokenizer in Python creates the required attention mask and passes it to the model call.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the linked candlerepro repository, its logs, and the batching examples to reproduce the BGE embedding mismatch with two padded inputs. Then trace the Bert embedding path and how tokenizer outputs reach the model; done means batched results remain correct with padding and match the Python reference implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.