Repository navigation
[DTensor] Fix to_local backward by providing default grad_placement type - #173454
acisseJZhong wants to merge 5 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/173454
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (6 Unrelated Failures)As of commit d1bc0d4 with merge base 5d1599c ( FLAKY - The following job failed but was likely due to flakiness present on trunk:
BROKEN TRUNK - The following jobs failed but was present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
from_local backward to_local backward
This isn't correct - in to_local, input gradient placement depends on the operations it goes through and requires variance tracking to ensure sharding in types |
1 similar comment
This isn't correct - in to_local, input gradient placement depends on the operations it goes through and requires variance tracking to ensure sharding in types |
| @@ -0,0 +1,50 @@ | |||
| # torchrun --nproc_per_node=2 test.py | |||
There was a problem hiding this comment.
did you mean to put this as an actual test in test/distributed/..?
There was a problem hiding this comment.
sorry let me repurpose this to a unit test, this was just my local testing.
| out = original_tensor.sum() | ||
| if plan == 1: | ||
| out = DTensor.from_local( | ||
| out.to_local(grad_placements=[Replicate()]), |
There was a problem hiding this comment.
maybe add a comment in the test that grad_placements=Replicate is only added for explicitness but the default behavior would have been the same? (or did i misunderstand)
There was a problem hiding this comment.
yes, let me remove this grad_placement, as it should work with the fix. And leave a comment suggesting that user should provide grad_placement at their best.
| tensor_stride = tuple(tensor_stride) | ||
| grad_placements = grad_placements or dtensor_spec.placements | ||
|
|
||
| if grad_placements is None: |
There was a problem hiding this comment.
maybe make this a helper fn and add a # see note normalized grad placements comment to direct readers to where you already documented this behavior previously
|
This is BC breaking but I support this change, it moves us closer to JAX style reduced/unreduced semantics, since the JAX cotangent sharding for unreduced is reduced (aka replicate). |
|
This needs to be doc'ed carefully in the release notes, and we need to carefully explain how to get the old behavior ( |
|
We should also clearly explain the connection to this change and the mapping change in #173153 |
to_local backward to_local backward by providing default grad_placement type
1cc4b8e to
d1bc0d4
Compare
|
@pytorchmergebot merge -i the failing inductor test is failing across all recent PRs https://hud.pytorch.org/hud/pytorch/pytorch/main/1?per_page=50 |
Merge startedYour change will be merged while ignoring the following 1 checks: inductor / inductor-test-cuda13 / test (inductor_timm, 1, 2, linux.g5.4xlarge.nvidia.gpu) Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
…t` type (pytorch#173454) Fixes pytorch#172932, see comment [here](pytorch#172932 (comment)) **Problem**: `DTensor.to_local()` followed by `DTensor.from_local()` on `Partial` produces different gradients than pure DTensor operations. **Root cause:** during from_local backward, we always redistribute gradients to Replicate when forward is Partial. However, this information is [not saved](https://github.com/pytorch/pytorch/blob/635ff65bc517c175360a0a85502488ee341059c1/torch/distributed/tensor/_api.py#L91) in to_local backward, where it take the `Replicated `tensor but still annotate as `Partial`. **Fix:** Similarly to the fix for `from_local` in pytorch#173153, we propose this fix to provide a default `Replicate` gradient for `Partial` input(as we always map forward partial to backward replicate in `from_local`) when `grad_placements` is not provided. However, this fix does not guarantee correctness and might break BC. To guarantee correctness, users are **strongly recommended** to pass in `grad_placement` type into `to_local`. Pull Request resolved: pytorch#173454 Approved by: https://github.com/wconstab
Fixes #172932, see comment here
Problem:
DTensor.to_local()followed byDTensor.from_local()onPartialproduces different gradients than pure DTensor operations.Root cause: during from_local backward, we always redistribute gradients to Replicate when forward is Partial. However, this information is not saved in to_local backward, where it take the
Replicatedtensor but still annotate asPartial.Fix: Similarly to the fix for
from_localin #173153, we propose this fix to provide a defaultReplicategradient forPartialinput(as we always map forward partial to backward replicate infrom_local) whengrad_placementsis not provided. However, this fix does not guarantee correctness and might break BC. To guarantee correctness, users are strongly recommended to pass ingrad_placementtype intoto_local.cc @KimbingNg (thanks for the test case!)