models: apply pad_id+1 position offset for RoBERTa-family encoders - #257
Merged
Conversation
The Encoder counted positions from 0 for everything except MPNet, but RoBERTa, XLM-R and CamemBERT count from pad_id+1 (HF create_position_ids_from_input_ids). With 0-based positions a RoBERTa embedder reads the wrong position rows and its embeddings diverge -- all-distilroberta-v1 scored cosine 0.65 vs HF, not ~1.0. Found while auditing the newly-published aneforge/all-distilroberta-v1 card. Extend the data-driven pos_offset (added for MPNet) to the RoBERTa family. BERT (0-based) and MPNet are unchanged; distilroberta now matches HF at cosine 1.0000. Test: on-device distilroberta-vs-HF cosine. mpnet + BERT drop-ins re-verified at 1.0000 (no regression).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug (found by auditing the just-published cards)
The sentence-transformers
Encodercounted positions from 0 for every model except MPNet. But RoBERTa, XLM-R and CamemBERT count frompad_id+1(HFcreate_position_ids_from_input_ids). With 0-based positions a RoBERTa embedder reads the wrong rows of the position table, so its output is wrong:aneforge/all-distilroberta-v1(which I published earlier this session) scored cosine 0.65 vs the HF reference -- not the ~1.0 a duplicate should. It only ever got a "loads, norm 1.0" check, not a correctness check.Fix
Extend the data-driven
pos_offset(added for MPNet) to the RoBERTa family (roberta,xlm-roberta,camembert). One line; BERT (0-based) and MPNet are unchanged.Verification
all-distilroberta-v1drop-in vs HF: 0.65 -> 1.0000Audit of all 10 published embedders: 8 are BERT (0-based correct, control at 0.9999), mpnet already handled, distilroberta was the only broken one. No card change needed -- the fix corrects the model for all users once released.
Test (
tests/test_roberta_encoder.py): on-device distilroberta-vs-HF cosine.