Skip to content

[None][perf] Prefix token cache: reuse token ids of unchanged chat messages - #20392

Draft
zheyuf wants to merge 2 commits into
NVIDIA:mainfrom
zheyuf:zheyuf/perf/prefix-token-cache-pieces
Draft

zheyuf wants to merge 2 commits into
NVIDIA:mainfrom
zheyuf:zheyuf/perf/prefix-token-cache-pieces

Conversation

@zheyuf

@zheyuf zheyuf commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

Description

The problem: the prefix-tokenization cache reuses token ids only when a new prompt extends a cached prompt. Agentic prompts often break that rule. A new conversation's first turn shares its system prompt and tool definitions with earlier conversations, and agents rewrite earlier context: in the MiniMax-M3 AgentX trace about 7% of turns change the end of the previous prompt, usually just its last ~1K characters. Those requests are re-tokenized in full, about 120-220 ms each for 70K-130K-token prompts on a B300 host CPU.

What this PR does:

  • A fast tokenizer already splits text at its non-normalized added tokens (the chat template's role and message markers) and encodes the text between them independently. Where that is exact for the tokenizer, the cache does the same and keeps a second LRU of each piece's token ids. Unchanged messages are then reused even when the prompt is not an extension of a cached one, and pieces not seen before are encoded in one batch call.
  • Cached prompts end at an added token, so a prompt that extends one needs no back-off or resync check and no offset mapping.
  • It is used only where it is exact and not slower. The tokenizer must have no added token that strips neighbouring spaces or contains another one, no first-word-only Metaspace, split_special_tokens off, no Python-side preprocessing, and a startup probe that encodes identically both ways. A prompt must also have an added token within its last 256 characters, as chat templates' generation prompts do. Otherwise the existing offsets splice runs unchanged.

MiniMax-M3 NVFP4 on B300:

  • Replay of the 2,031 chat prompts of an AgentX run (TP4 C20) in arrival order: ids identical to full tokenization. Per-request tokenization on a B300 host CPU goes from 29.6 to 9.6 ms mean, 8.9 to 3.9 ms p50 and 358 to 96 ms p99. Rewritten-context turns go from 218 to 24 ms, first turns from 123 to 61 ms and other cache hits from 11.6 to 5.3 ms.
  • AgentX vs the existing cache (one 3600 s run per arm; x90 noise about ±3-4%):
Point x90 (P90 interactivity) y (tok/s/GPU) TTFT p50 TTFT p90
TP8 C1 K7 +0.0% +0.0% −4.9% −34.9%
TP4 C5 K5 −2.3% +3.7% −3.6% −0.3%
TP4 C20 K3 +0.4% +0.2% −3.7% −1.4%
TP2 C30 K3 +0.6% +0.1% −3.7% −2.8%
TP2 C45 K3 +0.1% +0.0% +1.6% +5.1%

This is a TTFT change, not a throughput change. Paired per request, the server time before the response starts drops by 15-34 ms on average, as the replay predicts. Rewritten-context turns gain 130-270 ms (median), which is what moves TTFT p90 at TP8 C1. TP2 C45 TTFT is queue-bound, and its change is within noise.

Test Coverage

  • tests/unittest/inputs/test_prefix_token_cache.py, with a small BPE trained in the test:
    • piecewise growth, edited history and reordered history give the same ids as full tokenization;
    • prompts without an added token near their end keep the offsets splice;
    • piecewise mode is off for space-stripping markers, a first-word-only Metaspace, and a marker that contains another one.
  • The 2,031-prompt MiniMax-M3 replay above: ids identical on every prompt.

PR Checklist

Please review the following before submitting your PR:

  • PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.

  • PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.

  • Test cases are provided for new code paths (see test instructions)

  • If PR introduces API changes, an appropriate PR label is added - either api-compatible or api-breaking. For api-breaking, include BREAKING in the PR title.

  • Any new dependencies have been scanned for license and vulnerabilities

  • CODEOWNERS updated if ownership changes

  • Documentation updated as needed

  • Update tava architecture diagram if there is a significant design change in PR.

  • The reviewers assigned automatically/manually are appropriate for the PR.

  • Please check this after reviewing the above items as appropriate for this PR.

GitHub Bot Help

To see a list of available CI bot commands, please comment /bot help.

🤖 Generated with Claude Code

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