TECHNOLOGY

Preserve incremental decoder state across generated token IDs

In this article
THE QUICK TAKE

Preserve per-sequence decoder state and distinguish generated IDs, valid text chunks and delivered fragments when investigating output gaps.

A generation worker produces another token ID, yet its text decoder returns no new string. That can be valid incremental decoding rather than a slow detokenizer. A token boundary can split a multibyte character or require decoder context. Preserve decoder state and distinguish token progress from text availability before attributing the gap to a network or worker backlog.

Identify the decoding contract

Record tokenizer revision, decoder pipeline and special-token policy. Independent decoding of each ID need not compose into the same string as decoding the sequence. Byte-level mappings, fallback and whitespace rules belong to the tokenizer’s decoder. A token is not necessarily a whole word, character or displayable string, so a timing instrument must not assume one nonempty fragment per generated ID.

Preserve state across steps

The checked Tokenizers DecodeStream interface accepts successive IDs with the tokenizer and buffers enough state to produce valid text chunks. A step can return None while more token information is needed. Keep one stream state for the intended sequence rather than recreating it for every ID. Do not share that mutable state between independent requests whose token histories differ.

Separate absence from replacement

No newly available chunk is different from an emitted Unicode replacement character. Replacing every temporary incomplete decoding result with a visible placeholder can permanently corrupt output that a later token would complete. Likewise, dropping the pending token discards necessary context. Follow the documented step contract and retain generated IDs for a diagnostic comparison with complete-sequence decoding.

Keep stopping and transport separate

A serving detokenizer can additionally withhold text to recognize stop strings or return deltas. Those policies are not the same as waiting for a valid decoded character. Network chunking adds another boundary after text exists. Record generated-ID progress, decoder output and delivered fragments separately. Without those stages, a decoder’s valid buffering can look identical to downstream batching in one client timestamp series.

Test the text invariant

Use bounded multilingual and byte-fallback fixtures with special-token cases. Compare concatenated emitted text with the intended full decoding under the same policy, including request completion and cancellation behavior supported by the integration. Acceptance establishes faithful incremental text reconstruction. It does not make character arrivals a GPU token clock or prove that every serving decoder uses this particular DecodeStream implementation.

Primary references

Tokenizers Incremental DecodeStream.

Tokenizers Streaming Decoder Binding.

vLLM Incremental Detokenizer.

Related reading: Diagnose tokenizer and detokenizer worker bottlenecks in SGLang; Keep tokenizer vocabulary, special tokens and chat templates in one release.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles