Give each tensor parallel model in a process its own device mesh - #48383
Open
qgallouedec wants to merge 3 commits into
Open
Give each tensor parallel model in a process its own device mesh#48383qgallouedec wants to merge 3 commits into
qgallouedec wants to merge 3 commits into
Conversation
The mesh was built with no dimension names, and for tp_size == world_size torch's 1-D whole-world shortcut returns the default process group, so a second model parallelized in the same process shared the first one's communicator. Names matter too: a DeviceMesh's identity is its ranks, layout, device type, dimension names and creating thread, not the process group behind it, so two meshes over the same ranks compare and hash equal and DTensor's sharding propagation cache returns the first model's mesh for the second model's parameters. Nothing errors, the models just share a communicator, which deadlocks as soon as they are used at the same time.
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Contributor
CI recapDashboard: View test results in Grafana |
Collaborator
|
Fine by me for CB, but In will let @3outeille review for TP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Two tensor parallel models in the same process silently share a communicator, which deadlocks as soon as they are used at the same time.
initialize_tensor_parallelismbuilt its mesh withinit_device_mesh(device_type, (tp_size,)). That mesh has no dimension names, and whentp_size == world_sizetorch's 1-D whole-world shortcut returns the default process group outright, so the second model gets the same communicator as the first.Naming them apart is needed too, and this is the part that is easy to miss. A
DeviceMeshis identified by its ranks, layout, device type, dimension names and creating thread. The process group behind it is not part of that identity, so two meshes over the same ranks compare and hash equal.DTensor's sharding propagation is an LRU cache keyed onOpSchema, which hashes the mesh that way, so the second model hits the first model's entry and its parameters come back holding the first model's mesh.distribute_tensorkeeps the mesh it is given;torch.nn.Parameter(dtensor)is what loses it, becauseaten.detachgoes through the cache.Nothing errors either way. The models look correctly sharded, they just share a communicator.
_build_tp_meshnames the first meshtpand gives every later one its own process group and its own name (tp_1,tp_2, ...).DistributedHelper.extract_tp_meshmatches thetp_prefix so continuous batching still finds the TP mesh.Single-model runs are unchanged apart from the mesh now carrying the name
tp.Repro
torchrun --nproc-per-node 2 repro.py, Qwen3-0.6B, two models, TP 2:NoneandNone('tp',)and('tp_1',)TrueFalseThe underlying sharp edge is torch's, and is worth its own issue there: a mesh's identity arguably should include the process group it is backed by. Here is that part on its own, no transformers involved,
torchrun --nproc-per-node 2:Naming the two meshes apart (
mesh_dim_names=("tp",)and("tp_gen",)) is enough to keep them distinct, which is what this PR does.Who can review?
@SunMarc / @3outeille (tensor parallelism), @remi-or (continuous batching)
repro.py