Use standard tokenizer serialization to preserve configuration

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

HybridDNATokenizer currently saves the underlying BPE tokenizer's configuration and manually restores only a few fields. Consequently, changing EOS to <|im_end|> is lost after saving/reloading, and other standard tokenizer settings can be dropped too.

This change uses the standard Transformers persistence path used by Qwen:

  • Inherit save_pretrained() for the wrapper's configuration, special-token map, AddedToken metadata, chat template, and custom-code export.
  • Delegate only BPE asset writing through _save_pretrained(), retaining dna_config.json and the existing DNA vocabulary-layout checks.
  • Register k, tail, and auto_dna_tags as constructor configuration, and delegate loading to the parent implementation. Existing bio_config.json / dna_config.json options remain fallback defaults for older checkpoints.
  • Preserve restored added-token metadata instead of overwriting it with base-tokenizer defaults. Explicit caller overrides, including eos_token=None / pad_token=None, retain standard Transformers semantics.

DNA encoding and vocabulary IDs are unchanged. This PR changes only tokenizer.py; it does not change model weights or the saved model configuration.

Reproduction on current main

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)  # Currently <|endoftext|>; should remain <|im_end|>.

Validation

Eight tokenizer-only tests passed with both Transformers 5.8.1 and 5.12.1:

  • Existing Base checkpoint compatibility and unchanged vocabulary / representative DNA token IDs.
  • EOS, PAD, BOS, UNK, configured padding/truncation sides, model length, cleanup settings, and chat-template persistence.
  • AddedToken flags and extra-special-token metadata.
  • Explicit reload overrides and nullable EOS/PAD settings.
  • DNA constructor options across multiple k-mer sizes and tail modes.
  • Legacy DNA configuration fallbacks and vocabulary-layout mismatch rejection.
  • Independent AutoTokenizer loading and re-saving in a fresh process, without importing the Carbon package.

After this upstream fix is merged, the temporary tokenizer copy in Carbon SFT PR #33 can be removed.

edbeeching changed pull request status to closed

Sign up or log in to comment