Repository navigation
Add tensor parallelism for NucleusMoE-Image routed experts #15424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,9 +13,12 @@ | |
| # limitations under the License. | ||
|
|
||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from diffusers import NucleusMoEImageTransformer2DModel | ||
| from diffusers.hooks.tensor_parallel import TPShardSpec, resolve_tp_shard_specs | ||
| from diffusers.models.transformers.transformer_nucleusmoe_image import SwiGLUExperts | ||
| from diffusers.utils.torch_utils import randn_tensor | ||
|
|
||
| from ...testing_utils import enable_full_determinism, torch_device | ||
|
|
@@ -26,6 +29,7 @@ | |
| LoraTesterMixin, | ||
| MemoryTesterMixin, | ||
| ModelTesterMixin, | ||
| TensorParallelTesterMixin, | ||
| TorchCompileTesterMixin, | ||
| TrainingTesterMixin, | ||
| ) | ||
|
|
@@ -73,8 +77,7 @@ def get_init_dict(self) -> dict: | |
| "capacity_factors": [8.0, 8.0], | ||
| } | ||
|
|
||
| def get_dummy_inputs(self) -> dict: | ||
| batch_size = 1 | ||
| def get_dummy_inputs(self, batch_size=1) -> dict: | ||
| in_channels = 16 | ||
| joint_attention_dim = 16 | ||
| height = width = 4 | ||
|
|
@@ -128,6 +131,41 @@ def test_without_attention_mask(self): | |
| assert output.sample.shape[1] == inputs["hidden_states"].shape[1] | ||
|
|
||
|
|
||
| class TestNucleusMoEImageTransformerTensorParallel(NucleusMoEImageTransformerTesterConfig, TensorParallelTesterMixin): | ||
| def get_init_dict(self): | ||
| return { | ||
| **super().get_init_dict(), | ||
| "moe_enabled": True, | ||
| "dense_moe_strategy": "leave_first_block_dense", | ||
| "num_experts": 4, | ||
| "moe_intermediate_dim": 32, | ||
| "capacity_factors": [0, 2], | ||
| } | ||
|
|
||
| def test_packed_expert_shard_specs(self): | ||
| 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) == { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this supposed to run on 4 GPUs? |
||
| f"{prefix}.gate_up_proj": TPShardSpec(2, [32, 32]), | ||
| f"{prefix}.down_proj": TPShardSpec(1, [32]), | ||
| } | ||
| with pytest.raises(ValueError, match="not divisible by 3"): | ||
| resolve_tp_shard_specs(model, model._tp_plan, tp_degree=3) | ||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def initialize_experts(self, monkeypatch): | ||
| # The checkpoint normally supplies these two torch.empty parameters. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| original_init = SwiGLUExperts.__init__ | ||
|
|
||
| def init(module, *args, **kwargs): | ||
| original_init(module, *args, **kwargs) | ||
| torch.nn.init.normal_(module.gate_up_proj, std=0.1) | ||
| torch.nn.init.normal_(module.down_proj, std=0.1) | ||
|
|
||
| monkeypatch.setattr(SwiGLUExperts, "__init__", init) | ||
|
|
||
|
|
||
| class TestNucleusMoEImageTransformerMemory(NucleusMoEImageTransformerTesterConfig, MemoryTesterMixin): | ||
| """Memory optimization tests for NucleusMoE Image Transformer.""" | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the
moduledoesn't havedown_proj? I think we should validate better and early.