Repository navigation
Allow tokenizers to provide streaming detokenizers - #619
Conversation
|
This is a great idea! |
Thanks, see the performances notes 😀. |
Wow, that is huge and a big win especially for apps that want to show the streaming output. |
|
Edit: Apologies for the original version of this comment. It was posted by an agent without my consent or review. Let me rewrite it in my own words. |
ae6f3b0 to
39c89c4
Compare
39c89c4 to
d3cc22c
Compare
|
@davidkoski I think this should be merged as it is the right design to solve issues like #636 , however I have even opened #670 |
d3cc22c to
a9f9574
Compare
|
Just rebased on latest main |
|
@davidkoski when this gets eventually merged, write me a note on how you want to present the swift-tokenisers, so that I will then open the PR with the reference... Thanks. |
davidkoski
left a comment
There was a problem hiding this comment.
I think this is a great idea and a big step forward -- thank you for the contribution!
Brings in ml-explore#622 (exact prefix rewinds in rotating KV caches), ml-explore#619 (streaming detokenizers), ml-explore#698, ml-explore#699, ml-explore#700, ml-explore#701, ml-explore#705, ml-explore#697 and ml-explore#695. Conflicts: AttentionUtils.swift keeps TurboKV telemetry next to upstream's SharedAttentionKVCache; KVCache.swift takes upstream's serialization and rotating-cache view/trim while keeping the fork's TurboQuant state, public cache fields and temporallyOrdered. Follow-ups for upstream behavior the fork had changed: the Qwen2VL audio placeholder no longer indexes content.count - 1 when a tool-call message has no text part, and Gemma4MessageGenerator uses content parts for every non-system role. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Proposed changes
Generation currently constructs
NaiveStreamingDetokenizerdirectly, preventing tokenizer adapters from supplying an incremental implementation. This can require repeated decoding of a growing text segment, especially for long responses without newlines.Add
Tokenizer.makeStreamingDetokenizer()with a default implementation returning the existing naive decoder. Tokenizer adapters can opt into their own decoding implementation without adding a tokenizer dependency to MLX or changing the existing swift-transformers adapter.This is an extension point: existing adapters retain the default decoder. Performance improvements require an adapter that supplies an optimized implementation.
Example: swift-tokenizers
An application using aleroot/swift-tokenizers can implement the factory inside its existing
MLXLMCommon.Tokenizerbridge, whereupstreamis its wrappedTokenizers.Tokenizer:The following adapter connects that library's independent decoding session to MLX's append/next/finish lifecycle:
Special tokens remain enabled because MLX's tool and reasoning parsers consume them. Each factory call creates independent state.
finish()returns any unread output and the decoder's final text. Appending afterfinish()is a programmer error under this lifecycle.Note
In my own benchmark, coupled with
swift-tokenizers, incremental decoding substantially reduced tokenizer CPU time compared with repeatedly decoding the full generated prefix. For 4,096 tokens of newline-free text, decoding took approximately 0.19 ms instead of 55.6 ms for Qwen, and 1.64 ms instead of 1.16 seconds for Llama 2.These are decoder-only measurements, not end-to-end generation speedups. MLX’s default detokenizer resets its decoding segment at newlines, so its actual improvement will depend on the output and tokenizer. Long uninterrupted responses, JSON, and code are likely to benefit most.
Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.