没有改文件。结论:最小侵入点在 EAGLEWorker.draft() 里,build_tree_kernel_efficient(...) 之后、构造 EagleVerifyInput(...) 之前。也就是 eagle_worker.py 到 eagle_worker.py 之间。
更具体地说,推荐做成一个 shape-preserving graft:把 prompt/in-context ngram lookup 结果塞进现有 EAGLE tree 的某一条已有 branch/subtree,尽量不改变 topk/spec_steps/draft_token_num。这样可以复用现有 draft CUDA graph、target verify CUDA graph、KV 分配/free、GLA rollback 和 acceptance kernel。不要一开始就追加更多节点,否则会碰到大量 dtn 固定 shape 假设。
当前接口链路
- EAGLE decode 入口:
EAGLEWorker.forward_batch_generation() 调 draft() 再调 verify(),见 eagle_worker.py。
- EAGLE tree 由
draft_forward() 产生 parent_list/top_scores_index/draft_tokens,再由 build_tree_kernel_efficient() 生成 verify tensors,见 eagle_worker.py 和 eagle_utils.py。
- Verify 输入类型是
EagleVerifyInput,字段定义在 eagle_info.py。
- Verify acceptance 用
verify_tree_greedy_func() / sgl_kernel.verify_tree_greedy,只消费 draft_token + retrive_* + target_predict,见 eagle_info.py 和 eagle_utils.py。
最小接入方式
在 build_tree_kernel_efficient() 返回后,拿到:
python
tree_mask, position, retrive_index, retrive_next_token, retrive_next_sibling, draft_tokens
然后做一个 _inject_ngram_branch(...),只改同 shape tensor:
draft_tokens: 把选中 branch 上的 token id 替换为 ngram lookup 的 token。
position: 如果复用原 branch topology,通常不改;若 ngram branch 深度和原 branch 不一致,需要按 seq_len + depth 改。
tree_mask/custom_mask: 复用原 branch topology 时不改;若改 parent/sibling topology,必须重建。
retrive_index: 通常保持 bs x dtn 的 flat index,不改。
retrive_next_token/retrive_next_sibling: 只有改拓扑或 child 顺序时才改。
spec_steps/topk/draft_token_num: 强烈建议保持原值。
capture_hidden_mode: 保持 CaptureHiddenMode.FULL,否则 verify 后没有足够 hidden state 供下一轮 draft,见 eagle_worker.py。
各字段语义
draft_token: flat (bs * dtn,),每 req 第 0 个必须是 user_4813494d/verified_id。
custom_mask: flat bool,FULL_MASK 布局是每 req dtn x (seq_len + dtn),prefix 全可见,tree 部分按祖先可见。
positions: flat (bs * dtn,),tree 节点通常是 seq_len + depth,siblings 可共享 position。
retrive_index: (bs, dtn),通常是每 req 对应 flat logits row index。
retrive_next_token: (bs, dtn),每个 node 的 first child,-1 表示 leaf。
retrive_next_sibling: (bs, dtn),同父右 sibling,-1 表示无。
spec_steps: acceptance path 最大深度,accept_index 只有 (bs, spec_steps + 1),见 eagle_info.py。
topk: 不只是统计字段;topk > 1 会触发 GLA tree parent path,见 hybrid_linear_attn_backend.py。
draft_token_num: verify 每 req 节点数,也就是 dtn,影响 KV allocation、CUDA graph、FlashInfer wrapper。
capture_hidden_mode: EAGLE verify 当前必须是 FULL。
固定 topk*steps 假设
select_top_k_tokens() 固定按 topk 展开 draft tree,见 spec_utils.py。
organize_draft_results() 从所有 step candidate 中取 num_draft_token - 1,见 eagle_utils.py。
sgl_kernel.build_tree_kernel_efficient 由 Python wrapper 传入 topk/spec_steps/num_verify_tokens,这是 EAGLE 固定树构造点,见 eagle_utils.py。
- Draft CUDA graph 把
topk/speculative_num_steps 烘进 buffer shape,见 eagle_draft_cuda_graph_runner.py 和 eagle_draft_cuda_graph_runner.py。
- Target verify CUDA graph 按
speculative_num_draft_tokens 捕获 TARGET_VERIFY,见 cuda_graph_runner.py 和 cuda_graph_runner.py。
- Dynamic mode 只预期 D5/D7 的固定
topk/steps/dtn,且校验 dtn == 1 + topk * steps,见 spec_mode.py。
- MiniCPM target verify FlashInfer CUDA graph 按
(bs, draft_token_num) 存 metadata,并默认 causal=True,见 minicpm_backend.py 和 minicpm_backend.py。
主要风险
- 最大风险:MiniCPM 标准 attention 的 EAGLE verify path 当前基本忽略 EAGLE
custom_mask,走 causal=True。任意 ngram branch 如果需要 ancestor-only visibility,会被 sibling/cousin 污染。相关路径在 minicpm_backend.py。DDTree 为这个问题专门加了 dflash_full_custom_mask 和 manual SDPA,但那不是现有 EAGLE CUDA graph 热路径,见 dflash_worker.py。
- 如果增加
draft_token_num,需要重捕获/扩展 draft graph、target verify graph、MiniCPM verify wrapper、GLA intermediate buffers、metrics 和 overlap split。不是最小改动。
- 如果 ngram branch 深度超过
spec_steps,accept_index 容量不够,verify kernel/后处理会截断或越界风险。
- GLA path 要求 tree 是 topological order:parent index 必须早于 child。
_build_retrieve_parent_token_kernel() 明确依赖这个约定,见 hybrid_linear_attn_backend.py。
- Mamba/GLA rollback 用 accepted flat node index 当 intermediate state step,见 eagle_worker.py 和 hybrid_linear_attn_backend.py。改 topology 可以,但 accepted index 必须仍指向正确 node row。
- Sampling/grammar 路径也走
retrive_next_token/retrive_next_sibling,不是只影响 greedy,见 eagle_worker.py。
- 现有
NGRAMWorker 已能构造完整 ngram verify tree,参考 ngram_worker.py,但它是独立 algorithm,不接 EAGLE draft hidden/topk pipeline。
所以我的建议是:第一版只做 固定 dtn 内的 branch 替换,接在 EAGLEWorker.draft() 的 tree tensors 出口处;先不新增节点、不改 server args、不改 CUDA graph。验证通过后,如果确实需要任意 topology,再借鉴 DDTree 的 build_tree_retrive() 和 custom_mask 构造,但那会进入 MiniCPM custom-mask/graph 兼容风险区。