Skip to content

Fixing global_max_loss computation - #2314

Merged
meta-codesync[bot] merged 1 commit into
pytorch:mainfrom
Shagun-G:export-D92079302
Feb 3, 2026
Merged

meta-codesync[bot] merged 1 commit into
pytorch:mainfrom
Shagun-G:export-D92079302

Conversation

@Shagun-G

@Shagun-G Shagun-G commented Feb 2, 2026

Copy link
Copy Markdown
Contributor

Summary:
As per the fix to handle token imbalance D91432940 , the global_max_loss computed is below the global_avg_loss as shown in the test plan. This is because of the pre division of the loss to the global tokens. While the avg loss computation is correct, the global max loss in this case becomes the max of the loss contribution of each rank, no the max of the local loss of each rank.

One fix is to recale the local loss to the average loss for each rank to get the local average loss and then perform the max operation. The result can be seen in the test plan.

Differential Revision: D92079302

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Feb 2, 2026
@meta-codesync

meta-codesync Bot commented Feb 2, 2026

Copy link
Copy Markdown

@Shagun-G has exported this pull request. If you are a Meta employee, you can view the originating Diff in D92079302.

Comment thread torchtitan/train.py
@@ -619,9 +619,14 @@ def train_step(
# loss = local_loss_sum / global_valid_tokens

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
# loss = local_loss_sum / global_valid_tokens
# loss is define as local_loss_sum / global_valid_tokens

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! Let's make the calculation of global_avg_loss and global_avg_loss comment more clear. Can you help modify the comment about how to calculate global_avg_loss as well?

And please change the definition here as well:

global_max_loss: Maximum local loss across all ranks
Defined as max(local_loss_sum / global_valid_tokens)

Summary:

As per the fix to handle token imbalance D91432940 , the global_max_loss computed is below the global_avg_loss as shown in the test plan. This is because of the pre division of the loss to the global tokens. While the avg loss computation is correct, the global max loss in this case becomes the max of the loss contribution of each rank, no the max of the local loss of each rank.

One fix is to recale the local loss to the average loss for each rank to get the local average loss and then perform the max operation. The result can be seen in the test plan.

Differential Revision: D92079302

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

sounds ok to me as long as it's explicitly defined.

@meta-codesync
meta-codesync Bot merged commit 1a36996 into pytorch:main Feb 3, 2026
10 of 13 checks passed
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
Differential Revision: D92079302

Pull Request resolved: pytorch#2314
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
Differential Revision: D92079302

Pull Request resolved: pytorch#2314
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. fb-exported meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants