Skip to content

Decision: don't build the vocabulary on every request - #51156

Open
rycerzes wants to merge 1 commit into
huggingface:mainfrom
rycerzes:decision-sentinel-lookup
Open

rycerzes wants to merge 1 commit into
huggingface:mainfrom
rycerzes:decision-sentinel-lookup

Conversation

@rycerzes

@rycerzes rycerzes commented Nov 24, 2026 •

Copy link
Copy Markdown

🤗 Transformers CI

What does this PR do?

prepare_inputs checks MARK not in tokenizer.get_vocab() on every call, and get_vocab() builds the whole vocabulary for tokenizers-backed tokenizers. The decision pipeline calls it once per request, so /v1/systemone pays it every time. This looks the sentinels up with convert_tokens_to_ids instead (a missing token is None, or the unknown token) and adds only the missing ones. For 8 tokenizers (Qwen3.5, Qwen2.5, Llama fast and sentencepiece, T5, TinyLlama, Phi-3, SmolLM2) starting with no sentinels, both, or only <|split|>, 2,640 prompts from the default template and custom ones with marks, splits and control tokens in the request are token-identical to main, with the same sentinel ids. A tokenizer that already has <|mark|> but not <|split|> now gets <|split|> added too: main left it unknown.

Qwen/Qwen3.5-4B, bf16, decision pipeline on the causal LM (as transformers serve loads it), 200 typed-decisions requests of 5 questions, median, on an HF Jobs RTX PRO 6000 Blackwell (preprocessing runs on its CPU):

main this PR
preprocess 107.8 ms 4.8 ms
full call 328.4 ms 226.5 ms

Tokenizing 40k training prompts with prepare_inputs (as in TRL's decision model example, huggingface/trl#7784): about 70 min on main (extrapolated from 2,000 prompts in 210 s), 60 s measured with this PR.

The test makes get_vocab raise, so it fails on main, and checks that both sentinels map back to themselves for a tokenizer with an unknown token (Llama) and one without (Qwen).

Who can review?

@ArthurZucker

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution 🤗!

CI Security Gate — waiting for maintainer approval

CI is approved automatically only for authors who already have a contribution merged in this repository. A maintainer will review this PR and approve CI to run on it.

This branch has not been deployed

No deployments
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