Skip to content

feat(mesh): decode least_load by url@rank - #3509

Closed
MengqingCao wants to merge 11 commits into
mengqing/agentic-cost-weightsfrom
mengqing/mesh-decode-least-load
Closed

MengqingCao wants to merge 11 commits into
mengqing/agentic-cost-weightsfrom
mengqing/mesh-decode-least-load

Conversation

@MengqingCao

Copy link
Copy Markdown
Contributor

Summary

  • Add a decode policy, least_load. Its cost is the KV reserved on that url@rank plus 512 tokens per in-flight request, the same request weight as agentic and the engine's ATOM_DP_LB_REQ_EQUIV. The reservation is taken inside the selection, before the lock is released, so a request still in prefill or waiting on decode is visible to the next choice. Equal cost keeps the lower index. If any candidate has a reservation without a token count, the whole choice uses in-flight requests only.
  • Streaming decode responses add the cumulative completion_tokens delta until the body is dropped. The prefill token counter from feat(mesh): count in-flight prefill tokens per worker (url@rank) #3481 is a separate ledger and is not reused. Decode-first selection stays in feat(mesh): under idx2idx, select decode first and prefill on its rank #3493.
  • LoadMonitor fetches one URL per instance. ATOM reads atom:dp_inflight_requests{rank} from /metrics; other backends still use /get_load, and only for workers that are not DP ranks. A failed sample is left out of the power_of_two cache, while GET /get_loads still reports -1. The sample interval follows the smallest power_of_two.load_check_interval_secs.
  • The benchmark harness passes router.prefill_policy and router.decode_policy through. minimax-m3-fp4-2p1d-dpa-tp4 and the DSv4-0813 agentic DPA anchor select agentic prefill and least_load decode. Dropping those two keys restores dp_sticky or cache_aware. MI455 DP4 is unchanged, and the global policy default stays random.

Test plan

  • cargo test --offline --locked --lib -- least_load worker_manager::tests factory::tests (15 passed)
  • cargo test --offline --locked --lib -- placement:: (132 passed)
  • cargo test --offline --locked --lib -- load_tests (20 passed)
  • cargo test --offline --locked --lib -- http_pd_router::tests (25 passed)
  • MI355X DPA decode: MiniMax-M3 minimax-m3-fp4-2p1d-dpa-tp4 and DSv4-0813 1P1D DPA. P90 ITL should move beyond run noise; throughput and P90 TTFT should not worsen; 0 failed; GSM8K should not worsen.
  • MI455X MiniMax-M3 DP4 ([plugin][oot] Add Kimi-K2.5 support #401) should not regress.

Made with Cursor

MengqingCao and others added 10 commits November 18, 2026 04:22
Each worker now carries a prefill-token counter next to its request count:
the prompt tokens placed on it whose prefill has not finished, exported as
mesh_worker_prefill_tokens{worker}. DP-aware workers keep one per rank.

The planner charges the prompt's tokens, when the router has them, to the
worker that prefills it, and the load guard holds the charge until the
prefill is done:

- P/D: on the prefill worker's guard, released when the prefill response
  completes. Under idx2idx it follows the remapped prefill rank; the idx2idx
  reservation test carries a nonzero charge and checks where it lands.
- Regular HTTP, streaming: until the response's first frame.
- Regular HTTP, non-streaming: not charged, since its first frame arrives
  only after decoding.

ext-proc single-worker and gRPC placements do not charge yet. A release
larger than the charge saturates at zero and logs a warning.

Ported from #3210 with its review addressed: non-streaming requests are not
charged, with a test, and the charge lives on WorkerLoadGuard rather than a
public PrefillTokenGuard.

Co-authored-by: Jasen2201 <yajizhan@amd.com>
- The cancel and stalled error-body P/D tests check the prefill charge is
  taken and released, not only the request load.
- WorkerLoadGuard::with_prefill_tokens debug-asserts a fresh guard
  instead of releasing a charge no caller has.
- The mesh_worker_prefill_tokens help names the paths that charge it.

Co-authored-by: Cursor <cursoragent@cursor.com>
The request weight was a hardcoded copy of ATOM_DP_LB_REQ_EQUIV. AgenticCost
now carries it as request_token_equivalent (default 512); keep it equal to
the engine's ATOM_DP_LB_REQ_EQUIV, which its least_tokens DP balancer uses
for the same weight. The cost stays integer.
`--prefill-policy agentic` (or `--policy agentic` in regular mode) places
each request on the worker (url@rank) with the lowest AgenticCost:

- cached_tokens: the KV event index's GPU-tier hits; PolicyRegistry hands
  the index to agentic as it does to cache_aware. A request the index cannot
  match (no tokens, media, or no candidate's events indexed) falls back to a
  text tree of past placements, which credits the worker with the longest
  match its bytes / 4. The tree shares --eviction-interval and
  --max-tree-size with cache_aware.
- prompt_tokens: the tokens, or the text's bytes / 4 without them, so these
  requests are charged too.
- inflight_prefill_tokens and inflight_requests: the worker's counters.

The selected worker's uncached tokens go back to the planner as
RouteDecision::prefill_tokens, the charge it records. For a request the index
can match, agentic calls on_routed on the selected worker and reports
estimated_hit_ratio, so its placements join the KV events predictions and
reconciliation. Branches: hit, relocate and miss by the index; text_hit,
text_relocate and text_miss by the tree; unmatched.

Configuration rejects agentic for the decode pool, including --policy
agentic in P/D mode without --decode-policy: decode carries no prefill
charge, so long sessions would stay pinned to one decode.
--agentic-request-token-equivalent (512) sets the request weight. The module
is private, re-exported like the other policies.

agentic_cost.md documents the policy, its inputs and its limits; the README
and metrics.md list it and its branches. The CI step also runs the
placement, proxy body, factory, CLI and config tests.
The registry removed a departed worker only from cache_aware trees, so it
stayed a tenant of agentic's text tree: its sessions' next text-placed turn
still matched it, every candidate scored no hit, and its text stayed in
memory. remove_worker_from_cache_aware now forgets the worker in agentic's
trees as well; a matched tenant missing from the candidates is still kept,
since under idx2idx it may be alive on another rank.

Also:
- max_tree_size caps each worker's text, not the tree; the doc says so.
- The CI step is back to running policies::agentic only: the first step
  already runs every --lib test.
- The planner test of a policy's prefill estimate uses AgenticPolicy instead
  of a stub policy.
Co-authored-by: Cursor <cursoragent@cursor.com>
A decode still in prefill or waiting on KV transfer has to count toward
url@rank load, or the next choice cannot see it. Failed load samples stay
out of the power_of_two cache, and the two DPA acceptance cases select
agentic prefill with least_load decode.

Co-authored-by: Cursor <cursoragent@cursor.com>

@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.

a few small things..

MI355X DPA decode and MI455X DP4 regression results? both unchecked in test plan.

ci green.

Comment thread atom/mesh/src/cliargs.rs
long,
default_value = "cache_aware",
value_parser = ["random", "round_robin", "dp_sticky", "cache_aware", "power_of_two", "prefix_hash", "agentic"],
value_parser = ["random", "round_robin", "dp_sticky", "cache_aware", "power_of_two", "prefix_hash", "agentic", "least_load"],

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

least_load outside PD just degrades to in-flight load silently? either reject it in validation or carry the reservation through PlacementPlan::Single..

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Rejected in validation. least_load is PD decode only: a regular router, or a prefill policy that falls back to it, now fails config validation instead of dropping the selection reservation and keeping just the in-flight count. --decode-policy least_load with another prefill policy still passes. 6d9fb853c


/// Take over a reservation made at selection. Does not charge again.
pub fn adopt(reservation: LoadReservation) -> Self {
let (worker, tokens) = reservation.defuse();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

max_completion_tokens matches the key anywhere in a chunk, content can inflate the reservation. scope it to usage frames? also missing tests for adopt/observe_output_chunk..

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Usage is read from the SSE event JSON (usage, meta_info, or nested response/message usage). Generated content and completion_tokens_details no longer match. Split frames are reassembled before parse. adopt and observe_output_chunk are covered: the selection charge is not incremented again, content does not add tokens, a cumulative usage value adds the delta once, and drop releases prompt plus generated tokens. 6d9fb853c


/// Lowest outstanding decode cost at `url@rank`: reserved KV tokens plus
/// one request-equivalent per in-flight request.
#[serde(rename = "least_load")]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

new user-facing policy, but no least_load row in the README load balancing table? please add it and note it's for PD decode.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a least_load row to the mesh README load-balancing table, and noted it is PD decode only. 6d9fb853c

A regular router dropped the selection reservation, so least_load there
only saw the in-flight count. Reject that configuration. Completion
tokens now come from the SSE usage object, so generated text cannot
inflate the reservation.

Co-authored-by: Cursor <cursoragent@cursor.com>
@MengqingCao

Copy link
Copy Markdown
Contributor Author

Addressed the three code notes in 6d9fb853c.

MI355X DPA decode and MI455X DP4 regression are still unchecked. Those GPU runs have not been started, so the test plan stays open for them.

@MengqingCao

Copy link
Copy Markdown
Contributor Author

@valar-cop thanks, all three notes are addressed in `6d9fb853c`, each with a reply in its thread:

  1. `least_load` outside PD: config validation rejects it on a regular router and as the prefill policy. It is only accepted as the PD decode policy, so it no longer drops the reservation.
  2. `completion_tokens` from content: usage is read from the SSE event's usage object only. Generated text and `completion_tokens_details` no longer match, and split frames are reassembled. Unit tests cover adopt, content not counting, cumulative deltas, and drop.
  3. README: `least_load` is in the load-balancing table, marked PD decode only.

The MI355X DPA decode and MI455X DP4 regression items are still open and not run yet.

@valar-cop

Copy link
Copy Markdown

@MengqingCao checked 6d9fb85, all three are in: least_load is rejected as regular or prefill policy (atom/mesh/src/config/validation.rs:391, :403), usage comes from the frame's usage object with split SSE frames reassembled (atom/mesh/src/core/worker.rs:1253), and the README row is there (atom/mesh/README.md:264). the adopt/drop, content-not-counting and cumulative-delta tests cover what you said they do.

fine by me on those. holding the lgtm until the MI355X DPA decode and MI455X DP4 runs are back.

@MengqingCao

Copy link
Copy Markdown
Contributor Author

Splitting this PR into the three functional pieces, stacked on #3406:

  1. fix(mesh): sample power_of_two load per instance and drop failures #3566 samples power_of_two load per instance and drops failed samples.
  2. feat(mesh): charge decode least_load when the request is selected #3567 adds PD decode least_load.
  3. ci(mesh): select agentic prefill and least_load decode on two DPA cases #3568 switches the two DPA acceptance cases to agentic prefill and least_load decode.

Closing this combined PR so review continues on those three.

@MengqingCao
MengqingCao removed this pull request from stack #3499 November 19, 2026 07:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants