GRPOTrainer replays old buffer slices after resuming from a checkpoint saved mid generation buffer
What happens
When a GRPO run resumes from a checkpoint saved in the middle of a generation buffer, the resumed run does not train on the same data as an unbroken run. It trains again on buffer slices that were already used before the checkpoint, every later generation moves by the same offset, and the last slices are never reached. There is no error or warning.
I found this while testing resume behavior on a tiny model, not in a long production run. A real run hits it whenever save_steps is not a multiple of the generation interval, for example a preemptible or time limited job with steps_per_generation set above gradient_accumulation_steps to make generation cheaper, or num_iterations > 1.
Cause
self._step is set to 0 in __init__ and only increases in training_step. It is not restored on resume. After a resume, _buffered_inputs is None, so _prepare_inputs regenerates the buffer, which is expected since the buffer is not saved. But it then reads self._buffered_inputs[self._step % steps_per_generation] with _step back at 0, so it starts from slice 0 instead of the slice the run had reached.
Reproduction
Runs in about 30 seconds on CPU.
from datasets import load_dataset
from trl import GRPOConfig, GRPOTrainer
def length_reward(completions, **kwargs):
return [float(len(str(c))) for c in completions]
class RecordingGRPOTrainer(GRPOTrainer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.schedule = []
def _prepare_inputs(self, generation_batch):
if self.model.training:
generates = (
self._step % (self.args.steps_per_generation * self.num_iterations) == 0
or self._buffered_inputs is None
)
self.schedule.append((self.state.global_step, generates, self._step % self.args.steps_per_generation))
return super()._prepare_inputs(generation_batch)
def build(output_dir):
dataset = load_dataset("trl-internal-testing/zen", "conversational_prompt_only", split="train").select(range(16))
args = GRPOConfig(
output_dir=output_dir,
report_to="none",
per_device_train_batch_size=4,
num_generations=2,
steps_per_generation=4,
max_completion_length=8,
max_steps=8,
save_steps=2,
seed=0,
)
return RecordingGRPOTrainer(
model="trl-internal-testing/tiny-Qwen2ForCausalLM-2.5", reward_funcs=length_reward, args=args, train_dataset=dataset
)
straight = build("out/straight")
straight.train()
print("unbroken:", straight.schedule)
resumed = build("out/resumed")
resumed.train(resume_from_checkpoint="out/straight/checkpoint-2")
print("resumed: ", resumed.schedule)
Each tuple is (global_step, generates, buffer_slice).
| global_step | unbroken run | resumed from checkpoint-2 | resumed with fix |
|---|---|---|---|
| 0 | generate, slice 0 | ||
| 1 | slice 1 | ||
| 2 | slice 2 | generate, slice 0 | generate, slice 2 |
| 3 | slice 3 | slice 1 | slice 3 |
| 4 | generate, slice 0 | slice 2 | generate, slice 0 |
| 5 | slice 1 | slice 3 | slice 1 |
| 6 | slice 2 | generate, slice 0 | slice 2 |
| 7 | slice 3 | slice 1 | slice 3 |
In the resumed run, steps 2 and 3 train on slices 0 and 1 of the first batch again, which steps 0 and 1 already used. The second generation moves from step 4 to step 6, and slices 2 and 3 of the second batch are never used. The regenerated batch at step 2 starts with the same prompt as the unbroken batch at step 0, so the sampler is already in the right place. Only the slice index is wrong.
Proposed fix
In _prepare_inputs, when training and _buffered_inputs is None, restore _step before the generate check:
if self._buffered_inputs is None:
self._step = self.state.global_step * self.args.gradient_accumulation_steps
_step counts micro steps, so this is the value it would have had without the restart. With it, the resumed schedule matches the unbroken one, as shown in the last column above.
I would like to open a PR with this fix and a test that compares the resumed schedule with the unbroken one, through the public train(resume_from_checkpoint=...) path. Could I be assigned to this issue?
System info
- Platform: Linux-7.2.8-200.fc44.x86_64-x86_64-with-glibc2.43
- Python version: 3.12.15
- TRL version: 1.19.0.dev0+56d5149
- PyTorch version: 2.15.0+cpu
- accelerator(s): cpu
- Transformers version: 5.24.0
- Accelerate version: 1.16.0
- Accelerate config: not found
- Datasets version: 5.2.0
- HF Hub version: 2.4.0
- bitsandbytes version: 0.50.2
- DeepSpeed version: 0.20.0
- Liger-Kernel version: not installed
- PEFT version: 0.22.0
- vLLM version: not installed
Hey @amareshhebbar, thanks for the clear write-up and repro. This has the same root cause as #7787: _step isn't restored on resume, so the generation block boundaries shift. That issue is still open and already has a patch in progress, so I'm closing this one as a duplicate to keep the discussion in one place.
One note on the proposed fix: #7787 points out that global_step * gradient_accumulation_steps gives the wrong micro-step count once an epoch ends on a short accumulation window, so the restore has to count micro steps per epoch. If your steps_per_generation > gradient_accumulation_steps case adds coverage beyond what's there, please add it to #7787.