Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/199242
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 New FailureAs of commit 09ef1b6 with merge base 73795c6 ( NEW FAILURE - The following job has failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This was referenced Oct 1, 2026
weifengpy
added a commit
that referenced
this pull request
Oct 1, 2026
> Authored with Claude Code. > > #198668 keeps grad_dtype for a parameter without a gradient in a backward without gradient sync, so that its accumulation starts in FP32. Autograd therefore allocates FP32 gradients during that backward while the group's BF16 unsharded parameters are still alive, and post_backward runs any remaining upcasts before resharding. The base kept BF16 gradients through backward and upcast them after resharding. So the first backward of every gradient accumulation window holds about one group's unsharded BF16 parameters more than the base, about 436 MB per Llama3-8B layer. > > This change defers the upcast for every parameter whose unsharded gradient dtype is wider than its compute dtype, in every backward. A backward without gradient sync now reshards before _set_unsharded_grad_dtypes upcasts its new BF16 gradients, so they are widened once the unsharded parameters are freed. Parameters whose gradients may be reduced outside DP are still never deferred, as in #198668, so their accumulation and TP all-reduce stay in FP32. > > Gradients of parameters used once per backward are bit-identical, since BF16 to FP32 is exact. A parameter used more than once in the first backward of a window now sums its contributions in BF16 before the upcast, as the base and #198668's later backwards do, where #198668 summed them in FP32. Accumulation across backwards stays in FP32. Pipeline parallelism runs every microbatch with reshard_after_backward=False, so nothing is freed before the upcast and its memory doesn't change; post_backward still upcasts at the end of each microbatch's backward, so its accumulation stays in FP32 too. During a group's backward, its unsharded parameters now expose BF16 gradients and grad_dtype=None in the first backward of a window as well; after the backward they expose FP32 gradients as before. > > On two H100s, with 4 FSDP blocks of Linear(2048, 8192) and Linear(8192, 2048), param_dtype=torch.bfloat16 and reduce_dtype=torch.float32, the first backward without gradient sync peaks at 828 MiB, versus 872 MiB with #198668 and 828 MiB in the base, with reshard_after_forward=True or False. The second backward without gradient sync and the backward that reduces are unchanged, at 1068 and 1112 MiB with reshard_after_forward=True. > > Test Plan > > test_grad_dtype_accumulation_skips_upcast now expects BF16 gradients in all three backwards and still checks that 256 + 1 - 256 accumulates to 1. test_grad_dtype_upcast_after_reshard checks that the upcast in a backward without gradient sync runs after the group reshards and leaves the expected FP32 gradient. Both fail against #198668's sources. test_fully_shard_mixed_precision.py passes on four GPUs (24 tests). Lint passes. > > ```bash > PY=/home/weif/.conda/envs/code-review/bin/python > T=test/distributed/_composable/fsdp > CUDA_VISIBLE_DEVICES=0,1,2,3 $PY -m pytest $T/test_fully_shard_mixed_precision.py -q -rfs > PATH=/home/weif/.conda/envs/code-review/bin:$PATH lintrunner -a $(git diff HEAD~1 --name-only --diff-filter=d) > git diff HEAD~1 --check > ``` ghstack-source-id: 199cf1f Pull-Request: #199242
weifengpy
added a commit
that referenced
this pull request
Oct 1, 2026
> Authored with Claude Code. > > #198668 keeps grad_dtype for a parameter without a gradient in a backward without gradient sync, so that its accumulation starts in FP32. Autograd therefore allocates FP32 gradients during that backward while the group's BF16 unsharded parameters are still alive, and post_backward runs any remaining upcasts before resharding. The base kept BF16 gradients through backward and upcast them after resharding. So the first backward of every gradient accumulation window holds about one group's unsharded BF16 parameters more than the base, about 436 MB per Llama3-8B layer. > > This change defers the upcast for every parameter whose unsharded gradient dtype is wider than its compute dtype, in every backward. A backward without gradient sync now reshards before _set_unsharded_grad_dtypes upcasts its new BF16 gradients, so they are widened once the unsharded parameters are freed. Parameters whose gradients may be reduced outside DP are still never deferred, as in #198668, so their accumulation and TP all-reduce stay in FP32. > > Gradients of parameters used once per backward are bit-identical, since BF16 to FP32 is exact. A parameter used more than once in the first backward of a window now sums its contributions in BF16 before the upcast, as the base and #198668's later backwards do, where #198668 summed them in FP32. Accumulation across backwards stays in FP32. Pipeline parallelism runs every microbatch with reshard_after_backward=False, so nothing is freed before the upcast and its memory doesn't change; post_backward still upcasts at the end of each microbatch's backward, so its accumulation stays in FP32 too. During a group's backward, its unsharded parameters now expose BF16 gradients and grad_dtype=None in the first backward of a window as well; after the backward they expose FP32 gradients as before. > > On two H100s, with 4 FSDP blocks of Linear(2048, 8192) and Linear(8192, 2048), param_dtype=torch.bfloat16 and reduce_dtype=torch.float32, the first backward without gradient sync peaks at 828 MiB, versus 872 MiB with #198668 and 828 MiB in the base, with reshard_after_forward=True or False. The second backward without gradient sync and the backward that reduces are unchanged, at 1068 and 1112 MiB with reshard_after_forward=True. > > Test Plan > > test_grad_dtype_accumulation_skips_upcast now expects BF16 gradients in all three backwards and still checks that 256 + 1 - 256 accumulates to 1. test_grad_dtype_upcast_after_reshard checks that the upcast in a backward without gradient sync runs after the group reshards and leaves the expected FP32 gradient. Both fail against #198668's sources. test_fully_shard_mixed_precision.py passes on four GPUs (24 tests). Lint passes. > > ```bash > PY=/home/weif/.conda/envs/code-review/bin/python > T=test/distributed/_composable/fsdp > CUDA_VISIBLE_DEVICES=0,1,2,3 $PY -m pytest $T/test_fully_shard_mixed_precision.py -q -rfs > PATH=/home/weif/.conda/envs/code-review/bin:$PATH lintrunner -a $(git diff HEAD~1 --name-only --diff-filter=d) > git diff HEAD~1 --check > ``` ghstack-source-id: 199cf1f Pull-Request: #199242
weifengpy
added a commit
that referenced
this pull request
Oct 2, 2026
> Authored with Claude Code. > > #198668 keeps grad_dtype for a parameter without a gradient in a backward without gradient sync, so that its accumulation starts in FP32. Autograd therefore allocates FP32 gradients during that backward while the group's BF16 unsharded parameters are still alive, and post_backward runs any remaining upcasts before resharding. The base kept BF16 gradients through backward and upcast them after resharding. So the first backward of every gradient accumulation window holds about one group's unsharded BF16 parameters more than the base, about 436 MB per Llama3-8B layer. > > This change defers the upcast for every parameter whose unsharded gradient dtype is wider than its compute dtype, in every backward. A backward without gradient sync now reshards before _set_unsharded_grad_dtypes upcasts its new BF16 gradients, so they are widened once the unsharded parameters are freed. Parameters whose gradients may be reduced outside DP are still never deferred, as in #198668, so their accumulation and TP all-reduce stay in FP32. > > Gradients of parameters used once per backward are bit-identical, since BF16 to FP32 is exact. A parameter used more than once in the first backward of a window now sums its contributions in BF16 before the upcast, as the base and #198668's later backwards do, where #198668 summed them in FP32. Accumulation across backwards stays in FP32. Pipeline parallelism runs every microbatch with reshard_after_backward=False, so nothing is freed before the upcast and its memory doesn't change; post_backward still upcasts at the end of each microbatch's backward, so its accumulation stays in FP32 too. During a group's backward, its unsharded parameters now expose BF16 gradients and grad_dtype=None in the first backward of a window as well; after the backward they expose FP32 gradients as before. > > On two H100s, with 4 FSDP blocks of Linear(2048, 8192) and Linear(8192, 2048), param_dtype=torch.bfloat16 and reduce_dtype=torch.float32, the first backward without gradient sync peaks at 828 MiB, versus 872 MiB with #198668 and 828 MiB in the base, with reshard_after_forward=True or False. The second backward without gradient sync and the backward that reduces are unchanged, at 1068 and 1112 MiB with reshard_after_forward=True. > > Test Plan > > test_grad_dtype_accumulation_skips_upcast now expects BF16 gradients in all three backwards and still checks that 256 + 1 - 256 accumulates to 1. test_grad_dtype_upcast_after_reshard checks that the upcast in a backward without gradient sync runs after the group reshards and leaves the expected FP32 gradient. Both fail against #198668's sources. test_fully_shard_mixed_precision.py passes on four GPUs (24 tests). Lint passes. > > ```bash > PY=/home/weif/.conda/envs/code-review/bin/python > T=test/distributed/_composable/fsdp > CUDA_VISIBLE_DEVICES=0,1,2,3 $PY -m pytest $T/test_fully_shard_mixed_precision.py -q -rfs > PATH=/home/weif/.conda/envs/code-review/bin:$PATH lintrunner -a $(git diff HEAD~1 --name-only --diff-filter=d) > git diff HEAD~1 --check > ``` ghstack-source-id: e926bf5 Pull-Request: #199242
Contributor
|
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack from ghstack (oldest at bottom):