Skip to content

Sync the pad id to FastModel's fallback tokenizer - #16557

Merged
danielhanchen merged 3 commits into
mainfrom
fix-pad-token-out-of-range
Nov 3, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
fix-pad-token-out-of-range

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

FastModel can return a model whose config.pad_token_id names a token that the returned tokenizer does not have.

What

When patch_tokenizer raises inside FastBaseModel.from_pretrained, vision.py reloads a fresh tokenizer and patches that instead. An older unsloth_zoo fix_pad_token could raise after it had already added a pad token, grown the embedding and written the new pad id into model.config. The fallback then returned the fresh tokenizer, which has no such token, while the config still pointed at it. Generation and collators that read the pad from the config used an id the tokenizer never emits.

The fallback now calls a small helper, _sync_pad_token_id, after patching the fallback tokenizer. It sets config.pad_token_id and generation_config.pad_token_id to the returned tokenizer's pad id when that id is a valid embedding row. It does nothing when the tokenizer has no pad, the id is out of range, or the model has no input embeddings, and it only runs on the fallback path.

The matching unsloth_zoo change stops fix_pad_token from raising in this case; this side keeps FastModel consistent with zoo releases that still do.

Tests

tests/test_fastmodel_pad_fallback_sync.py (new, CPU only, no unsloth import: the helper is read from source):

  • the config and generation config follow the fallback tokenizer's pad;
  • a processor's inner tokenizer is the one read;
  • an out-of-range or missing pad leaves the config untouched;
  • the vision.py fallback calls the helper after patch_tokenizer.

All 4 pass on this branch; on main the helper does not exist.

Not covered

No GPU load of a real FastModel checkpoint; the normal (non-fallback) tokenizer path is unchanged.

When patch_tokenizer fails, FastModel reloads a fresh tokenizer and patches
it again. An older unsloth_zoo raised after adding a pad and growing the
embedding, so the model config kept the dropped pad's id while the returned
tokenizer padded with another one. Point config and generation_config
pad_token_id at the fallback tokenizer's pad when it has an embedding row.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 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 ✅ Completed 2026-11-03T15:11:44.762961Z 44be7c2 Manual request
ℹ️ 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

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 44be7c29d6

ℹ️ 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
danielhanchen merged commit 3d4aa86 into main Nov 3, 2026
@danielhanchen
danielhanchen deleted the fix-pad-token-out-of-range branch November 3, 2026 18:28
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