Skip to content

Keep the pad embedding row when the tokenizer emits the pad token - #13628

Merged
danielhanchen merged 3 commits into
mainfrom
fix-keep-emitted-pad-embedding
Oct 14, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
fix-keep-emitted-pad-embedding

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Fixes #4984

Problem

FastSentenceTransformer returns wrong embeddings for microsoft/harrier-oss-v1-0.6b, in 16-bit and in 4-bit. Compared with a stock SentenceTransformer load 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 sets pad_token_id = 151643 (<|endoftext|>), and its tokenizer.json post_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. FastLlamaModel zeroed whatever config.pad_token_id pointed at, even when the tokenizer pads with a different token. unsloth/Phi-3.5-mini-instruct has config.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:

  • the row is not the tokenizer's pad token (padding_idx != tokenizer.pad_token_id; FastBaseModel already required this);
  • the pad token is an EOS id of the tokenizer, config or generation_config (pad == eos keeps EOS intact, as before);
  • the tokenizer adds the pad token to plain text by itself (post-processor BOS / EOS, the harrier case);
  • the embedding is tied to lm_head (unchanged).

_special_ids_added_on_encode encodes "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 c35052e vs this branch on one Colab A100, real checkpoints. FLM is FastLanguageModel, FM is FastModel, ST is FastSentenceTransformer:

model (loader) tokenizer pad / eos base zeroes this PR zeroes
harrier-oss-v1-0.6b (ST) 151643 / 151645, pad appended to every input pad row 151643 none
Phi-3.5-mini-instruct 4-bit (FLM) 32009 / 32000, config pad 32000 EOS row 32000 none
tinyllama-chat 4-bit (FLM) 0 / 2 pad row 0 pad row 0
mistral-7b-instruct-v0.3 4-bit (FLM) 770 / 2 pad row 770 pad row 770
Llama-3.2-1B-Instruct (FLM) 128004 / 128009, tied none none
Qwen2.5-0.5B-Instruct (FLM), Qwen3-0.6B (FM), gemma-3-270m-it (FM) tied none none

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:

model step base this PR
harrier-oss-v1-0.6b 16-bit load vs stock 0.471 0.9999
harrier-oss-v1-0.6b 4-bit load vs stock 0.525 0.987
harrier-oss-v1-0.6b merged save reloaded vs trained model (16-bit) 0.523 0.9999
harrier-oss-v1-0.6b merged save reloaded vs trained model (4-bit) 0.553 0.987
harrier-oss-v1-270m (control) 16-bit / 4-bit vs stock 0.9999 / 0.983 0.9999 / 0.983 (losses bit-identical)

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.py and jobs/inference_smoke.py report NO_REGRESSION, and regression_smoke reports 0 regressions.

Tests: tests/python/test_pad_row_kept_when_emitted.py covers the helper and every skip branch, including pad == eos and an EOS in a config / generation_config list. 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.

@danielhanchen danielhanchen self-assigned this Oct 13, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-14T09:48:09.635438Z d552f53 New commits
🔒 Security Review ⚠️ Failed 2026-10-13T16:07:35.577659Z a1ebcfe PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: a1ebcfe2c0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 5b01e4e776

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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

danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Oct 14, 2026
danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Oct 14, 2026
@danielhanchen
danielhanchen merged commit 4ec4b4c into main Oct 14, 2026
@danielhanchen
danielhanchen deleted the fix-keep-emitted-pad-embedding branch October 14, 2026 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Model Support] Microsoft's harrier-oss-v1 multilingual text embedding models family

1 participant