Skip to content

Fix BM25 term frequency for colliding token IDs - #942

Open
remote-controlled-man wants to merge 1 commit into
qdrant:mainfrom
remote-controlled-man:codex/prflow-7c0645972af9
Open

remote-controlled-man wants to merge 1 commit into
qdrant:mainfrom
remote-controlled-man:codex/prflow-7c0645972af9

Conversation

@remote-controlled-man

Copy link
Copy Markdown
Contributor

Two distinct BM25 terms can hash to the same sparse index. When their frequencies differ, the last distinct term to be processed overwrites the weight at that index, so reordering the same document changes its embedding.

Count occurrences per hashed index before applying BM25 saturation. The regression test uses two real colliding tokens in both orders and checks the expected weight, a non-colliding document, and binary query weights. It runs without downloading a model.

Local validation: 9 focused tests passed, along with the Ruff lint and format hooks. The full multi-model test suite is left to CI.

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

Copilot AI balanced review requested due to automatic review settings December 4, 2026 17:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@coderabbitai

coderabbitai Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Bm25._term_frequency now combines occurrence counts for stems that share a hashed token ID before applying BM25 saturation. A regression test checks document weights for colliding and distinct tokens, verifies that token order does not affect collision weights, and confirms that query embeddings retain unit weights.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Merge Risk: Minimal · up to db90a

This fixes order-dependent BM25 weights when different terms hash to the same sparse index. The change is small and has regression coverage, so no merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the actual change: BM25 term frequency is now computed per hashed token ID so colliding IDs no longer overwrite each other. It matches the raw summary and the PR objectives, and it is …
Description check Passed The description explains the problem (distinct BM25 terms hashing to the same sparse index overwrite each other, making embeddings order-dependent), the fix (count occurrences per hashed index before …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the actual change: BM25 term frequency is now computed per hashed token ID so colliding IDs no longer overwrite each other. It matches the raw summary and the PR objectives, and it is specific enough for a reviewer scanning history.

Full details: Description check

Explanation

The description explains the problem (distinct BM25 terms hashing to the same sparse index overwrite each other, making embeddings order-dependent), the fix (count occurrences per hashed index before BM25 saturation), the regression test, and local validation. It is related to the changeset and fully on-topic.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e2467bdc-2d01-4170-b24c-43845a425803
Commits

Reviewing files that changed from the base of the PR and between ac9ab5c and db90a43.

Files selected for processing: 2

  • fastembed/sparse/bm25.py
  • tests/test_sparse_embeddings.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants