Skip to content

[DTensor] Fix to_local backward by providing default grad_placement type - #173454

Closed
acisseJZhong wants to merge 5 commits into
pytorch:mainfrom
acisseJZhong:to_local_backward
Closed

acisseJZhong wants to merge 5 commits into
pytorch:mainfrom
acisseJZhong:to_local_backward

Conversation

@acisseJZhong

@acisseJZhong acisseJZhong commented Jan 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #172932, see comment here

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 in to_local backward, where it take the Replicated tensor but still annotate as Partial.

Fix: Similarly to the fix for from_local in #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.

cc @KimbingNg (thanks for the test case!)

@pytorch-bot

pytorch-bot Bot commented Jan 27, 2026 •

Copy link
Copy Markdown

🔗 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 (image):

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.

@acisseJZhong acisseJZhong changed the title [DTensor] Fix from_local backward [DTensor] Fix to_local backward Jan 27, 2026
@vishal9-team

Copy link
Copy Markdown
Contributor

Root cause: during from_local backward, we always redistribute to Replicate when forward is Partial. However, this information is lost in to_local backward, where it take the Replicated tensor but still annotate as Partial.

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
@vishal9-team

Copy link
Copy Markdown
Contributor

Root cause: during from_local backward, we always redistribute to Replicate when forward is Partial. However, this information is lost in to_local backward, where it take the Replicated tensor but still annotate as Partial.

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

Comment thread test.py Outdated
@@ -0,0 +1,50 @@
# torchrun --nproc_per_node=2 test.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean to put this as an actual test in test/distributed/..?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry let me repurpose this to a unit test, this was just my local testing.

Comment thread test.py Outdated
out = original_tensor.sum()
if plan == 1:
out = DTensor.from_local(
out.to_local(grad_placements=[Replicate()]),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@ezyang ezyang added the topic: bc breaking topic category label Jan 27, 2026
@ezyang

ezyang commented Jan 27, 2026

Copy link
Copy Markdown
Contributor

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).

@ezyang

ezyang commented Jan 27, 2026

Copy link
Copy Markdown
Contributor

This needs to be doc'ed carefully in the release notes, and we need to carefully explain how to get the old behavior (grad_placements to the old thing).

@vishal9-team

Copy link
Copy Markdown
Contributor

We should also clearly explain the connection to this change and the mapping change in #173153

@acisseJZhong acisseJZhong changed the title [DTensor] Fix to_local backward [DTensor] Fix to_local backward by providing default grad_placement type Jan 27, 2026
@acisseJZhong

Copy link
Copy Markdown
Contributor Author

@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

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jan 29, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your 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

Advanced Debugging
Check the merge workflow status
here

kapilsh pushed a commit to kapilsh/pytorch that referenced this pull request Feb 2, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: distributed (dtensor) release notes category topic: bc breaking topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DTensor] to_local + from_local on Partial DTensor produce different gradient

5 participants