Skip to content

[TorchComms] Support N-D parallelism device mesh setup - #1876

Merged
mori360 merged 5 commits into
pytorch:mainfrom
mori360:nd_test
Oct 15, 2025
Merged

mori360 merged 5 commits into
pytorch:mainfrom
mori360:nd_test

Conversation

@mori360

@mori360 mori360 commented Oct 14, 2025 •

Copy link
Copy Markdown
Contributor

Support ND device mesh among [pp, dp, cp, tp]
Used comm.split() to split the sub device mesh at each parallelism
Flatten the mesh to dp, dp_shard_cp, dp_cp, which will be access in titan training

TEST_BACKEND=nccl TRAIN_FILE=torchtitan.experiments.torchcomms.train CONFIG_FILE="./torchtitan/models/llama3/train_configs/debug_model.toml" ./run_train.sh --parallelism.context_parallel_degree 2 --parallelism.tensor_parallel_degree 2

[rank0]:[titan] 2025-10-14 14:56:30,464 - root - INFO - step: 1 loss: 8.1440 grad_norm: 1.3609 memory: 0.78GiB(0.82%) tps: 775 tflops: 0.06 mfu: 0.01%
[rank0]:[titan] 2025-10-14 14:56:30,465 - root - INFO - Synchronizing and adjusting timeout for all ProcessGroups to 0:01:40
[rank0]:[titan] 2025-10-14 14:56:30,615 - root - INFO - step: 2 loss: 7.8313 grad_norm: 1.4321 memory: 0.85GiB(0.89%) tps: 27,316 tflops: 1.96 mfu: 0.20%
[rank0]:[titan] 2025-10-14 14:56:30,736 - root - INFO - step: 3 loss: 7.1126 grad_norm: 1.8422 memory: 0.85GiB(0.89%) tps: 33,869 tflops: 2.42 mfu: 0.25%
[rank0]:[titan] 2025-10-14 14:56:30,849 - root - INFO - step: 4 loss: 6.2534 grad_norm: 2.2657 memory: 0.85GiB(0.89%) tps: 36,315 tflops: 2.60 mfu: 0.26%
[rank0]:[titan] 2025-10-14 14:56:30,960 - root - INFO - step: 5 loss: 5.3031 grad_norm: 2.3692 memory: 0.85GiB(0.89%) tps: 37,302 tflops: 2.67 mfu: 0.27%
[rank0]:[titan] 2025-10-14 14:56:31,078 - root - INFO - step: 6 loss: 4.8114 grad_norm: 2.2013 memory: 0.85GiB(0.89%) tps: 34,679 tflops: 2.48 mfu: 0.25%
[rank0]:[titan] 2025-10-14 14:56:31,188 - root - INFO - step: 7 loss: 4.5279 grad_norm: 2.2304 memory: 0.85GiB(0.89%) tps: 37,238 tflops: 2.67 mfu: 0.27%
[rank0]:[titan] 2025-10-14 14:56:31,302 - root - INFO - step: 8 loss: 4.3251 grad_norm: 2.1220 memory: 0.85GiB(0.89%) tps: 36,187 tflops: 2.59 mfu: 0.26%
[rank0]:[titan] 2025-10-14 14:56:31,413 - root - INFO - step: 9 loss: 4.5047 grad_norm: 1.7700 memory: 0.85GiB(0.89%) tps: 37,154 tflops: 2.66 mfu: 0.27%
[rank0]:[titan] 2025-10-14 14:56:31,528 - root - INFO - step: 10 loss: 4.1099 grad_norm: 1.7139 memory: 0.85GiB(0.89%) tps: 35,564 tflops: 2.55 mfu: 0.26%
[rank0]:[titan] 2025-10-14 14:56:31,528 - root - INFO - Sleeping 2 seconds for other ranks to complete

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 14, 2025
@mori360
mori360 requested a review from fduwjj October 14, 2025 22:04
@mori360
mori360 marked this pull request as ready for review October 14, 2025 22:04

@fduwjj fduwjj 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.

I think the title should be "[TorchComms] Support N-D parallelism device mesh setup"?

backend,
device,
name="comms_test_n_d_parallel",
timeout=datetime.timedelta(seconds=60),

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.

is this expected? I mean what would be the actual timeout?

Comment on lines +82 to +85
comm.finalize()
for sub_comm in comm_per_dim.values():
sub_comm.finalize()
return

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.

I think the order is wrong. comm.finalize() needs to be after sub_comm.finalize(). This was fixed in trunk already.

Comment on lines +108 to +120
for idx, dim_name in enumerate(flattened_mesh_dim_names):
# Calculate global ranks mapping for this mesh dimension
global_ranks = (
flatten_mesh[idx]
.transpose(idx, -1)
.reshape(-1, reshape_size[idx])
.tolist()
)

for row in global_ranks:
if cur_rank in row:
flatten_ranks_per_dim[dim_name] = row
break

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.

Can you make this a util function?

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.

make into _calculate_ranks_per_dimension for the 2 similar logic here

@mori360 mori360 changed the title [TorchComms] Support ND device mesh [TorchComms] Support N-D parallelism device mesh setup Oct 14, 2025

@fduwjj fduwjj 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.

Also where do you call comm.finalize()? Both sub-comm and the global comm needs to call it

@mori360

mori360 commented Oct 14, 2025

Copy link
Copy Markdown
Contributor Author

Also where do you call comm.finalize()? Both sub-comm and the global comm needs to call it

comm.finalize() are called here: https://github.com/mori360/torchtitan/blob/fa135f0c21181d519f7e6aae0cd9de0635259606/torchtitan/experiments/torchcomms/train.py#L56-L58
with all the comms stored in TorchCommsParallelDims.comms

flatten_layout,
)

self.comms = [comm, *comm_per_dim.values()]

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.

let's do:

self.comms = [*comm_per_dim.values(), comm]

?

@mori360
mori360 requested a review from fduwjj October 14, 2025 22:49

@fduwjj fduwjj 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.

Thanks, lgtm

dim_sizes: List[int],
cur_rank: int,
) -> Dict[str, List[int]]:
"""Calculate global ranks mapping for each mesh dimension.

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.

Nit: Util function to calculate global ranks mapping for each mesh dimension.

Args:
meshes: List of mesh tensors to calculate ranks from
dim_names: List of dimension names corresponding to each mesh
dim_sizes: List of dimension sizes for reshaping

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.

Just say: List of dimension sizes corresponding to each mesh

comm_per_dim["cp"],
comm_per_dim["tp"],
),
mesh_dim_names=("pp", "dp_replicate", "dp_shard", "cp", "tp"),

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.

nit: mesh_dim_names=tuple(mesh_dim_names)

@mori360
mori360 merged commit d15b59f into pytorch:main Oct 15, 2025
fduwjj pushed a commit that referenced this pull request Oct 15, 2025
nd parallelism is available after
#1876
update readme, and fix the font issue
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
Support ND device mesh among [pp, dp, cp, tp]
Used comm.split() to split the sub device mesh at each parallelism
Flatten the mesh to dp, dp_shard_cp, dp_cp, which will be access in
titan training


`TEST_BACKEND=nccl TRAIN_FILE=torchtitan.experiments.torchcomms.train
CONFIG_FILE="./torchtitan/models/llama3/train_configs/debug_model.toml"
./run_train.sh --parallelism.context_parallel_degree 2
--parallelism.tensor_parallel_degree 2`


[rank0]:[titan] 2025-10-14 14:56:30,464 - root - INFO - step: 1 loss:
8.1440 grad_norm: 1.3609 memory: 0.78GiB(0.82%) tps: 775 tflops: 0.06
mfu: 0.01%
[rank0]:[titan] 2025-10-14 14:56:30,465 - root - INFO - Synchronizing
and adjusting timeout for all ProcessGroups to 0:01:40
[rank0]:[titan] 2025-10-14 14:56:30,615 - root - INFO - step: 2 loss:
7.8313 grad_norm: 1.4321 memory: 0.85GiB(0.89%) tps: 27,316 tflops: 1.96
mfu: 0.20%
[rank0]:[titan] 2025-10-14 14:56:30,736 - root - INFO - step: 3 loss:
7.1126 grad_norm: 1.8422 memory: 0.85GiB(0.89%) tps: 33,869 tflops: 2.42
mfu: 0.25%
[rank0]:[titan] 2025-10-14 14:56:30,849 - root - INFO - step: 4 loss:
6.2534 grad_norm: 2.2657 memory: 0.85GiB(0.89%) tps: 36,315 tflops: 2.60
mfu: 0.26%
[rank0]:[titan] 2025-10-14 14:56:30,960 - root - INFO - step: 5 loss:
5.3031 grad_norm: 2.3692 memory: 0.85GiB(0.89%) tps: 37,302 tflops: 2.67
mfu: 0.27%
[rank0]:[titan] 2025-10-14 14:56:31,078 - root - INFO - step: 6 loss:
4.8114 grad_norm: 2.2013 memory: 0.85GiB(0.89%) tps: 34,679 tflops: 2.48
mfu: 0.25%
[rank0]:[titan] 2025-10-14 14:56:31,188 - root - INFO - step: 7 loss:
4.5279 grad_norm: 2.2304 memory: 0.85GiB(0.89%) tps: 37,238 tflops: 2.67
mfu: 0.27%
[rank0]:[titan] 2025-10-14 14:56:31,302 - root - INFO - step: 8 loss:
4.3251 grad_norm: 2.1220 memory: 0.85GiB(0.89%) tps: 36,187 tflops: 2.59
mfu: 0.26%
[rank0]:[titan] 2025-10-14 14:56:31,413 - root - INFO - step: 9 loss:
4.5047 grad_norm: 1.7700 memory: 0.85GiB(0.89%) tps: 37,154 tflops: 2.66
mfu: 0.27%
[rank0]:[titan] 2025-10-14 14:56:31,528 - root - INFO - step: 10 loss:
4.1099 grad_norm: 1.7139 memory: 0.85GiB(0.89%) tps: 35,564 tflops: 2.55
mfu: 0.26%
[rank0]:[titan] 2025-10-14 14:56:31,528 - root - INFO - Sleeping 2
seconds for other ranks to complete
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
nd parallelism is available after
pytorch#1876
update readme, and fix the font issue
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 25, 2026
Support ND device mesh among [pp, dp, cp, tp]
Used comm.split() to split the sub device mesh at each parallelism
Flatten the mesh to dp, dp_shard_cp, dp_cp, which will be access in
titan training


`TEST_BACKEND=nccl TRAIN_FILE=torchtitan.experiments.torchcomms.train
CONFIG_FILE="./torchtitan/models/llama3/train_configs/debug_model.toml"
./run_train.sh --parallelism.context_parallel_degree 2
--parallelism.tensor_parallel_degree 2`


[rank0]:[titan] 2025-10-14 14:56:30,464 - root - INFO - step: 1 loss:
8.1440 grad_norm: 1.3609 memory: 0.78GiB(0.82%) tps: 775 tflops: 0.06
mfu: 0.01%
[rank0]:[titan] 2025-10-14 14:56:30,465 - root - INFO - Synchronizing
and adjusting timeout for all ProcessGroups to 0:01:40
[rank0]:[titan] 2025-10-14 14:56:30,615 - root - INFO - step: 2 loss:
7.8313 grad_norm: 1.4321 memory: 0.85GiB(0.89%) tps: 27,316 tflops: 1.96
mfu: 0.20%
[rank0]:[titan] 2025-10-14 14:56:30,736 - root - INFO - step: 3 loss:
7.1126 grad_norm: 1.8422 memory: 0.85GiB(0.89%) tps: 33,869 tflops: 2.42
mfu: 0.25%
[rank0]:[titan] 2025-10-14 14:56:30,849 - root - INFO - step: 4 loss:
6.2534 grad_norm: 2.2657 memory: 0.85GiB(0.89%) tps: 36,315 tflops: 2.60
mfu: 0.26%
[rank0]:[titan] 2025-10-14 14:56:30,960 - root - INFO - step: 5 loss:
5.3031 grad_norm: 2.3692 memory: 0.85GiB(0.89%) tps: 37,302 tflops: 2.67
mfu: 0.27%
[rank0]:[titan] 2025-10-14 14:56:31,078 - root - INFO - step: 6 loss:
4.8114 grad_norm: 2.2013 memory: 0.85GiB(0.89%) tps: 34,679 tflops: 2.48
mfu: 0.25%
[rank0]:[titan] 2025-10-14 14:56:31,188 - root - INFO - step: 7 loss:
4.5279 grad_norm: 2.2304 memory: 0.85GiB(0.89%) tps: 37,238 tflops: 2.67
mfu: 0.27%
[rank0]:[titan] 2025-10-14 14:56:31,302 - root - INFO - step: 8 loss:
4.3251 grad_norm: 2.1220 memory: 0.85GiB(0.89%) tps: 36,187 tflops: 2.59
mfu: 0.26%
[rank0]:[titan] 2025-10-14 14:56:31,413 - root - INFO - step: 9 loss:
4.5047 grad_norm: 1.7700 memory: 0.85GiB(0.89%) tps: 37,154 tflops: 2.66
mfu: 0.27%
[rank0]:[titan] 2025-10-14 14:56:31,528 - root - INFO - step: 10 loss:
4.1099 grad_norm: 1.7139 memory: 0.85GiB(0.89%) tps: 35,564 tflops: 2.55
mfu: 0.26%
[rank0]:[titan] 2025-10-14 14:56:31,528 - root - INFO - Sleeping 2
seconds for other ranks to complete
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 25, 2026
nd parallelism is available after
pytorch#1876
update readme, and fix the font issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants