Skip to content

Conversation

@fduwjj
Copy link
Contributor

@fduwjj fduwjj commented Oct 21, 2025

Stack from ghstack (oldest at bottom):

To avoid creating extra PGs when not needed, (for example when calling unflatten many times and some dims share the same layout), we want to create a pg cache mechanism. We cache a PG by a pair of layout (_MeshLayout) and its pg_option, so if users flatten or unflatten into the same layout+pg option (by default pg_option will be None if not set, that way the cache key falls back to layout itself) we will not create process group if already created.

Also to further consolidate the bookkeeping of DM, we created a device_type, shared_state to store root_mesh, rank_map and pg caches. This way all shared info for a given device mesh universe now becomes a singleton.

cc @H-Huang @awgu @wanchaol @fegin @wz337 @wconstab @d4l3k @pragupta @msaroufim @dcci

@pytorch-bot
Copy link

pytorch-bot bot commented Oct 21, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/166010

Note: Links to docs will display an error until the docs builds have been completed.

❌ 5 New Failures

As of commit 54abae7 with merge base a5f3035 (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot bot added the oncall: distributed Add this issue/PR to distributed oncall triage queue label Oct 21, 2025
fduwjj added a commit that referenced this pull request Oct 21, 2025
…h and rank_map

ghstack-source-id: b50659e
Pull Request resolved: #166010
…t, root_mesh and rank_map"

cc H-Huang awgu wanchaol fegin wz337 wconstab d4l3k pragupta msaroufim dcci

[ghstack-poisoned]
fduwjj added a commit that referenced this pull request Oct 21, 2025
…h and rank_map

ghstack-source-id: 1cea7a4
Pull Request resolved: #166010
@fduwjj fduwjj requested review from fegin, lw and wconstab October 21, 2025 22:43
@fduwjj fduwjj changed the title [WIP][DeviceMesh] Use a shared_state to cache pg per layout, root_mesh and rank_map [For discussion][DeviceMesh] Use a shared_state to cache pg per layout, root_mesh and rank_map Oct 21, 2025
…g per layout, root_mesh and rank_map"


We want to create a shared_state to store root_mesh, rank_map and pg caches. We can add more into it down the road, so that it becomes a singleton for bookkeeping and also align with our original proposal to move toward the idea of mesh universe.

cc H-Huang awgu wanchaol fegin wz337 wconstab d4l3k pragupta msaroufim dcci

[ghstack-poisoned]
fduwjj added a commit that referenced this pull request Oct 21, 2025
…h and rank_map

ghstack-source-id: 8865ebf
Pull Request resolved: #166010
…g per layout, root_mesh and rank_map"


We want to create a shared_state to store root_mesh, rank_map and pg caches. We can add more into it down the road, so that it becomes a singleton for bookkeeping and also align with our original proposal to move toward the idea of mesh universe.

cc H-Huang awgu wanchaol fegin wz337 wconstab d4l3k pragupta msaroufim dcci

[ghstack-poisoned]
fduwjj added a commit that referenced this pull request Oct 27, 2025
…h and rank_map

ghstack-source-id: f96d104
Pull Request resolved: #166010
…g per layout, root_mesh and rank_map"


We want to create a shared_state to store root_mesh, rank_map and pg caches. We can add more into it down the road, so that it becomes a singleton for bookkeeping and also align with our original proposal to move toward the idea of mesh universe.

cc H-Huang awgu wanchaol fegin wz337 wconstab d4l3k pragupta msaroufim dcci

[ghstack-poisoned]
fduwjj added a commit that referenced this pull request Oct 28, 2025
…h and rank_map

ghstack-source-id: cffc81f
Pull Request resolved: #166010
Copy link
Contributor

@lw lw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I think this PR is a great starting point for the discussion around PG caching, and defining a clear mental model for how different DeviceMesh should share state.

Comment on lines 530 to 531
def _get_root_mesh(self) -> "DeviceMesh":
return self._root_mesh if self._root_mesh else self
return not_none(self._shared_state.get_root_mesh())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only instance where we're accessing the root mesh of the shared state. In principle, once we introduce the concept of shared state, we can get rid of the concept of root meshes.

What is preventing us from simply removing this _get_root_mesh method?

If there's any internal usage of _get_root_mesh, could we find ways of codemod it away so that we can fully remove this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need more PRs to remove _get_root_mesh not in this PR.

…g per layout, root_mesh and rank_map"


We want to create a shared_state to store root_mesh, rank_map and pg caches. We can add more into it down the road, so that it becomes a singleton for bookkeeping and also align with our original proposal to move toward the idea of mesh universe.

cc H-Huang awgu wanchaol fegin wz337 wconstab d4l3k pragupta msaroufim dcci

[ghstack-poisoned]
@fduwjj fduwjj requested a review from kwen2501 as a code owner October 30, 2025 23:27
fduwjj added a commit that referenced this pull request Oct 30, 2025
…h and rank_map

ghstack-source-id: 93e45d9
Pull Request resolved: #166010
@fduwjj fduwjj added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 30, 2025
@fduwjj fduwjj requested review from lw and wconstab October 30, 2025 23:28
…g per layout, root_mesh and rank_map"


We want to create a shared_state to store root_mesh, rank_map and pg caches. We can add more into it down the road, so that it becomes a singleton for bookkeeping and also align with our original proposal to move toward the idea of mesh universe.

cc H-Huang awgu wanchaol fegin wz337 wconstab d4l3k pragupta msaroufim dcci

[ghstack-poisoned]
fduwjj added a commit that referenced this pull request Oct 30, 2025
…h and rank_map

ghstack-source-id: 2835343
Pull Request resolved: #166010
…g per layout, root_mesh and rank_map"


We want to create a shared_state to store root_mesh, rank_map and pg caches. We can add more into it down the road, so that it becomes a singleton for bookkeeping and also align with our original proposal to move toward the idea of mesh universe.

cc H-Huang awgu wanchaol fegin wz337 wconstab d4l3k pragupta msaroufim dcci

[ghstack-poisoned]
…g per layout, root_mesh and rank_map"


We want to create a shared_state to store root_mesh, rank_map and pg caches. We can add more into it down the road, so that it becomes a singleton for bookkeeping and also align with our original proposal to move toward the idea of mesh universe.

cc H-Huang awgu wanchaol fegin wz337 wconstab d4l3k pragupta msaroufim dcci

[ghstack-poisoned]
fduwjj added a commit that referenced this pull request Nov 14, 2025
…h and rank_map

ghstack-source-id: 49b401d
Pull Request resolved: #166010
…g per layout, root_mesh and rank_map"


We want to create a shared_state to store root_mesh, rank_map and pg caches. We can add more into it down the road, so that it becomes a singleton for bookkeeping and also align with our original proposal to move toward the idea of mesh universe.

cc H-Huang awgu wanchaol fegin wz337 wconstab d4l3k pragupta msaroufim dcci

[ghstack-poisoned]
fduwjj added a commit that referenced this pull request Nov 14, 2025
…h and rank_map

ghstack-source-id: 7a6f37e
Pull Request resolved: #166010
Comment on lines +1065 to +1066
non_ep_mesh = global_mesh._unflatten(0, (2, 2, 2), ("dp", "cp", "tp"))
ep_mesh = global_mesh._unflatten(0, (2, 2, 2), ("dp", "ep", "ep_tp"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between these two lines vs a user giving multiple names to a dimension? Or a complex name such as "cp_or_ep"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is to generate a use case where we want to test PG cache. The cache is per layout so when multiple names are assigned to one dimension, the PG will be shared.

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

Labels

ciflow/trunk Trigger trunk jobs on your pull request oncall: distributed Add this issue/PR to distributed oncall triage queue release notes: DeviceMesh

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants