Repository navigation
support DTensor S(1) -> Partial("sum") - #191828
acisseJZhong wants to merge 1 commit into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/191828
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 Cancelled JobAs of commit 383f0b6 with merge base d2f6cf4 ( CANCELLED JOB - The following job was cancelled. Please retry:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
I support the motivation. From DTensor perspective, is your intention to gate Shard-to-Partial to avoid changing exsting callsite? current this PR changes _StridedShard behavior. Repro as below here is my prompt "code review code-reivew/pytorch 4356b41 . identify if it breaks existing callsite because the commit allowed Shard to Partial redistribution" _StridedShard is the exercised path for FSDP2 + TP and FSDP2 + EP when they shard on the same dim |
My intention was to avoid the planner exposes S -> P, the planer considers S -> P only when it is the final requested destination. regarding to the breakage: instead of However I checked that before this PR, Updated the code with the added check |
|
Remembering the context.. X->Partial("sum") can be numerically dangerous if you do it by scaling, or can be SPMD-dangerous if you do it by padding (different ranks have different paddings, which means your graphs are non-spmd). IIUC your PR is taking the latter approach, padding different sized chunks per rank as prefix/suffix. I would prefer not to support this in the current form, as we want DTensor to produce SPMD code. I am not familiar with the current MoE sharding situation though. A few thoughts
|
fwiw, spmd_types did decide to accomodate the padding version, having a though it doesn't support uneven S yet |
|
Ok, if we are doing the padding approach for spmd_types we may as well do it here too. It's a separate question of being spmd-friendly for graphs. I wonder how much we care about that e.g for compile-on-one-rank compatibility- cc @aorenste I also wonder if it is inconsistent to prevent the planner from doing s->p if we claim it is a supported operation. I think we should at least have a clear story here. |
I vaguely remember one of the approaches to CooR spmd-ness was to hide non-spmd-ness behind custom ops, e.g. pytorch/torch/distributed/tensor/_collective_utils.py Lines 64 to 79 in 9d4d32d @wconstab just wondering how that works for your concerns? I imagine it delegates more work to lowering, but could also be easier to catch? |
|
@wconstab I applied your suggestion to change to the fixed/global -> index_add approach, would compiler compile same graph with this approach?
I initially added s->p into the planner and it broke a bunch of tests as it changes the planner's decision? Therefore I banned it from the planner as I don't want the planner to do s->p(kind of meaningless?). Do you have a better idea in mind? |
There was a problem hiding this comment.
I think this is OK. But we should document our reasoning at least (the design, in code comment block), and make sure our API docs are clear too.
The important caveat is: “valid explicit redistribution” and “eligible implicit planner move” are different sets. The reason we prefer this rather than having the sets be equal is partly due to a weakness in our cost model- it models comm cost only, not memory usage or computation (copy) time. If we put S->P as zero cost, we'd over-prefer it in the planner, and that would be worse than skipping it as an option.
For completeness sake, do we have an argument for supporting only explicit S->P rather than also R->P?
We should also check the reasoning between the transform-planner and the strategy-planner.
transform-planner implements S->P with zero cost now, while strategy planner has inf cost. Is this inconsistency a problem? We could argue that as long as the transform planner DOES implement every finite cost transition in the strategy planner, we're good. A more extreme guarantee would be, they both follow the same paths always with the same costs.
| output_shape[self.dim] = logical_dim_size | ||
| output = local_tensor.new_zeros(output_shape) | ||
| offset = torch.scalar_tensor( | ||
| local_offset, dtype=torch.int64, device=local_tensor.device |
There was a problem hiding this comment.
Just checking, is local_offset a device tensor or a host scalar? I still think structurally this index_add framing is cleaner as it produces structurally identical graphs, but I'm not sure if it literally solves the rank-identical graphs problem without validating whether we bake per-rank constants (Scalars) into the graph. I'd be curious to know what happens, but I wouldn't block this PR over it. cc @IvanKobzarev @aorenste
There was a problem hiding this comment.
local_offset is host-side shape metadata represented as an int or SymInt
|
Added doc string to clarify that every transition selected by the strategy planner must be executable by the transform planner, while the transform planner may support additional transitions used only by explicit redistribution.
I assume user would never want to explicitly redistribute R->P? Also codex told me that `Replicate -> Partial("sum") divides by the mesh size and may introduce floating-point error, so keep this lossy conversion out of the public API. |
9bc8c99 to
4dee73e
Compare
The proper way probably should be only keeping value on rank 0, i.e. Owned-style Partial(sum). With that I don't see the value of putting S->P as an intermediate step where either S is not original source or P is not final destination. |
wconstab
left a comment
There was a problem hiding this comment.
lgtm. thanks for making those changes!
| Shard -> Partial("sum") is intentionally absent. Although explicit | ||
| redistribution supports it, the conversion materializes a logical-shape | ||
| local tensor. The communication-only strategy cost model cannot represent | ||
| that memory and copy cost, so implicit planning must not select it. |
There was a problem hiding this comment.
nit: logical-shape -> global-shape
must not select it -> we avoid selecting it based only on comm cost without considering other costs
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Merge failedReason: 1 jobs have failed, first few of them are: inductor / unit-test / inductor-cpu-test / test (inductor_amx, 1, 2, mt-l-x86iamx-8-64) Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge -i |
Merge startedYour change will be merged while ignoring the following 1 checks: inductor / unit-test / inductor-cpu-test / test (inductor_amx, 1, 2, mt-l-x86iamx-8-64) Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
## Summary
Add DTensor support for Shard(dim) -> Partial("sum") redistribution
using local zero-fill. This enables the S(1) -> P transition required
by TorchTitan's new MoE sharding
(pytorch/torchtitan#3996).
## Testing
Added forward, backward, uneven-sharding, and graph-planner coverage.
```
python test/distributed/tensor/test_redistribute.py -k shard_to_partial
```
Pull Request resolved: pytorch#191828
Approved by: https://github.com/wconstab
Summary
Add DTensor support for Shard(dim) -> Partial("sum") redistribution
using local zero-fill. This enables the S(1) -> P transition required
by TorchTitan's new MoE sharding
(pytorch/torchtitan#3996).
Testing
Added forward, backward, uneven-sharding, and graph-planner coverage.