Skip to content

[MoE] Fix experts DTensor metadata bug for dcp - #2227

Merged
shuhuayu merged 10 commits into
pytorch:mainfrom
shuhuayu:modeldev
Jan 22, 2026
Merged

shuhuayu merged 10 commits into
pytorch:mainfrom
shuhuayu:modeldev

Conversation

@shuhuayu

Copy link
Copy Markdown
Contributor

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.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jan 14, 2026
@shuhuayu shuhuayu changed the title [MoE] fix experts device_mesh metadata bug for dcp [MoE] Fix experts device_mesh metadata bug for dcp Jan 14, 2026
@shuhuayu shuhuayu changed the title [MoE] Fix experts device_mesh metadata bug for dcp [MoE] Fix experts DTensor metadata bug for dcp Jan 14, 2026
Comment thread torchtitan/models/utils.py Outdated
# 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 = []

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 do we keep this?

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.

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:

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.

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

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.

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)?

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.

hmm, my bad, I think you are right

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.

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:

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.

hmm, my bad, I think you are right

Comment thread torchtitan/models/utils.py Outdated
local_expert_tensors[expert_key] = expert_weight
else:
# Add batch dimension temporarily for DTensor creation
expert_weight_3d = expert_weight.unsqueeze(0)

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.

could you remind me why we needed this unsqueeze + squeeze?

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.

Sure. We use unsqueeze(0) and squeeze(0) to automatically take care of Shard(i) for $i \ge 1$. Turning a 2d tensor to 3d using 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.

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 see. In the DTensor case, is it more straightforward if we use torch.index_select instead of indexing + unsqueeze?

Comment thread torchtitan/models/utils.py Outdated
local_expert_tensors[expert_key] = expert_weight
else:
# Add batch dimension temporarily for DTensor creation
expert_weight_3d = expert_weight.unsqueeze(0)

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 see. In the DTensor case, is it more straightforward if we use torch.index_select instead of indexing + unsqueeze?

Comment thread torchtitan/models/utils.py Outdated

local_expert_tensors[expert_key] = expert_dtensor
if use_plain_tensor:
local_expert_tensors[expert_key] = expert_weight

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.

It seems this code can be shared.

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.

Fixed. Thanks for the suggestions.

@shuhuayu
shuhuayu marked this pull request as draft January 15, 2026 01:43
@aghilann

aghilann commented Jan 16, 2026 •

Copy link
Copy Markdown

@shuhuayu

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 initial_load_in_hf, the run begins training successfully. logs.txt having it enabled is important for my use case since I am fine tuning a model.

@shuhuayu

shuhuayu commented Jan 16, 2026 •

Copy link
Copy Markdown
Contributor Author

@shuhuayu

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 initial_load_in_hf, the run begins training successfully. logs.txt having it enabled is important for my use case since I am fine tuning a model.

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.

@shuhuayu

Copy link
Copy Markdown
Contributor Author

Fixed a bug in from_hf when plain tensors going to the offline path. @aghilann

@aghilann

aghilann commented Jan 16, 2026 •

Copy link
Copy Markdown

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 local_tensor = torch.stack(sorted_experts, dim=0) line. Here's a stack trace if it's useful
logs.txt

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.

@shuhuayu
shuhuayu marked this pull request as ready for review January 21, 2026 19:31
@shuhuayu

Copy link
Copy Markdown
Contributor Author

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 local_tensor = torch.stack(sorted_experts, dim=0) line. Here's a stack trace if it's useful logs.txt

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 torch.stack(), which creates a new tensor and is memory heavy. Like you can create a empty tensor. and assign values to this empty tensor one expert by one expert, and delete an expert right away once it is copied. Hope that helps.

@shuhuayu

Copy link
Copy Markdown
Contributor Author

Tested offline conversion, and memory usage before/after fix is similar in a test training loaded from Qwen3 30B-A3B hf ckpt.

@shuhuayu
shuhuayu merged commit 8ff9e42 into pytorch:main Jan 22, 2026
wwwjn pushed a commit to wwwjn/torchtitan that referenced this pull request Jan 30, 2026
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`.
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
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`.
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
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`.
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
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`.
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.

4 participants