Repository navigation
feat(mesh): encode the prompts of listed models with fastokens - #3596
Conversation
--fastokens-models names the models, by the name or source their
tokenizer registers under, or `*`, whose prompts the router encodes with
fastokens 0.3.6 rather than HuggingFace `tokenizers`. Decoding, chat
templates and vocabulary lookups stay with the HuggingFace tokenizer.
A model encodes with fastokens only once fastokens encodes a set of
probe texts exactly as its HuggingFace tokenizer does, with special
tokens and without: added tokens, digits, whitespace, several scripts, a
chat and a 1 MiB text. The ids the HuggingFace post-processor puts
around a text are added to fastokens' own, so a tokenizer_config.json
that adds a BOS the tokenizer.json does not still gets it. Otherwise the
model keeps HuggingFace with a warning, or under --fastokens-strict its
tokenizer fails to register; mesh_fastokens_tokenizers_total{outcome}
counts tokenizers as loaded, fallback or refused. Tokens only
tokenizer_config.json declares encode as the tokens they declare, as in
transformers and so on the engine.
These models tokenize up to --fastokens-max-tokenize-bytes, /v1/tokenize
included: 64 MiB by default, or --prepare-max-tokenize-bytes if that is
larger. --fastokens-segment-cache-mb sizes fastokens' segment cache,
which takes the place of --tokenizer-cache-* for these models.
test_requests_tokenize_as_on_atom checks, with either backend, that the
DeepSeek-V4.1-Flash requests of tests/fixtures/atom_tokens tokenize for
routing as on ATOM, two prompts over 2 MiB among them, with the tokenizer
files of the revision recorded there, which it downloads.
scripts/atom_token_parity.py records the fixture, or another checkpoint's
requests, by sending them to ATOM's own chat and completion endpoints in
process, with an engine that keeps the prompt tokens it is given.
Co-authored-by: Cursor <cursoragent@cursor.com>
🏷️ CI GuideRuns automatically on every eligible PR before approval:
Heavy model tests:
Path scope:
|
valarLip
left a comment
There was a problem hiding this comment.
⚠️ Action required before merge (7)
- Refuse tokens that only
tokenizer_config.jsondeclares unless their ids are the ones transformers assigns.- Where:
routers/prepare/fast_tokenizer.rs:192-209(check()). - What goes wrong: fastokens keeps each declared id. transformers (
AutoTokenizer, which the engine uses) gives such tokens the next free ids instead. CPU probes on the Llama-4 tokenizer files:- a gap: declared 201140, transformers 201135;
- a vocab collision: declared 1000, transformers 201135, and 1000 decodes as "ств".
- Why nothing catches it:
check()never compares ids, and it skips every probe text that contains such a token. - Where it lands: on gRPC/atom-engine,
prepare_chat/prepare_generate(prepare/mod.rs:61-72,286-295) send these ids as the model prompt. - Fix: fall back (or fail under strict) unless the config-only ids, sorted, are exactly N, N+1, …, where N = tokenizer.json vocab size including its added tokens. Add gap and collision tests.
- Docs: correct the README line "as transformers, and so the engine". Say that on gRPC the fastokens ids are the model input.
- Where:
- Show that an llm-tokenizer bump does not already solve this before adding a second encoder.
- mesh pins
llm-tokenizer =1.4.2. - 1.11.0 (2026-11-03) has a native byte-level BPE path that produces HF-identical ids, names DeepSeek, and encodes inputs ≥64 KiB in parallel.
- This PR adds a second encoder, a second cache (it switches
CachedTokenizeroff for listed models,tokenizer_registration.rs:142-145), a second byte limit and a new crate. - Please time the 2 MiB DeepSeek-V4.1 prompt with llm-tokenizer ≥1.11. Keep fastokens only if that misses the target, and give the numbers.
- mesh pins
- Keep the HuggingFace download out of the default
cargo test --librun.- Where:
tests/routing/token_parity.rs:41-72, which panics on any download failure (:66). It is compiled into the lib tests (ingress.rs:911-914). - Effect: CI runs it twice (the unit step, then the KV-events step's
routers::ingressfilter). An HF outage or an offline run fails every mesh PR. - Fix: mark it
#[ignore]and run it in one dedicated CI step with--ignored. Don't just skip on failure, which would silently drop the only real-vocabulary check. Treatchat_template.jinjaandconfig.jsonas optional (valar-cop threads 1–2).
- Where:
- Split the PR (first review: +1982/−36 across 18 files, over 1000 lines / 15 files).
- Move the parity harness to its own PR:
token_parity.rs, the fixture,scripts/atom_token_parity.py, the README paragraph and the mod hook. That is about 830 lines. - Drop the duplicated tests:
fast_tokenizer.rs:564-628repeatstokenizer_registration.rs:399-459and the ingress limit tests.
- Move the parity harness to its own PR:
- Fix the help and docs text this PR made stale (Nit).
tokenizer_registration.rs:47,68-75,87-96,286-291still say the tokenizer is wrapped inCachedTokenizer, or log "with caching".- The
--prepare-max-tokenize-byteshelp (config/types.rs:157), the--ext-proc-max-tokenize-byteshelp (ext_proc/config.rs:48) andscripts/ext-proc/README.md:55-58give a limit that no longer applies to fastokens models.
- Don't let one config-only token switch off the 1 MiB and chat probes (Nit).
- Where:
fast_tokenizer.rs:206-209skips any probe text containing such a token, so a token like</s>or[INST]drops the long probe. The chat probe is also dropped silently when the template fails to render (:295-304). - Fix: strip those tokens from the probe texts instead of skipping them. Also add a probe of ≥256 KiB with no newlines.
- Where:
- Count a
--fastokens-strictrefusal once (Nit).LoadTokenizerStepretries it 3× (tokenizer_registration.rs:217-219,300-304). Each retry re-runs the 1 MiB probe and adds another{outcome="refused"}.- Fix: make the refusal non-retryable.
valar-cop thread 3: enable_segment_cache(0) does turn the cache off (fastokens lib.rs:1360). Please add a Some(0) assert and resolve the thread.
Reviewed at 8f929e0 (deep; 4 hunters, 2 verifiers, C2). Flag-off per-request cost was not measured (no Rust toolchain on this host); from reading the code it is one TypeId compare. CI unit lane is green, including all new tests.
|
ci triage for cfc168a:
|
Tokens only tokenizer_config.json declares must have the ids transformers gives them: the ones after the vocabulary's and the added tokens', in the order of the ids they declare. fastokens gives them the ids they declare, so a model whose tokens declare others is refused. The probe texts leave those tokens out instead of being skipped where they have one. The chat probe renders without a system message for a template that has no system role, and a template that renders neither is logged. A 256 KiB text with no newline makes fastokens search for added tokens across its threads, which split a text only at newlines. A --fastokens-strict refusal is what the probe found, so registering is not retried. test_requests_tokenize_as_on_atom downloads, so it is ignored by default and runs in a CI step of its own; a checkpoint may have no config.json or chat_template.jinja. The help of the byte limits fastokens models do not have names --fastokens-max-tokenize-bytes, and the registration's docs and log say which tokenizers fastokens encodes. A test checks that --fastokens-segment-cache-mb 0 turns the cache off, and two tests the registration and ingress tests repeat are dropped. Co-authored-by: Cursor <cursoragent@cursor.com>
|
Thanks for the review. Addressed in 0827639, a commit on top of 8f929e0, item by item:
valar-cop thread 3: Locally, |
valarLip
left a comment
There was a problem hiding this comment.
⚠️ Action required before merge (5)
- Retry source-resolution failures; add the strict prefix only to real probe refusals.
- Where:
fast_tokenizer.rs:80-88,111. - Problem: every
with_fastokenserror getsSTRICT_REFUSAL, and that includestokenizer_json()'s second hub call for HF ids (llm-tokenizerrepo.info(), which always goes to the network).is_retryable(tokenizer_registration.rs:218-225) then never retries it. - Effect: under
--fastokens-strict, one network blip leaves the model unregistered for good. Without strict, it falls back to HF for good, with the warning "fastokens cannot encode these prompts as HuggingFace does". - Fix: prefix only
FastTokenizer::new(and JoinError) errors. Returntokenizer_jsonerrors unprefixed, so strict retries them, and give non-strict an accurate fallback message. - Text and tests: fix the
STRICT_REFUSALdoc (:40-42), theis_retryablecomment, README "with no retries" and the squash text. Add a test for a download failure.
- Where:
- Make config-only ids decodable, or document that they are not.
- Where:
fast_tokenizer.rs:408-441sends decode,token_to_idandid_to_tokento the HF inner tokenizer. The inner does not know these tokens (llm-tokenizerhuggingface.rs:55-63) and silently drops their ids. - Effect:
/v1/tokenize→/v1/detokenizenow loses that text (probev3/scripts/probe_decode.py), which never happened on main. - Fix: either decode them from fastokens' added-token table, or say so in the module doc, the README and the
--fastokens-modelshelp. Add a round-trip test either way.
- Where:
- Fix the docs and help (Nit).
- ext-proc limit: the fastokens limit is
max(64 MiB, prepare limit), and the prepare limit falls back to--ext-proc-max-tokenize-bytes(types.rs:551-553,596-600). So "Neither byte limit applies…" (scripts/ext-proc/README.md:58-60,ext_proc/config.rs:49) and the README/help limit lines are wrong above 64 MiB. - Config-only ids: README:296-298 and the test doc at
fast_tokenizer.rs:582-584should say "after the vocabulary's and added tokens'". - Segment cache:
tokenizer_registration.rs:47-49says fastokens "has fastokens' segment cache instead", but that cache is off by default. - Help heading: use
next_help_heading = "Tokenizer"(types.rs:181), not a second "Tokenization" heading, and note in the--tokenizer-cache-*help that it does not apply to these models.
- ext-proc limit: the fastokens limit is
- Close the test gaps (Nit).
- Limit:
fastokens_models_tokenize_up_to_their_own_limitnever sends a fastokens prompt over its limit, so makingingress.rs:873unlimited passes. Add one. Some(0): withFASTOKENS_SEGMENT_CACHEunset the assert atfast_tokenizer.rs:743-746cannot fail. Set the env in a#[serial]test.- Timeout: add a timeout to
reqwest::get(token_parity.rs:61) ortimeout-minutesto the new CI step.
- Limit:
- Small cleanups (Nit).
- Delete the unreachable
FastTokenizer::set_chat_template(:471-475) and the now-unusedanyhow!import. - Have the recorder store an existing checkpoint path resolved (
atom_token_parity.py:352). A relative path breaks the test, which runs fromatom/mesh. - Refuse a config-only token whose
added_tokens_decoderentry leaves outnormalizedwhen the tokenizer has a normalizer: transformers reads it as!special, fastokens asfalse. - Resolve
TokenizeLimitsonce at build instead of per request (ingress.rs:650,tokenize.rs:132), which is zero-cost.
- Delete the unreachable
Not required here (pre-existing): the gRPC prepare path applies no tokenize byte limit (prepare/mod.rs:69,290).
Reviewed at 0827639 (deep; 3 hunters, 4 verifiers, C2). Already fixed: config-only id check, #[ignore] + one CI step (ran and passed), optional files, duplicated tests removed, stale docs at the named sites, probes stripped / 256 KiB flat / system-less chat, strict refusal not retried, valar-cop threads (all resolved). llm-tokenizer ≥1.11 numbers given; keeping fastokens is a design call. Items 2–5 were present at 8f929e0 and missed by our last round. Only CI failure: Slurm sbatch submission (infra).
A tokenizer.json fastokens cannot find or download says nothing of how it encodes, so under --fastokens-strict the registration fails and is retried, not refused, and without it the model keeps HuggingFace with a warning that says so. Only what fastokens' probe finds is a refusal. HuggingFace tokenizers does not have the tokens only tokenizer_config.json declares and dropped their ids when decoding, so /v1/detokenize lost the text /v1/tokenize encoded as them. fastokens now looks them up and decodes the ids that include them, at once and token by token, and a model with such tokens must also decode the probe texts as HuggingFace does, keeping special tokens and skipping them. A model with a normalizer and such a token that is not special and does not say whether it is normalized is refused: transformers takes it as normalized, fastokens as not. The tokenization byte limits resolve once, as the app context is built. The help and docs say fastokens models have the larger of 64 MiB and the other models' limit, which falls back to --ext-proc-max-tokenize-bytes; the fastokens options are under the Tokenizer heading, and the --tokenizer-cache-* help says it is for the models fastokens does not encode. A test sends a fastokens prompt over its own limit, one sets FASTOKENS_SEGMENT_CACHE where --fastokens-segment-cache-mb 0 turns the cache off, the parity test's downloads time out, and the recorder stores a checkpoint path absolute. The unreachable set_chat_template is gone. Co-authored-by: Cursor <cursoragent@cursor.com>
|
Thanks. All five are in 11ca1b8, and #3597 is rebased on it.
Nine mutations, one per new behaviour, each fail at least one test. |
… its segment cache These routers tokenize every prompt. fastokens encodes them as Hugging Face does, and its segment cache, which checks every hit against the text it keeps, takes the place of a prefix cache: 1 GiB, of which 72 sessions of up to 1M tokens take about 0.6 GB. The options come with #3596.
…ture The token parity step now has ATOM tokenize the requests in the job, with this commit's code: it installs ATOM and a CPU torch in a venv, and atom_token_parity.py downloads a DeepSeek-V4.1-Flash revision's tokenizer files and chat encoder from HuggingFace and writes ATOM's tokens, which the test compares the router's with. A change to ATOM's rendering and one to the router's are checked together, where a fixture recorded from another ATOM could go stale. The fixture and the test's own download are gone. The script takes a HuggingFace repo@revision for the checkpoint and downloads its tokenizer files and the chat encoder some models ship, which ATOM renders DeepSeek-V4.1's chats with. The test reads $ATOM_TOKEN_PARITY only, and takes the median of 5 encodes of every prompt.
…-chat-controls Brings in #3596's check of token parity against ATOM in CI, which replaces the recorded fixture. This PR's 10 requests are in the script's built-in set, so CI checks them too.
An agent re-sends its history with every request, and fastokens encodes the text between special tokens one segment after another on the calling thread, so without its segment cache the router encodes the whole history again on every turn. The cache keeps the ids of those segments, checked against their text on every hit, so a turn encodes little more than its newest segments. --fastokens-segment-cache-mb now defaults to 1024 MiB per tokenizer, which the cache grows into as it fills; 0 turns it off. The token parity test turns it off, as its timed encodes repeat a prompt.
|
Pushed 9d88181: fastokens' segment cache is on by default, 1 GiB per tokenizer, and It follows new timings of the five encoders across prompt sizes, concurrent requests and an agent's session, with DeepSeek-V4.1-Flash's tokenizer on a 64-vCPU host, each library on its default threads and a new agentic text each encode. All five give the same tokens.
2.0.0 leads only on one prompt of 2 MiB or more, by 1.3x to 1.8x, on about 25 cores to fastokens' 2.3. That is the case the earlier timings measured, and repeating one 2 MiB prompt still gives 6.0 ms to 9.8 ms. fastokens encodes a chat's text between special tokens one segment after another, so an agent's turn re-encodes the history it re-sends; with the segment cache, a turn at 2 MiB encodes in 1.6 ms instead of 6.4 ms. fastokens also loads in about 1 s, and its first encode takes 60 to 100 ms; registration does both, with the probe texts, before the router serves. |
fastokens' `from_file` also adds the tokens only `tokenizer_config.json` declares, which HuggingFace `tokenizers` encodes as text. Giving them the ids `transformers` gives took an id check, a normalization check, a decode check and a decode path through fastokens. Of 54 local checkpoints, only Qwen3.5 declares such tokens (7 audio and TTS tokens), and main's HuggingFace tokenizer encodes them as text too. Loading `tokenizer.json` alone keeps fastokens' tokens HuggingFace's, so decoding and lookups stay with HuggingFace and the probe only compares encodings. fast_tokenizer.rs goes from 944 to 664 lines.
|
Pushed cfc168a: fastokens now loads So the id, normalization and decode checks for those tokens, the decode path through fastokens, and their tests are gone. Decoding and lookups stay with HuggingFace, and the probe only compares encodings.
|
Jasen2201
left a comment
There was a problem hiding this comment.
No blocking issues @ cfc168a: one stale comment, posted inline. Comment review, not a verdict.
Checked: the probe (texts, affixes, the 1 MiB and 256 KiB cases), strict refusal versus retried download, the per-tokenizer limits, and in fastokens 0.3.6 the segment cache (hits confirmed against the stored text, a byte budget per shard) and its single process-wide thread pool. I found no code defect.
Evidence for parity, beyond CI:
- A review-only fuzz: 1500 random texts per tokenizer, with and without special tokens, each encoded twice so the second goes through a 256 MiB segment cache, plus a 64-text batch. 0 mismatches against HuggingFace on the 10 local checkpoints the probe accepts (DeepSeek-V4.1-Flash, R1-0528, Qwen3.5, GLM-5.2, GLM-4.7, MiniMax-M3.1, Llama-3.1, gpt-oss, Ling, diffusiongemma).
- The owner's runs with this PR and #3597 merged: prompt tokens identical to ATOM's HTTP chat on 59 of 59 DeepSeek-V4.1-Flash prompts, up to 690K tokens, and 0 failed requests at 6P16D C1228.
More detail
- Probe refusal works as meant:
amd/DeepSeek-R1-0528-MXFP4-v2ships atokenizer.jsonwithtruncation.max_length: 512, and the probe refuses it ("fastokens encodes ... (special tokens: true) as [5822] from token 512, HuggingFace as []"). Not this PR's: onmainthe HuggingFace path truncates that model's prompts to 512 tokens too, wheretransformersin ATOM does not. - Owner's encode timing, single core, 512 MiB segment cache: 0.64 ms first turn and 0.15 ms later turns, against 126 ms and 5.3 ms for ATOM's Python front end, identical ids on 363 AgentX prompts.
cargo test --lib --features atom-engineat the #3597 head 51ad82f: 1381 passed.- For the author, merging with the owner's ATOM gRPC stack (#3672 to #3679, heads of
jasen/atom-grpc-*today): this PR conflicts intokenizer_registration.rsandchat_template.rswith #3676, and incliargs.rswith #3679. #3676 sets a worker's inline chat template withArc::get_mut(..).set_chat_template(..)on the loaded tokenizer; in the merge that has to run beforewith_fastokens, becauseFastTokenizerholds the innerArcand keeps the trait's refusingset_chat_template.
… its segment cache These routers tokenize every prompt. fastokens encodes them as Hugging Face does, and its segment cache, which checks every hit against the text it keeps, takes the place of a prefix cache: 1 GiB, of which 72 sessions of up to 1M tokens take about 0.6 GB. The options come with #3596.
The first of two PRs, both against
main: #3597 builds on this one, and targets its branch until it merges.Why
For
prefix_hash, andcache_awarewith--kv-events, the router tokenizes requests in its preparation pool with the model's HuggingFace tokenizer: a chat's prompt, which it renders with the model's chat template, a completion's prompt, and a/v1/tokenizebatch. HuggingFacetokenizers, throughllm-tokenizer1.4.2, takes 1.2 s of a preparation thread to encode a 2 MiB agentic prompt of DeepSeek-V4.1-Flash, and--prepare-max-tokenize-bytesrefuses prompts over 1 MiB by default. fastokens encodes the same tokens in 10 ms.llm-tokenizer1.11 and later also encode these prompts in milliseconds, with the same tokens, but on most of the host's cores. On a 64-vCPU host, with a new agentic text each encode, 2.0.0 leads fastokens only from 2 MiB, by 1.3x to 1.8x on about 25 cores of CPU to fastokens' 2.3, and trails it below, by 1.1x at 1 MiB to 6.5x at 64 KiB. Weighted by the prompt sizes of an agentic trace (median 262K tokens), a request takes 3.6 ms with either, for 95 ms of CPU with 2.0.0 and 8 ms with fastokens. With 32 requests at once fastokens encodes 2x to 10x the bytes a second, and the p99 of a 256 KiB prompt is 3.4 ms to 2.0.0's 187 ms. An agent's session of 127 turns to 2 MiB encodes in 172 ms with fastokens and its segment cache, 243 ms with 2.0.0 and its L1 cache, and 72 s with 1.4.2. The earlier timings, where 2.0.0 was 1.6x as fast, re-encoded one 2 MiB prompt, a size where it leads. From 1.11 on they also pick a built-in DeepSeek-V4.1 chat encoder fromconfig.json, whichmainrefuses to route by (501 token_routing_unsupported), and which, allowed, renders the V4.1 chats that turn thinking off otherwise than ATOM. This PR leavesllm-tokenizeras it is and lets a deployment opt models into fastokens; the token parity test it adds is the check an upgrade would have to pass.Change
--fastokens-modelsnames the models whose prompts the router encodes with fastokens 0.3.6, by the name or source their tokenizer registers under, or*for every model.FastTokenizer(routers/prepare/fast_tokenizer.rs) encodes with fastokens, and leaves decoding, chat templates and vocabulary lookups to the HuggingFace tokenizer. fastokens is built withouthf-hub: it loads thetokenizer.jsonthe factory loaded, from a file, a directory or a HuggingFace download.tokenizer_config.jsonthat adds a BOS itstokenizer.jsondoes not still gets it. A model that fails the probe keeps HuggingFace, with a warning, or under--fastokens-strictits tokenizer fails to register, with no retries. Atokenizer.jsonthat fastokens cannot find or download fails no probe: the model keeps HuggingFace, with a warning that says so, or under--fastokens-strictits registration fails and is retried.mesh_fastokens_tokenizers_total{outcome}counts tokenizers asloaded,fallbackorrefused.tokenizer_config.jsondeclares encode as text, as HuggingFacetokenizersencodes them onmain, wheretransformers, and so the engine, encodes them as tokens: fastokens loadstokenizer.jsonalone, not with itsfrom_file, which adds them. Of 54 local checkpoints, only Qwen3.5 declares such tokens (7 audio and TTS tokens). With gRPC workers the router sends the ids it encodes as the prompt, so the model runs on fastokens' ids, not only routes by them.--fastokens-max-tokenize-bytes,/v1/tokenizebatches included: by default the larger of 64 MiB and the other models' limit (--prepare-max-tokenize-bytes, or with ext-proc--ext-proc-max-tokenize-bytes), so a prompt over 1 MiB encodes. The limits resolve once, as the app context is built.--fastokens-segment-cache-mbsizes fastokens' cache of the ids of the text between special tokens, per tokenizer: 1024 MiB by default, which it grows into as it fills, and0turns it off. An agent re-sends its history with every request, and with the cache a turn encodes little more than its newest segments; every hit is checked against the segment's text. It takes the place of--tokenizer-cache-*for these models.test_requests_tokenize_as_on_atom(tests/routing/token_parity.rs) tokenizes a set of DeepSeek-V4.1-Flash requests for routing with each backend, and checks their tokens against the ones ATOM runs them with, which ATOM gives in the same CI job, with the same commit's code. The 19 requests are chats with reasoning controls, tools and tool history, multi-turn chats, scripts and special-token text, a completion, and an agentic history and a completion over 2 MiB, which the test builds from the lines the script records and checks by token count and BLAKE2b digest. The test is#[ignore]d, and the mesh workflow (atomesh-mocker-benchmark.yaml) runs it in a step of its own after the unit tests, with their build: the step installs ATOM and a CPU torch in a venv, runsscripts/atom_token_parity.pyfor a DeepSeek-V4.1-Flash revision, and thencargo test --lib -- --ignored routers::ingress::token_parityon what the script writes ($ATOM_TOKEN_PARITY), within 20 minutes. No tokens are kept in the tree, so a change to ATOM's rendering and one to the router's are checked together.scripts/atom_token_parity.pysends the requests, or recorded ones, to ATOM's own/v1/chat/completionsand/v1/completionsin process, with an engine that keeps the prompt tokens it is given and generates nothing. It needs ATOM's Python dependencies but no GPU, and copies none of ATOM's rules. For a HuggingFacerepo@revisionit downloads the files ATOM and the router read, from HuggingFace or$HF_ENDPOINT: the tokenizer files,config.json,chat_template.jinja, and the chat encoder some models ship, which ATOM renders DeepSeek-V4.1's chats with. It stores the checkpoint's path absolute, as the test reads it fromatom/mesh.--prepare-max-tokenize-bytesand--ext-proc-max-tokenize-bytesand the ext-proc README say which limit these models have, the fastokens options are under the Tokenizer heading, the--tokenizer-cache-enable-l0/l1help says it is for the models fastokens does not encode, and the registration's log says which tokenizers fastokens encodes.21 files, +2128 −65.
Cost
A tokenizer that fastokens encodes for is loaded twice, by HuggingFace and by fastokens, and probed once. fastokens splits a long text between special tokens across a pool of its own threads, one per logical core up to 32 (
FASTOKENS_BPE_THREADS), next to the preparation pool, and its segment cache takes up to 1 GiB per tokenizer. The token parity step installs ATOM and a CPU torch, downloads 6.4 MB from HuggingFace, or$HF_ENDPOINT, and has ATOM tokenize the requests, which took 49 s and 1 GB on a CPU host.Results on DeepSeek-V4.1-Flash
A release build, against ATOM's tokens, median of 5:
reasoning_effortATOM refusesfastokens gives HuggingFace's tokens for every request, and encodes them 13.5x to 120x as fast (median 35.8x). #3597 adds 10 requests that set reasoning controls ATOM resolves under DeepSeek-V4.1's own rules, or send text blocks. The router renders those otherwise than ATOM before it, with either backend.
Tests
routers/prepare/fast_tokenizer.rs:fastokens_encodes_as_huggingface_and_leaves_the_rest_to_it: texts with added tokens, digits and other scripts encode, one at a time and in a batch, as HuggingFace encodes them, with and without a BOS and EOS, and decoding, templates and lookups are HuggingFace's.special_tokens_are_the_ones_huggingface_adds: DeepSeek-R1-0528'stokenizer.jsonadds no BOS and itstokenizer_config.jsondoes, and fastokens adds the one HuggingFace adds.tokens_only_tokenizer_config_declares_encode_as_huggingface_does: a token onlytokenizer_config.jsondeclares encodes as text, as HuggingFace encodes it, with special tokens and without.probe_texts_keep_the_long_ones_and_the_chat: the probe texts keep the 1 MiB text, the 256 KiB one with no newline, with added tokens, and the chat.the_chat_probe_needs_no_system_role: a template that refuses a system message is probed with a chat without one, and one that renders no chat is logged.a_tokenizer_fastokens_encodes_differently_is_refusedandtokenizers_fastokens_cannot_load_are_refused: the probe refuses a tokenizer with other merges, aWhitespacepre-tokenizer, which fastokens does not have, and a tokenizer that is not HuggingFace's.listed_models_encode_with_fastokens: by name, source and*; the segment cache has the size--fastokens-segment-cache-mbgives, 1 GiB without it, and none with0.core/steps/tokenizer_registration.rs:test_registration_encodes_listed_models_with_fastokens: the registered tokenizer is fastokens' and leaves the tokenizer cache out; a tokenizer fastokens cannot load keeps HuggingFace, or fails to register under--fastokens-strictwith an error the workflow does not retry, where it retries a tokenizer that fails to load.test_fastokens_download_failures_are_retried: with$HF_ENDPOINTat a closed port, atokenizer.jsonfastokens cannot download fails a--fastokens-strictregistration with an error the workflow retries, and otherwise keeps HuggingFace.tests/routing/ingress_tests.rs:fastokens_models_tokenize_up_to_their_own_limit: a completion and a chat over--prepare-max-tokenize-bytesare refused with HuggingFace and tokenize with fastokens, and over--fastokens-max-tokenize-bytesare refused with both.test_requests_tokenize_as_on_atom(tests/routing/token_parity.rs, ignored by default): the requests tokenize as ATOM tokenizes them in the same job, with both backends, and a request ATOM refuses is refused.cli_fastokens_options_map_to_router_configandfastokens_tokenize_limit_must_be_positive: the options and their validation.The tests that set
HF_ENDPOINT, and the token parity test, are#[serial]. The token parity test turns the segment cache off, as its timed encodes repeat a prompt.cargo test --lib --features atom-enginehttpx:scripts/atom_token_parity.py deepseek-ai/DeepSeek-V4.1-Flash@314113f8a7da, thencargo test --lib --features atom-engine -- --ignored routers::ingress::token_parityon its output$ATOM_TOKEN_PARITYunsetcargo check --features ext-proc --testscargo test --libon its filters, and--test api_tests -- kv_events_testcargo fmt --check, and black and ruff on the scriptcargo clippy --lib --tests --features atom-engineFastTokenizer, the registration not wrapping listed models, the test's synthetic lines changed, fastokens loaded withfrom_file, the 256 KiB probe dropped, the chat probe's retry without a system message dropped, an unrendered chat not logged, a strict refusal retried, its message reworded, a segment cache of0turned on, a download failure refused under--fastokens-strictor failing the registration without it, no limit for fastokens prompts in ingress, and the segment cache's default size dropped