Repository navigation
Keep the pad embedding row when the tokenizer emits the pad token - #13628
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Fixes #4984
Problem
FastSentenceTransformerreturns wrong embeddings formicrosoft/harrier-oss-v1-0.6b, in 16-bit and in 4-bit. Compared with a stockSentenceTransformerload of the same checkpoint, the cosine similarity per text is only about 0.45 to 0.56.The cause is the load step that sets
embed_tokens.weight[padding_idx] = 0. harrier-oss-v1-0.6b setspad_token_id = 151643(<|endoftext|>), and itstokenizer.jsonpost_processor appends that same token to every input. Last-token pooling then reads exactly the row that was zeroed. When the checks compare the layer 0 hidden states with plain transformers, every position matches except the appended<|endoftext|>, which has cosine 0.0.The same step also zeroes an EOS row on main.
FastLlamaModelzeroed whateverconfig.pad_token_idpointed at, even when the tokenizer pads with a different token.unsloth/Phi-3.5-mini-instructhasconfig.pad_token_id = 32000, which is its EOS<|endoftext|>, while the tokenizer pads with 32009, so main loads it with the EOS input row zeroed.Fix
Both loaders now call one helper,
zero_pad_embedding_rows. It still zeroes the pad row in the ordinary case, and skips it when any of these hold:padding_idx != tokenizer.pad_token_id;FastBaseModelalready required this);configorgeneration_config(pad == eos keeps EOS intact, as before);lm_head(unchanged)._special_ids_added_on_encodeencodes"A"with and without special tokens and returns the difference. If the tokenizer cannot be called, it returns an empty set, which keeps the old behaviour.Evidence
Rows zeroed at load, base
c35052evs this branch on one Colab A100, real checkpoints. FLM isFastLanguageModel, FM isFastModel, ST isFastSentenceTransformer:On
SmolLM2-135M-Instruct, a 30-step SFT where every target ends in EOS gives the same losses on both sides ([5.1604, 4.3891, 3.317, 2.5316, 2.0518, 1.7869]), and greedy generation stops on<|im_end|>after 2 tokens on both sides.Embeddings vs stock sentence-transformers (sentence-transformers 6.1.0, transformers 5.17.0, torch 2.14.1). Values are the minimum per-text cosine over 4 prompted queries / 6 documents. The run loads the model, encodes, trains 8 LoRA steps (MultipleNegativesRankingLoss), saves with
save_pretrained_merged, then reloads the save with stock sentence-transformers:harrier-oss-v1-27b in 4-bit (pad id 0, never emitted, so unaffected) loads in 16.2 GB, matches a bf16 transformers reference at 0.966, and trains with QLoRA.
Regression A/B on this PR:
jobs/sft.py,jobs/vision_sft.py(plus the gemma4 preset),jobs/vision_grpo.pyandjobs/inference_smoke.pyreport NO_REGRESSION, andregression_smokereports 0 regressions.Tests:
tests/python/test_pad_row_kept_when_emitted.pycovers the helper and every skip branch, including pad == eos and an EOS in aconfig/generation_configlist. With the old predicate restored in a scratch copy, 5 of its cases fail. The helper gives the same answers on transformers 4.57.6 and 5.17.0 for the Llama 3.2, Phi-3.5, Qwen2.5 / 3, SmolLM2, Gemma 3, TinyLlama and harrier tokenizers.