Repository navigation
perf(tokenizer): encode a long prompt in pieces in parallel - #926
Draft
RaunakGengiti2725 wants to merge 1 commit into
Draft
RaunakGengiti2725 wants to merge 1 commit into
RaunakGengiti2725 wants to merge 1 commit into
Conversation
A long templated prompt was tokenized as one text on one thread. Under the Qwen2 and Qwen3.6 split patterns it is now cut at safe points into at most 32 pieces of at least about 2,048 characters, encoded in parallel by the Rust backend and joined, with the same ids. Other tokenizers and raw-string prompts keep the whole-text encode.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related: #921. This is one of the changes in the fork build measured there.
A long templated prompt was tokenized on one thread. Under the Qwen2 and Qwen3.6 split patterns it is now cut where the pre-tokenizer cannot look across, into at most 32 pieces of at least about 2,048 characters, encoded in parallel (
encode_batch) and joined. Short prompts, other tokenizers, raw strings and the DSV4 encoder keeptokenizer.encode.A/B against main 2fb371e. RTX 5090 (575 W), AMD EPYC 9354, driver 615.71.09,
nvidia/Qwen3.6-35B-A3B-NVFP4. One interleaved run, 2 rounds, order reversed. Both arms:ft serve --model nvidia/Qwen3.6-35B-A3B-NVFP4 --enable-timings-report --moe-strategy fused --max-running-requests 1 --speculative-algorithm mtp --speculative-num-steps 3. Unique chat prompts, greedy, 128-token replies; client first token, median.Server prompt time unchanged (8,192: 282.1 against 281.6 ms). No A/A arm: the 128 and 1,024 rows are within noise; 16K and 32K have 2 prompts a round. Exact: 48 of 48 replies and every prompt token count equal main's.
Tests (
tests/tokenizer/test_tokenize.py, no GPU):test_pieces_encode_to_the_ids_of_the_whole_text(a small built BPE, plus two Qwen tokenizers when in the HF cache) fails if a cut lands where the split could look across it;test_pieces_keep_the_whole_text_encode_where_a_cut_could_change_idsfails if that fallback is lost;test_tokenize_encodes_a_long_templated_prompt_in_pieces(skips without Qwen3-0.6B cached) fails ifTokenizeManagerstops calling it, or calls it for raw strings.Tested on real hardware by me.