已合并
feat: 支持 Dynamic RL history-tree speculative decoding #149
thuZyQiu创建于 6月27日
feat: 支持 Dynamic RL history-tree speculative decoding #149
已合并
Pull Request已成功合入, 合并人@CANN-robot
(感谢 thuZyQiu 的贡献)6月27日 添加了label:cann-cla/yes
CANN-robot
6月27日 评论:
6月27日 评论:
Thanks for your pull-request.
The full list of commands accepted by me can be found at here。
You can get sig-info at here
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| repo-cann/cann-recipes-train | ✅ lrwei0709, xuyujun (2/2) | ✅ xuyujun (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
qq_62640183, thanks for your pull request. All authors of the commits have signed the CLA. 👍


atomgit-bot
6月27日 评论:
6月27日 评论:
⚠️ 本次变更过大(6 个文件、8631 行),已超出 AI 代码评审的处理范围,本次跳过。建议拆分为更小的 PR 以获得有效评审。


atomgit-bot
6月27日 评论:
6月27日 评论:
⚠️ 本次变更过大(6 个文件、8631 行),已超出 AI 代码评审的处理范围,本次跳过。建议拆分为更小的 PR 以获得有效评审。


此处折叠了48条消息 查看更多
CANN-robot
7 天前 评论:
7 天前 评论:
The following labels are not ready.
lgtm: Please wait for reviewers to review the code.
approved: Please wait for committers to review the code.


7 天前 添加了label:lgtmapproved
7 天前 合入了pull request
History Tree Dynamic RL 投机解码
1. 简介
Dynamic RL 是面向 RL Rollout 长序列采样的投机解码策略。它把同一套 Rollout 请求在解码过程中的历史响应复用能力和 EAGLE3 草稿模型能力组合起来,在不同 batch size 和不同阶段下动态选择更合适的草稿来源:
history_tree:从上一轮 Rollout 产生的响应中提取高 reward 片段,构建固定长度历史草稿缓存,在后续相同 prompt 或相似上下文中直接给出草稿 token。eagle3:使用独立 EAGLE3 draft model 生成草稿 token,适合较小 batch size 下的长尾解码阶段。dynamic_rl:同时接入history_tree与eagle3,按 batch size、历史缓存预热状态和在线 timing 统计动态切换。本文档覆盖当前 patch 已实现的 Dynamic RL 相关能力。
2. 使用说明
2.1 最小启动配置
在 veRL 启动脚本中配置 Rollout 投机方法为
dynamic_rl,并指定 EAGLE3 draft model:actor_rollout_ref.rollout.spec_method='dynamic_rl' actor_rollout_ref.rollout.eagle3_draft_model='/path/to/Qwen3-30B-moe-eagle3' actor_rollout_ref.rollout.spec_num_speculative_tokens=10当前 EAGLE3 分支使用 chain draft,只需要配置 draft model 路径和草稿 token 数。
如果只验证历史响应复用分支,可将
spec_method设置为history_tree,此时不需要eagle3_draft_model:actor_rollout_ref.rollout.spec_method='history_tree' actor_rollout_ref.rollout.spec_num_speculative_tokens=4仓库内提供了 Qwen3-30B 16 卡 GRPO 最小复现脚本:
cd llm_rl/qwen3 MODEL_PATH=/path/to/Qwen3-30B-A3B \ DISTCP_PATH=/path/to/Qwen3-30B-A3B_megatron \ TRAIN_FILE=/path/to/train.parquet \ TEST_FILE=/path/to/test.parquet \ bash internal/train_grpo_qwen3_30b_16die_dynamic_rl.sh脚本默认使用
/home/data/Qwen3-30B-moe-eagle3作为 EAGLE3 draft model 路径。如需覆盖,可在脚本末尾通过 Hydra 参数传入:2.2 配置项
actor_rollout_ref.rollout.spec_methodnullhistory_tree、eagle3或dynamic_rl。actor_rollout_ref.rollout.eagle3_draft_modelnulldynamic_rl与eagle3需要。未配置时会尝试自动查找常见本地路径。actor_rollout_ref.rollout.spec_num_speculative_tokens4VLLM_HISTORY_TREE_MAX_SPEC_REQS64VLLM_DYNAMIC_RL_EAGLE_MAX_BSZ8VLLM_DYNAMIC_RL_EAGLE_PROBE_MAX_BSZ2VLLM_DYNAMIC_RL_HISTORY_UPPER_BSZ_EXCLUSIVEVLLM_HISTORY_TREE_MAX_SPEC_REQSVLLM_DYNAMIC_RL_POLICYtiming_guardtiming_guard、history_first、threshold、threshold_only。VLLM_DYNAMIC_RL_HISTORY_WARMUP_RECORDS1VLLM_DYNAMIC_RL_EAGLE_COLD_START_GUARD1VLLM_DYNAMIC_RL_COMPARE_AFTER_STEPS64VLLM_DYNAMIC_RL_EAGLE_PROBE_STEPS8VLLM_DYNAMIC_RL_SCORE_EMA_ALPHA0.3VLLM_DYNAMIC_RL_SWITCH_MARGIN1.02VLLM_DYNAMIC_RL_EAGLE_COOLDOWN_STEPS8192VLLM_DYNAMIC_RL_EAGLE_MAX_COOLDOWN_STEPS65536VLLM_DYNAMIC_RL_EAGLE_COOLDOWN_GROWTH4.0VLLM_DYNAMIC_RL_EAGLE_BAD_STEP_RATIO0.98VLLM_DYNAMIC_RL_EAGLE_ENFORCE_EAGER1dynamic_rl的 EAGLE3 分支默认关闭 draft graph,走 eager 路径。VLLM_ASCEND_SPEC_TIMINGVLLM_ASCEND_SPEC_TIMING_LOG_EVERY1VLLM_ASCEND_SPEC_TIMING_FIRST_N203. 实现方案
3.1 veRL 接入
veRL 侧新增 Rollout 配置
spec_method、eagle3_draft_model、spec_num_speculative_tokens。当spec_method为history_tree或dynamic_rl时,训练主流程初始化全局 history tree actor,并在每轮 Rollout 前后传递历史记录。Rollout 结束后,trainer 会从 batch 中提取:
prompt_id;这些记录通过
history_tree_records传入 vLLM Rollout worker,并同步到 proposer 的 history cache。相关代码:
3.2 vLLM 配置与 EAGLE3 适配
vLLM 侧将
history_tree和dynamic_rl注册为 speculative method,并复用 EAGLE3 的 draft model 配置路径。dynamic_rl在 EAGLE 分支中会被标准化为 EAGLE3 chain draft 行为,确保 auxiliary hidden states 与 EAGLE3 draft head 正确启用。相关代码:
3.3 vLLM-Ascend proposer
vLLM-Ascend 侧新增两个 proposer:
HistoryRolloutProposer:维护按 prompt 隔离的FixedDraftHistoryCache,同时保留全局 fallback cache。它从历史响应中按固定 prefix 长度提取候选草稿,并按 reward 优先更新。DynamicProposer:内部持有HistoryRolloutProposer与EagleProposer,在每个 decode step 基于 batch bucket、预热状态和 timing 统计选择当前分支。dynamic_rl默认策略timing_guard的核心逻辑:VLLM_DYNAMIC_RL_SWITCH_MARGIN时才切换。相关代码:
4. 指标与观测
Dynamic RL 会通过 Rollout
meta_info["metrics"]汇总投机解码指标,包括总体指标和按分支拆分的指标:speculative_decoding/effective_percentspeculative_decoding/effective_lengthspeculative_decoding/predict_timesspeculative_decoding/effective_timesspeculative_decoding/total_right_lengthspeculative_decoding/dynamic_rl/eagle_*speculative_decoding/dynamic_rl/history_*如需分析分支切换原因,可开启 speculative timing:
export VLLM_ASCEND_SPEC_TIMING=1 export VLLM_ASCEND_SPEC_TIMING_LOG_EVERY=200 export VLLM_ASCEND_SPEC_TIMING_FIRST_N=5timing 信息中会记录
dynamic_mode、dynamic_reason、dynamic_bucket、dynamic_history_ready、dynamic_eagle_tokens_per_ms、dynamic_history_tokens_per_ms等字段,用于判断当前 step 选择 history 还是 EAGLE3。5. 使能效果
我们在 Qwen3-30B-A3B MoE 模型上,于 Deepscaler 数学推理数据集的真实 GRPO Rollout 场景中进行了端到端验证。结果来源于
/home/qiuzy3/llm_rl/qwen3/outputs/rl/logs。相关配置:
max_prompt_length):1024。max_response_length):16384。train_batch_size):64。rollout_n):8。max_num_seqs):64。tensor_model_parallel_size):4。spec_num_speculative_tokens):4。VLLM_DYNAMIC_RL_POLICY=timing_guard,VLLM_DYNAMIC_RL_HISTORY_UPPER_BSZ_EXCLUSIVE=64,VLLM_DYNAMIC_RL_EAGLE_MAX_BSZ=8。dynamic_rl + length-aware同时启用 Length-Aware Resampler,并在日志中动态下发rollout/response_max_tokens_cap=10000。使用日志:
qwen3_30b_verl_true_weights_baseline_no_resampler.log。qwen3_30b_verl_true_weights_history_tree_no_resampler.log。qwen3_30b_verl_true_weights_dynamic_rl_no_resampler.log。qwen3_30b_verl_true_weights_length_aware_resampler.log。speculative_decoding/effective_percentspeculative_decoding/effective_lengthhistory_tree)单步总推理时间对应日志中的
timing_s/generate_sequences,单步总时间对应perf/time_per_step。Qwen3-30B-A3B(HistorySpec,非 length-aware)
history_tree)Qwen3-30B-A3B(Dynamic RL,非 length-aware)
Qwen3-30B-A3B(Dynamic RL + Length-Aware)
6. 约束与注意事项
dynamic_rl需要可加载的 EAGLE3 draft model;如果只验证历史复用能力,请使用history_tree。spec_num_speculative_tokens控制每步草稿长度。dynamic_rl的第一轮 Rollout 没有历史响应可用,history cache 需要在后续 Rollout 中逐步预热。USE_ALLTOALL_OVERLAP、ALL_TO_ALL_RESHARD、VLLM_ENABLE_EXPERT_PARALLEL等基础 MoE 环境变量。