Skip to content

feat: support chunked prefill#1174

Open
Jonathan-hwx wants to merge 1 commit into
alibaba:mainfrom
Jonathan-hwx:feature/chunked-prefill
Open

feat: support chunked prefill#1174
Jonathan-hwx wants to merge 1 commit into
alibaba:mainfrom
Jonathan-hwx:feature/chunked-prefill

Conversation

@Jonathan-hwx

Copy link
Copy Markdown
Collaborator

Summary

  • add prefill_chunk_size config, alignment, and validation
  • implement chunked prefill stream windowing for PREFILL/PDFUSION
  • support MTP/speculative prefill chunk boundary handling
  • add config, stream, scheduler, MTP, batch processor, and smoke coverage

@Jonathan-hwx Jonathan-hwx requested a review from LLLLKKKK as a code owner July 8, 2026 13:48
@LLLLKKKK

LLLLKKKK commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

AI Code Review - PR #1174

Status: LGTM

Summary: P0/0 · P1/0 · P2/0 · P3/0

lgtm ready to ci

Checklist ✅ (100 items passed)

Strengths

  • 配置链路从 argparse、Python finalize、C++ struct、pybind pickle 到 pyi stub 都补齐了默认值和兼容反序列化。
  • middle chunk 的 token 丢弃、RNG 不推进、position_ids 只拷贝当前窗口、MTP draft 输入使用下一 prompt token 等关键语义都有针对性单测。
  • 对 MLA、hybrid attention、beam、logits/loss/hidden_states/all_probs、多模态等未验证组合做了显式拒绝或禁用,降低了新路径误开的风险。

@Jonathan-hwx Jonathan-hwx force-pushed the feature/chunked-prefill branch from c0675fb to 96ba8ff Compare July 9, 2026 02:19
@LLLLKKKK

LLLLKKKK commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

AI Code Review - PR #1174

Status: BLOCKING

Summary: P0/0 · P1/1 · P2/0 · P3/0

Blocking Issues

P1

  • PDFUSION 中间 chunk 会被当作 decode 轮次推进 KV block @ rtp_llm/cpp/engine_base/stream/GenerateStream.cc:94
    • 建议:在 PDFUSION 调度/状态机中把 isChunkedMiddleChunk() 或仍为 context 的 chunk stream 保持在 prefill 语义下,跳过 incrKVBlock() 和 decode promotion;或者先禁止 PDFUSION 启用 chunked prefill。补一个 PDFUSION scheduler 级测试,覆盖两段 middle chunk 后 KV block 数和最终输出。

Checklist Violations (2 fail / 52 total)

General Principles Checklist

  • [6.1] Architecture — 状态不变量:创建/更新/失败/重试/回滚路径有效 → issue PDFUSION 中间 chunk 会被当作 decode 轮次推进 KV block
    这里允许 RoleType::PDFUSION 启用 chunked prefill;中间 chunk 的 update()advanceChunk() 并保持 context。下一轮 PDFUSION scheduler 会对 RUNNING stream 调 moveToNext(),而现有状态机只对 PREFILL 跳过 incrKVBlock(),PDFUSION 会在未追加 token 时额外申请/重排 KV block,可能导致 block table 错位或提前 OOM。
  • [6.1] Tests — 新逻辑有聚焦单测 + 相关集成/smoke 测试 → issue PDFUSION 中间 chunk 会被当作 decode 轮次推进 KV block
    这里允许 RoleType::PDFUSION 启用 chunked prefill;中间 chunk 的 update()advanceChunk() 并保持 context。下一轮 PDFUSION scheduler 会对 RUNNING stream 调 moveToNext(),而现有状态机只对 PREFILL 跳过 incrKVBlock(),PDFUSION 会在未追加 token 时额外申请/重排 KV block,可能导致 block table 错位或提前 OOM。

Strengths

  • 对 MLA、hybrid attention、beam、logits、loss、hidden states、all_probs、多模态等未验证组合增加了显式 gate,避免静默输出不完整结果。
  • 对 position ids 当前 chunk 拷贝、MTP middle chunk tail token、sampler RNG 跳过、PREFILL scheduler 推进增加了聚焦测试。
  • 新配置同步到了 pybind、pyi、server args 和 to_string(),默认值为 0,默认路径保持关闭。

@Jonathan-hwx Jonathan-hwx force-pushed the feature/chunked-prefill branch from 96ba8ff to 0eaf5ab Compare July 9, 2026 03:44
@LLLLKKKK

LLLLKKKK commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

AI Code Review - PR #1174

Status: LGTM

Summary: P0/0 · P1/0 · P2/0 · P3/0

lgtm ready to ci

Checklist ✅ (52 items passed)

Strengths

  • 配置侧对 MLA、hybrid attention、非 PREFILL/PDFUSION role 和请求级不兼容选项做了显式 gate,降低了新能力误开的风险。
  • chunk 中间轮保持 context 状态、避免 RNG 推进,并补充了 scheduler、GenerateStream、normal gather、MTP draft tail 和 smoke 回归覆盖。

@Jonathan-hwx Jonathan-hwx force-pushed the feature/chunked-prefill branch from 0eaf5ab to efcba9f Compare July 9, 2026 07:07
@LLLLKKKK

LLLLKKKK commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

AI Code Review - PR #1174

Status: LGTM

Summary: P0/0 · P1/0 · P2/0 · P3/0

lgtm ready to ci

Checklist ✅ (52 items passed)

Strengths

  • chunked prefill 的高风险组合做了明确 hard gate,并补充了 C++ 单测、Python 配置单测和 H20 smoke golden,覆盖了普通 prefill、PDFusion、MTP、采样 RNG 跳过和 position ids 分片拷贝等关键路径。

@wht21

wht21 commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

internal source has been updated, please review the changes!

@Jonathan-hwx Jonathan-hwx force-pushed the feature/chunked-prefill branch from efcba9f to e46805b Compare July 9, 2026 08:31
@LLLLKKKK

LLLLKKKK commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

AI Code Review - PR #1174

Status: LGTM

Summary: P0/0 · P1/0 · P2/0 · P3/0

lgtm ready to ci

Checklist ✅ (52 items passed)

Strengths

  • chunked prefill 的启用条件在配置层和请求层都做了显式 gate,MLA/hybrid、多模态、beam、logits/loss/hidden/all_probs 等高风险组合有拒绝或禁用路径。
  • 普通路径和 MTP 路径都补了针对中间 chunk 的测试,覆盖 position ids 只复制当前窗口、sampler RNG 不前进、dispatch slot 对齐、PDFusion 不误走 decode KV 增量等关键不变量。
  • pybind pickle 兼容旧的 2/4 元组状态,同时 pyi 和 C++ to_string() 同步了新增字段,跨语言配置边界基本一致。

@wht21

wht21 commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

internal source has been updated, please review the changes!

2 similar comments
@wht21

wht21 commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

internal source has been updated, please review the changes!

@wht21

wht21 commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

internal source has been updated, please review the changes!

self.decode_prefill_ratio,
self.prefill_chunk_size);
},
[](py::tuple t) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

一起清理一下吧,不用这么复杂。

const StreamGroups& stream_groups) const {
std::vector<torch::Tensor> gathered_mm_features;
std::vector<torch::Tensor> gathered_mm_extra_input;
auto ctx = createGatherContext(config_, model_input, stream_groups, GatherContextMode::CONTEXT);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

整体实现在 scheduler 里是不是更科学一点?

@Jonathan-hwx Jonathan-hwx force-pushed the feature/chunked-prefill branch from e46805b to afc55a4 Compare July 10, 2026 04:04
@LLLLKKKK

Copy link
Copy Markdown
Collaborator

AI Code Review - PR #1174

Status: BLOCKING

Summary: P0/0 · P1/2 · P2/0 · P3/0

Blocking Issues

P1

  • 默认 warmup 绕过 chunking,仍按整段 prefill 消耗和预留显存 @ rtp_llm/cpp/normal_engine/NormalEngine.cc:192
    • 建议:让 prefill warmup 按归一化后的 chunk size 驱动完整分块循环,或提供等价的 chunk 峰值显存测量方式;同时增加保持默认 warmup 开启的启动测试,并验证最终 KV block 数和实际 chunked forward 一致。
  • MTP 与 chunked prefill 的真实缓存链路没有集成覆盖 @ rtp_llm/test/smoke/suites_h20_oss.bzl:236
    • 建议:增加 MTP + chunked prefill 的端到端 smoke/integration case:prompt 至少跨两个 middle chunk,启用 gen_num_per_cycle > 1,并与未分块结果对比最终 token;同时覆盖 draft/target KV 延续到首轮 decode 的路径。

Checklist Violations (2 fail / 118 total)

General Principles Checklist

  • [6.1] Architecture — 状态不变量:创建/更新/失败/重试/回滚路径有效 → issue 默认 warmup 绕过 chunking,仍按整段 prefill 消耗和预留显存
    保持默认 warm_up=True 时,prefillWarmUp 会构造接近 max_seq_len 的请求,但 preRun 在此处无条件清零 prefill_chunk_size。该整段 forward 可能在必须依赖 chunking 才能启动的配置上 OOM,其 max_used_memory 还会进入 KV cache 预算,抵消 chunking 的容量收益。新增 smoke 显式使用 --warm_up 0,未覆盖默认部署路径。
  • [6.1] Tests — 新逻辑有聚焦单测 + 相关集成/smoke 测试 → issue MTP 与 chunked prefill 的真实缓存链路没有集成覆盖
    新增 smoke 只运行普通 Qwen3 dense 路径,没有配置 speculative/MTP。MTP 单测通过手工构造 combo_tokensinput_lengths 和 sampler 输出验证局部变换,没有执行 target/draft forward、跨多个 middle chunk 的 KV 写入、最终 speculative decode 与输出对齐,因此 boundary token、position ids 或 draft cache 偏移错误都无法被当前测试发现。

Strengths

  • 配置默认关闭,并对角色、MLA、混合注意力、batch decode scheduler 和不兼容请求选项设置了明确门禁。
  • 单元测试覆盖 chunk 窗口推进、KV block 对齐、position ids 切片、采样 RNG 隔离和 MTP 边界 token,普通 dense 路径也增加了 smoke golden。

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