Skip to content

Use new DeviceMesh unflatten to rewrite parallel_dims - #1660

Merged
fegin merged 38 commits into
mainfrom
chienchin/new_device_mesh
Dec 17, 2025
Merged

fegin merged 38 commits into
mainfrom
chienchin/new_device_mesh

Conversation

@fegin

@fegin fegin commented Aug 29, 2025 •

Copy link
Copy Markdown
Contributor

Summary
This PR utilizes the latest APIs provided by DeviceMesh to simplify the creation of all different meshes.

The design philosophy is as follow:

  1. Create one world mesh with the shape as [world_size,]
  2. Create all 1-D submeshes by using 1) unflattening from the world mesh, or 2) slicing and flatten from other derived meshes.
  3. ParallelDims now provides an API, get_mesh() and get_optional_mesh(). which accepts str or list[str]. When the argument is str, the API directly return the corresponding 1-D submesh. If the argument is list[str], the dim names will be used to concatenate to form a n-D device mesh. The main difference between the two APIs is that the former one will raise an ValueError if the result mesh is None the later one will just return None.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 29, 2025
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
@fegin
fegin force-pushed the chienchin/new_device_mesh branch 7 times, most recently from 12eca61 to 19e4a23 Compare October 15, 2025 20:39
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/utils.py Resolved
@fegin
fegin force-pushed the chienchin/new_device_mesh branch from 19e4a23 to 178bc11 Compare October 28, 2025 20:34
@fegin
fegin marked this pull request as ready for review October 28, 2025 21:01
@fegin
fegin requested a review from wwwjn as a code owner October 28, 2025 21:01
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/utils.py Resolved

ghost 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.

We should modify FLUX train.py as it's in core now.

@ruisizhang123 let's adapt SimpleFSDP after this PR is merged.
oh it seems being fixed in #1959

Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
Comment thread torchtitan/distributed/utils.py Resolved
Comment thread torchtitan/train.py Resolved
Comment thread torchtitan/distributed/utils.py Resolved
Comment thread torchtitan/distributed/parallel_dims.py Resolved
@fegin
fegin force-pushed the chienchin/new_device_mesh branch from aba17ea to f3a0c0c Compare December 16, 2025 06:50

ghost 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.

main remaining question is around the newly added get_optional_mesh -- why we have it, can we combine, etc.

# sequences would require https://github.com/pytorch/torchtitan/pull/686
# pyrefly: ignore [bad-argument-type]
apply_tp_minus_sp(model, parallel_dims.world_mesh["tp"])
apply_tp_minus_sp(model, parallel_dims.get_mesh("tp"))

ghost Dec 17, 2025

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.

After converging to full dtensor TP plan, we no longer need this minus_sp plan. cc @wwwjn

Comment thread torchtitan/distributed/parallel_dims.py Outdated
k: v for k, v in self._meshes.items() if v.ndim == 1 and v.size() > 1
}
else:
return {k: v for k, v in self._meshes.items() if v.size() > 1}

ghost Dec 17, 2025

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.

when do we need this branch?

ghost Dec 17, 2025

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.

Nice catch, it's a legacy from some version. I forgot to delete it . Since it is not used anymore I delete the branch and rename the API to match the logic.

if dp_cp_mesh_dim_names != []:
mesh[tuple(dp_cp_mesh_dim_names)]._flatten(mesh_dim_name="dp_cp")

def get_optional_mesh(self, dims: str | list[str]) -> DeviceMesh | None:

ghost Dec 17, 2025

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.

what's the consideration of separating the two? can we have an arg like check_exists?

ghost Dec 17, 2025 •

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.

Mostly typing. If we don't separate the two, more than 70+ use cases need extra check to ensure typing system doesn't complain None doesn't have some fields. The benefit is that a user will be forced to understand which API should be used, optional or not, and the typing system will give the user an error if the usage is not correct. This can reduce chance of error usages.

So I want to keep this optional case unless there are other concerns.

dist_utils.dist_mean(loss, parallel_dims.world_mesh["dp_cp"]),
dist_utils.dist_max(loss, parallel_dims.world_mesh["dp_cp"]),
dist_utils.dist_mean(loss, parallel_dims.get_optional_mesh("loss")),
dist_utils.dist_max(loss, parallel_dims.get_optional_mesh("loss")),

ghost Dec 17, 2025

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.

why the loss mesh is optional, because it could be of size 1 and we'd want to return None?

ghost Dec 17, 2025

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.

return funcol.all_reduce(x, reduceOp=reduceOp, group=mesh).item()


def dist_max(

ghost Dec 17, 2025

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.

if reduction may not happen, then probably need to rename to maybe_dist_max

ghost Dec 17, 2025

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.

We should do this in a separate PR, this PR already encapsulate too much logic. This is a pre-exist design.

Comment thread torchtitan/distributed/utils.py Resolved
@@ -206,7 +206,7 @@ def monkey_patch_checks(moe):
assert not list(moe.reorderer.buffers())


ghost Dec 17, 2025

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 can feel how much work it is to work on this PR, lol

@fegin
fegin force-pushed the chienchin/new_device_mesh branch from f3a0c0c to 3e3c9e5 Compare December 17, 2025 21:29
@fegin
fegin requested a review from tianyu-l December 17, 2025 21:31

ghost 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

@fegin

ghost commented Dec 17, 2025

Copy link
Copy Markdown
Contributor Author

Please raise an issue and tag me if you see a DevicMesh issue. I'm going to merge this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu 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.

8 participants