Skip to content

[KVCache] Support request-level prefix cache disable#7854

Open
kevincheng2 wants to merge 1 commit into
PaddlePaddle:developfrom
kevincheng2:feature/disable-prefix-caching-20260519
Open

[KVCache] Support request-level prefix cache disable#7854
kevincheng2 wants to merge 1 commit into
PaddlePaddle:developfrom
kevincheng2:feature/disable-prefix-caching-20260519

Conversation

@kevincheng2
Copy link
Copy Markdown
Collaborator

Motivation

支持请求级禁用 prefix caching。部分请求需要跳过 prefix cache 的匹配、写入和释放复用路径,以避免污染或复用全局缓存;默认值保持 False,继续遵循全局 prefix caching 配置。

Modifications

  • OpenAI Completion/ChatCompletion 请求新增 disable_prefix_caching 参数,并贯通到内部 Request 序列化/反序列化。
  • ResourceManager 和 ResourceManagerV1 在请求级禁用时跳过 prefix cache 匹配、cache block 更新、输出/存储写入和 prefix tree 释放路径。
  • 补充 legacy ResourceManager、ResourceManagerV1、Request/OpenAI protocol 单测。
  • 更新中英文 online serving 请求参数文档。

Usage or Command

# 单测
/root/paddlejob/inference-public/chengyanfu/.venv/py310/bin/python -m pytest \
  tests/engine/test_request.py \
  tests/engine/test_resource_manager.py \
  tests/v1/test_resource_manager_v1.py -q

请求示例:

client.chat.completions.create(
    model="null",
    messages=[{"role": "user", "content": "hello"}],
    extra_body={"disable_prefix_caching": True},
)

Accuracy Tests

不涉及模型计算逻辑或算子变更,未执行精度测试。

Checklist

  • Add at least a tag in the PR title.
  • Format your code, run pre-commit before commit.
  • Add unit tests. Please write the reason in this PR if no unit tests.
  • Provide accuracy results.
  • If the current PR is submitting to the release branch, make sure the PR has been submitted to the develop branch, then cherry-pick it to the release branch with the [Cherry-Pick] PR tag.

@paddle-bot
Copy link
Copy Markdown

paddle-bot Bot commented May 19, 2026

Thanks for your contribution!

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 19, 2026

Codecov Report

❌ Patch coverage is 77.41935% with 7 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@bda1756). Learn more about missing BASE report.

Files with missing lines Patch % Lines
fastdeploy/engine/sched/resource_manager_v1.py 70.83% 0 Missing and 7 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop    #7854   +/-   ##
==========================================
  Coverage           ?   63.84%           
==========================================
  Files              ?      462           
  Lines              ?    64388           
  Branches           ?     9874           
==========================================
  Hits               ?    41106           
  Misses             ?    20487           
  Partials           ?     2795           
Flag Coverage Δ
GPU 73.02% <77.41%> (?)
XPU 7.13% <12.90%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

@PaddlePaddle-bot PaddlePaddle-bot left a comment

Choose a reason for hiding this comment

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

🤖 Paddle-CI-Agent | pr_review | 2026-05-19 18:08:20

📋 Review 摘要

PR 概述:新增请求级 prefix cache 禁用控制,允许单个请求绕过全局 prefix caching 的匹配、写入和释放路径
变更范围engine/request.pyengine/resource_manager.pyengine/sched/resource_manager_v1.pyentrypoints/openai/protocol.py、docs
影响面 TagKVCache Engine APIServer

问题

级别 文件 概述
🟡 建议 tests/v1/test_resource_manager_v1.py:726 测试未设置 kvcache_storage_backend,未能真正覆盖 disable_prefix_caching 的 skip 分支
❓ 疑问 fastdeploy/engine/sched/resource_manager_v1.py:1433 get_prefix_cached_blocks 早返回路径计算 cache_info 缺少 enc_dec_block_num
❓ 疑问 fastdeploy/engine/sched/resource_manager_v1.py:1726 finish_requests 引入的 kvcache_storage_backend guard 超出本 PR 功能范围,属隐式行为变更

📝 PR 规范检查

PR 标题含合法 Tag [KVCache],格式规范;描述五个章节完整,Checklist 勾选合理(Accuracy Tests 未勾且在正文已说明不涉及精度测试)。规范合规 ✓

总体评价

整体设计清晰,通过 _enable_prefix_cache_for_request 辅助方法统一控制请求级禁用,代码侵入面小;全链路(protocol → request → resource_manager)传递完整,legacy 和 v1 两套实现均同步更新。主要关注点:一处测试有效性需补强,两处细节请作者确认后可合入。


manager.finish_requests([request.request_id])

manager.cache_manager.write_cache_to_storage.assert_not_called()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 建议 测试未能有效验证 disable_prefix_caching 的跳过逻辑

本测试未设置 manager.config.cache_config.kvcache_storage_backend,其默认值为 falsy,因此 finish_requests 外层的 if self.config.cache_config.kvcache_storage_backend: 直接短路,根本不进入写入循环。assert_not_called 通过,但验证的是「没有 backend 时不写入」而非「disable_prefix_caching=True 时跳过写入」,目标分支实际上未被覆盖。

建议在测试中加上 backend 设置后再验证:

manager.config.cache_config.kvcache_storage_backend = "mock_backend"
# ... 其余不变 ...
manager.finish_requests([request.request_id])
manager.cache_manager.write_cache_to_storage.assert_not_called()
manager.cache_manager.write_cache_to_storage_decode.assert_not_called()

"""
if not self._enable_prefix_cache_for_request(request):
block_size = self.config.cache_config.block_size
request.cache_info = [0, (request.need_prefill_tokens + block_size - 1) // block_size]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

❓ 疑问 cache_info 计算可能遗漏 enc_dec_block_num

早返回路径使用:

request.cache_info = [0, (request.need_prefill_tokens + block_size - 1) // block_size]

preallocate_resource_in_p 中使用:

need_prealloc_prefill_blocks = (
    request.need_prefill_tokens + block_size - 1
) // block_size + self.config.cache_config.enc_dec_block_num

对于 enc_dec 模型(enc_dec_block_num > 0),两处计算不一致,可能导致后续块分配数不足。请确认 cache_info[1] 是否需要加上 enc_dec_block_num

else:
# P instance / Mixed instance uses standard write method (relies on Radix Tree)
self.cache_manager.write_cache_to_storage(req)
if self.config.cache_config.kvcache_storage_backend:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

❓ 疑问 引入 kvcache_storage_backend guard 超出本 PR 范围

原始代码对 need_postprocess_reqs 中的每个请求无条件调用 write_cache_to_storage*,新代码在最外层加了 if self.config.cache_config.kvcache_storage_backend: 检查。

这是一处隐式的行为变更(超出 disable_prefix_caching 特性范围):当 kvcache_storage_backend 未配置时,原来仍会调用写入方法(结果视方法实现而定),新代码则完全跳过。若 write_cache_to_storagekvcache_storage_backend=None 时是安全的 no-op,此改动影响有限;若方法内有实际操作,则可能改变现有语义。

建议:若此改动是有意修复,请在 PR 描述的 Modifications 中单独说明;否则考虑拆分以保持变更内聚。

@PaddlePaddle-bot
Copy link
Copy Markdown

PaddlePaddle-bot commented May 19, 2026

🤖 Paddle-CI-Agent | ci_status_monitor | 2026-05-19 21:49:11

CI报告基于以下代码生成(30分钟更新一次):


1 任务总览

✅ 所有 Required 任务全部通过,可以合并(2 个可选任务失败,不阻塞合并)。

总执行(rerun次数) 总任务 ✅ 通过 ❌ 失败 ⏳ 运行中 ⏸️ 等待中 跳过
41(0) 41 39 2 0 0 0

2 任务状态汇总

2.1 Required 任务:10/10 通过

必选任务阻塞合并,失败需优先处理。

状态 任务 耗时 根因 修复建议 日志 重跑
其余 10 个必选任务通过 - - - - -

2.2 可选任务 — 29/31 通过

可选任务不阻塞合并,失败仅供参考。

状态 任务 耗时 日志 重跑
Run iluvatar Tests / run_iluvatar_cases 1m56s Job -
CI_HPU 1h29m Job -
其余 29 个可选任务通过 - - -

ℹ️ 失败任务说明:

  • Run iluvatar Tests / run_iluvatar_cases:自定义容器启动失败(环境问题,与 PR 代码无关)
  • CI_HPU:HPU 环境任务退出码 1(环境问题,与 PR 代码无关)

3 失败详情(仅 Required)

无 Required 失败任务。

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