Skip to content

fix(pd): avoid first-token cache stats race - #1450

Open
sufubao wants to merge 14 commits into
ModelTC:mainfrom
sufubao:fix/pd-first-token-cache-stats-race
Open

fix(pd): avoid first-token cache stats race#1450
sufubao wants to merge 14 commits into
ModelTC:mainfrom
sufubao:fix/pd-first-token-cache-stats-race

Conversation

@sufubao

@sufubao sufubao commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

背景

P、D 节点都会上报首 token,PD Master 原先使用先到达的一份。D 先到时,P 首 token 携带的 CPU/磁盘缓存命中会被丢弃,导致 OpenAI cached_tokens 偏低;实测缓存命中率从 59.90% 被误报为 28.95%。

直接等待 P 也不安全:如果 P 转发异常,已经完成的 D 输出会一直被缓存,请求无法结束。

修改

  • 需要 prefill 时,暂存提前到达的 D 输出,先返回 P 首 token;
  • 用 P 的 prompt_cache_len 更新后续 D token,并丢弃重复的 D 首 token;
  • D 已完整命中 prompt KV 时不等待 P;
  • D 已完成但 5 秒内仍未收到 P 首 token 时,返回完整 D 输出。

验证

  • PD Master 相关测试:22 passed
  • Black、flake8:通过

@sufubao
sufubao force-pushed the fix/pd-first-token-cache-stats-race branch from 3b2e8a4 to 3d3773d Compare August 9, 2026 11:55
@sufubao
sufubao force-pushed the fix/pd-first-token-cache-stats-race branch from 958df35 to 729ed9b Compare August 10, 2026 12:47
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.

1 participant