Skip to content

Add tensor parallelism for NucleusMoE-Image routed experts - #15424

Open
yzhautouskay wants to merge 1 commit into
huggingface:mainfrom
yzhautouskay:yzhautouskay/nucleus-packed-expert-tp
Open

yzhautouskay wants to merge 1 commit into
huggingface:mainfrom
yzhautouskay:yzhautouskay/nucleus-packed-expert-tp

Conversation

@yzhautouskay

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds the "packed_gated_experts" tensor-parallel style and enables it for the routed experts in NucleusMoE-Image at TP2 and TP4. Attention, routing and shared experts stay replicated. Public checkpoint

Expert weight sharding

Both projections are bias-free. Each rank processes the same routed tokens across all 64 experts.

Weight Checkpoint TP2 per rank TP4 per rank Shard per rank
gate_up_proj [64, 2048, 2688] [64, 2048, 1344] [64, 2048, 672] dim 2. Concatenate a:b (gate) and 1344+a:1344+b (up)
down_proj [64, 1344, 2048] [64, 672, 2048] [64, 336, 2048] dim 1. Take a:b

For rank r at TP degree P, w = 1344 // P, a = r * w, and b = a + w. PyTorch sums the partial outputs across ranks.

Results

On GB200, TP4 used 41% less peak memory per GPU and took 1.22 s longer per image than TP1. Latency is the median of three end-to-end BF16 generations after warmup (1024 × 1024, batch size 1, 50 steps).

GPUs Median latency Peak allocated per GPU Lighthouse SSIM vs. TP1 Bicycle SSIM vs. TP1
1 7.11 s 52.30 GiB — —
2 8.07 s 38.03 GiB 0.989 0.964
4 8.33 s 30.89 GiB 0.986 0.935
nucleus_tp_comparison

Before submitting

  • Did you use an AI agent (Claude Code, Codex, Cursor, etc.) to help with this PR? If so:
    • Did you read the Coding with AI agents guide?
    • Did you run the self-review skill on the diff?
    • Did you share the final self-review notes in the PR description or a comment?
  • Did you read the contributor guideline?
  • Did you read our philosophy doc? (important for complex PRs)
  • Was this discussed/approved via a GitHub issue or GitHub Discussions? Please add a link to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?
  • Are you the author (or part of the team we work with) of the model/pipeline this PR relates to?

Who can review?

@sayakpaul @JingyaHuang, could you review how packed MoE expert sharding fits into Diffusers’ tensor parallelism support?

@sippycoder, could you review how NucleusMoE-Image’s expert forward pass uses the sharded weights?

@github-actions github-actions Bot added documentation Improvements or additions to documentation models tests hooks size/M PR with diff < 200 LOC labels Nov 27, 2026
@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Hi @yzhautouskay, thanks for the PR! It does not appear to link an issue it fixes.

  • If this PR addresses an existing issue, please add a closing keyword (e.g. Fixes #1234) to the PR description so the issue is linked.
  • If you are the author of the model/pipeline this PR relates to, or part of the team we work with on it, tick the Are you the author (or part of the team we work with) of the model/pipeline this PR relates to? box in the PR description (if you removed the template, paste that line back, checked). This reminder then no longer applies.
  • Otherwise there is nothing you need to do: a maintainer will look at the PR and add the no-issue-needed label to silence this reminder if it should stay open without an issue.

See the contribution guide for more details.

Please note that PRs without a linked issue are likely to be automatically closed 10 days after this notice.

Once the PR links an existing issue, the author box is ticked, or the no-issue-needed label is added, you can ignore this message — it stays here as a comment, but it no longer applies.

@sayakpaul

Copy link
Copy Markdown
Member

Thanks for the PR!

We're discussing a bit if we want TP for a model that doesn't have much usage yet.

@sayakpaul sayakpaul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

I opened yzhautouskay#2 with some suggestions to keep the modeling changes simpler and to move changes to a hook-based implementation.


All ranks process the same routed tokens. Each rank takes a different slice of the intermediate channels, pairing the corresponding gate and up channels with the same input channels of the down projection. The expert computes its own elementwise gate activation on those local shards. After the down projection, PyTorch sums the partial outputs across ranks.

NucleusMoE-Image uses this plan entry because only some blocks contain routed experts.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's hyperlink the NucleusMoE-Image pipeline page here.


def _packed_gated_expert_specs(module) -> dict[str, TPShardSpec]:
"""Shard matching intermediate channels of the packed gate, up, and down projections for every expert."""
width = module.down_proj.shape[1]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What if the module doesn't have down_proj? I think we should validate better and early.

with torch.device("meta"):
model = self.model_class(**self.get_init_dict())
prefix = "transformer_blocks.1.img_mlp.experts"
assert resolve_tp_shard_specs(model, model._tp_plan, tp_degree=4) == {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this supposed to run on 4 GPUs?


@pytest.fixture(autouse=True)
def initialize_experts(self, monkeypatch):
# The checkpoint normally supplies these two torch.empty parameters.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this could be confusing. It's not the checkpoint that supplies torch.empty, it's the initialization block.

This branch has not been deployed

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

Labels

documentation Improvements or additions to documentation hooks models size/M PR with diff < 200 LOC tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants