Skip to content

[RFC] ATOM gRPC interface for atomesh: token ids in, token ids out #3657

Open

Description

@Jasen2201

Summary

atomesh, the router in atom/mesh, talks to ATOM over HTTP today. Every ATOM API process renders the chat template, tokenizes, detokenizes, parses and writes SSE, in Python. This RFC adds atom.grpc.v1, a gRPC service beside ATOM's HTTP server: atomesh sends token ids, ATOM streams token ids back, and the Python work moves into atomesh's Rust. ATOM's scheduler, its P/D KV path and kv_transfer_params do not change. The change lands as 8 PRs, #3672 to #3679.

Problem and motivation

With the model forward priced at 0 s on fake engines in the 6P16D layout, AgentX at C1228 over HTTP spends about 140 ms of CPU per request in each P instance's API processes. The busiest one sits at a full core, requests queue behind it, and TTFT P99 reaches 26.2 s with no model time at all. That work is Python under one GIL per API process; --api-server-count spreads it over more processes but does not shrink it.

atomesh's gRPC mode already renders, tokenizes, detokenizes and parses in Rust for SGLang and vLLM workers, but ATOM had no service that takes token ids. Tokens in, tokens out buys three things:

  • The prompt is tokenized once, in atomesh, on many threads. With fastokens (feat(mesh): encode the prompts of listed models with fastokens #3596), a later AgentX turn encodes in 0.15 ms on one core against 5.27 ms in ATOM's Python.
  • ATOM's API process only admits requests and streams ids: 7.4 ms of CPU per request on P instead of 140 ms.
  • Parity is exact and checkable: both doors' prompt ids can be compared id for id.

Goals and non-goals

Goals:

  • ATOM serves atom.grpc.v1.AtomEngine with --grpc-port, beside HTTP, from the process that owns the engine.
  • For n = 1, a request through atomesh's gRPC mode answers as ATOM's HTTP chat and completions routes answer it: admission, refusals, stops, usage and the response's shape.
  • ATOM's P/D flow runs over gRPC unchanged: relay and chunked transfer, rank mapping, DP ranks, kv_transfer_params.
  • An opt-in Session carries many requests on one stream, one message per engine step.

Non-goals:

Architecture overview

Where the serving work runs, before and after

Today atomesh forwards OpenAI JSON with the prompt as text, and ATOM answers with SSE text. With gRPC, atomesh sends a GenerateRequest holding the prompt's token ids and the stop strings as token ids, and ATOM answers with token ids, one chunk per engine step. Rendering, tokenizing, detokenizing, reasoning and tool parsing and the SSE answer leave ATOM's GIL-bound API processes and run once per request in atomesh. ATOM's API process keeps admission and the prefix block hashes. The engine, the scheduler and the P/D KV path are untouched.

Design decisions

Decision Why PR
Token ids on the wire The Python front end is the bottleneck: about 140 ms of CPU per request on each P instance at C1228. atomesh's gRPC mode already tokenizes, so ATOM need not again, and prompt ids become comparable id for id. #3673, #3675
The server is a thread in the API process It calls the HTTP routes' own admission helpers, so both doors accept, refuse and answer alike. No new process; one new port. #3673
kv_transfer_params and the P/D flow stay atomesh's ATOM pair reuses the HTTP P/D router's AtomAdapter and rank mapping, so HTTP and gRPC P/D share instances and the KV path, with no new failure mode. #3675, #3676
Prefix hashes keyed by token ids The text-keyed tokenizer cache never sees a token-id prompt, so every turn rehashed its whole chain: 7,500 blocks for a 120K-token turn. #3672
An idle EngineCore waits on its input event Its spinning loop held the GIL the input thread needs, so a new request waited about 5 ms, the GIL switch interval, before its first forward. #3672
atomesh renders ATOM's answers as ATOM's HTTP routes do A client must not see which door served it: the text past a stop string, content, cached_tokens, refusal statuses. #3677
The Session, opt-in A Generate call holds one HTTP/2 stream per in-flight request, about 1,228 at C1228; the Session sends one message per engine step for all of them. It stays opt-in until it has run longer on GPU. #3674, #3678, #3679

Component design

Where each PR lands

ATOM side:

atomesh side:

Protocol

RPC Carries HTTP counterpart
Generate, server stream In: request_id, prompt_token_ids, engine_v1 SamplingParams, stop_token_sequences, kv_transfer_params_json, data_parallel_rank, stream, StructuredOutput. Out: one GenerateStreamChunk per step (ids, prompt, completion and cached tokens), then GenerateComplete (finish reason, usage, kv_transfer_params_json), or GenerateError with http_status. /v1/completions with token ids, plus the chat route's structured output
Abort request_id; unknown or finished is not an error a dropped HTTP stream
GetModelInfo model and tokenizer path, served name, max context, EOS ids, chat template, default template kwargs, reasoning open markers the files atomesh would otherwise need
GetServerInfo protocol version, the bodies of /kv_transfer_info, /server_info and /kv_event_sources those routes
HealthCheck healthy, and why not /health
PrepareStructuredOutput a chat's StructuredOutput and sampling in; the four structured fields of SamplingParams out the chat route's grammar setup
Session, bidirectional engine_v1 messages: SubmitBatch and Abort in; Ready, StreamBatch, Completions and Rejected out none

GetServerInfo and every Session message carry a protocol version, and atomesh refuses a worker of another version.

Key flows

One request, single instance

sequenceDiagram
  autonumber
  participant C as Client
  participant M as atomesh
  participant S as ATOM gRPC server (API process)
  participant E as EngineCore
  C->>M: POST /v1/chat/completions
  M->>M: render with ATOM's template (#3676), encode
  M->>M: pick the worker and DP rank, cache-aware (#3676)
  M->>S: Generate(prompt ids, sampling, stops as ids, DP rank) (#3675, #3673)
  S->>S: admission, as the HTTP route (#3673)
  S->>S: block-hash chain, reuse the stored prefix, hash new blocks (#3672)
  S->>E: add_request, an idle loop wakes at once (#3672)
  loop every engine step
    E-->>S: new token ids
    S-->>M: chunk(token ids, usage so far) (#3673)
  end
  E-->>S: finished
  S-->>M: Complete(finish reason, usage, cached tokens) (#3673)
  M->>M: detokenize without the stops, reasoning and tool parsing (#3677)
  M-->>C: SSE chunks and usage, as ATOM's HTTP route gives them (#3677)
Loading

The same chat through both doors, on DeepSeek-V4.1-Flash 1P1D:

{"messages": [{"role": "user", "content": "What is 17 * 23? Answer briefly."}],
 "temperature": 0, "max_tokens": 512, "stop": ["Question:", "</s>", "<|im_end|>"]}
HTTP door gRPC door
Renders and tokenizes ATOM's API process, Python atomesh, Rust
The stops reach the engine as [[10375, 28], [1718, 85, 32], [30, 94, 328, 42616, 94, 32]] the same ids
On the wire to ATOM OpenAI JSON with the text prompt ids and stop ids
The answer finish_reason stop, 18 completion tokens, content "391" the same, reasoning text identical

P/D disaggregation

sequenceDiagram
  autonumber
  participant M as atomesh
  participant P as ATOM P (prefill)
  participant D as ATOM D (decode)
  M->>M: pick P rank and D rank, DP-aware, ATOM's rank mapping (#3675, #3676)
  M->>P: Generate(prompt ids, max_tokens 1, kv_transfer_params for remote decode) (#3676)
  P->>P: prefill, publish KV (Mooncake, unchanged)
  P-->>M: Complete(kv_transfer_params for D)
  M->>M: AtomAdapter enriches D's params, as the HTTP P/D router does (#3676)
  M->>D: Generate(prompt ids, P's kv_transfer_params) (#3676)
  D->>P: pull KV over RDMA (unchanged)
  loop decode steps
    D-->>M: chunk(token ids)
  end
  D-->>M: Complete
Loading

In chunked mode atomesh starts both calls at once and answers once P completes; either failing cancels the other, as over HTTP.

The Session (--atom-grpc-session)

sequenceDiagram
  autonumber
  participant R as atomesh requests
  participant L as atomesh Session link (one per instance)
  participant S as ATOM Session (one seq-id window)
  participant E as EngineCore
  R->>L: first request to the instance (#3678)
  L->>S: open Session (#3678)
  S-->>L: Ready(protocol version, KV pool size) (#3674)
  R->>L: requests queue up (#3679)
  L->>S: SubmitBatch(requests, client seq ids below 2^24) (#3678)
  S->>E: admit each as Generate does, in the Session's window (#3674)
  loop every engine step
    E-->>S: the step's outputs
    S-->>L: one StreamBatch with every request's new ids (#3674)
    L-->>R: split per request (#3678)
  end
  S-->>L: Completions(finish reason, usage, no ids) or Rejected (#3674)
  R->>L: a client goes away
  L->>S: Abort(seq ids) (#3678)
Loading

A chat over the Session first calls PrepareStructuredOutput and copies its four fields into the request (#3679). When the Session's call ends, ATOM aborts its requests and atomesh reopens it on the next request.

Abort and cancel

sequenceDiagram
  participant C as Client
  participant M as atomesh
  participant S as ATOM gRPC server
  participant E as EngineCore
  C->>M: request
  M->>S: Generate
  S->>E: add_request
  S-->>M: chunks
  C--xM: disconnects
  M--xS: the handle drops, the gRPC call is cancelled (#3675)
  S->>E: the HTTP stream teardown aborts the sequence (#3673)
  Note over E: its blocks return to the free pool after the HTTP path's deferrals
  Note over M,S: Session: Abort(seq ids) on the shared stream (#3678, #3674)
  Note over M,S: Abort RPC: before submission the request never reaches the engine (#3673)
Loading

In P/D, a cancel while P prefills aborts P and D, and a cancel while D decodes aborts D after P has let go of its load (#3676).

Structured output

sequenceDiagram
  participant C as Client
  participant M as atomesh
  participant S as ATOM gRPC server
  participant H as HTTP chat helpers (same process)
  C->>M: chat with tools, tool_choice, response_format
  M->>M: render, then fill StructuredOutput (#3677)
  Note over M: tools, tool_choice, response_format, merged template kwargs, whether the prompt opens reasoning, per-message tools
  M->>S: Generate(prompt ids, ..., StructuredOutput) (#3677)
  S->>H: the chat request, validated and configured by the chat route's code (#3673)
  H-->>S: required_channel, json_schema, structured_tool_format, structured_reasoning_end
  S->>S: admit with those fields, each rank's grammar prepared (#3673)
  Note over S: a grammar failure ends as the HTTP chat's 500
Loading

Prefix block hashes on token-id prompts

Prefix block hashes for token-id prompts

A token-id prompt arrives without text, so it reuses the chain of the stored prompt it shares the most leading tokens with, and hashes only the blocks past that. /v1/completions with prompt_token_ids gains the same reuse.

Correctness and HTTP-parity contract

For n = 1, a request answers alike through either door:

Item How it holds Checked by
Prompt token ids atomesh renders with the template and default kwargs GetModelInfo serves, and encodes with the model's tokenizer GPU: 59 of 59 prompts identical; CPU: 363 of 363 AgentX prompts and renders identical
Stops atomesh encodes stop strings as ATOM does, the engine matches them on ids, and atomesh does not cut the text again GPU: identical stop ids; fake engine against HTTP; a Rust test that fails with the fix reverted
Admission and refusals the HTTP routes' helpers, in their order, with their status and message fake engine: each refusal through atomesh against the HTTP route
The response finish reason, usage with cached_tokens, content and reasoning_content, SSE chunk shapes fake engine against HTTP; Rust unit tests; SSE golden snapshots
Structured output derived in ATOM by the chat route's code fake engine: the engine's fields equal HTTP's for three chats
P/D same ranks, kv_transfer_params and sampling at P and at D fake engine: 1P1D, P DP2 + D DP4, 22 ranks

By design, a few things differ:

  • Refused over gRPC: n above 1 (400), media (400), Responses and Messages (501), logprobs (400).
  • --grpc-port needs --api-server-count 1; one gRPC-serving API process carries the load (7.4 ms CPU per request on P).
  • Under a forced --spec-decode-acceptance-length, a benchmark mode, ATOM's HTTP route answers synthetic output with placeholder text, while atomesh decodes the real ids. A 1-token turn can then come back without visible text: 21 to 36 per GPU warmup.

In P/D, transfer_id matches by form, and D's params carry no prompt_token_ids, since D gets the ids in the request.

Results

Serving path, model forward at 0 s

Fake engines on CPU in the 6P16D layout: 3 P (TP2, DP2) and 4 D (TP4, DP4), relay P/D over a fake Mooncake, every step and transfer priced at 0 s. AgentX at C1228 and C64, 600 s windows, in the order HTTP, gRPC, gRPC, HTTP on one host; one atomesh binary with fastokens (#3596). HTTP runs 4 API processes per instance, gRPC one.

C1228, mean of 2 runs HTTP gRPC
TTFT mean / P50 / P90 / P99 (ms) 2,273 / 466 / 6,333 / 26,172 496 / 187 / 856 / 6,000
ITL mean (ms) 3.07 3.02
Requests/s, output tok/s 53.5, 49.8K 56.7, 52.3K
CPU per request, atomesh + ATOM API processes (ms) 192 39.4
of which the P instances' API processes (ms) 140 7.4
C64: TTFT mean / P50 (ms), CPU per request (ms) 316 / 228, 143 280 / 198, 52

CPU counts atomesh and every API and front-end process, not EngineCore. ITL matches on both doors. At C64 the trace's idle gaps, not the server, set the pace.

Tokenizer, one core

363 AgentX prompts from 73 sessions, 55.8M tokens, encoded in process; every encoder gives identical ids for every prompt.

Encoder Mean ms per request, all / later turns
ATOM Python, segment cache 512 MiB (the HTTP default) 29.60 / 5.27
ATOM Python, cache off 197.21 / 215.81
atomesh, HuggingFace tokenizer in Rust 118.13 / 128.47
atomesh, fastokens with a 512 MiB segment cache (#3596) 0.25 / 0.15

GPU, DeepSeek-V4.1-Flash on MI355X

6P16D: 3 P instances (TP2, DP2) and 4 D instances (TP4, DP4), a shared Mooncake Store, AgentX at C1228 for 900 s, acceptance length forced to 3.51. One build for all six runs, in the order gRPC, HTTP, Session, Session, HTTP, gRPC.

Mean of 2 runs HTTP gRPC Session
TTFT mean (s), run-to-run spread 11.78, 0.34 11.56, 0.12 11.44, 0.29
TTFT P50 / P90 / P99 (s) 11.62 / 20.13 / 35.04 11.35 / 19.58 / 35.02 11.30 / 19.64 / 34.51
ITL mean (ms) 6.85 7.00 6.93
Output tok/s 34.7K 34.8K 34.9K
Prefix hit, ATOM P counters 95.8% 95.8% 95.8%
Failed requests 0 0 0

TTFT is bound by the prefill queue here, so the serving path's saving does not show. No difference exceeds HTTP's own run-to-run spread. atomesh's chat template stage took 2.68 ms per request on average and its fastokens encode 0.51 ms.

Accuracy

GSM8K through lm_eval's chat completions: 1,319 questions, 3-shot as multi-turn, real acceptance, two runs per door.

gRPC HTTP Empty answers, gRPC vs HTTP
Before the stop fix, 6P16D 0.9204 / 0.9303 0.9325 / 0.9325 41 vs 21
With the stop fix, 1P1D 0.9303 / 0.9287 0.9272 / 0.9295 23 vs 23

With the fix, the paired McNemar chi2 between any gRPC run and any HTTP run is at most 0.20.

Verification methodology

Parity verification design

Every comparison sends the same request through atomesh's HTTP path and its gRPC path to the same ATOM instances, at the same commit.

Experiment What is compared Setup Runs Result
Prompt token parity ids ATOM's HTTP chat admits (return_token_ids) vs ids its Generate admits from atomesh GPU, 21 GSM8K chats, 30 AgentX chats up to 690,728 tokens, 8 tool-call chats 6P16D, then 1P1D after the stop fix 59 of 59 identical, both times
Stop ids stop_token_sequences atomesh sends vs ATOM's own encode lm_eval's three stops GPU identical
Tokenizer and render 5 encoders' ids; atomesh's render vs ATOM's prompt text 363 AgentX prompts, CPU 3 timed passes 363 of 363 identical
Development harness every answer field, status, P/D parameter against the HTTP route CPU fake engines; over Generate and the Session 41 acceptance cases all pass
GPU functional tool calls (unary, streamed, result turn), streaming, cancel, P to D 6P16D once per door all pass
GSM8K score, empty answers, per-question agreement 1P1D 2 per door no gap (above)
Zero-forward A/B TTFT, ITL, throughput, CPU per request fake engines, C1228 and C64 2 per door above
GPU performance TTFT, ITL, throughput, prefix hit, failures 6P16D, C1228 2 per configuration parity (above)

The development harness lives on the development branch, not in these PRs. It runs ATOM's own API server, CoreManager, EngineCore, scheduler, BlockManager and P/D connectors on CPU, and replaces only the model runner. Output ids depend on the request and the position, so a lost, repeated or reordered token shows; steps cost a fitted DeepSeek-V4.1 MI355X step law, and KV transfers follow a bandwidth law with injected faults. Its single-request TTFT came within 3.6% of the law, and the Session's c1024 delivery lag P99 was 1.2 ms at DP1 and 2.3 ms at DP4.

Bugs the comparisons caught, each fixed in the PR named:

Found by Symptom Cause and fix PR
GSM8K A/B on GPU gRPC left 41 answers empty, HTTP 21; replaying the 34 questions with lm_eval's stops gave 11 vs 1 atomesh cut ATOM's text at the first stop string it decoded, though ATOM ends only where a stop's ids match. It now decodes ATOM's output with none of the stops. F
The same check an output ending inside the reasoning had no content key over gRPC, "" over HTTP the unary answer now always carries content, and a stream opens with role and empty content F
GPU A/B AIPerf read no prefix-hit rate over gRPC chat usage lacked cached_tokens; it now comes from Complete F
Development harness /generate and /v1/completions streams lost text at chunk boundaries each chunk was decoded alone; streams now decode across chunks F
Timing against the step law a new request waited about 5 ms before its first forward the idle loop held the GIL; it now waits on the input event A
Session timing at c1024 DP4 delivery lag P99 of 42 ms Completions resent every id on the one stream; they now carry none, and each atomesh Session has its own connection and thread C, G
Sources of the numbers
  • Zero-forward A/B: 2 runs per door at each concurrency, one host per concurrency. Failures on both doors (0.53% to 0.61% at C1228) are one relay receive failure of whole-block prompts, on HTTP and gRPC alike.
  • Tokenizer: median of 3 timed passes after a warm-up pass, each encoder pinned to one core; the render of every AgentX chat by atomesh equals ATOM's prompt text.
  • GPU 6P16D: the two gRPC configurations ran atomesh with fastokens and the DeepSeek-V4.1 render (feat(mesh): encode the prompts of listed models with fastokens #3596, fix(mesh): take DeepSeek-V4.1 reasoning efforts as ATOM does #3597) and with --reasoning-parser deepseek_r1 --tool-call-parser dsml_v41; the code was the integration branch these PRs were cut from.
  • GSM8K before the fix ran on 6P16D, after it on 1P1D without the Store; replaying the questions without stop strings gave 0 empty answers on both doors.
  • Session lag: the time from a step's outputs reaching the gRPC layer to their arrival in atomesh, at 1,024 concurrent requests on fake engines. The Completions fix first measured 5.4 ms at DP4; the final acceptance run measured 2.3 ms.

Rollout

PR What it adds Decision it implements Enables Size
#3672 TokenPrefixChains; idle EngineCore and PP-head loops wait for input prefix hashes by token ids; the idle wait token-id prompts that hash only new blocks 15 files, +601, 60% tests
#3673 the atom.grpc.v1 proto and modules; the server thread with the info RPCs, Generate, Abort and PrepareStructuredOutput token ids; the server in the API process; P/D unchanged #3674 and #3675 14 files, +1,979, 31% tests, 28% generated
#3674 the Session RPC: seq-id windows, one StreamBatch per step, id-free Completions the Session #3678 and #3679 at runtime 5 files, +688, 10% tests
#3675 ATOM's protos in every atomesh build; AtomClient; the shared rank mapping token ids; P/D unchanged #3676 28 files, +1,382, 57% tests
#3676 discovery of ATOM workers and their DP ranks; ATOM P/D pairs, relay and chunked P/D unchanged #3677 and #3678 21 files, +1,305, 58% tests
#3677 structured output, refusals, answers as ATOM's HTTP routes give them rendering parity #3679 25 files, +999, 62% tests
#3678 the Session link, one per instance the Session #3679 5 files, +1,052, 53% tests
#3679 --atom-grpc-session, one call path for single requests and both P/D legs; README the Session, opt-in 12 files of its own, +571, 52% tests

Merge order: one linear stack, #3672 to #3679 in the order above, each PR based on the one before it.

Why the change touches 95 files

atomesh already serves SGLang and vLLM over gRPC, with one backend piece in each of its layers. ATOM, as a third backend, adds its piece in each layer, plus the server on ATOM's side, so the change spreads over 95 files with +8,428 and -539 lines. Tests are 48% of the added lines and generated code 7%.

Flags and defaults:

  • ATOM: --grpc-port <port> is off by default and needs --api-server-count 1. ATOM_TOKENIZE_CACHE_MB (512) also sizes the token-id chain store, and ATOM_TOKENIZE_CACHE_BLOCK_HASHES=0 turns reuse off.
  • atomesh: a grpc://host:port worker is detected as ATOM without a flag, and --dp-aware reads its DP size from GetServerInfo. --atom-grpc-session is off by default.
  • atomesh parses ATOM's output itself, so its reasoning and tool parsers must match the model, named with --reasoning-parser and --tool-call-parser when the model id does not select them.
  • Dependencies: grpcio>=1.78.0 and protobuf>=6.31.1,<7 in pyproject.toml; the generated modules are committed and rewritten by python -m atom.entrypoints.grpc.generate.
  • HTTP serving is unchanged, so a deployment moves to gRPC by adding --grpc-port and pointing atomesh at grpc:// workers.

Activity

  1. Jasen2201 commented on Nov 21, 2026

    @Jasen2201
    CollaboratorAuthor

    Part of this RFC: its code as 14 stacked draft PRs, each on the one before it, the first on main.

    The 14 PRs, bottom to top
  2. Jasen2201 commented on Nov 21, 2026

    @Jasen2201
    CollaboratorAuthor

    Part of this RFC: its code regrouped by function as 8 draft PRs, which replace #3658 to #3671. Merge order: A and B, then C and D, then E, then F and G, then H.

    The 8 PRs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions