Skip to content

[FSDP2] Upcast unsynced gradients after resharding - #199242

Draft
weifengpy wants to merge 3 commits into
gh/weifengpy/208/basefrom
gh/weifengpy/208/head
Draft

weifengpy wants to merge 3 commits into
gh/weifengpy/208/basefrom
gh/weifengpy/208/head

Conversation

@weifengpy

@weifengpy weifengpy commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

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.

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-poisoned]
@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests release notes: distributed (fsdp) release notes category labels Oct 1, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

🔗 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 Failure

As of commit 09ef1b6 with merge base 73795c6 (image):

NEW FAILURE - The following job has failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

[ghstack-poisoned]
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
[ghstack-poisoned]
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
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Dec 1, 2026

This branch has not been deployed

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

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests release notes: distributed (fsdp) release notes category Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant