Repository navigation
fix(mesh): take DeepSeek-V4.1 reasoning efforts as ATOM does - #3597
wanzhenchn wants to merge 16 commits into
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>
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>
cbf07b7 to
947cb58
Compare
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>
The router renders the chats it tokenizes by passing the request's reasoning_effort and chat_template_kwargs straight to the template. ATOM renders DeepSeek-V4.1 chats under the model's own rules instead, so for most reasoning controls the router's prompt, and the tokens prefix_hash and cache_aware with --kv-events route by, were not the engine's: a minimal, medium, xhigh or none effort, a disabled thinking, a thinking.effort budget, and content sent as text blocks. A tokenizer whose config.json has model_type deepseek_v41 now registers with those rules, and the router renders its chats as ATOM's _check_chat and V4.1 encoder adapter do: - thinking, which the typed request does not keep, is read from the body as sent. thinking, reasoning_effort and chat_template_kwargs resolve as in reasoning_controls under V4.1's rules, and prepare_kwargs maps the effort aliases and takes a none effort as chat mode. - Only the kwargs V4.1's encode_messages takes reach the template, and message content keeps its text blocks, which the template joins with blank lines as the encoder does. - add_default_bos_token false drops the BOS. A chat after a context kwarg, which the template does not render, is 501 token_routing_unsupported, and controls ATOM refuses are 400. The token parity fixture gets the 10 requests that tokenized otherwise than on ATOM before, and all 29 now tokenize as on ATOM with both HuggingFace and fastokens. Co-authored-by: Cursor <cursoragent@cursor.com>
947cb58 to
f56b46c
Compare
…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.
… zwan/mesh-deepseek-v41-chat-controls-r2
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.
… zwan/mesh-deepseek-v41-chat-controls-r2
Jasen2201
left a comment
There was a problem hiding this comment.
No blocking issues @ 51ad82f: one minor finding, posted inline. Comment review, not a verdict.
Checked against ATOM at this PR's base: resolve_reasoning_controls and apply_reasoning_controls under deepseek_v41_encoder.REASONING_RULES, its normalize_effort and prepare_kwargs, the adapter's thinking_mode default applied before prepare_kwargs, and the kwarg filter against the checkpoint's encode_messages signature. The Rust port matches each, including a null thinking_mode (400 on both sides), a null add_default_bos_token (no BOS on both), and an effort inherited as "none" from chat_template_kwargs. SentControls keeps only the top-level thinking and leaves the typed parse unchanged.
Evidence: the owner's runs with this PR and #3596 merged give prompt tokens identical to ATOM's HTTP chat on 59 of 59 DeepSeek-V4.1-Flash prompts, tool calls and multi-turn included. cargo test --lib --features atom-engine at 51ad82f: 1381 passed.
For the author: the owner's ATOM gRPC stack
Against today's heads of jasen/atom-grpc-* (#3672 to #3679), this PR conflicts in tokenizer_registration.rs and chat_template.rs with #3676, in inference.rs with #3677, and through #3596 in cliargs.rs with #3679. #3676 reads a worker's default_chat_template_kwargs from GetModelInfo (default_template_kwargs); once both land, deepseek_v41::template_kwargs can start from them, as ATOM's _chat_completions merges its defaults before the request's kwargs, which closes the README's --default-chat-template-kwargs gap for gRPC workers.
|
ci triage for 51ad82f:
|
…-chat-controls # Conflicts: # atom/mesh/src/core/steps/tokenizer_registration.rs
…for chat controls
…1-chat-controls-r2 # Conflicts: # atom/mesh/README.md # atom/mesh/scripts/atom_token_parity.py # atom/mesh/src/app_context.rs # atom/mesh/src/core/steps/tokenizer_registration.rs # atom/mesh/src/routers/ingress.rs # atom/mesh/src/routers/prepare/chat_template.rs # atom/mesh/src/routers/prepare/fast_tokenizer.rs # atom/mesh/tests/routing/ingress_tests.rs # atom/mesh/tests/routing/token_parity.rs
|
Blocked on the ATOM gRPC stack (#3672 to #3679, RFC #3657): please hold this PR until it lands, then apply V4.1's controls on its gRPC path and keep the HTTP change small. Over gRPC, atomesh's tokens are the prompt ATOM runs. Over HTTP, atomesh forwards the body and ATOM renders it again, so the router's tokens only pick a worker; a mismatch there costs prefix hits, not a different prompt. Proposed shape, and the GPU run behind it
We ran this shape on GPU: the stack with this PR and #3596 merged, plus a 55-line |
Keep the HTTP change small, as Jasen2201 asked: over HTTP, ATOM renders the chat again, so the router's tokens only pick a worker. V4.1's full reasoning controls (c872a6d) wait for the ATOM gRPC stack, whose prepare path runs the router's tokens. HTTP routing renders a chat it tokenizes with the request's effort as sent. V4.1's template rejects `minimal`, `xhigh` and `none`, which ATOM's V4.1 encoder accepts, so `prefix_hash` refused those chats with 400, and it renders `medium` with a smaller budget than the `high` ATOM takes it as. For a checkpoint whose config.json has `model_type` `deepseek_v41`, the router now takes the effort as the encoder does: `minimal` as `low`, `medium` and `xhigh` as `high`, and `none` as chat mode. Back to main's rendering: `thinking` as sent (`SentControls`), the kwargs filter, text blocks, `add_default_bos_token` and `context`.
|
Agreed, thanks. This PR is now the HTTP fix only (a655d0b, 13 files, +406 −43): for a The full controls stay at c872a6d on |
…1-chat-controls-r2 # Conflicts: # atom/mesh/src/app_context.rs
valar-cop
left a comment
There was a problem hiding this comment.
LGTM
ci Accuracy (gpt-oss-120b) failed — not sure yet (low): The log file in .valar/ci is empty (0 bytes), so the failure mode cannot be read. The job serves a local gpt-oss-120b checkpoint path (not deepseek_v41), whose rendering this diff leaves unchanged, so its code path is not implicated; the ATOMesh Accuracy Gate also failed on main at 2026-11-24T03:31 and 01:21.
(.valar/ci/job_138216785077.log is empty — no lines)
same job on main: ['2026-11-23T16:11 success', '2026-11-23T10:10 success']
next: Re-run the job and fetch its log; if it reproduces, compare against the same job on main before attributing it to this PR. https://github.com/ROCm/ATOM/actions/runs/45324369764/job/138216785077
ci ATOMesh Accuracy Gate failed — not sure yet (low): The log file is empty (0 bytes). This gate job also failed on main at 2026-11-24T03:31 and 01:21 (and 2026-11-23T08:53) while passing 7 other recent runs, so it may be a baseline/flaky failure rather than a regression from this PR.
(.valar/ci/job_138242408589.log is empty — no lines)
same job on main: ['2026-11-24T03:31 failure', '2026-11-24T01:21 failure', '2026-11-23T16:11 success', '2026-11-23T10:10 success', '2026-11-23T08:53 failure']
next: Re-run the gate and fetch its log; if main fails it as well, treat it as a baseline/CI issue rather than a PR defect. https://github.com/ROCm/ATOM/actions/runs/45324369764/job/138242408589
|
On the red |
|
This PR is now part of #3684, which merges its head (4c421bc) and upgrades llm-tokenizer to 2.0.0. Neither lands alone:
Please review there. This PR closes once #3684 merges. |
Following Jasen2201's request, this PR is now the HTTP fix only: the router stops refusing the DeepSeek-V4.1 chats ATOM serves. V4.1's full reasoning controls, this PR up to c872a6d, wait for the ATOM gRPC stack (#3672 to #3679, RFC #3657), whose prepare path runs the router's tokens. They stay on
zwan/mesh-deepseek-v41-chat-controls-full, which #3684 and #3685 now target, held until then.Why
To tokenize a chat for
prefix_hash, orcache_aware --kv-events, the router renders it with the model's chat template and the request'sreasoning_effortas sent. DeepSeek-V4.1's template takeslow,medium,high,maxor a budget in [1, 100], and raises on anything else. ATOM's V4.1 encoder also takesminimalaslow,mediumandxhighashigh(normalize_effort), andnoneas chat mode (prepare_kwargs). So onmain, for DeepSeek-V4.1-Flash:minimal,xhighandnoneefforts,nonewiththinkingenabled, andnoneafter a reasoned tool call.prefix_hashrefuses them with400, andcache_aware --kv-eventsplaces them by their text.mediumrenders with the template's budget of 62, where ATOM renders it ashigh, 75, so its tokens diverge from ATOM's at token 8.Change
core/steps/tokenizer_registration.rs): a tokenizer whoseconfig.json, beside itstokenizer.json, hasmodel_typedeepseek_v41registers with V4.1's controls (AppContext::chat_controls); ATOM picks V4.1's encoder by the samemodel_type(_MODEL_ENCODERS). A HuggingFace model ID whose tokenizer cannot be downloaded fails to register, and the workflow retries it, rather than register without them. Removing the tokenizer drops them.routers/prepare/chat_controls.rs): for such a model, the router takesreasoning_effort, the request's or one inchat_template_kwargs, as the encoder does. Other efforts reach the template as sent; ATOM refuses an unknown one too.IngressRouting::render_chatrenders a chat under its model's controls, if any, and the token parity test tokenizes the prompt it gives.process_chat_messagesrenders as onmain.scripts/atom_token_parity.py, 25 in all:effort_medium,effort_minimal,effort_xhigh,effort_none,thinking_enabled_effort_noneandtool_history_effort_none.13 files, +406 −43.
Left for the gRPC path
From c872a6d:
thinkingas sent and athinking.effortbudget,{"enable_thinking": false}, a message's text blocks,add_default_bos_tokenandcontext. Over HTTP, ATOM renders the chat again, so the router rendering these otherwise costs prefix hits, not a different prompt.Tests
scripts/atom_token_parity.py, thencargo test --lib --features atom-engine -- --ignored routers::ingress::token_parityon its outputmain: 5 refused, andeffort_mediumdiverges at token 8cargo test --lib --features atom-enginecargo check --features ext-proc --tests,cargo fmt --check, and black and ruff on the scriptcargo clippy --lib --tests --features atom-engineminimalarm, nononearm,render_chatignoring the controls, and the registration not keeping themdeepseek_v41_efforts_are_atoms(routers/prepare/chat_controls.rs): 13 combinations ofreasoning_effortandchat_template_kwargsgive the kwargs ATOM's encoder renders with.chats_tokenize_with_the_effort_their_model_takes(tests/routing/ingress_tests.rs): under a template that rejects efforts as V4.1's does, a V4.1 tokenizer's chats tokenize with the effort ATOM takes, another model's with the effort as sent, and an effort the template rejects gets400.test_registration_finds_the_checkpoint_chat_controlsandtest_chat_controls_download_failures_are_retried(core/steps/tokenizer_registration.rs), kept from c872a6d.