Repository navigation
Fix MedLink BM25 hard-negative mining - #1195
Conversation
|
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 |
|
This now keeps every positive, but |
Already fixed in previous commits I believe |
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.
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: