mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #1591] [CLOSED] tokenizer: sweep protected spans in match order #3549
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#3549
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/1591
Author: @InB4DevOps
Created: 10/8/2026
Status: ❌ Closed
Base:
main← Head:perf/tokenizer-plain-span-sweep📝 Commits (1)
d5e6c4dtokenizer: sweep protected spans in match order📊 Changes
2 files changed (+116 additions, -3 deletions)
View changed files
📝
tools/strata_tokenizer.py(+19 -3)➕
tools/test_tokenizer_plain_spans.py(+97 -0)📄 Description
Summary
Avoid scanning every protected plain-text span for every special-token regex match in
Tokenizer._encode_matching().Prompts quoting reasoning/control-token syntax (for example, a tool result containing a chat template) can have many protected spans. The old
any(a <= m.start() < b for a, b in plain)repeats the scan from the beginning for each match.The span check becomes O(matches + spans) after sorting, rather than O(matches × spans). Sorting requires a temporary span list and is O(spans log spans) generally; the server already supplies ordered spans. Empty-span prompts do not allocate or sort a span list.
Scope and isolated base
Only
tools/strata_tokenizer.pyandtools/test_tokenizer_plain_spans.pyare included.Both the untouched baseline and candidate started at upstream
fb58e0dbc8399662c0e47c76578c6e878b14f6cf. The cache-initialization change from #1582 is excluded, as are the earlier parser optimizations and profiling instrumentation. Server, frontend, template and synthetic-fixture hashes match across checkouts. BPE, cache behavior and special-token definitions are unchanged.Correctness
290 passed; 5 tests requiring unavailable tokenizer assets were skipped. New exact-token-ID parity tests compare against the original implementation for unsorted/overlapping/adjacent/nested spans, empty/reversed/out-of-range spans, Unicode offsets, boundaries inside literals, both special-token modes, BPE merges, input immutability, empty text, absent special patterns and one-shot iterables.
git diff --checkpassed.Windows correctness and performance validation: pending.
Same-day paired CPU measurements
Intel Core i7-12700KF, 20 logical CPUs; Linux 7.0.0-38-generic, glibc 2.39, CPython 3.12.3, regex 2026.9.29, Jinja2 3.1.6. Default scheduling/power policy, no affinity pinning. Seven fresh-process samples per checkout, alternating baseline/candidate order, two warmups per workload. Fixture creation, validation and hashing were outside timing. No model, GPU or profiling timers were needed.
Measured real
Service.prepare()using the existing synthetic tokenizer fixture (seed 268, 1,500 merges), the repository chat template, and warm piece caches. Each quoted snippet contains four protected literals.The larger quoted prompt used 95.3% less elapsed time, with a candidate range of 4.01–4.26 ms versus 85.32–88.72 ms upstream. Ordinary controls stayed close to baseline; this is a quoted-prompt CPU preparation improvement, not an inference-throughput claim.
All 84 measured outputs matched between checkouts and samples, including exact prompt-token hashes. The synthetic vocabulary is not a full model vocabulary.
Reproduction
With
regexandjinja2installed, run this from each checkout root. It reproduces one post-warmup sample for the quoted prompt at each size. Repeat in fresh processes and alternate checkout order for comparisons; the printed hash should match between branches.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.