Skip to content

Fix MedLink BM25 hard-negative mining - #1195

Merged
lehendo merged 5 commits into
sunlabuiuc:masterfrom
fbonc:medlink-fix
Aug 30, 2026
Merged

lehendo merged 5 commits into
sunlabuiuc:masterfrom
fbonc:medlink-fix

Conversation

@fbonc

@fbonc fbonc commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Problem
get_bm25_hard_negatives scored the corpus against the positive document (bm25_model.get_scores(d)) instead of the query (q). Hard negatives should be documents that the query ranks highly but that are not correct matches.

Negative selection also excluded only the current positive (neg_d_id != d_id). Once scoring uses the query, another valid match can rank near the top and be incorrectly selected as a negative when a query has multiple positives, which record linkage allows.

After preserving all positives, the resulting qrels could contain multiple positives and one negative. get_train_dataloader still asserted that each query had at most two entries and stored only one positive, causing valid multi-positive qrels to crash before training.

Fix
Score documents against the query and exclude every positive document from negative selection.

Preserve all positives in the mined qrels. During dataloader construction, create one training sample per positive and reuse the query’s hard negative:

(query, positive_1, negative)
(query, positive_2, negative)

Single-positive queries and queries without a hard negative retain their existing behavior.

Tests
Added controlled fake-BM25 regression coverage verifying that:

  • The negative comes from scoring the query, not the positive document.
  • No positive is selected as a negative when a query has multiple matches.
  • All positives remain after mining and are emitted as separate training samples by the dataloader.

@lehendo

lehendo commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

qrels_w_neg[q_id] = {d_id: 1, neg_d_id: -1} on line 140 ish I think, overwrites the query's entry each loop iteration instead of merging. So if you see for queries with 2+ positives, then only the last one survives. earlier positives are silently dropped from the output, not just correctly excluded as negatives. Also, test_hard_negatives_exclude_all_positives only checks positives aren't labeled -1, not that they're still present. the test doesn't catch this.

@fbonc

fbonc commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

qrels_w_neg[q_id] = {d_id: 1, neg_d_id: -1} on line 140 ish I think, overwrites the query's entry each loop iteration instead of merging. So if you see for queries with 2+ positives, then only the last one survives. earlier positives are silently dropped from the output, not just correctly excluded as negatives. Also, test_hard_negatives_exclude_all_positives only checks positives aren't labeled -1, not that they're still present. the test doesn't catch this.

fixed

@DarylOkeke

Copy link
Copy Markdown
Contributor

This now keeps every positive, but get_train_dataloader() still allows at most two qrel entries. Two positives plus the new negative makes three, so the new multi-positive case crashes when passed to the loader. Could we either keep one positive or update the loader and add an end-to-end test?

@fbonc

fbonc commented Aug 29, 2026

Copy link
Copy Markdown
Contributor Author

This now keeps every positive, but get_train_dataloader() still allows at most two qrel entries. Two positives plus the new negative makes three, so the new multi-positive case crashes when passed to the loader. Could we either keep one positive or update the loader and add an end-to-end test?

Already fixed in previous commits I believe

@lehendo lehendo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me!

@lehendo
lehendo merged commit 0a9f36a into sunlabuiuc:master Aug 30, 2026
2 checks passed
jhnwu3 pushed a commit that referenced this pull request Sep 2, 2026
get_train_dataloader's s_n (hard negative) key is only present on samples
where a hard negative was successfully mined. collate_fn initialized its
output dict's keys from only samples[0], so depending on sample order this
either raised KeyError('s_n') (the first sample lacked it but a later one
had it) or silently produced a shorter, misaligned list for that key (the
first sample had it, a later one didn't) -- both reproduced directly.

MedLink.forward consumes s_n as a whole-batch field (corpus = s_p + s_n),
so a partially-present s_n would corrupt that concatenation rather than
just misalign cleanly. Fixed by computing the union of keys across all
samples (order-preserving via dict.fromkeys, not a set, so doctest output
is deterministic) and dropping s_n for the whole batch if any sample in
it lacks one -- equivalent to training that batch without hard negatives,
a mode the model already explicitly supports via s_n=None.

This bug pre-dates PR #1195 (byte-identical on master before any of those
changes) and is orthogonal to what that PR fixes; branched separately per
the maintainer's direction so #1195 isn't blocked on this.
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.

3 participants