Repository navigation
[MoE] Fix experts DTensor metadata bug for dcp - #2227
Conversation
| # Step 3: Identify mesh dimensions that shard on dim-0 (expert dimension) | ||
| # remove expert dimension | ||
| # and build new sub-mesh/placements for individual expert weights | ||
| dim0_mesh_names = [] |
There was a problem hiding this comment.
I removed it, it is not necessary.
| # Replicate (hybrid) doesn't shard any dim, keep in sub-mesh | ||
| sub_mesh_names.append(name) | ||
| sub_placements.append(Replicate()) | ||
| elif isinstance(placement, (Shard, _StridedShard)) and placement.dim == 0: |
There was a problem hiding this comment.
this doesn't sound right --
Imagine we have (fsdp, ep) mesh, and placements are [_SS(0), S(0)], would this code make it a plain tensor? I think it should be (fsdp,) with S(0).
There was a problem hiding this comment.
Do you mean (dp_shard_mod_ep , dp_shard_in_ep) mesh? The original code make groupedExpert's weights from [_SS(0), S(0)] to [R, R]. Now with this change, it will make the groupedExpert's weight to a plain tensor.
Can you explain more why the targeted placement should be (fsdp,) with S(0)?
There was a problem hiding this comment.
hmm, my bad, I think you are right
There was a problem hiding this comment.
I think in the (fsdp, ep) case, both are splitting dim0, i.e., expert dim. As a complete expert now is stored in one rank, if we use (fsdp, ) with S(0), this is confusing since the expert tensor is now complete, not a Shard along fsdp ranks.
| # Replicate (hybrid) doesn't shard any dim, keep in sub-mesh | ||
| sub_mesh_names.append(name) | ||
| sub_placements.append(Replicate()) | ||
| elif isinstance(placement, (Shard, _StridedShard)) and placement.dim == 0: |
There was a problem hiding this comment.
hmm, my bad, I think you are right
| local_expert_tensors[expert_key] = expert_weight | ||
| else: | ||
| # Add batch dimension temporarily for DTensor creation | ||
| expert_weight_3d = expert_weight.unsqueeze(0) |
There was a problem hiding this comment.
could you remind me why we needed this unsqueeze + squeeze?
There was a problem hiding this comment.
Sure. We use unsqueeze(0) and squeeze(0) to automatically take care of Shard(i) for unsqueeze(0) enables us to use from_local in the previous global device_mesh, without changing i in Shard(i). Then, when we use .squeeze(0), and dim0 has size 1, dim0 will be removed, and all Shard(i) will change to Shard(i-1). But the Replicate in the device_mesh will not be removed, which is the problem.
There was a problem hiding this comment.
I see. In the DTensor case, is it more straightforward if we use torch.index_select instead of indexing + unsqueeze?
| local_expert_tensors[expert_key] = expert_weight | ||
| else: | ||
| # Add batch dimension temporarily for DTensor creation | ||
| expert_weight_3d = expert_weight.unsqueeze(0) |
There was a problem hiding this comment.
I see. In the DTensor case, is it more straightforward if we use torch.index_select instead of indexing + unsqueeze?
|
|
||
| local_expert_tensors[expert_key] = expert_dtensor | ||
| if use_plain_tensor: | ||
| local_expert_tensors[expert_key] = expert_weight |
There was a problem hiding this comment.
It seems this code can be shared.
There was a problem hiding this comment.
Fixed. Thanks for the suggestions.
|
Were you able to test this end to end? I ran into an NCCL error once training started. This change did fix the earlier OOM issue when loading the model, but I think there may still be a bug remaining. I have included the stack trace below in case it is helpful. When I disable |
Hi @aghilann, thanks for reporting this issue. Do you have a smaller setup where I can reproduce this bug? And just to confirm you are exactly on this commit right? We tested training locally loading from a Qwen3 30B-A3B hf ckpt. |
|
Fixed a bug in |
|
Hey @shuhuayu , I'm not on the same commit. I'm on a fork where I've modified some stuff to add LoRA's. I just directly copied the code over, with the changes you added last night I went back to OOM'ing at the I did not OOM when running this on a single node with a debug version of DSV3. Will mention it does work fine when I don't load from HF. |
It's kind of difficult to reproduce this particular issue. We will merge the current fix for now, and look into this later if we find it reproducible locally on a torchtitan branch. For now, I would suggest adding an ad hoc fix to replace |
|
Tested offline conversion, and memory usage before/after fix is similar in a test training loaded from Qwen3 30B-A3B hf ckpt. |
Previously, individual experts are marked as `Replicate` in EP dimension in global `global_device_mesh`. Local experts are first created on `global_device_mesh` and are turned into a 2d tensor using `squeeze(0)`, which only removes the extra dimension, but the remaining metadata `Replicate` is still there. The wrong metadata results in bug when DCP saves `DTensor`. This PR fixes this bug by: 1. Use a sub-mesh that excludes expert dimension, i.e., dim 0. 2. When sub-mesh is empty, use plain tensor instead of `DTensor`.
Previously, individual experts are marked as `Replicate` in EP dimension in global `global_device_mesh`. Local experts are first created on `global_device_mesh` and are turned into a 2d tensor using `squeeze(0)`, which only removes the extra dimension, but the remaining metadata `Replicate` is still there. The wrong metadata results in bug when DCP saves `DTensor`. This PR fixes this bug by: 1. Use a sub-mesh that excludes expert dimension, i.e., dim 0. 2. When sub-mesh is empty, use plain tensor instead of `DTensor`.
Previously, individual experts are marked as `Replicate` in EP dimension in global `global_device_mesh`. Local experts are first created on `global_device_mesh` and are turned into a 2d tensor using `squeeze(0)`, which only removes the extra dimension, but the remaining metadata `Replicate` is still there. The wrong metadata results in bug when DCP saves `DTensor`. This PR fixes this bug by: 1. Use a sub-mesh that excludes expert dimension, i.e., dim 0. 2. When sub-mesh is empty, use plain tensor instead of `DTensor`.
Previously, individual experts are marked as `Replicate` in EP dimension in global `global_device_mesh`. Local experts are first created on `global_device_mesh` and are turned into a 2d tensor using `squeeze(0)`, which only removes the extra dimension, but the remaining metadata `Replicate` is still there. The wrong metadata results in bug when DCP saves `DTensor`. This PR fixes this bug by: 1. Use a sub-mesh that excludes expert dimension, i.e., dim 0. 2. When sub-mesh is empty, use plain tensor instead of `DTensor`.
Previously, individual experts are marked as
Replicatein EP dimension in globalglobal_device_mesh. Local experts are first created onglobal_device_meshand are turned into a 2d tensor usingsqueeze(0), which only removes the extra dimension, but the remaining metadataReplicateis still there. The wrong metadata results in bug when DCP savesDTensor. This PR fixes this bug by:DTensor.