Token ID computation in BM25/BM42: Possible id overlap due to absolute value casting of hash
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 3.2k
- Forks
- 248
- Avg merge
- 4d 8h
- Merged PRs (30d)
- 4
Description
Found thus only cause I was curious how fastembed calculates token ids for bm25/bm42, so I took a deep dive:
Problem
The function compute_token_id computes the absolute value of a signed 32 bit integer returned by the mmh3 hash lib.
@classmethod
def compute_token_id(cls, token: str) -> int:
return abs(mmh3.hash(token))
This could lead to token id overlap, e.g. if hash of "black" is -42 and "white" is 42 (this is exaggerated of course), the token id would be 42 for both of them.
Is this expexted behaviour?
Proposed Solution
The hash call could also provide an unsigned 32 bit int (casted to a positive 64bit int, which would be no problem for qdrant since its 64 bit native).
This could be achieved by specifying the following extra arguments:
@classmethod
def compute_token_id(cls, token: str) -> int:
return mmh3.hash(token, seed=0, signed=False)
I'm happy to provide a PR if you would like this behaviour to be changed.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in fastembed/sparse/bm25.py at compute_token_id and read the linked mmh3.hash API documentation, including its signed and unsigned return behavior. Check how the token ID is used for BM25 and BM42, then validate that the chosen behavior avoids sign-based collisions without changing the expected identifier type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- search
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100