Skip to content

models: apply pad_id+1 position offset for RoBERTa-family encoders - #257

Merged
sbryngelson merged 1 commit into
mainfrom
fix/roberta-position-offset
Aug 31, 2026
Merged

models: apply pad_id+1 position offset for RoBERTa-family encoders#257
sbryngelson merged 1 commit into
mainfrom
fix/roberta-position-offset

Conversation

@sbryngelson

Copy link
Copy Markdown
Owner

Bug (found by auditing the just-published cards)

The sentence-transformers Encoder counted positions from 0 for every model 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 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-v1 drop-in vs HF: 0.65 -> 1.0000
  • regression: MPNet and BERT (all-MiniLM-L6) drop-ins re-verified at 1.0000

Audit 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.

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).
@sbryngelson
sbryngelson merged commit f36151a into main Aug 31, 2026
19 checks passed
@sbryngelson
sbryngelson deleted the fix/roberta-position-offset branch August 31, 2026 01:10
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.

1 participant