Skip to content

Fix BERT pretraining periodic loss logging to average across all ranks - #1470

Open
SID-6921 wants to merge 1 commit into
NVIDIA:masterfrom
SID-6921:fix/bert-pretraining-distributed-loss-logging
Open

SID-6921 wants to merge 1 commit into
NVIDIA:masterfrom
SID-6921:fix/bert-pretraining-distributed-loss-logging

Conversation

@SID-6921

Copy link
Copy Markdown

Summary

In PyTorch/LanguageModeling/BERT/run_pretraining.py, the periodic logging branch computed average_loss as a per-rank sum and logged it directly, without the div_(world_size) + all_reduce the final-step branch in the same function performs. Every periodic log line reported rank 0's local-shard loss instead of the true cross-rank mean; only the one-time final_loss logged at the end of training was ever actually averaged.

Fix

Mirror the final-step branch's existing pattern in the periodic branch: divide by log_freq * gradient_accumulation_steps, then divide by world_size and all_reduce, before copying into the host-side stat — all placed before the is_main_process() gate, since the collective op must run unconditionally on every rank or it deadlocks.

Test plan

No torch install available on this host to run the actual test suite. Verified via:

  • git diff / ast.parse for a clean, syntactically valid change
  • A standalone 8-rank simulation confirming old behavior reports rank 0's value (0.12) while the fix reports the true cross-rank mean (0.1235)

🤖 Generated with Claude Code

The periodic logging branch in run_pretraining computed average_loss as a
per-rank sum and logged it directly, without the div_(world_size) and
all_reduce that the final-step branch in the same function performs. Every
periodic log line reported rank 0's local-shard loss instead of the true
cross-rank mean; only the one-time final_loss logged at the end of training
was ever actually averaged.

Mirrors the final-step branch's existing pattern: divide by log_freq *
gradient_accumulation_steps, then divide by world_size and all_reduce before
copying into the host-side stat, all before the is_main_process() gate (the
collective op must run unconditionally on every rank or it deadlocks).

Verified via a standalone 8-rank simulation: old behavior reports rank 0's
value (0.12), fixed behavior reports the true cross-rank mean (0.1235).
No torch install available on this host to run the real test suite.

Signed-off-by: Siddhardha Nanda <99672439+SID-6921@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 28, 2026 16:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants