Repository navigation
[perf] custom DTensor strategy for reductions of .any and .all - #201840
francesco-bertolotti wants to merge 3 commits into
Conversation
PR Status: missing actionable issueAuto PR Triage did not find either of the following when it checked this PR:
Such PRs are a low priority for maintainers to review and might not get a response given the very high volume of PRs we receive. Please focus on If one of these applies, add the matching line to the PR description. If there is no 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/201840
Note: Links to docs will display an error until the docs builds have been completed. ❌ 4 Awaiting Approval, 6 New Failures, 6 Unrelated FailuresAs of commit f3a408d with merge base 1776b98 ( AWAITING APPROVAL - The following workflows need approval before CI can run:
NEW FAILURES - The following jobs have failed:
NEW FAILURES SUPPRESSED BY AI (non-blocking) - The following jobs failed, but the AI CI Advisor judged the failures not to be evidence against this change:
UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@pytorchbot label "release notes: distributed (dtensor)" "topic: performance" |
Skylion007
left a comment
There was a problem hiding this comment.
I don't think this is the right way to handle this, a custom DTensor strategy used in the registry is.
|
Hi @Skylion007, Thanks for the quick review. I had considered changing how any is sharded, but held off because of this comment: pytorch/torch/distributed/tensor/_ops/_math_ops.py Lines 405 to 406 in 23520dd If I read it right, it concerns mapping any/all to the linear sum/product partials. Would a dedicated strategy for any/all that produces Partial("max")/Partial("min") on the reduced sharded dims be what you have in mind? If so, I'll try to rework the PR that way. |
get Partial(max) for any and Partial(min) for all. This avoids expensive all-gathers when calling any on huge masks. Example: Input Placement: (Shard(0), Shard(1)), Op: any() Strat Placement: (Partial(max), Partial(max))
|
|
|
Hi @Skylion007, thanks for the pointer. I reworked the PR along those lines: the change in A shard on a reduction dim now maps to With this, f3a408d also registers aten.all.dims with the bool strategy (it was missing, unlike any.dims), and fixes |
any/all only shard non-reduction dims, so DTensor all-gathers an input sharded along a reduced dim just to compute a predicate. nn.init.trunc_normal_ calls mask.any() in its rejection loop, so initializing a sharded parameter all-gathers a full-size mask per iteration (#204864, #201840). bool_reduction_handler (next to argminmax_handler) runs the normal sharding propagation, but keeps the mesh dims it would redistribute that shard a reduced dim (and bool Partial inputs matching the op's max/min combine). The op reduces them locally and only the result is redistributed, from Partial("max"/"min") to the output placements propagation chose. Output placements are unchanged and no Partial leaves the op. aten.all.dims now gets the bool reduction strategy like aten.any.dims. #204895 instead returned Partial from any/all and made .item()/bool() reduce Partial inputs: rank-asymmetric .item() callers hung, and the Partial rules leaked into decomposition-derived strategies (isin, binary_cross_entropy). Returning the Partial without that (#201840) makes bool() rank-local, so trunc_normal_ ranks leave the loop at different iterations and their RNG offsets diverge. A strategy cannot express "reduce locally, then reduce the output", hence the handler; it adds ~10 us of Python dispatch to calls that need no collective. TorchTitan DeepSeekV3 init_weights on 8 B200s (FSDP, identical weights): | config | main | this PR | |---|---|---| | 671B, first 16 of 61 layers | 0.51 s | 0.24 s | | 16B, CPU offload (torchtitan#6161) | 66.5 s | 40.6 s | Test Plan: ``` python test/distributed/tensor/test_math_ops.py DTensorBoolReductionTestCPU DTensorBoolReduction2DMeshTestCPU python test/distributed/tensor/test_math_ops.py DTensorBoolReductionTestCUDA DTensorBoolReduction2DMeshTestCUDA python test/distributed/tensor/test_math_ops.py python -m pytest test/distributed/tensor/test_dtensor_ops.py -k "any or all_ or isin or binary_cross_entropy" python test/distributed/tensor/test_decompositions.py test/distributed/tensor/test_utils.py ``` 128 bool-reduction tests pass on CPU/gloo and 128 on CUDA/NCCL; the other suites pass. Authored with an AI assistant.
any/all only shard non-reduction dims, so DTensor all-gathers an input sharded along a reduced dim just to compute a predicate. nn.init.trunc_normal_ calls mask.any() in its rejection loop, so initializing a sharded parameter all-gathers a full-size mask per iteration (#204864, #201840). bool_reduction_handler (next to argminmax_handler) runs the normal sharding propagation, but keeps the mesh dims it would redistribute that shard a reduced dim (and bool Partial inputs matching the op's max/min combine). The op reduces them locally and only the result is redistributed, from Partial("max"/"min") to the output placements propagation chose. Output placements are unchanged and no Partial leaves the op. aten.all.dims now gets the bool reduction strategy like aten.any.dims. #204895 instead returned Partial from any/all and made .item()/bool() reduce Partial inputs: rank-asymmetric .item() callers hung, and the Partial rules leaked into decomposition-derived strategies (isin, binary_cross_entropy). Returning the Partial without that (#201840) makes bool() rank-local, so trunc_normal_ ranks leave the loop at different iterations and their RNG offsets diverge. A strategy cannot express "reduce locally, then reduce the output", hence the handler; it adds ~8 us of Python dispatch per call. TorchTitan DeepSeekV3 init_weights on 8 B200s (FSDP, identical weights): | config | main | this PR | |---|---|---| | 671B, first 16 of 61 layers | 0.51 s | 0.24 s | | 16B, CPU offload (torchtitan#6161) | 66.5 s | 40.6 s | Test Plan: ``` python test/distributed/tensor/test_math_ops.py DTensorBoolReductionTestCPU DTensorBoolReduction2DMeshTestCPU python test/distributed/tensor/test_math_ops.py DTensorBoolReductionTestCUDA DTensorBoolReduction2DMeshTestCUDA python test/distributed/tensor/test_math_ops.py python -m pytest test/distributed/tensor/test_dtensor_ops.py -k "any or all_ or isin or binary_cross_entropy" python test/distributed/tensor/test_decompositions.py test/distributed/tensor/test_utils.py ``` 128 bool-reduction tests pass on CPU/gloo and 128 on CUDA/NCCL; the other suites pass. Authored with an AI assistant.
any/all only shard non-reduction dims, so DTensor all-gathers an input sharded along a reduced dim just to compute a predicate. nn.init.trunc_normal_ calls mask.any() in its rejection loop, so initializing a sharded parameter all-gathers a full-size mask per iteration (#204864, #201840). bool_reduction_handler (next to argminmax_handler) runs the normal sharding propagation, but keeps the mesh dims it would redistribute that shard a reduced dim (and bool Partial inputs matching the op's max/min combine). The op reduces them locally and only the result is redistributed, from Partial("max"/"min") to the output placements propagation chose. Output placements are the ones propagation chose, and no Partial leaves the op. aten.all.dims now gets the bool reduction strategy like aten.any.dims, which changes some of its output placements. Returning Partial from any/all and making .item()/bool() reduce Partial inputs would hang rank-asymmetric .item() callers and leak the Partial into decomposition-derived strategies (isin, binary_cross_entropy). Returning the Partial without that (#201840) makes bool() rank-local, so trunc_normal_ ranks leave the loop at different iterations and their RNG offsets diverge. A strategy cannot express "reduce locally, then reduce the output", hence the handler; it adds ~8 us of Python dispatch per call. TorchTitan DeepSeekV3 init_weights on 8 B200s (FSDP, identical weights): | config | main | this PR | |---|---|---| | 671B, first 16 of 61 layers | 0.51 s | 0.24 s | | 16B, CPU offload (torchtitan#6161) | 66.5 s | 40.6 s | Test Plan: ``` python test/distributed/tensor/test_math_ops.py DTensorBoolReductionTestCPU DTensorBoolReduction2DMeshTestCPU python test/distributed/tensor/test_math_ops.py DTensorBoolReductionTestCUDA DTensorBoolReduction2DMeshTestCUDA python test/distributed/tensor/test_math_ops.py python -m pytest test/distributed/tensor/test_dtensor_ops.py -k "any or all_ or isin or binary_cross_entropy" python test/distributed/tensor/test_decompositions.py python test/distributed/tensor/test_utils.py ``` 128 bool-reduction tests pass on CPU/gloo and 128 on CUDA/NCCL; the other suites pass. Authored with an AI assistant.
|
@francesco-bertolotti -- We had an earlier attempt that tried to propagate Partial(max/min) for any/all, but that breaks subsequent bool()/.item() accesses on the DTensor which operate only on the local shards. Forcing a Replicate prior to those operators broke internal tests hence we had to revert. I have reworked the PR to do the any/all on local shards and then reduce the scalar outputs #204895 ; this will preserve BC on these ops w.r.t output placements, but avoid the expensive AG collective. Can you give this a spin if it works for your needs? |
|
Tested #204895 (applied to the 2026-11-23 nightly) on 4xGH200 with our FSDP2 trainer's init test, with and without CPUOffloadPolicy: both pass (replicas identical, shards distinct, values within the truncation bounds). trunc_normal_ now does 4 small all-reduces instead of 4 all-gathers, and bool(t.any()) agrees across ranks. With this PR's Partial(max) approach it did not: ranks returned [False, False, False, True]. So I am closing this PR. |
any/all only shard non-reduction dims, so DTensor all-gathers an input sharded along a reduced dim just to compute a predicate. nn.init.trunc_normal_ calls mask.any() in its rejection loop, so initializing a sharded parameter all-gathers a full-size mask per iteration (#204864, #201840). bool_reduction_handler (next to argminmax_handler) runs the normal sharding propagation, but keeps the mesh dims it would redistribute that shard a reduced dim (and bool Partial inputs matching the op's max/min combine). The op reduces them locally and only the result is redistributed, from Partial("max"/"min") to the output placements propagation chose. Output placements are the ones propagation chose, and no Partial leaves the op. aten.all.dims now gets the bool reduction strategy like aten.any.dims, which changes some of its output placements. Returning Partial from any/all and making .item()/bool() reduce Partial inputs would hang rank-asymmetric .item() callers and leak the Partial into decomposition-derived strategies (isin, binary_cross_entropy). Returning the Partial without that (#201840) makes bool() rank-local, so trunc_normal_ ranks leave the loop at different iterations and their RNG offsets diverge. A strategy cannot express "reduce locally, then reduce the output", hence the handler; it adds ~8 us of Python dispatch per call. TorchTitan DeepSeekV3 init_weights on 8 B200s (FSDP, identical weights): | config | main | this PR | |---|---|---| | 671B, first 16 of 61 layers | 0.51 s | 0.24 s | | 16B, CPU offload (torchtitan#6161) | 66.5 s | 40.6 s | Test Plan: ``` python test/distributed/tensor/test_math_ops.py DTensorBoolReductionTestCPU DTensorBoolReduction2DMeshTestCPU python test/distributed/tensor/test_math_ops.py DTensorBoolReductionTestCUDA DTensorBoolReduction2DMeshTestCUDA python test/distributed/tensor/test_math_ops.py python -m pytest test/distributed/tensor/test_dtensor_ops.py -k "any or all_ or isin or binary_cross_entropy" python test/distributed/tensor/test_decompositions.py python test/distributed/tensor/test_utils.py ``` 128 bool-reduction tests pass on CPU/gloo and 128 on CUDA/NCCL; the other suites pass. Authored with an AI assistant.
Linked issue or supporting maintainer
I don't think there's a reported issue for this.
BC-breaking?
No.
Summary (human written only)
I recently switched to
trunc_normal_for LLM weight initialization, and init time spiked to 10 minutes. After investigating, I found thattrunc_normal_internally calls.any()several times. OnDTensor, this triggers expensive and unnecessary all-gathers.The naive solution would be to iterate over each local shard independently. However, Claude gaslighted me into believing that it might be useful to have the RNG state of all ranks advance in sync. So this PR introduces a single boolean all-reduce to decide whether any rank needs to redraw samples, avoiding repeated all-gathers of the full mask.
This issue affects DeepSeek Initialization of TorchTitan
Agent details (if applicable)