Repository navigation
Conversation
| torch.distributed.all_reduce( | ||
| finite_flags, | ||
| op=torch.distributed.ReduceOp.MIN, | ||
| group=parallel_dims.world_mesh.get_group(), |
There was a problem hiding this comment.
question 1: should be able to use loss_mesh, instead of world_mesh, because e.g. across all TP ranks the value should be the same
There was a problem hiding this comment.
we probably should let PP last rank always broadcast loss to other ranks @sanketpurandare
| finite_flags = torch.stack( | ||
| [loss_is_finite, torch.isfinite(grad_norm).all()] | ||
| ).to(dtype=torch.int32) | ||
| torch.distributed.all_reduce( |
There was a problem hiding this comment.
question 2: do we need to all-reduce before erroring out, or put in another way, do we need all ranks to error out together? I think
- even if some rank failed, it's going to be detected and all ranks would stop
- the introduction of this small collective seems not a big deal, but I'd like to understand the tradeoff
|
@claude help review this PR. In particular, comment on my review feedback. |
|
Claude finished @tianyu-l's task in 7m 18s —— View job Review: Stop non-finite steps before optimizer updates
The core direction is right: moving the finiteness check ahead of Q1 —
|
| if not finite_flags.all().item(): | ||
| raise RuntimeError( | ||
| "Loss or gradient norm is not finite on at least one rank at " | ||
| f"step {self.step}. Stopping training before the optimizer update." | ||
| ) |
There was a problem hiding this comment.
yeah as claude said, this is very bad as it creates device-host sync -- I think that's the reason we didn't do this before
| torch.distributed.all_reduce( | ||
| finite_flags, | ||
| op=torch.distributed.ReduceOp.MIN, | ||
| group=parallel_dims.world_mesh.get_group(), |
There was a problem hiding this comment.
we probably should let PP last rank always broadcast loss to other ranks @sanketpurandare
|
@claude review again |
|
Claude finished @tianyu-l's task in 7m 21s —— View job Re-review: Stop non-finite steps before optimizer updates
The PR changed substantially since the last pass and it now addresses both of your questions well. One concrete blocker remains (a stale unit test that will fail against the new behavior), plus a tradeoff worth a sentence in the PR description. What's now resolvedPer-step host sync (your main objection) — fixed. The
PP propagation (your follow-up + Q2) — handled. Non-last PP stages skip the Blocker: the unit test now contradicts the implementationCommit But the whole point of this PR is that the check is no longer gated on logging — This needs to be reconciled before landing:
One tradeoff to document (not necessarily change)Switching from
For a "training diverged, stop now" path, an un-catchable hard abort is arguably acceptable — but since this replaces a clean, catchable error, it's worth one line in the PR description explaining the choice (and confirming the message actually surfaces on your target PyTorch/GPU). If graceful teardown matters, the alternative is a single per-step Minor notes
Summary: the fix is correct and both of your review questions are now properly addressed (sync removed, |
Please fix.
Let's add this note into the trainer code. |
Our NaN guard runs AFTER self.optimizers.step() and inspects only the loss, so a NaN gradient is written into the weights before anything notices. Job 12473142 is the case in point, from our own 80b/README.md: grad_norm nan @30, loss nan @31, nan-abort @35 grad_norm went bad a full step before the loss, so the earliest available signal was one ahead of the one we were watching -- and five more updates landed on top of it. A separate incident (job 8663177) ran ~370 NaN steps unbounded. The existing guard is also weaker than it looks in practice: nan_abort_ consecutive defaults to 0 (off, trainer.py:221) and production omits the flag ENTIRELY, because the pinned pre-pytorch#3623 clones have no such field and passing it crashes every rank (killed umbrella 8680578). So today, at production, a NaN grad is applied and nothing stops the run. This adds a host-side isfinite check on grad_norm immediately before the step. Non-finite -> zero_grad, log, skip the step, still advance the lr scheduler. The model is left unchanged rather than poisoned; the loss guard keeps its existing job of deciding whether to abort. grad_norm is already reduced across ranks (and across pp when pp_mesh is passed) inside clip_grad_norm_, so every rank sees the same value and branches identically. No new collective. The .item() is on a value already read for logging a few lines later. Deliberately NOT torch._assert_async, which upstream's pytorch#4226 uses for the same purpose: on CUDA a failed device-side assert invalidates the process, which our failover machinery would read as a crash rather than a clean stop, and its XPU behavior is undocumented. Upstream's fix also lands in Trainer.train_step, which FaultTolerantTrainer overrides wholesale -- so merging pytorch#4226 would give us nothing here. This is the hand-port. maybe_wait_for_staging() stays outside the branch: it is a checkpoint concern, and skipping it on a skipped step would leave an async save un-awaited. nanguard.pbs smokes it. The bar is bit-identity against the parent commit on a healthy run, seeded and deterministic at 12 ranks / 1 node / seq=2048 -- the regime where the control actually holds. A guard in the hot path that perturbs normal training would be worse than no guard.
…ning fixes validated Records the trainer.py ordering bug (guard ran after the optimizer step and only saw loss, while our own 80B data shows grad_norm going bad a step earlier), why upstream's pytorch#4226 was unmergeable value (patches a train_step we override; uses torch._assert_async, undocumented on XPU), both smoke results with an honest note that ~0.3% tps is below what a single 10-step comparison resolves, and the sync ancestry finding that invalidated my earlier land-group-1-first advice.
Summary
Root cause
The existing non-finite loss check lived in the metrics logging path. Non-logging steps skipped it entirely, and logging steps reached the check only after
optimizer.step()andlr_scheduler.step(). A NaN or Inf could therefore corrupt model parameters and optimizer state before training stopped.Impact
Every gradient-accumulation loss and the clipped gradient norm are now checked through device-side flags. A world-wide reduction makes all ranks take the same failure path before the optimizer update, including pipeline stages that do not own the loss.
_assert_asyncavoids a per-step host sync; on CPU it raises a catchableRuntimeError, while on CUDA a failed assertion invalidates the process.