Skip to content

[DTensor] Add grad_placement to from_local - #175867

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

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

Conversation

@acisseJZhong

@acisseJZhong acisseJZhong commented Feb 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

While fixing a MoE numerics bug (pytorch/torchtitan#2416), we discovered the root cause was a lack of strict gradient placement control/strict typing in from_local. Specifically, from_local(Replicate) in ColwiseLinear (used by the shared expert) produced correct forward behavior, but the backward pass performed an unnecessary all-reduce. The desired backward gradient placement was Partial, not Replicate.

Solution

To address this, we add a grad_placements keyword argument to from_local, mirroring the existing grad_placements support in to_local. When grad_placements is None (the default), the backward gradient placement follows the original guarantees established in #173153. When specified, it overrides the default and uses the user-provided gradient placements directly.

In the longer term, we hope to address this kind of issues through spmd_types as a type checker.

Test

python3 test/distributed/tensor/test_dtensor.py -k test_from_local_grad_placements

@pytorch-bot

pytorch-bot Bot commented Feb 26, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/175867

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 8f95e77 with merge base 3b59243 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@tianyu-l
tianyu-l requested a review from ezyang February 26, 2026 20:51
@ezyang

ezyang commented Feb 26, 2026

Copy link
Copy Markdown
Contributor

need test

Comment thread torch/distributed/tensor/_api.py Outdated
If not provided, ``stride`` will be computed assuming the given distributed
tensor is evenly sharded across ranks. default: None
grad_placements (List[:class:`Placement`], optional): the placements describes
the future layout of any gradient layout of the DTensor created from this

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.

weird sentence " the future layout of any gradient layout of the DTensor created from this"
suggest "specifies the expected input gradient placements. The input gradient will be a plain tensor, but before exiting DTensor it will be redistributed to the specified placement. ..."

Is my understanding correct btw?

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, this is the correct understanding. Let me rephrase this comment.

@@ -85,6 +85,7 @@ def __init__(
input_layouts: Placement | None = None,
output_layouts: Placement | None = None,
use_local_output: bool = True,
grad_placements: tuple[Placement, ...] | None = 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.

should at least call it

Suggested change
grad_placements: tuple[Placement, ...] | None = None,
local_input_grad_placements: tuple[Placement, ...] | None = None,

and update docstring to say when it's effective

@acisseJZhong

Copy link
Copy Markdown
Contributor Author

Added a testcase test_from_local_grad_placements for Replicate forward Partial backward, all other cases without passing grad_placements should be unaffected.

@wconstab wconstab left a comment

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.

lgtm! thanks for fixing.

@acisseJZhong

Copy link
Copy Markdown
Contributor Author

@pytorchmergebot merge -i

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

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 0 checks:

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

EmanueleCoradin pushed a commit to EmanueleCoradin/pytorch that referenced this pull request Mar 30, 2026
## Problem
While fixing a MoE numerics bug (pytorch/torchtitan#2416), we discovered the root cause was a lack of strict gradient placement control/strict typing in `from_local`. Specifically, `from_local(Replicate)` in `ColwiseLinear` (used by the shared expert) produced correct forward behavior, but the backward pass performed an unnecessary all-reduce. The desired backward gradient placement was `Partial`, not `Replicate`.

## Solution
To address this, we add a `grad_placements` keyword argument to `from_local`, mirroring the existing `grad_placements` support in `to_local`. When `grad_placements` is `None` (the default), the backward gradient placement follows the original guarantees established in pytorch#173153. When specified, it overrides the default and uses the user-provided gradient placements directly.

In the longer term, we hope to address this kind of issues through spmd_types as a type checker.

## Test
```
python3 test/distributed/tensor/test_dtensor.py -k test_from_local_grad_placements
```
Pull Request resolved: pytorch#175867
Approved by: https://github.com/wconstab, https://github.com/tianyu-l
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants