Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/prime_rl/trainer/sft/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,19 @@ def run_eval_loop(data_iter):
total_token_count = torch.tensor(0, dtype=torch.int64, device="cuda")
nan_count = torch.tensor(0, device="cuda")

# Variable-length packing yields different per-rank batch counts. Under FSDP
# every forward is a collective, so all ranks must agree on when to stop —
# otherwise the first rank to exit deadlocks the rest in the next all-gather.
# Sync per batch and exit together as soon as any rank exhausts its iterator.
data_iter = iter(data_iter)

with torch.no_grad():
for micro_batch in data_iter:
while True:
micro_batch = next(data_iter, None)
has_data = torch.tensor(micro_batch is not None, dtype=torch.int32, device="cuda")
dist.all_reduce(has_data, op=dist.ReduceOp.MIN)
if has_data.item() == 0:
break
loss_sum, token_count = compute_loss(micro_batch)
if not torch.isnan(loss_sum.detach()):
total_loss_sum += loss_sum.detach()
Expand Down
Loading