mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #567] [CLOSED] serve: tokenise the prompt from the last shared prefix, not from the start #2695
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#2695
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/567
Author: @gputier
Created: 10/3/2026
Status: ❌ Closed
Base:
main← Head:pr/prompt-encoder-0.1.37📝 Commits (10+)
5a30024calibrate: the engine's arguments come from the server's engine_args; a failed tuning says why (#447)9f60d13serve, setup: name the small split card that caps the prompt chunk; setup recommends the big card alone (#448)eb8da1d/metrics: each request's speculative draft counts, and their sums in the totals (#457)6a09387setup (--gguf-dir): GGUFs Strata cannot run are named as such, with the --family/--model to use (#444)d6a91e2docs: the Coder is weaker outside code, Chinese/CJK text included; the unpruned sizes for that (#438)d8d4ad3Merge branch 'f-135-eng' into rel-0.1.357b600d5Merge branch 'f-135-small' into rel-0.1.35d9ab843Engine 0.1.35: Windows AMD uses its bundled HIP runtime (#468 #461), the low-RAM resident mode on Windows 32 GB PCs (#467), fixes #460 #459 #446 #447 #457 #448 #444; setup requires it57bd713sycl: pipeline the expert cache's profile fill (model load 3-4x faster)e3bdd74A prompt cancelled mid-read is logged and recorded with the tokens it read (#471)📊 Changes
1 file changed (+6 additions, -3 deletions)
View changed files
📝
tools/test_strata_tokenizer.py(+6 -3)📄 Description
Based on v0.1.39 since 5 October (it was first written on v0.1.37). The rebase takes blange48's port: on 0.1.39 the plain spans of #537 (a
</think>quoted in a message is text) also decide which special matches are taken, so the shared prefix also ends where the two prompts' plain spans first differ, and the rest is encoded with its spans shifted by the resume point.A chat client sends the whole conversation every turn, and
Service.prepareruns BPE over all of it each time (serve/server.py,self.tok.encode(prompt, parse_special=True)), even when the engine's conversation cache already holds the whole prefix./v1/messages/count_tokensdoes the same.This PR adds
PromptEncoderintools/strata_tokenizer.py. It keeps the last few rendered prompts with their ids. For a new prompt it finds the last special-token boundary shared with one of them, reuses the ids up to that boundary, and sends only the text after it through BPE.The ids match a full encode by construction. Encoding splits the text at special-token matches and encodes each stretch between two matches on its own, so nothing crosses a match. Whether a match is taken at position q depends only on
text[q : q + max_special_len]and on whether q lies inside a plain span, so the cut sits at leastmax_special_len - 1characters before the end of the common prefix. The class docstring has the reasoning.The tokenizer gains
max_special_lenandencode_marked()(encode()plus the offset and id count after each special-token match).ByteTokenizergets the same two, so the mock-engine tests exercise the encoder. 0.1.39'sService.encode_prompt(), whichprepareandcount_tokensalready use, now calls the encoder.encode()itself is untouched.Diff: 5 files, 3 of them tests.
Measurement
A conversation of 100,893 tokens (433 K characters) that grows by one turn, encoding time for the new prompt, 5 repetitions:
PromptEncoder: 1.5 ms median (1.05 to 2.07)Container with 2 cores, Python 3.12, llama.cpp's
ggml-vocab-qwen35.gguf(with the template's added tokens), synthetic text mixing English, code and CJK. This is not the model pack's tokenizer and not the target machine, so the absolute numbers will differ there.Evidence that the ids are identical
On v0.1.39, in a Linux container (Python 3.13, llama.cpp's qwen35 vocabulary):
serve.test_prompt_encoderandserve.test_serverpass 146 tests,tools/test_strata_tokenizer.pypasses 11 with 1 skipped (the pack tokenizer). The rest of this section was measured on the v0.1.37 version.serve/test_prompt_encoder.py(blange48's, added with the rebase): a fuzz against the full encode with random plain spans, spans that change inside the shared part, and edited histories, plus quoted<think>/</think>across turns throughService.encode_prompt.tools/test_strata_tokenizer.py(new, 11 tests): llama.cpp's qwen35 vectors,encode_markedagainstencode,chat_golden.json, and conversations compared token for token withtok.encode. Those cover several turns with unicode and literal special tokens in the text, tool definitions, calls and responses, images, a changed reasoning effort or system message, an edited earlier turn, a conversation that gets shorter, an empty message, and interleaved conversations, with 1, 2 and 4 kept prompts. Separate tests use byte-level vocabularies whose special tokens overlap each other, which is where a cut without the look-ahead margin breaks.serve/test_server.py,IncrementalPrompts: six turns over HTTP against the mock engine, the same conversations throughService.encode_promptwith both the byte tokenizer and qwen35, and a tokenizer withoutencode_marked.Mutations: with the margin set to 0, 5 tests fail. With the cut one id too late, 305 fail.
Fuzz run outside the test suite: 40,000 cases, no difference in ids. 8,000 on qwen35 (seeds 1000 to 1099) and 32,000 on byte vocabularies with overlapping literals.
Full Python suite (
tools/test_*.py,serve/test_*.py) in a Linux container: 454 passed, 9 skipped, 0 failed on this branch, against 440 passed, 9 skipped on v0.1.37. That run uses scratch copies of both trees wheresetup.EXEis forced tostrata.exe. Without it, 47 tests fail on Linux, the same 47 on a clean v0.1.37 (test_setup_golden and one test_setup_amd test), because they expect the Windows executable name. The change is not in this PR. The skips are the pack tokenizer,jsonschemaand the Windows job objects. No existing test is modified.Limits
Servicenow importsstrata_tokenizer, and soregex, whenever the tokenizer hasencode_marked. That includes the byte tokenizer used in tests without a pack.regexis already pinned inrequirements.txt./v1/messages/count_tokensshares the same encoder, so a count request can evict another client's entry. The cost is speed only, the ids stay the same.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.