Repository navigation
[DTensor] Add grad_placement to from_local - #175867
acisseJZhong wants to merge 5 commits into
Conversation
🔗 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 FailuresAs of commit 8f95e77 with merge base 3b59243 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
45ca8dd to
1a23cb2
Compare
|
need test |
| 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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, | |||
There was a problem hiding this comment.
should at least call it
| grad_placements: tuple[Placement, ...] | None = None, | |
| local_input_grad_placements: tuple[Placement, ...] | None = None, |
and update docstring to say when it's effective
|
Added a testcase |
wconstab
left a comment
There was a problem hiding this comment.
lgtm! thanks for fixing.
|
@pytorchmergebot merge -i |
Merge startedYour 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 |
## 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
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)inColwiseLinear(used by the shared expert) produced correct forward behavior, but the backward pass performed an unnecessary all-reduce. The desired backward gradient placement wasPartial, notReplicate.Solution
To address this, we add a
grad_placementskeyword argument tofrom_local, mirroring the existinggrad_placementssupport into_local. Whengrad_placementsisNone(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