Skip to content

perf(tokenizer): encode a long prompt in pieces in parallel - #926

Draft
RaunakGengiti2725 wants to merge 1 commit into
FlashML-org:mainfrom
RaunakGengiti2725:pr/tokenize-in-pieces
Draft

RaunakGengiti2725 wants to merge 1 commit into
FlashML-org:mainfrom
RaunakGengiti2725:pr/tokenize-in-pieces

Conversation

@RaunakGengiti2725

@RaunakGengiti2725 RaunakGengiti2725 commented Nov 20, 2026 •

Copy link
Copy Markdown

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 keep tokenizer.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.

prompt tokens main ms this PR ms per round, saved ms
128 (control) 63.2 64.2 -1.0 / -1.1
1,024 67.3 66.3 1.4 / 1.0
4,096 169.6 153.8 16.7 / 16.7
8,192 328.5 295.2 35.1 / 31.0
16,384 704.8 655.2 39.5 / 53.0
32,768 1,625.0 1,565.1 64.1 / 55.4

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_ids fails if that fallback is lost; test_tokenize_encodes_a_long_templated_prompt_in_pieces (skips without Qwen3-0.6B cached) fails if TokenizeManager stops calling it, or calls it for raw strings.

Tested on real hardware by me.

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.
@github-actions github-actions Bot added the perf label Nov 20, 2026

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant