Fix EOS override persistence on save and reload

#2
by edbeeching HF Staff - opened
Hugging Face Biology Research org

Preserve the observed EOS override across save_pretrained() and from_pretrained().

This is a narrow alternative to the broader tokenizer persistence PR: four added lines in tokenizer.py.

  • Copy the wrapper's effective EOS token into the underlying BPE tokenizer before saving.
  • Restore EOS from tokenizer_config.json in the existing custom loader, accepting string and legacy dictionary representations. The existing final kwargs merge preserves explicit caller overrides.

Constructor defaults, PAD handling, other attributes, DNA encoding, and vocabulary IDs retain their existing behavior. The custom save/load implementation is retained.

Reproduction

from transformers import AutoTokenizer

tok = AutoTokenizer.from_pretrained("HuggingFaceBio/Carbon-X-Base", trust_remote_code=True)
tok.eos_token = "<|im_end|>"
tok.save_pretrained("./checkpoint")

tok = AutoTokenizer.from_pretrained("./checkpoint", trust_remote_code=True)
print(tok.eos_token)  # Expected: <|im_end|>; current main returns <|endoftext|>.

Validation

Three focused tokenizer tests passed on Transformers 5.8.1 and 5.12.1:

  • Reproduce the reset on the original tokenizer.
  • Verify the fix preserves <|im_end|> / ID 151645, serializes the EOS setting, respects explicit reload overrides, and leaves the vocabulary and representative DNA encodings unchanged.
  • Check the unchanged default EOS and loading an EOS stored as a legacy AddedToken dictionary.

This PR has tokenizer-only validation. The SFT GPU validation currently running targets the broader PR #1.

edbeeching changed pull request status to closed

Sign up or log in to comment