Transformers v5 + BOS token

#11
by Molbap HF Staff - opened

The chat template doesn't emit a BOS token, so a rendered conversation starts directly on the first image or text token. The model was trained with BOS, and the processing_molmo2.py in this repo compensates by inserting it in python after rendering (insert_bos). The native transformers integration (huggingface/transformers#43451) renders the template as-is, with no processor-side fixup, so prompts built with apply_chat_template currently lose that token.

This adds {{ bos_token }} at the very start of the template and changes nothing else: everything after the 15-character prefix is byte-for-byte the current file. Rendered output gains exactly one leading token, the bos_token each repo already declares (<|im_end|>, 151645, for the Qwen-based 4B and 8B; <|endoftext|>, 100257, for the OLMo-based O-7B). A single-image prompt goes from 422 to 423 tokens and matches what processing_molmo2.py produces today, token for token.

No double BOS for current users: insert_bos checks the first token before inserting, so trust_remote_code output is unchanged, and vLLM's native molmo2 implementation guards the same way (prompt_tokens[0] != bos_token_id). Verified against the current repo files: rendering with the patched template gives exactly [bos_token_id] + previous_ids, nothing else moves.

Will have to land for https://github.com/huggingface/transformers/pull/43451 to be merged and Molmo2 be available in transformers v5!

Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment