Repository navigation
Conversation
weifengpy
added a commit
to pytorch/pytorch
that referenced
this pull request
Oct 8, 2026
* accumulation across backwards without gradient sync stays in FP32 * TP grad reduction stays in FP32 > Authored with Claude Code. > > #198668 defers gradient upcasts by setting an unsharded parameter's grad_dtype to None when the backward reduces or the parameter already holds a gradient, so autograd doesn't cast every BF16 gradient to FP32. It chooses in pre_backward, so a forward sees FP32 while its backward runs with None. On trunk a compiled backward doesn't depend on grad_dtype: it casts leaf gradients to the parameter's dtype, and the autograd engine then applies the grad_dtype set when the backward runs. #199856 makes the compiled backward cast to the grad_dtype seen while tracing the forward, and guards on it. With the pre_backward choice, every compiled backward would then cast its BF16 weight gradients to FP32 inside the graph (the fourth row of #199856's table), undoing #198668 under compile. This PR is the FSDP-side prerequisite for #199856. > > The choice moves to pre_forward, after the unshard, and holds until post_backward, so a forward and its backward see the same grad_dtype. The rule is unchanged: None if the group reduces gradients or the parameter already holds a gradient, else the unsharded gradient dtype. It is now set either way rather than only to None, so a choice from an earlier forward can't carry over. post_backward and reset_iter_state() restore the unsharded gradient dtype and widen a deferred gradient that wasn't reduced, as on trunk; after a backward that reduces, this restore is what returns grad_dtype to FP32. No-grad forwards skip the choice, since no backward would restore it, and so does recomputation under activation checkpointing, so a backward keeps its forward's choice. The predicate moves to FSDPParam.defers_grad_upcast, and the group's list is renamed after it. Parameters whose sharded tensor implements FSDP's all-gather extension (fsdp_pre_all_gather) never defer: an extension runs its own compute, which may read grad_dtype to pick the dtype its backward produces, so grad_dtype stays the unsharded gradient dtype for them. torchtitan's MXFP8 linear reads it in forward and writes its weight gradient in that dtype; under the pre_forward choice alone it would see None and write BF16. > > Outside a forward and its backward, grad_dtype is the unsharded gradient dtype, as on trunk, so gradients deposited without FSDP's hooks still accumulate in FP32. That covers a zero-bubble schedule's weight passes, which run after their input pass's post_backward and assign or add dW themselves in stage_backward_weight, and gradients computed from unsharded parameters without calling their module. The previous revision of this PR set grad_dtype=None for the parameter's lifetime instead, and consecutive weight passes, e.g. on ScheduleInterleavedZeroBubble's last rank, then accumulated in BF16. A fixed FP32 grad_dtype would also keep forward and backward consistent, but gives up the deferral. > > Differences from trunk: in a backward whose forward chose None, a parameter used more than once sums its contributions in BF16 before the copy-in or AccumulateGrad widens the total. Trunk already does this in a backward that reduces, and parameters used once per backward get bit-identical gradients. It now also happens for pipeline microbatches forwarded before the step's first backward disables gradient sync, and in the synced backward of a group like fully_shard([norm, head]) used for a chunked loss, whose pre_backward trunk ran only after the per-chunk contributions had been cast. Extension-backed parameters no longer defer. For one whose backward writes grad_dtype, such as torchtitan's MXFP8 linear, nothing changes. One that hard-codes a BF16 weight gradient, such as torchtitan's NVFP4 linear, now gets autograd's per-parameter cast to FP32 and holds the FP32 gradient until the copy-in; pytorch/torchtitan#5180 makes NVFP4 write its weight gradient in grad_dtype, which removes the cast. Other code that reads grad_dtype during forward sees None in a forward that defers. Once #199856 lands, a compiled module whose forward runs both with FP32 (a first microbatch without gradient sync) and with None will compile once for each, since #199856 guards on grad_dtype. Extension-backed parameters keep a fixed grad_dtype, so they add no recompiles. FSDP only sets None or the wider dtype, so #199856's narrowing check can't fire, even when a later post_backward restores FP32 before an earlier-traced backward runs. > > The choice is the same loop over the group's parameters that trunk ran in pre_backward. For 3000 four-element parameters in one group on CPU, it takes 0.6 ms with gradient sync and 0.9 ms without, versus 0.9 and 0.7 ms for trunk's loop; the post_backward restore is unchanged at 0.9 ms. On a B200, the 3000 BF16-to-FP32 cast kernels the deferral avoids take 17.6 ms. > > Test Plan > > test_grad_dtype_chosen_in_forward checks that a backward sees its forward's grad_dtype when gradient sync changes in between, and that grad_dtype and the gradient are FP32 afterwards. test_grad_dtype_zero_bubble_weight_passes runs four input passes and then four weight passes over residual blocks, as on a zero-bubble schedule's last stage, and checks that the gradient is FP32 after each weight pass and equals FP32 accumulation. test_grad_dtype_outside_forward_and_backward checks that after a no-grad forward, gradients from the unsharded weight accumulate 256 + 1 to 257. All three fail on the previous revision of this PR; the zero-bubble test fails at its dtype check after the first weight pass (bfloat16 != float32). test_grad_dtype_accumulation_skips_upcast and test_grad_dtype_restored_after_deferred_upcast keep trunk's expectations. test_grad_dtype_fixed_for_all_gather_extensions attaches a plain all-gather extension and checks that the forward sees FP32 in two microbatches without gradient sync and a synced one, that autograd delivers FP32 gradients each time, and that 256 + 1 - 256 accumulates to 1. test_grad_dtype_seen_by_all_gather_extension_compute uses a custom autograd Function that, like torchtitan's MXFP8 linear, picks its weight gradient's dtype in forward; it sees FP32, and the reduced gradient keeps (1 + 2^-7)^2 exactly. Both fail without the extension rule, where the forward sees None. > > At this revision on B200 GPUs, the mixed precision file (31 tests), the all-gather extensions file (7), and the training and pipeline-with-FSDP mixed precision selections (3 and 8, including the zero-bubble schedules) pass, and lint passes. The compile selection (7) passed on the previous revision; the extension rule doesn't change those tests. The zero-bubble, chunked-loss, interleaved-microbatch and multi-use claims above come from a single-rank CPU (gloo) script, run before the extension rule, which doesn't affect those scenarios. > > ```bash > PY=/home/weif/.conda/envs/code-review/bin/python > $PY test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py > $PY test/distributed/_composable/fsdp/test_fully_shard_extensions.py > $PY -m pytest -q -p no:cacheprovider test/distributed/_composable/fsdp/test_fully_shard_training.py -k "grad_dtype or mixed_precision" > $PY -m pytest -q -p no:cacheprovider test/distributed/_composable/fsdp/test_fully_shard_compile.py -k "mixed_precision or mp" > $PY -m pytest -q -p no:cacheprovider test/distributed/test_composability.py -k "test_pp_fsdp and FSDP_MP" > PATH=/home/weif/.conda/envs/code-review/bin:$PATH lintrunner -a $(git diff HEAD~1 --name-only --diff-filter=d) > ``` ghstack-source-id: 28eaeed Pull-Request: #200196
weifengpy
marked this pull request as ready for review
October 8, 2026 21:31
weifengpy
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
October 8, 2026 21:31
weifengpy
added a commit
that referenced
this pull request
Oct 8, 2026
> Authored with Claude Code. > > NVFP4Linear's backward always wrote its weight gradient in BF16. Under FSDP2 with an FP32 reduce dtype, that gradient has already lost its low bits before it is widened, either by autograd (one cast kernel per weight per backward, plus a BF16 temporary) or by the reduce-scatter copy-in. This records the parameter's grad_dtype in forward, as MXFP8Linear does, and has the WGRAD scaled_mm write that dtype directly. The GEMM accumulates in FP32, so its FP32 output is the BF16 one before rounding. Without FSDP, or with a BF16 reduce dtype, grad_dtype is BF16 and nothing changes. > > pytorch/pytorch#200196 makes FSDP2 keep grad_dtype at the unsharded gradient dtype for parameters backed by its all-gather extension, as NVFP4 and MXFP8 weights are, because their compute may read it. Without this change, NVFP4 weights then pay autograd's FP32 cast on every backward; with it, the GEMM writes FP32 and there is nothing to cast. On current PyTorch trunk, FSDP's forward also sees the FP32 grad_dtype, so this removes the BF16 rounding there too, at the cost of holding NVFP4 weight gradients in FP32 instead of BF16 until the reduce-scatter. > > Test Plan > > test_nvfp4_linear_wgrad_follows_grad_dtype runs the same NVFP4Linear with grad_dtype BF16 and FP32, for a plain weight and for FSDP's unsharded-weight holder, and checks that the FP32 weight gradient is FP32 and rounds exactly to the BF16 one. On a B200, a standalone NVFP4 x NVFP4 scaled_mm with two-level scales accepts output_dtype=torch.float32, and its output rounded to BF16 equals the BF16 output. Lint passes except for 7 pyrefly errors in other files, which main also has. On B200, with a torchao developer build (0.19.0+gitcff77b46e) and a PyTorch build of pytorch/pytorch#200196 plus #199856: test_nvfp4_linear.py and test_nvfp4_fsdp.py give 9 passed and 1 failed with this change, versus 7 passed and the same failure on main. The failure is test_nvfp4_linear_runs_outside_plain_eager[compile], which can't find the cutlass (CuTeDSL) package in that environment. The environment also lacks attn-gym's optional CuTeDSL backend, which torchtitan.models.common imports at load, so these runs stub attn_gym.linear.short_conv with a sitecustomize. The MXFP8 tests, test_mxfp8_fsdp.py and test_mxfp8_linear.py, pass (32 tests), including the fused WGRAD accumulation with an FP32 reduce dtype under #200196's rule. > > ```bash > PYTHONPATH=<dir with the attn_gym stub sitecustomize.py>:$PWD pytest tests/unit_tests/gpu/test_nvfp4_linear.py tests/unit_tests/gpu/test_nvfp4_fsdp.py > PYTHONPATH=$PWD pytest tests/unit_tests/gpu/test_mxfp8_fsdp.py tests/unit_tests/gpu/test_mxfp8_linear.py > pre-commit run --files torchtitan/quantization/nvfp4/linear.py tests/unit_tests/gpu/test_nvfp4_linear.py > ``` ghstack-source-id: 3f46a55 Pull-Request: #5180
weifengpy
marked this pull request as draft
October 8, 2026 21:41
weifengpy
added a commit
that referenced
this pull request
Oct 8, 2026
> Authored with Claude Code. > > NVFP4Linear's backward always wrote its weight gradient in BF16. Under FSDP2 with an FP32 reduce dtype, that gradient has already lost its low bits before it is widened, either by autograd (one cast kernel per weight per backward, plus a BF16 temporary) or by the reduce-scatter copy-in. This records the parameter's grad_dtype in forward, as MXFP8Linear does, and has the WGRAD scaled_mm write that dtype directly. The GEMM accumulates in FP32, so its FP32 output is the BF16 one before rounding. Without FSDP, or with a BF16 reduce dtype, grad_dtype is BF16 and nothing changes. > > pytorch/pytorch#200196 makes FSDP2 keep grad_dtype at the unsharded gradient dtype for parameters backed by its all-gather extension, as NVFP4 and MXFP8 weights are, because their compute may read it. Without this change, NVFP4 weights then pay autograd's FP32 cast on every backward; with it, the GEMM writes FP32 and there is nothing to cast. On current PyTorch trunk, FSDP's forward also sees the FP32 grad_dtype, so this removes the BF16 rounding there too, at the cost of holding NVFP4 weight gradients in FP32 instead of BF16 until the reduce-scatter. > > Test Plan > > test_nvfp4_linear_wgrad_follows_grad_dtype runs the same NVFP4Linear with grad_dtype BF16 and FP32, for a plain weight and for FSDP's unsharded-weight holder, and checks that the FP32 weight gradient is FP32 and rounds exactly to the BF16 one. On a B200, a standalone NVFP4 x NVFP4 scaled_mm with two-level scales accepts output_dtype=torch.float32, and its output rounded to BF16 equals the BF16 output. Lint passes except for 7 pyrefly errors in other files, which main also has. On B200, with a torchao developer build (0.19.0+gitcff77b46e), attn-gym[linear] installed (CuTeDSL, so NVFP4's KernelPreference.AUTO runs the CuTeDSL kernels), and a PyTorch build of pytorch/pytorch#200196: test_nvfp4_linear.py and test_nvfp4_fsdp.py pass with this change (10 tests, including the two new ones), as they do on main (8). The MXFP8 tests, test_mxfp8_fsdp.py and test_mxfp8_linear.py, pass (32 tests), including the fused WGRAD accumulation with an FP32 reduce dtype under #200196's rule. > > ```bash > PYTHONPATH=$PWD pytest tests/unit_tests/gpu/test_nvfp4_linear.py tests/unit_tests/gpu/test_nvfp4_fsdp.py > PYTHONPATH=$PWD pytest tests/unit_tests/gpu/test_mxfp8_fsdp.py tests/unit_tests/gpu/test_mxfp8_linear.py > pre-commit run --files torchtitan/quantization/nvfp4/linear.py tests/unit_tests/gpu/test_nvfp4_linear.py > ``` ghstack-source-id: 3f46a55 Pull-Request: #5180
pytorchmergebot
pushed a commit
to pytorch/pytorch
that referenced
this pull request
Oct 9, 2026
…rd (#200196) ---- * accumulation across backwards without gradient sync stays in FP32 * TP grad reduction stays in FP32 > Authored with Claude Code. > > #198668 defers gradient upcasts by setting an unsharded parameter's grad_dtype to None when the backward reduces or the parameter already holds a gradient, so autograd doesn't cast every BF16 gradient to FP32. It chooses in pre_backward, so a forward sees FP32 while its backward runs with None. On trunk a compiled backward doesn't depend on grad_dtype: it casts leaf gradients to the parameter's dtype, and the autograd engine then applies the grad_dtype set when the backward runs. #199856 makes the compiled backward cast to the grad_dtype seen while tracing the forward, and guards on it. With the pre_backward choice, every compiled backward would then cast its BF16 weight gradients to FP32 inside the graph (the fourth row of #199856's table), undoing #198668 under compile. This PR is the FSDP-side prerequisite for #199856. > > The choice moves to pre_forward, after the unshard, and holds until post_backward, so a forward and its backward see the same grad_dtype. The rule is unchanged: None if the group reduces gradients or the parameter already holds a gradient, else the unsharded gradient dtype. It is now set either way rather than only to None, so a choice from an earlier forward can't carry over. post_backward and reset_iter_state() restore the unsharded gradient dtype and widen a deferred gradient that wasn't reduced, as on trunk; after a backward that reduces, this restore is what returns grad_dtype to FP32. No-grad forwards skip the choice, since no backward would restore it, and so does recomputation under activation checkpointing, so a backward keeps its forward's choice. The predicate moves to FSDPParam.defers_grad_upcast, and the group's list is renamed after it. Parameters whose sharded tensor implements FSDP's all-gather extension (fsdp_pre_all_gather) never defer: an extension runs its own compute, which may read grad_dtype to pick the dtype its backward produces, so grad_dtype stays the unsharded gradient dtype for them. torchtitan's MXFP8 linear reads it in forward and writes its weight gradient in that dtype; under the pre_forward choice alone it would see None and write BF16. > > Outside a forward and its backward, grad_dtype is the unsharded gradient dtype, as on trunk, so gradients deposited without FSDP's hooks still accumulate in FP32. That covers a zero-bubble schedule's weight passes, which run after their input pass's post_backward and assign or add dW themselves in stage_backward_weight, and gradients computed from unsharded parameters without calling their module. The previous revision of this PR set grad_dtype=None for the parameter's lifetime instead, and consecutive weight passes, e.g. on ScheduleInterleavedZeroBubble's last rank, then accumulated in BF16. A fixed FP32 grad_dtype would also keep forward and backward consistent, but gives up the deferral. > > Differences from trunk: in a backward whose forward chose None, a parameter used more than once sums its contributions in BF16 before the copy-in or AccumulateGrad widens the total. Trunk already does this in a backward that reduces, and parameters used once per backward get bit-identical gradients. It now also happens for pipeline microbatches forwarded before the step's first backward disables gradient sync, and in the synced backward of a group like fully_shard([norm, head]) used for a chunked loss, whose pre_backward trunk ran only after the per-chunk contributions had been cast. Extension-backed parameters no longer defer. For one whose backward writes grad_dtype, such as torchtitan's MXFP8 linear, nothing changes. One that hard-codes a BF16 weight gradient, such as torchtitan's NVFP4 linear, now gets autograd's per-parameter cast to FP32 and holds the FP32 gradient until the copy-in; pytorch/torchtitan#5180 makes NVFP4 write its weight gradient in grad_dtype, which removes the cast. Other code that reads grad_dtype during forward sees None in a forward that defers. Once #199856 lands, a compiled module whose forward runs both with FP32 (a first microbatch without gradient sync) and with None will compile once for each, since #199856 guards on grad_dtype. Extension-backed parameters keep a fixed grad_dtype, so they add no recompiles. FSDP only sets None or the wider dtype, so #199856's narrowing check can't fire, even when a later post_backward restores FP32 before an earlier-traced backward runs. > > The choice is the same loop over the group's parameters that trunk ran in pre_backward. For 3000 four-element parameters in one group on CPU, it takes 0.6 ms with gradient sync and 0.9 ms without, versus 0.9 and 0.7 ms for trunk's loop; the post_backward restore is unchanged at 0.9 ms. On a B200, the 3000 BF16-to-FP32 cast kernels the deferral avoids take 17.6 ms. > > Test Plan > > test_grad_dtype_chosen_in_forward checks that a backward sees its forward's grad_dtype when gradient sync changes in between, and that grad_dtype and the gradient are FP32 afterwards. test_grad_dtype_zero_bubble_weight_passes runs four input passes and then four weight passes over residual blocks, as on a zero-bubble schedule's last stage, and checks that the gradient is FP32 after each weight pass and equals FP32 accumulation. test_grad_dtype_outside_forward_and_backward checks that after a no-grad forward, gradients from the unsharded weight accumulate 256 + 1 to 257. All three fail on the previous revision of this PR; the zero-bubble test fails at its dtype check after the first weight pass (bfloat16 != float32). test_grad_dtype_accumulation_skips_upcast and test_grad_dtype_restored_after_deferred_upcast keep trunk's expectations. test_grad_dtype_fixed_for_all_gather_extensions attaches a plain all-gather extension and checks that the forward sees FP32 in two microbatches without gradient sync and a synced one, that autograd delivers FP32 gradients each time, and that 256 + 1 - 256 accumulates to 1. test_grad_dtype_seen_by_all_gather_extension_compute uses a custom autograd Function that, like torchtitan's MXFP8 linear, picks its weight gradient's dtype in forward; it sees FP32, and the reduced gradient keeps (1 + 2^-7)^2 exactly. Both fail without the extension rule, where the forward sees None. > > At this revision on B200 GPUs, the mixed precision file (31 tests), the all-gather extensions file (7), and the training and pipeline-with-FSDP mixed precision selections (3 and 8, including the zero-bubble schedules) pass, and lint passes. The compile selection (7) passed on the previous revision; the extension rule doesn't change those tests. The zero-bubble, chunked-loss, interleaved-microbatch and multi-use claims above come from a single-rank CPU (gloo) script, run before the extension rule, which doesn't affect those scenarios. > > ```bash > PY=/home/weif/.conda/envs/code-review/bin/python > $PY test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py > $PY test/distributed/_composable/fsdp/test_fully_shard_extensions.py > $PY -m pytest -q -p no:cacheprovider test/distributed/_composable/fsdp/test_fully_shard_training.py -k "grad_dtype or mixed_precision" > $PY -m pytest -q -p no:cacheprovider test/distributed/_composable/fsdp/test_fully_shard_compile.py -k "mixed_precision or mp" > $PY -m pytest -q -p no:cacheprovider test/distributed/test_composability.py -k "test_pp_fsdp and FSDP_MP" > PATH=/home/weif/.conda/envs/code-review/bin:$PATH lintrunner -a $(git diff HEAD~1 --name-only --diff-filter=d) > ``` Pull Request resolved: #200196 Approved by: https://github.com/anshul-si, https://github.com/anijain2305
cyyever
pushed a commit
to BakeLab/pytorch
that referenced
this pull request
Oct 11, 2026
…rd (pytorch#200196) ---- * accumulation across backwards without gradient sync stays in FP32 * TP grad reduction stays in FP32 > Authored with Claude Code. > > pytorch#198668 defers gradient upcasts by setting an unsharded parameter's grad_dtype to None when the backward reduces or the parameter already holds a gradient, so autograd doesn't cast every BF16 gradient to FP32. It chooses in pre_backward, so a forward sees FP32 while its backward runs with None. On trunk a compiled backward doesn't depend on grad_dtype: it casts leaf gradients to the parameter's dtype, and the autograd engine then applies the grad_dtype set when the backward runs. pytorch#199856 makes the compiled backward cast to the grad_dtype seen while tracing the forward, and guards on it. With the pre_backward choice, every compiled backward would then cast its BF16 weight gradients to FP32 inside the graph (the fourth row of pytorch#199856's table), undoing pytorch#198668 under compile. This PR is the FSDP-side prerequisite for pytorch#199856. > > The choice moves to pre_forward, after the unshard, and holds until post_backward, so a forward and its backward see the same grad_dtype. The rule is unchanged: None if the group reduces gradients or the parameter already holds a gradient, else the unsharded gradient dtype. It is now set either way rather than only to None, so a choice from an earlier forward can't carry over. post_backward and reset_iter_state() restore the unsharded gradient dtype and widen a deferred gradient that wasn't reduced, as on trunk; after a backward that reduces, this restore is what returns grad_dtype to FP32. No-grad forwards skip the choice, since no backward would restore it, and so does recomputation under activation checkpointing, so a backward keeps its forward's choice. The predicate moves to FSDPParam.defers_grad_upcast, and the group's list is renamed after it. Parameters whose sharded tensor implements FSDP's all-gather extension (fsdp_pre_all_gather) never defer: an extension runs its own compute, which may read grad_dtype to pick the dtype its backward produces, so grad_dtype stays the unsharded gradient dtype for them. torchtitan's MXFP8 linear reads it in forward and writes its weight gradient in that dtype; under the pre_forward choice alone it would see None and write BF16. > > Outside a forward and its backward, grad_dtype is the unsharded gradient dtype, as on trunk, so gradients deposited without FSDP's hooks still accumulate in FP32. That covers a zero-bubble schedule's weight passes, which run after their input pass's post_backward and assign or add dW themselves in stage_backward_weight, and gradients computed from unsharded parameters without calling their module. The previous revision of this PR set grad_dtype=None for the parameter's lifetime instead, and consecutive weight passes, e.g. on ScheduleInterleavedZeroBubble's last rank, then accumulated in BF16. A fixed FP32 grad_dtype would also keep forward and backward consistent, but gives up the deferral. > > Differences from trunk: in a backward whose forward chose None, a parameter used more than once sums its contributions in BF16 before the copy-in or AccumulateGrad widens the total. Trunk already does this in a backward that reduces, and parameters used once per backward get bit-identical gradients. It now also happens for pipeline microbatches forwarded before the step's first backward disables gradient sync, and in the synced backward of a group like fully_shard([norm, head]) used for a chunked loss, whose pre_backward trunk ran only after the per-chunk contributions had been cast. Extension-backed parameters no longer defer. For one whose backward writes grad_dtype, such as torchtitan's MXFP8 linear, nothing changes. One that hard-codes a BF16 weight gradient, such as torchtitan's NVFP4 linear, now gets autograd's per-parameter cast to FP32 and holds the FP32 gradient until the copy-in; pytorch/torchtitan#5180 makes NVFP4 write its weight gradient in grad_dtype, which removes the cast. Other code that reads grad_dtype during forward sees None in a forward that defers. Once pytorch#199856 lands, a compiled module whose forward runs both with FP32 (a first microbatch without gradient sync) and with None will compile once for each, since pytorch#199856 guards on grad_dtype. Extension-backed parameters keep a fixed grad_dtype, so they add no recompiles. FSDP only sets None or the wider dtype, so pytorch#199856's narrowing check can't fire, even when a later post_backward restores FP32 before an earlier-traced backward runs. > > The choice is the same loop over the group's parameters that trunk ran in pre_backward. For 3000 four-element parameters in one group on CPU, it takes 0.6 ms with gradient sync and 0.9 ms without, versus 0.9 and 0.7 ms for trunk's loop; the post_backward restore is unchanged at 0.9 ms. On a B200, the 3000 BF16-to-FP32 cast kernels the deferral avoids take 17.6 ms. > > Test Plan > > test_grad_dtype_chosen_in_forward checks that a backward sees its forward's grad_dtype when gradient sync changes in between, and that grad_dtype and the gradient are FP32 afterwards. test_grad_dtype_zero_bubble_weight_passes runs four input passes and then four weight passes over residual blocks, as on a zero-bubble schedule's last stage, and checks that the gradient is FP32 after each weight pass and equals FP32 accumulation. test_grad_dtype_outside_forward_and_backward checks that after a no-grad forward, gradients from the unsharded weight accumulate 256 + 1 to 257. All three fail on the previous revision of this PR; the zero-bubble test fails at its dtype check after the first weight pass (bfloat16 != float32). test_grad_dtype_accumulation_skips_upcast and test_grad_dtype_restored_after_deferred_upcast keep trunk's expectations. test_grad_dtype_fixed_for_all_gather_extensions attaches a plain all-gather extension and checks that the forward sees FP32 in two microbatches without gradient sync and a synced one, that autograd delivers FP32 gradients each time, and that 256 + 1 - 256 accumulates to 1. test_grad_dtype_seen_by_all_gather_extension_compute uses a custom autograd Function that, like torchtitan's MXFP8 linear, picks its weight gradient's dtype in forward; it sees FP32, and the reduced gradient keeps (1 + 2^-7)^2 exactly. Both fail without the extension rule, where the forward sees None. > > At this revision on B200 GPUs, the mixed precision file (31 tests), the all-gather extensions file (7), and the training and pipeline-with-FSDP mixed precision selections (3 and 8, including the zero-bubble schedules) pass, and lint passes. The compile selection (7) passed on the previous revision; the extension rule doesn't change those tests. The zero-bubble, chunked-loss, interleaved-microbatch and multi-use claims above come from a single-rank CPU (gloo) script, run before the extension rule, which doesn't affect those scenarios. > > ```bash > PY=/home/weif/.conda/envs/code-review/bin/python > $PY test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py > $PY test/distributed/_composable/fsdp/test_fully_shard_extensions.py > $PY -m pytest -q -p no:cacheprovider test/distributed/_composable/fsdp/test_fully_shard_training.py -k "grad_dtype or mixed_precision" > $PY -m pytest -q -p no:cacheprovider test/distributed/_composable/fsdp/test_fully_shard_compile.py -k "mixed_precision or mp" > $PY -m pytest -q -p no:cacheprovider test/distributed/test_composability.py -k "test_pp_fsdp and FSDP_MP" > PATH=/home/weif/.conda/envs/code-review/bin:$PATH lintrunner -a $(git diff HEAD~1 --name-only --diff-filter=d) > ``` Pull Request resolved: pytorch#200196 Approved by: https://github.com/anshul-si, https://github.com/anijain2305
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):