Skip to content

Avoid per-microbatch D2H caused by isfinite - #3873

Merged
fegin merged 2 commits into
mainfrom
gh/fegin/148/head
Jul 7, 2026
Merged

fegin merged 2 commits into
mainfrom
gh/fegin/148/head

Conversation

@fegin

@fegin fegin commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

#3863 introduces a good feature to raise if loss is NaN but also introduces per-micro-batch D2H sync. This PR fixes the issue with the penalty that the actual batch that causes the NaN is unkown (within a certain range).

#2728 is a potential solution to remove the penalty.

Another mitigation from @mlazos: you could also copy N loss values on GPU to keep a record of which of the N steps had the NaN.

fegin added 2 commits July 6, 2026 16:27
[ghstack-poisoned]
[ghstack-poisoned]
@fegin
fegin requested review from tianyu-l, wconstab and wwwjn as code owners July 6, 2026 23:27
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 6, 2026
@francesco-bertolotti

Copy link
Copy Markdown
Collaborator

Hi @fegin,

This looks like a good compromise to avoid the D2H overhead. I didn't realize it could have such a significant impact. Is the issue primarily that it interferes with kernel scheduling across successive microbatches, or is it mostly the overhead of transferring a single boolean to the host?

One alternative that might be worth considering for the logging condition is to perform the isfinite check after all microbatches have been processed and the optimizer step has completed. For example, roughly after:

self.checkpointer.maybe_wait_for_staging()
self.optimizers.step()
self.lr_schedulers.step()
# Reduce the data collected over gradient accumulation steps.
loss = torch.sum(torch.stack(accumulated_losses))

@fegin
fegin changed the base branch from gh/fegin/148/base to main July 7, 2026 16:41
@fegin
fegin merged commit da3be38 into main Jul 7, 2026
7 of 12 checks passed
jemitche1 pushed a commit to jemitche1/torchtitan that referenced this pull request Jul 15, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* __->__ pytorch#3873

pytorch#3863 introduces a good
feature to raise if loss is NaN but also introduces per-micro-batch D2H
sync. This PR fixes the issue with the penalty that the actual batch
that causes the NaN is unkown (within a certain range).

pytorch#2728 is a potential solution
to remove the penalty.

Another mitigation from @mlazos: you could also copy N loss values on
GPU to keep a record of which of the N steps had the NaN.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants