Reuse exact chat-template prefixes during long-history tokenization - #960
Merged
Merged
Conversation
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.
Long histories with structured tool calls repeatedly render the same earlier prefixes while proving each sampled-output boundary. On a retained 229,871-token history, this change reduced uninstrumented tokenization from 433.04s to 263.00s (39.3%, 1.65x). The entire serialized tokenized result and every field digest matched the baseline.
This change reuses exact unchanged prefixes within a single history and normalizes tool arguments once per eligible segmented render. It still runs the existing assistant-span and output-boundary proofs. Reuse requires stock Transformers rendering, plain JSON context, and a conservative deterministic Jinja subset; other renderers retain the existing path. Prefix results use one baseline plus bounded deltas instead of retaining quadratic text.
Validation at
ca8e021d6a53831bab4b7524b916694c201e67f6:The improvement is a constant factor on one long-history witness: later changed prefixes still render, so the remaining quadratic render-call growth is not eliminated. Unsupported templates retain ordinary rendering. No public API, training behavior, or frozen-run adoption changes. Validation and source reviews are complete; this PR is ready for final review and is intentionally unmerged.