Skip to content

fix(mesh): take DeepSeek-V4.1 reasoning efforts as ATOM does - #3597

Closed
wanzhenchn wants to merge 16 commits into
mainfrom
zwan/mesh-deepseek-v41-chat-controls
Closed

wanzhenchn wants to merge 16 commits into
mainfrom
zwan/mesh-deepseek-v41-chat-controls

Conversation

@wanzhenchn

@wanzhenchn wanzhenchn commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

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.

flowchart LR
  main["main"] --> fix["#3597: HTTP effort fix"]
  full["...-chat-controls-full<br/>c872a6d: full V4.1 controls"] --> p3684["#3684 (held)"] --> p3685["#3685 (held)"]
  grpc["gRPC stack #3672 to #3679"] -. "after it lands" .-> full
Loading

Why

To tokenize a chat for prefix_hash, or cache_aware --kv-events, the router renders it with the model's chat template and the request's reasoning_effort as sent. DeepSeek-V4.1's template takes low, medium, high, max or a budget in [1, 100], and raises on anything else. ATOM's V4.1 encoder also takes minimal as low, medium and xhigh as high (normalize_effort), and none as chat mode (prepare_kwargs). So on main, for DeepSeek-V4.1-Flash:

  • 5 of the token parity chats fail to render, though ATOM serves them: the minimal, xhigh and none efforts, none with thinking enabled, and none after a reasoned tool call. prefix_hash refuses them with 400, and cache_aware --kv-events places them by their text.
  • medium renders with the template's budget of 62, where ATOM renders it as high, 75, so its tokens diverge from ATOM's at token 8.

Change

  • Which model (core/steps/tokenizer_registration.rs): a tokenizer whose config.json, beside its tokenizer.json, has model_type deepseek_v41 registers with V4.1's controls (AppContext::chat_controls); ATOM picks V4.1's encoder by the same model_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.
  • The effort (routers/prepare/chat_controls.rs): for such a model, the router takes reasoning_effort, the request's or one in chat_template_kwargs, as the encoder does. Other efforts reach the template as sent; ATOM refuses an unknown one too.
  • IngressRouting::render_chat renders a chat under its model's controls, if any, and the token parity test tokenizes the prompt it gives. process_chat_messages renders as on main.
  • Token parity: 6 more requests in the built-in set of scripts/atom_token_parity.py, 25 in all: effort_medium, effort_minimal, effort_xhigh, effort_none, thinking_enabled_effort_none and tool_history_effort_none.
  • README: what the router takes V4.1's efforts as.

13 files, +406 −43.

Left for the gRPC path

From c872a6d: thinking as sent and a thinking.effort budget, {"enable_thinking": false}, a message's text blocks, add_default_bos_token and context. Over HTTP, ATOM renders the chat again, so the router rendering these otherwise costs prefix hits, not a different prompt.

Tests

Check Result
CI's token parity step on DeepSeek-V4.1-Flash@314113f: scripts/atom_token_parity.py, then cargo test --lib --features atom-engine -- --ignored routers::ingress::token_parity on its output 25 of 25 requests as on ATOM, with HuggingFace and fastokens; the unknown effort is refused, as on ATOM. Rendering as on main: 5 refused, and effort_medium diverges at token 8
cargo test --lib --features atom-engine 1380 passed, 8 ignored
cargo check --features ext-proc --tests, cargo fmt --check, and black and ruff on the script Clean
cargo clippy --lib --tests --features atom-engine No new warnings
4 mutations, one at a time: no minimal arm, no none arm, render_chat ignoring the controls, and the registration not keeping them Each fails at least one test
  • deepseek_v41_efforts_are_atoms (routers/prepare/chat_controls.rs): 13 combinations of reasoning_effort and chat_template_kwargs give 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 gets 400.
  • test_registration_finds_the_checkpoint_chat_controls and test_chat_controls_download_failures_are_retried (core/steps/tokenizer_registration.rs), kept from c872a6d.

--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>
@wanzhenchn wanzhenchn added the ci:mesh Mesh-only PRs: pass the Accuracy checks on the ATOMesh CI label Nov 19, 2026
@wanzhenchn
wanzhenchn added this pull request to stack #3598 November 19, 2026 15:21
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>
@wanzhenchn
wanzhenchn force-pushed the zwan/mesh-deepseek-v41-chat-controls branch from cbf07b7 to 947cb58 Compare November 20, 2026 02:53

@valar-cop valar-cop left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

ci green.

wanzhenchn and others added 2 commits November 20, 2026 12:27
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>
@wanzhenchn
wanzhenchn force-pushed the zwan/mesh-deepseek-v41-chat-controls branch from 947cb58 to f56b46c Compare November 20, 2026 13:21
…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.
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.

@Jasen2201 Jasen2201 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@valar-cop

Copy link
Copy Markdown

Base automatically changed from zwan/mesh-fastokens-encode to main November 23, 2026 06:27
…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
@Jasen2201

Copy link
Copy Markdown
Collaborator

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
  • gRPC: prepare_chat renders a V4.1 chat with chat_controls, starting from the worker's default_chat_template_kwargs ([ATOM gRPC] atomesh: discover ATOM gRPC workers and run ATOM P/D over gRPC #3676 reads them from GetModelInfo) with the request's kwargs over them, as ATOM's _chat_completions merges them. This also closes the README's --default-chat-template-kwargs gap.
  • HTTP: only what stops the router refusing the 4 chats ATOM accepts.
  • Token parity: the 10 requests through the gRPC prepare path, where a mismatch changes the model's input.

We ran this shape on GPU: the stack with this PR and #3596 merged, plus a 55-line render_chat in prepare/mod.rs for the gRPC part. Prompt tokens matched ATOM's HTTP chat on 59 of 59 DeepSeek-V4.1-Flash prompts. Happy to share that diff.

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`.
@wanzhenchn
wanzhenchn removed this pull request from stack #3598 November 23, 2026 09:24
@wanzhenchn wanzhenchn changed the title feat(mesh): render DeepSeek-V4.1 chats under ATOM's reasoning controls fix(mesh): take DeepSeek-V4.1 reasoning efforts as ATOM does Nov 23, 2026
@wanzhenchn

Copy link
Copy Markdown
Contributor Author

Agreed, thanks. This PR is now the HTTP fix only (a655d0b, 13 files, +406 −43): for a deepseek_v41 checkpoint, the router takes reasoning_effort as V4.1's encoder does, minimal as low, medium and xhigh as high, and none as chat mode. It was 5 chats, not 4: the template rejects xhigh too. All 25 token parity requests now tokenize as on ATOM; everything else renders as on main.

The full controls stay at c872a6d on zwan/mesh-deepseek-v41-chat-controls-full, which #3684 and #3685 now target, held. Once the stack lands, I'll move them onto its gRPC prepare_chat, with the worker's default_chat_template_kwargs under the request's, and run the parity requests through that path. Please do share your 55-line render_chat diff; I'll start from it.

…1-chat-controls-r2

# Conflicts:
#	atom/mesh/src/app_context.rs

@valar-cop valar-cop left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@wanzhenchn

Copy link
Copy Markdown
Contributor Author

On the red Accuracy (gpt-oss-120b): it is main's, not this PR's. main's own push run on b73e5de fails it the same way (job): since #3698 the router renders standalone chats, and llm-tokenizer 1.4.2 has no strftime_now, which gpt-oss's template calls. #3684 fixes it; this PR merges main once that lands.

@wanzhenchn

Copy link
Copy Markdown
Contributor Author

This PR is now part of #3684, which merges its head (4c421bc) and upgrades llm-tokenizer to 2.0.0. Neither lands alone:

  • This PR cannot pass ATOMesh Accuracy Gate while main's gpt-oss-120b chats fail to render on 1.4.2.
  • The upgrade alone breaks the token parity step for V4.1.

Please review there. This PR closes once #3684 merges.

@wanzhenchn

Copy link
Copy Markdown
Contributor Author

Landed through #3684, merged as 15dec0e. That PR merged this head (4c421bc) in and added what DeepSeek-V4.1 needs under llm-tokenizer 2.0.0. Closing.

@wanzhenchn wanzhenchn closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:mesh Mesh-only PRs: pass the Accuracy checks on the ATOMesh CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants