Skip to content

models: support MPNet encoders (all-mpnet-base-v2) in the drop-in - #255

Merged
sbryngelson merged 2 commits into
mainfrom
feat/mpnet-encoder
Aug 31, 2026
Merged

models: support MPNet encoders (all-mpnet-base-v2) in the drop-in#255
sbryngelson merged 2 commits into
mainfrom
feat/mpnet-encoder

Conversation

@sbryngelson

Copy link
Copy Markdown
Owner

What

Adds MPNet support to the sentence-transformers Encoder, unlocking all-mpnet-base-v2 -- the most popular embedding model -- on the ANE. Previously it failed to load with KeyError: embeddings.token_type_embeddings.weight.

Why it needed code

MPNet differs from BERT/RoBERTa in three ways the encoder didn't handle:

  • no token_type embedding (the KeyError)
  • different attention key names (attention.attn.{q,k,v,o} vs BERT's attention.self.*)
  • a T5-style relative-position bias added to the attention scores, plus a pad_id+1 position offset

How

The relative bias is added post-scale, exactly where mha's additive mask already goes, so it folds into the per-head [H,S,S] mask with no graph or mha change:

  • _MPNET_KEYS map + model_type == "mpnet" branch
  • token_type is now optional; the position offset is data-driven (0 for BERT/RoBERTa, so they're unchanged)
  • host-compute the relative bias once per sequence length (numpy port of HF's bucketing), cache it, add to the padding mask

Verification

all-mpnet-base-v2 through the drop-in matches HF mean-pooled + normalized embeddings at cosine 1.0000 (three sentences).

Tests (tests/test_mpnet.py):

  • off-device: our bucketing matches HF's MPNetEncoder.relative_position_bucket across -140..140; bias shape [H,S,S]
  • on-device (requires_ane): drop-in vs HF cosine > 0.99

Encoder + cross-encoder + RAG suites: 23 passed, no regressions.

Follow-up: publish an aneforge/all-mpnet-base-v2 card once this is released.

…ansformers drop-in

MPNet differs from BERT/RoBERTa in three ways the Encoder didn't handle: it has
no token_type embedding (the loader KeyError'd on it), its attention weights use
different key names (attention.attn.{q,k,v,o}), and it adds a T5-style relative-
position bias to the attention scores plus a pad_id+1 position offset.

The relative bias is post-scale additive, exactly where mha's mask goes, so it
folds into the per-head [H,S,S] mask with no graph/mha change: load the bucket
table, host-compute the bias once per sequence length (numpy port of HF's
bucketing), and add it to the padding mask. token_type is now optional and the
position offset is data-driven, so BERT/RoBERTa are unchanged (offset 0).

all-mpnet-base-v2 through the drop-in matches HF mean-pooled embeddings at
cosine 1.0000. Tests: off-device bucket match vs HF + bias shape; on-device
drop-in-vs-HF cosine.

Encoder/cross-encoder/RAG suites: 23 passed, no regressions.
… no-ANE job

The off-device smoke job installs .[dev] (no torch/transformers), so importing
transformers in an off-device test failed collection. Assert against hard-coded
HF reference bucket values instead (verified to match HF over -400..400).
@sbryngelson
sbryngelson merged commit 1bc257e into main Aug 31, 2026
19 checks passed
@sbryngelson
sbryngelson deleted the feat/mpnet-encoder branch August 31, 2026 00:47
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