Skip to content

Commit 9d09b38

Browse files
mengqing.cao@amd.comcursoragent
authored andcommitted
fix(mesh): agentic's text tree credits only healthy candidates
Like cache_aware, agentic's text fallback matches among the healthy candidates (Tree::prefix_match_among), so under idx2idx a prefix another rank's worker placed first is credited to the candidate sharing it. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 523e5f7 commit 9d09b38

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

‎atom/mesh/docs/agentic_cost.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ with no extra bonus, as in the engine's `least_tokens` DP balancer.
1616
| Input | Source |
1717
| --- | --- |
1818
| `prompt_tokens` | The request's tokens (`--model-path` or `--tokenizer-path`, prompts up to `--prepare-max-tokenize-bytes`); without them, its text bytes / 4. |
19-
| `cached_tokens` | The KV event index (`--kv-events`): the leading prompt tokens each worker's GPU cache holds. A request the index cannot match (no tokens, media, or no candidate's events indexed) uses a text tree of past placements: the worker with the longest text match is credited its bytes / 4. The tree shares `--eviction-interval` and `--max-tree-size` (a per-worker cap) with `cache_aware`. |
19+
| `cached_tokens` | The KV event index (`--kv-events`): the leading prompt tokens each worker's GPU cache holds. A request the index cannot match (no tokens, media, or no candidate's events indexed) uses a text tree of past placements: the healthy candidate with the longest text match is credited its bytes / 4. The tree shares `--eviction-interval` and `--max-tree-size` (a per-worker cap) with `cache_aware`. |
2020
| `inflight_prefill_tokens` | Tokens charged to the worker whose prefill has not finished, `mesh_worker_prefill_tokens{worker}`. |
2121
| `inflight_requests` | `mesh_worker_requests_active{worker}`. |
2222
| `request_token_equivalent` | `--agentic-request-token-equivalent` (512). Keep it equal to the engine's `ATOM_DP_LB_REQ_EQUIV`. |

‎atom/mesh/docs/metrics.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Defaults are the CLI defaults.
112112
| `agentic` | `hit` | The lowest [prefill cost](agentic_cost.md); the KV event index says it holds the longest prefix. | ratio, estimate |
113113
| | `relocate` | The lowest cost, though the index says another worker holds a longer prefix: that worker's queue outweighs the reuse. | ratio, estimate when the index knows the worker |
114114
| | `miss` | The lowest cost; by the index, no candidate holds any of the prompt. | ratio, estimate when the index knows the worker |
115-
| | `text_hit` | The request cannot match the index (no tokens, media, or no candidate's events indexed): the lowest cost, crediting the worker holding the longest match in the text tree. | ratio, estimate |
115+
| | `text_hit` | The request cannot match the index (no tokens, media, or no candidate's events indexed): the lowest cost, crediting the healthy candidate holding the longest match in the text tree. | ratio, estimate |
116116
| | `text_relocate` | The same, but another worker holds the longest text match. | ratio |
117117
| | `text_miss` | The same; nothing in the text tree matches. | ratio |
118118
| | `unmatched` | Neither tokens nor text: the lowest cost by load. | |

‎atom/mesh/src/policies/agentic.rs‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ enum Hits {
141141
/// A request the KV event index can match takes its hits from the index. Others
142142
/// (without tokens, with media, or with no candidate's events indexed) take them
143143
/// from a text tree of the requests this policy placed, which only credits the
144-
/// worker holding the longest text match.
144+
/// healthy candidate holding the longest text match.
145145
#[derive(Debug)]
146146
pub struct AgenticPolicy {
147147
cost: AgenticCost,
@@ -208,10 +208,16 @@ fn estimated_tokens(bytes: usize) -> usize {
208208
bytes.div_ceil(TEXT_BYTES_PER_TOKEN)
209209
}
210210

211-
/// Each worker's hit in `tree`: the longest text match's worker holds its tokens,
212-
/// estimated from its bytes, and every other worker none.
213-
fn text_hits(tree: &Tree, text: &str, workers: &[Arc<dyn Worker>]) -> Vec<Option<usize>> {
214-
let matched = tree.prefix_match_with_counts(text);
211+
/// Each worker's hit in `tree`: the healthy candidate holding the longest text match
212+
/// holds its tokens, estimated from its bytes, and every other worker none.
213+
fn text_hits(
214+
tree: &Tree,
215+
text: &str,
216+
workers: &[Arc<dyn Worker>],
217+
healthy: &[usize],
218+
) -> Vec<Option<usize>> {
219+
let candidates: Vec<&str> = healthy.iter().map(|&idx| workers[idx].url()).collect();
220+
let matched = tree.prefix_match_among(text, &candidates);
215221
let bytes = text
216222
.char_indices()
217223
.nth(matched.matched_char_count)
@@ -258,7 +264,7 @@ impl LoadBalancingPolicy for AgenticPolicy {
258264
.map(|hit| hit.map(|tokens| tokens as usize))
259265
.collect(),
260266
),
261-
(None, Some((text, tree))) => (Hits::Text, text_hits(tree, text, workers)),
267+
(None, Some((text, tree))) => (Hits::Text, text_hits(tree, text, workers, &healthy)),
262268
(None, None) => (Hits::Unmatched, vec![None; workers.len()]),
263269
};
264270

@@ -569,6 +575,14 @@ mod tests {
569575
assert_eq!(decide(&policy, &workers, &no_tokens(&text)).await, first);
570576
let next = (Some(1), "text_hit", Some(0.5), Some(100));
571577
assert_eq!(decide(&policy, &workers, &no_tokens(&longer)).await, next);
578+
// Worker 1 placed the shared prefix first; worker 0, sharing it, is credited it.
579+
workers[1].set_healthy(false);
580+
let [joins, asks] = ["z", "w"].map(|tail| format!("{text}{}", tail.repeat(400)));
581+
let joined = (Some(0), "text_miss", None, Some(200));
582+
assert_eq!(decide(&policy, &workers, &no_tokens(&joins)).await, joined);
583+
let shared = (Some(0), "text_hit", Some(0.5), Some(100));
584+
assert_eq!(decide(&policy, &workers, &no_tokens(&asks)).await, shared);
585+
workers[1].set_healthy(true);
572586

573587
// Media, and tokens without indexed events, use the tree too, with the tokens
574588
// as the prompt; only the request without media predicts its blocks.

0 commit comments

Comments
 (0)