Repository navigation
core: support conditional layers in parallelism. #15246
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2d6870d
core: support conditional layers in l layers in parallelism.
sayakpaul 9a0be9a
up
sayakpaul b80fb5c
Merge branch 'main' into conditional-layers-parallelism
sayakpaul 24baa7e
Merge branch 'main' into conditional-layers-parallelism
sayakpaul 45c0388
debug instead of warning.
sayakpaul 2e9bb1e
Merge branch 'main' into conditional-layers-parallelism
sayakpaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1220,7 +1220,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: str | os.PathLike | None | |
| tp_config = ( | ||
| parallel_config | ||
| if isinstance(parallel_config, TensorParallelConfig) | ||
| else parallel_config.tensor_parallel_config | ||
| else getattr(parallel_config, "tensor_parallel_config", None) | ||
|
Member
Author
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. To support passing bare |
||
| ) | ||
| if tp_config is not None and tp_config.tp_degree == 1 and tp_config.mesh is None: | ||
| # Nothing to shard, so take the ordinary loader rather than building 1-rank DTensors. | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| # coding=utf-8 | ||
| # Copyright 2026 HuggingFace Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
|
|
||
| import pytest | ||
| import torch | ||
| import torch.distributed as dist | ||
| import torch.multiprocessing as mp | ||
|
|
||
| from diffusers.models._modeling_parallel import ContextParallelConfig, TensorParallelConfig | ||
| from diffusers.utils import logging | ||
|
|
||
| from ..testing_utils import ( | ||
| CaptureLogger, | ||
| is_context_parallel, | ||
| is_tensor_parallel, | ||
| require_torch_multi_accelerator, | ||
| torch_device, | ||
| ) | ||
| from .testing_utils.parallelism import DEVICE_CONFIG, DummyConditionalPlanModel, _find_free_port | ||
|
|
||
|
|
||
| def _conditional_plan_worker(rank, world_size, master_port, checkpoint_dir, parallelism, inputs_dict, return_dict): | ||
| """Load `DummyConditionalPlanModel` with a `parallel_config` and run a forward pass. | ||
|
|
||
| Rank 0 reports its output, what the hooks logged while loading, and where the plan was applied: the parameters | ||
| sharded into `DTensor`s for tensor parallelism, or the modules carrying a hook for context parallelism. | ||
| """ | ||
| try: | ||
| os.environ["MASTER_ADDR"] = "localhost" | ||
| os.environ["MASTER_PORT"] = str(master_port) | ||
| os.environ["RANK"] = str(rank) | ||
| os.environ["WORLD_SIZE"] = str(world_size) | ||
|
|
||
| device_config = DEVICE_CONFIG.get(torch_device, DEVICE_CONFIG["cuda"]) | ||
| dist.init_process_group(backend=device_config["backend"], rank=rank, world_size=world_size) | ||
| device_config["module"].set_device(rank) | ||
| device = torch.device(f"{torch_device}:{rank}") | ||
|
|
||
| from torch.distributed.tensor import DTensor | ||
|
|
||
| if parallelism == "tensor": | ||
| parallel_config = TensorParallelConfig(tp_degree=world_size) | ||
| else: | ||
| parallel_config = ContextParallelConfig(ulysses_degree=world_size) | ||
|
|
||
| # A skipped `Conditional` entry is logged at debug level. | ||
| hooks_logger = logging.get_logger("diffusers.hooks") | ||
| hooks_logger.setLevel(logging.DEBUG) | ||
| with CaptureLogger(hooks_logger) as cap_logger: | ||
| model = DummyConditionalPlanModel.from_pretrained(checkpoint_dir, parallel_config=parallel_config) | ||
| if parallelism == "context": | ||
| # A tensor-parallel load already places each rank's shard on its device. | ||
| model.to(device) | ||
|
|
||
| inputs_on_device = {k: v.to(device) for k, v in inputs_dict.items()} | ||
| with torch.no_grad(): | ||
| output = model(**inputs_on_device) | ||
|
|
||
| if rank == 0: | ||
| if parallelism == "tensor": | ||
| applied = [name for name, param in model.state_dict().items() if isinstance(param, DTensor)] | ||
| else: | ||
| applied = [name for name, module in model.named_modules() if hasattr(module, "_diffusers_hook")] | ||
|
|
||
| return_dict["status"] = "success" | ||
| return_dict["output"] = output.cpu().tolist() | ||
| return_dict["logs"] = cap_logger.out | ||
| return_dict["applied"] = applied | ||
|
|
||
| except Exception as e: | ||
| if rank == 0: | ||
| return_dict["status"] = "error" | ||
| return_dict["error"] = f"{type(e).__name__}: {e}" | ||
| finally: | ||
| if dist.is_initialized(): | ||
| dist.destroy_process_group() | ||
|
|
||
|
|
||
| @require_torch_multi_accelerator | ||
| class TestConditionalPlanEntries: | ||
| """`Conditional` entries of a `_tp_plan` / `_cp_plan`, on a checkpoint with and without the conditional layers.""" | ||
|
|
||
| unmatched_message = "match no module of 'DummyConditionalPlanModel'" | ||
|
|
||
| def _save_and_load_with_parallelism(self, tmp_path, parallelism, use_conditional_layers): | ||
| """Save the model, load it on two ranks with parallelism, and check the output against a single device.""" | ||
| if not torch.distributed.is_available(): | ||
| pytest.skip("torch.distributed is not available.") | ||
|
|
||
| torch.manual_seed(0) | ||
| model = DummyConditionalPlanModel(use_conditional_layers=use_conditional_layers).eval() | ||
| inputs_dict = {"hidden_states": torch.randn(1, 8, 8), "position_ids": torch.arange(8)} | ||
| with torch.no_grad(): | ||
| reference = model(**inputs_dict) | ||
| model.save_pretrained(tmp_path) | ||
|
|
||
| world_size = 2 | ||
| return_dict = mp.Manager().dict() | ||
| mp.spawn( | ||
| _conditional_plan_worker, | ||
| args=(world_size, _find_free_port(), str(tmp_path), parallelism, inputs_dict, return_dict), | ||
| nprocs=world_size, | ||
| join=True, | ||
| ) | ||
|
|
||
| assert return_dict.get("status") == "success", ( | ||
| f"Loading with {parallelism} parallelism failed: {return_dict.get('error', 'Unknown error')}" | ||
| ) | ||
| torch.testing.assert_close(reference, torch.tensor(return_dict["output"]), atol=1e-4, rtol=1e-4) | ||
| return return_dict | ||
|
|
||
| @is_tensor_parallel | ||
| @pytest.mark.parametrize("use_conditional_layers", [True, False]) | ||
| def test_tensor_parallel(self, tmp_path, use_conditional_layers): | ||
| result = self._save_and_load_with_parallelism(tmp_path, "tensor", use_conditional_layers) | ||
|
|
||
| if use_conditional_layers: | ||
| gate_params = {f"blocks.{i}.gate_proj.{name}" for i in range(2) for name in ("weight", "bias")} | ||
| assert gate_params <= set(result["applied"]) | ||
| assert self.unmatched_message not in result["logs"] | ||
| else: | ||
| assert f"{self.unmatched_message}: ['blocks.*.gate_proj']" in result["logs"] | ||
|
|
||
| @is_context_parallel | ||
| @pytest.mark.parametrize("use_conditional_layers", [True, False]) | ||
| def test_context_parallel(self, tmp_path, use_conditional_layers): | ||
| result = self._save_and_load_with_parallelism(tmp_path, "context", use_conditional_layers) | ||
|
|
||
| if use_conditional_layers: | ||
| assert "pos_embed" in result["applied"] | ||
| assert self.unmatched_message not in result["logs"] | ||
| else: | ||
| assert f"{self.unmatched_message}: ['pos_embed']" in result["logs"] |
Oops, something went wrong.
Oops, something went wrong.
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.
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.
i don't think we need warn here, it's expected behavior -> change to debug maybe?