verl icon indicating copy to clipboard operation
verl copied to clipboard

[Bug Report] Step 模式下断点续训因 Epoch 循环限制导致提前退出(且无日志提示)(已给出修改建议)

Open solution3tl opened this issue 10 months ago • 3 comments

System Info

Environment matches verl requirements.

Information

  • [ ] The official example scripts
  • [x] My own modified scripts

Tasks

  • [ ] An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • [x] My own task or dataset (give details below)

Reproduction

1. 我遇到的问题现象 (Phenomenon)

我在跑训练时遇到了一个 “训练未完成就提前自动断开” 的问题。具体场景是:我配置的是 Step 模式(total_training_steps = 100),因为数据集不大,DataLoader 长度只有 25,配置文件里 total_epochs 用的是默认值 1。

故障复现步骤:

  1. 配置:设置 total_training_steps = 100,total_epochs = 1(保持默认)。
  2. 数据:使用较小的数据集,len(train_dataloader) = 25(小于总步数)。
  3. 操作:从 global_step = 20 的 Checkpoint 处恢复训练 (Resume)。
  4. 结果:训练器启动后,仅运行了 5 个 step(从 step 20 跑到 25,即当前 Epoch 结束),然后直接停止并退出。
  5. 关键痛点:训练停止时没有任何日志输出或报错提示(Silent Shutdown),难以排查是因为步数不够还是其他原因导致的退出。

2. 原因排查 (Root Cause Analysis)

我看了一下 dapo_ray_trainer.py 的 fit 函数源码,发现逻辑存在冲突:

  1. 主要原因:外层循环被 Epoch 锁死 代码里的主循环写的是 for epoch in range(config.total_epochs)。在我这个场景下,epoch 上限是 1。 因为我是 Resume 接着跑,Epoch 0 剩下的数据只有 5 个 batch。程序跑完这 5 个 batch 后,Dataloader 耗尽,外层 range(1) 也耗尽,导致 fit 函数直接 return,触发了 Ray 的退出逻辑。 本质问题是: 代码没有根据我设定的 total_training_steps 自动计算需要循环多少个 Epoch 才能喂饱这些步数。

  2. 次要发现:步数判定不一致 136 行附近的 is_last_step 判断用的是 gen_steps。在 PPO 这类算法里,如果采样步数和更新步数不一致,这个判断逻辑可能会导致 Checkpoint 保存时机不对。


3. 修改建议 (Proposed Solution)

建议在 fit 函数开头加一个动态计算逻辑,不要强依赖配置里的 total_epochs。

修改位置: dapo_ray_trainer.py

建议改法 1:解耦 Loop 限制 (Line ~110)

import math

# [原逻辑]:只读配置,容易不够跑
# for epoch in range(self.config.trainer.total_epochs):

# [建议逻辑]:根据目标 Step 自动算需要跑几轮数据
needed_epochs = math.ceil(self.total_training_steps / len(self.train_dataloader))
actual_epochs = max(self.config.trainer.total_epochs, needed_epochs)

for epoch in range(actual_epochs):
    # ...

建议改法 2:统一时钟 (Line ~136)

Python

# [原逻辑]:
# is_last_step = self.gen_steps >= self.total_training_steps

# [建议逻辑]:统一用 global_steps (更新步数) 判定结束
is_last_step = self.global_steps >= self.total_training_steps```

### Expected behavior

在配置为 Step-based 模式时,训练器应该自动循环数据(开启新的 Epoch),直到 global_steps 达到设定的 100 步为止。即使从 step 20 恢复且当前 Epoch 仅剩 5 个 batch,程序也应自动衔接到下一个 Epoch 继续训练,而不是直接退出。

solution3tl avatar Jan 22 '26 07:01 solution3tl

Great bug report with root cause analysis and proposed fix! This is a real issue affecting step-based training with resume.

Summary of the problem:

  • total_epochs=1 limits the training loop even when total_training_steps is not reached
  • Resume from checkpoint only runs remaining batches in current epoch, then exits
  • No warning when training exits early

Your proposed fixes are correct:

Fix 1: Dynamic epoch calculation

needed_epochs = math.ceil(self.total_training_steps / len(self.train_dataloader))
actual_epochs = max(self.config.trainer.total_epochs, needed_epochs)
for epoch in range(actual_epochs):
    # ...

Fix 2: Unified step counting

is_last_step = self.global_steps >= self.total_training_steps

Additional suggestion: Add a warning when training exits before reaching total_training_steps:

if self.global_steps < self.total_training_steps:
    logger.warning(
        f"Training stopped at step {self.global_steps} before reaching "\
        f"total_training_steps={self.total_training_steps}. "\
        f"Consider increasing total_epochs."
    )

This should be addressed in the DAPO trainer. Consider submitting a PR with these fixes.

Kirrito-k423 avatar Mar 17 '26 12:03 Kirrito-k423

I prepared a fix and opened PR #5805. The patch expands the effective epoch budget for step-based PPO runs, preserves correct resume epoch bounds, and adds CPU tests for mid-epoch and epoch-boundary resume cases.

chenshui223 avatar Mar 30 '26 07:03 chenshui223

中文:我复核了当前状态,这个问题仍然有效,不建议现在直接关闭。

证据:main 分支的 RayPPOTrainer.fit() 仍然从 current_epoch = self.global_steps // len(self.train_dataloader) 开始,并使用 range(current_epoch, self.config.trainer.total_epochs) 作为外层 epoch 循环边界,因此在 step-based 训练目标大于配置 epoch 可覆盖步数时,仍可能在达到 total_training_steps 前退出:https://github.com/verl-project/verl/blob/main/verl/trainer/ppo/ray_trainer.py#L1407-L1440 。当前 main 的 is_last_step 已使用 self.global_steps >= self.total_training_steps,但这不能单独解决外层 epoch budget 不足的问题:https://github.com/verl-project/verl/blob/main/verl/trainer/ppo/ray_trainer.py#L1482-L1482 。

已有 PR #5805 针对该问题提出修复,并在 PR 描述中写明 Closes #5013:https://github.com/verl-project/verl/pull/5805 。不过该 PR 目前仍是 open、未合并,并且还有关于 early-stop warning 未接入 fit() 结束路径的审查意见:https://github.com/verl-project/verl/pull/5805#discussion_r3340468005 。因此建议保持本 issue 打开,等待 #5805 补齐审查意见并合并后再关闭。

English: I re-checked the current state, and this issue still appears valid; I would not close it yet.

Evidence: on main, RayPPOTrainer.fit() still computes current_epoch = self.global_steps // len(self.train_dataloader) and iterates with range(current_epoch, self.config.trainer.total_epochs), so step-based training can still stop before total_training_steps if the configured epoch budget is too small: https://github.com/verl-project/verl/blob/main/verl/trainer/ppo/ray_trainer.py#L1407-L1440 . The current is_last_step condition on main already uses self.global_steps >= self.total_training_steps, but that alone does not fix the insufficient outer epoch budget: https://github.com/verl-project/verl/blob/main/verl/trainer/ppo/ray_trainer.py#L1482-L1482 .

PR #5805 proposes a targeted fix and says Closes #5013: https://github.com/verl-project/verl/pull/5805 . However, that PR is still open and unmerged, and it still has review feedback about the early-stop warning helper not being wired into the fit() end-of-loop path: https://github.com/verl-project/verl/pull/5805#discussion_r3340468005 . I suggest keeping this issue open until #5805 addresses the review feedback and is merged.

This response was generated by a scheduled Codex GPT5.5 issue processing task.

Kirrito-k423 avatar Jul 31 '26 01:07 Kirrito-k423