From 2d6870d6b4c2f0e02373526494d626e4c11d17cd Mon Sep 17 00:00:00 2001 From: Sayak Paul Date: Wed, 11 Nov 2026 12:41:46 +0000 Subject: [PATCH 1/3] core: support conditional layers in l layers in parallelism. --- src/diffusers/hooks/context_parallel.py | 38 ++++- src/diffusers/hooks/tensor_parallel.py | 55 +++++-- src/diffusers/models/_modeling_parallel.py | 23 ++- src/diffusers/models/modeling_utils.py | 2 +- .../test_parallelism_conditional_layers.py | 146 ++++++++++++++++++ tests/models/testing_utils/parallelism.py | 67 +++++++- 6 files changed, 311 insertions(+), 20 deletions(-) create mode 100644 tests/models/test_parallelism_conditional_layers.py diff --git a/src/diffusers/hooks/context_parallel.py b/src/diffusers/hooks/context_parallel.py index 1310b20c5c11..591c02204bee 100644 --- a/src/diffusers/hooks/context_parallel.py +++ b/src/diffusers/hooks/context_parallel.py @@ -24,6 +24,7 @@ import torch.distributed._functional_collectives as funcol from ..models._modeling_parallel import ( + Conditional, ContextParallelConfig, ContextParallelInput, ContextParallelModelPlan, @@ -85,10 +86,17 @@ def apply_context_parallel( """Apply context parallel on a model.""" logger.debug(f"Applying context parallel with CP mesh: {parallel_config._mesh} and plan: {plan}") + unmatched = [] for module_id, cp_model_plan in plan.items(): - submodule = _get_submodule_by_name(module, module_id) + conditional = isinstance(cp_model_plan, Conditional) + if conditional: + cp_model_plan = cp_model_plan.value + submodule = _get_submodule_by_name(module, module_id, conditional) if not isinstance(submodule, list): submodule = [submodule] + if conditional and len(submodule) == 0: + unmatched.append(module_id) + continue logger.debug(f"Applying ContextParallelHook to {module_id=} identifying a total of {len(submodule)} modules") @@ -108,10 +116,19 @@ def apply_context_parallel( registry = HookRegistry.check_if_exists_or_initialize(m) registry.register_hook(hook, hook_name) + if unmatched: + logger.warning( + f"Skipping the `Conditional` context parallel plan entries that match no module of " + f"'{module.__class__.__name__}': {unmatched}." + ) + def remove_context_parallel(module: torch.nn.Module, plan: dict[str, ContextParallelModelPlan]) -> None: for module_id, cp_model_plan in plan.items(): - submodule = _get_submodule_by_name(module, module_id) + conditional = isinstance(cp_model_plan, Conditional) + if conditional: + cp_model_plan = cp_model_plan.value + submodule = _get_submodule_by_name(module, module_id, conditional) if not isinstance(submodule, list): submodule = [submodule] @@ -354,13 +371,17 @@ def _all_gather_anything(tensor: torch.Tensor, dim: int, group: dist.device_mesh return gathered_tensor -def _get_submodule_by_name(model: torch.nn.Module, name: str) -> torch.nn.Module | list[torch.nn.Module]: +def _get_submodule_by_name( + model: torch.nn.Module, name: str, conditional: bool = False +) -> torch.nn.Module | list[torch.nn.Module]: if name.count("*") > 1: raise ValueError("Wildcard '*' can only be used once in the name") - return _find_submodule_by_name(model, name) + return _find_submodule_by_name(model, name, conditional) -def _find_submodule_by_name(model: torch.nn.Module, name: str) -> torch.nn.Module | list[torch.nn.Module]: +def _find_submodule_by_name( + model: torch.nn.Module, name: str, conditional: bool = False +) -> torch.nn.Module | list[torch.nn.Module]: if name == "": return model first_atom, remaining_name = name.split(".", 1) if "." in name else (name, "") @@ -369,14 +390,17 @@ def _find_submodule_by_name(model: torch.nn.Module, name: str) -> torch.nn.Modul raise ValueError("Wildcard '*' can only be used with ModuleList") submodules = [] for submodule in model: - subsubmodules = _find_submodule_by_name(submodule, remaining_name) + subsubmodules = _find_submodule_by_name(submodule, remaining_name, conditional) if not isinstance(subsubmodules, list): subsubmodules = [subsubmodules] submodules.extend(subsubmodules) return submodules else: + if conditional and getattr(model, first_atom, None) is None: + # A `Conditional` entry matches nothing where its module is missing or set to `None`. + return [] if hasattr(model, first_atom): submodule = getattr(model, first_atom) - return _find_submodule_by_name(submodule, remaining_name) + return _find_submodule_by_name(submodule, remaining_name, conditional) else: raise ValueError(f"'{first_atom}' is not a submodule of '{model.__class__.__name__}'") diff --git a/src/diffusers/hooks/tensor_parallel.py b/src/diffusers/hooks/tensor_parallel.py index 28460f8e8ae1..71275db72e3a 100644 --- a/src/diffusers/hooks/tensor_parallel.py +++ b/src/diffusers/hooks/tensor_parallel.py @@ -16,7 +16,7 @@ import torch -from ..models._modeling_parallel import TensorParallelConfig +from ..models._modeling_parallel import Conditional, TensorParallelConfig from ..utils import get_logger, is_peft_available @@ -183,38 +183,69 @@ def resolve_tp_shard_specs(model: torch.nn.Module, tp_plan: dict, tp_degree: int return specs -def _resolve_tp_plan(model: torch.nn.Module, tp_plan: dict) -> list: +def _maybe_get_submodule(module: torch.nn.Module, path: str) -> "torch.nn.Module | None": + """Resolve the dotted `path` on `module`, or return `None` if an attribute along it is missing or `None`.""" + for atom in path.split("."): + module = getattr(module, atom, None) + if module is None: + return None + return module + + +def _resolve_tp_plan(model: torch.nn.Module, tp_plan: dict, warn_unmatched: bool = False) -> list: """Group a flat `_tp_plan` into per-block `(submodule, {relative_path: style})` plans. Each glob is split at its single `*`; the prefix must resolve to a `ModuleList` and the suffix is the per-element key. Grouping by block lets the caller issue one `parallelize_module` call per block, which `RowwiseParallel` needs to attach its input redistribution at the block boundary. + A `Conditional` entry is unwrapped here and kept only for the blocks where its module exists, so the returned plans + hold plain styles. `warn_unmatched` logs the `Conditional` entries that match no module; it is off by default + because the plan is resolved several times per model. + Example: when `transformer_blocks` is a `ModuleList` of length 2, the input `{"transformer_blocks.*.ff.linear_out": "rowwise"}` returns `[(transformer_blocks[0], {"ff.linear_out": "rowwise"}), (transformer_blocks[1], {"ff.linear_out": "rowwise"})]`. """ grouped: dict[int, tuple] = {} order: list[int] = [] + unmatched: list[str] = [] for pattern, style in tp_plan.items(): if pattern.count("*") > 1: raise ValueError(f"Wildcard '*' can only be used once in a `_tp_plan` key, got '{pattern}'.") + conditional = isinstance(style, Conditional) + if conditional: + style = style.value + if "*" in pattern: prefix, _, suffix = pattern.partition("*") - container = model - for atom in prefix.strip(".").split("."): - container = getattr(container, atom) - if not isinstance(container, torch.nn.ModuleList): + prefix = prefix.strip(".") + if conditional: + container = _maybe_get_submodule(model, prefix) + else: + container = model + for atom in prefix.split("."): + container = getattr(container, atom) + if conditional and container is None: + blocks = [] + elif not isinstance(container, torch.nn.ModuleList): raise ValueError( f"`_tp_plan` wildcard '{pattern}' must expand over a `ModuleList`, but " - f"'{prefix.strip('.')}' resolved to '{container.__class__.__name__}'." + f"'{prefix}' resolved to '{container.__class__.__name__}'." ) - relative, blocks = suffix.strip("."), list(container) + else: + blocks = list(container) + relative = suffix.strip(".") else: relative, blocks = pattern, [model] + if conditional: + blocks = [block for block in blocks if _maybe_get_submodule(block, relative) is not None] + if not blocks: + unmatched.append(pattern) + for block in blocks: key = id(block) if key not in grouped: @@ -222,6 +253,12 @@ def _resolve_tp_plan(model: torch.nn.Module, tp_plan: dict) -> list: order.append(key) grouped[key][1][relative] = style + if warn_unmatched and unmatched: + logger.warning( + f"Skipping the `Conditional` `_tp_plan` entries that match no module of '{model.__class__.__name__}': " + f"{unmatched}." + ) + return [grouped[key] for key in order] @@ -512,7 +549,7 @@ def apply_tensor_parallel( ) backend = "neuron" if tp_mesh.device_type == "neuron" else "default" - groups = _resolve_tp_plan(model, tp_plan) + groups = _resolve_tp_plan(model, tp_plan, warn_unmatched=True) logger.debug(f"Applying tensor parallel (backend={backend}) over {len(groups)} module group(s) on mesh {tp_mesh}.") from torch.distributed.tensor.parallel import parallelize_module diff --git a/src/diffusers/models/_modeling_parallel.py b/src/diffusers/models/_modeling_parallel.py index 58cbc822f8bf..44bf07664fcb 100644 --- a/src/diffusers/models/_modeling_parallel.py +++ b/src/diffusers/models/_modeling_parallel.py @@ -16,7 +16,7 @@ # limitations under the License. from dataclasses import dataclass -from typing import TYPE_CHECKING, Literal +from typing import TYPE_CHECKING, Any, Literal import torch import torch.distributed as dist @@ -285,6 +285,25 @@ def __repr__(self): return f"ContextParallelOutput(gather_dim={self.gather_dim}, expected_dims={self.expected_dims})" +@dataclass(frozen=True) +class Conditional: + """ + Marks a `_cp_plan` or `_tp_plan` entry whose module does not exist on every instance of the model class. + For example, different checkpoints of the same model class might have different layers / modules. + + A plan entry must resolve to a module, so a typo in a key raises. Wrap the value of an entry in + `Conditional` when its module depends on the config or the checkpoint. The entry is skipped + wherever the module is missing or set to `None`, and a warning is logged if it matches no module at all. + + Args: + value: + The value the entry would have without the wrapper, e.g. `Conditional("colwise")` in a `_tp_plan` or + `Conditional(ContextParallelOutput(gather_dim=1, expected_dims=3))` in a `_cp_plan`. + """ + + value: Any + + # A dictionary where keys denote the input to be split across context parallel region, and the # value denotes the sharding configuration. # If the key is a string, it denotes the name of the parameter in the forward function. @@ -300,7 +319,7 @@ def __repr__(self): # A dictionary where keys denote the module id, and the value denotes how the inputs/outputs of # the module should be split/gathered across context parallel region. -ContextParallelModelPlan = dict[str, ContextParallelInputType | ContextParallelOutputType] +ContextParallelModelPlan = dict[str, ContextParallelInputType | ContextParallelOutputType | Conditional] # Example of a ContextParallelModelPlan (QwenImageTransformer2DModel): diff --git a/src/diffusers/models/modeling_utils.py b/src/diffusers/models/modeling_utils.py index 3928561468c2..ef90fe4f5dd0 100644 --- a/src/diffusers/models/modeling_utils.py +++ b/src/diffusers/models/modeling_utils.py @@ -1242,7 +1242,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) ) 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. diff --git a/tests/models/test_parallelism_conditional_layers.py b/tests/models/test_parallelism_conditional_layers.py new file mode 100644 index 000000000000..0fb52aa14e61 --- /dev/null +++ b/tests/models/test_parallelism_conditional_layers.py @@ -0,0 +1,146 @@ +# 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) + + with CaptureLogger(logging.get_logger("diffusers.hooks")) 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_warning = "match no module" + + 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_warning not in result["logs"] + else: + assert self.unmatched_warning in result["logs"] + assert "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_warning not in result["logs"] + else: + assert self.unmatched_warning in result["logs"] + assert "pos_embed" in result["logs"] diff --git a/tests/models/testing_utils/parallelism.py b/tests/models/testing_utils/parallelism.py index de19a7edd212..a4da999cb350 100644 --- a/tests/models/testing_utils/parallelism.py +++ b/tests/models/testing_utils/parallelism.py @@ -21,7 +21,15 @@ import torch.distributed as dist import torch.multiprocessing as mp -from diffusers.models._modeling_parallel import ContextParallelConfig, TensorParallelConfig +from diffusers.configuration_utils import ConfigMixin, register_to_config +from diffusers.models import ModelMixin +from diffusers.models._modeling_parallel import ( + Conditional, + ContextParallelConfig, + ContextParallelInput, + ContextParallelOutput, + TensorParallelConfig, +) from diffusers.models.attention_dispatch import AttentionBackendName, _AttentionBackendRegistry from diffusers.utils import SAFE_WEIGHTS_INDEX_NAME @@ -57,6 +65,63 @@ def _find_free_port(): return port +class DummyConditionalPlanBlock(torch.nn.Module): + def __init__(self, hidden_dim: int, use_conditional_layers: bool): + super().__init__() + self.up_proj = torch.nn.Linear(hidden_dim, 2 * hidden_dim) + self.gate_proj = torch.nn.Linear(hidden_dim, 2 * hidden_dim) if use_conditional_layers else None + self.down_proj = torch.nn.Linear(2 * hidden_dim, hidden_dim) + + def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: + projected = torch.nn.functional.silu(self.up_proj(hidden_states)) + if self.gate_proj is not None: + projected = projected * self.gate_proj(hidden_states) + return hidden_states + self.down_proj(projected) + + +class DummyConditionalPlanModel(ModelMixin, ConfigMixin): + """A tiny model whose `_cp_plan` and `_tp_plan` have `Conditional` entries. + + The conditional layers exist only with `use_conditional_layers=True`. Without it, `pos_embed` is not created and + each block's `gate_proj` is `None`, which are the two ways a layer can be absent. The forward breaks on a shape + mismatch if a conditional layer exists but its plan entry is not applied. + """ + + _cp_plan = { + "": {"hidden_states": ContextParallelInput(split_dim=1, expected_dims=3)}, + "pos_embed": Conditional({0: ContextParallelInput(split_dim=0, expected_dims=2, split_output=True)}), + "proj_out": ContextParallelOutput(gather_dim=1, expected_dims=3), + } + _tp_plan = { + "blocks.*.up_proj": "colwise", + "blocks.*.gate_proj": Conditional("colwise"), + "blocks.*.down_proj": "rowwise", + } + + @register_to_config + def __init__( + self, + hidden_dim: int = 8, + num_layers: int = 2, + max_positions: int = 16, + use_conditional_layers: bool = True, + ): + super().__init__() + if use_conditional_layers: + self.pos_embed = torch.nn.Embedding(max_positions, hidden_dim) + self.blocks = torch.nn.ModuleList( + [DummyConditionalPlanBlock(hidden_dim, use_conditional_layers) for _ in range(num_layers)] + ) + self.proj_out = torch.nn.Linear(hidden_dim, hidden_dim) + + def forward(self, hidden_states: torch.Tensor, position_ids: torch.Tensor) -> torch.Tensor: + if self.config.use_conditional_layers: + hidden_states = hidden_states + self.pos_embed(position_ids) + for block in self.blocks: + hidden_states = block(hidden_states) + return self.proj_out(hidden_states) + + def _context_parallel_worker( rank, world_size, From 9a0be9ae23db84f73bc00d009437b1cfeb31141b Mon Sep 17 00:00:00 2001 From: sayakpaul Date: Wed, 11 Nov 2026 18:16:49 +0530 Subject: [PATCH 2/3] up --- src/diffusers/models/_modeling_parallel.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/diffusers/models/_modeling_parallel.py b/src/diffusers/models/_modeling_parallel.py index 44bf07664fcb..8fbdb392f179 100644 --- a/src/diffusers/models/_modeling_parallel.py +++ b/src/diffusers/models/_modeling_parallel.py @@ -288,12 +288,12 @@ def __repr__(self): @dataclass(frozen=True) class Conditional: """ - Marks a `_cp_plan` or `_tp_plan` entry whose module does not exist on every instance of the model class. - For example, different checkpoints of the same model class might have different layers / modules. + Marks a `_cp_plan` or `_tp_plan` entry whose module does not exist on every instance of the model class. For + example, different checkpoints of the same model class might have different layers / modules. - A plan entry must resolve to a module, so a typo in a key raises. Wrap the value of an entry in - `Conditional` when its module depends on the config or the checkpoint. The entry is skipped - wherever the module is missing or set to `None`, and a warning is logged if it matches no module at all. + A plan entry must resolve to a module, so a typo in a key raises. Wrap the value of an entry in `Conditional` when + its module depends on the config or the checkpoint. The entry is skipped wherever the module is missing or set to + `None`, and a warning is logged if it matches no module at all. Args: value: From 45c038811f57a1ef55c057402877d35bfc065e8d Mon Sep 17 00:00:00 2001 From: Sayak Paul Date: Fri, 13 Nov 2026 02:13:07 +0000 Subject: [PATCH 3/3] debug instead of warning. --- src/diffusers/hooks/context_parallel.py | 2 +- src/diffusers/hooks/tensor_parallel.py | 10 +++++----- src/diffusers/models/_modeling_parallel.py | 2 +- .../test_parallelism_conditional_layers.py | 17 +++++++++-------- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/diffusers/hooks/context_parallel.py b/src/diffusers/hooks/context_parallel.py index 591c02204bee..07e59624f39f 100644 --- a/src/diffusers/hooks/context_parallel.py +++ b/src/diffusers/hooks/context_parallel.py @@ -117,7 +117,7 @@ def apply_context_parallel( registry.register_hook(hook, hook_name) if unmatched: - logger.warning( + logger.debug( f"Skipping the `Conditional` context parallel plan entries that match no module of " f"'{module.__class__.__name__}': {unmatched}." ) diff --git a/src/diffusers/hooks/tensor_parallel.py b/src/diffusers/hooks/tensor_parallel.py index 71275db72e3a..8e98a507ee46 100644 --- a/src/diffusers/hooks/tensor_parallel.py +++ b/src/diffusers/hooks/tensor_parallel.py @@ -192,7 +192,7 @@ def _maybe_get_submodule(module: torch.nn.Module, path: str) -> "torch.nn.Module return module -def _resolve_tp_plan(model: torch.nn.Module, tp_plan: dict, warn_unmatched: bool = False) -> list: +def _resolve_tp_plan(model: torch.nn.Module, tp_plan: dict, log_unmatched: bool = False) -> list: """Group a flat `_tp_plan` into per-block `(submodule, {relative_path: style})` plans. Each glob is split at its single `*`; the prefix must resolve to a `ModuleList` and the suffix is the per-element @@ -200,7 +200,7 @@ def _resolve_tp_plan(model: torch.nn.Module, tp_plan: dict, warn_unmatched: bool to attach its input redistribution at the block boundary. A `Conditional` entry is unwrapped here and kept only for the blocks where its module exists, so the returned plans - hold plain styles. `warn_unmatched` logs the `Conditional` entries that match no module; it is off by default + hold plain styles. `log_unmatched` logs the `Conditional` entries that match no module; it is off by default because the plan is resolved several times per model. Example: when `transformer_blocks` is a `ModuleList` of length 2, the input `{"transformer_blocks.*.ff.linear_out": @@ -253,8 +253,8 @@ def _resolve_tp_plan(model: torch.nn.Module, tp_plan: dict, warn_unmatched: bool order.append(key) grouped[key][1][relative] = style - if warn_unmatched and unmatched: - logger.warning( + if log_unmatched and unmatched: + logger.debug( f"Skipping the `Conditional` `_tp_plan` entries that match no module of '{model.__class__.__name__}': " f"{unmatched}." ) @@ -549,7 +549,7 @@ def apply_tensor_parallel( ) backend = "neuron" if tp_mesh.device_type == "neuron" else "default" - groups = _resolve_tp_plan(model, tp_plan, warn_unmatched=True) + groups = _resolve_tp_plan(model, tp_plan, log_unmatched=True) logger.debug(f"Applying tensor parallel (backend={backend}) over {len(groups)} module group(s) on mesh {tp_mesh}.") from torch.distributed.tensor.parallel import parallelize_module diff --git a/src/diffusers/models/_modeling_parallel.py b/src/diffusers/models/_modeling_parallel.py index 8fbdb392f179..35b8fe3e0950 100644 --- a/src/diffusers/models/_modeling_parallel.py +++ b/src/diffusers/models/_modeling_parallel.py @@ -293,7 +293,7 @@ class Conditional: A plan entry must resolve to a module, so a typo in a key raises. Wrap the value of an entry in `Conditional` when its module depends on the config or the checkpoint. The entry is skipped wherever the module is missing or set to - `None`, and a warning is logged if it matches no module at all. + `None`, and a debug message is logged if it matches no module at all. Args: value: diff --git a/tests/models/test_parallelism_conditional_layers.py b/tests/models/test_parallelism_conditional_layers.py index 0fb52aa14e61..eb5c8e1854ba 100644 --- a/tests/models/test_parallelism_conditional_layers.py +++ b/tests/models/test_parallelism_conditional_layers.py @@ -57,7 +57,10 @@ def _conditional_plan_worker(rank, world_size, master_port, checkpoint_dir, para else: parallel_config = ContextParallelConfig(ulysses_degree=world_size) - with CaptureLogger(logging.get_logger("diffusers.hooks")) as cap_logger: + # 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. @@ -91,7 +94,7 @@ def _conditional_plan_worker(rank, world_size, master_port, checkpoint_dir, para class TestConditionalPlanEntries: """`Conditional` entries of a `_tp_plan` / `_cp_plan`, on a checkpoint with and without the conditional layers.""" - unmatched_warning = "match no module" + 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.""" @@ -128,10 +131,9 @@ def test_tensor_parallel(self, tmp_path, 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_warning not in result["logs"] + assert self.unmatched_message not in result["logs"] else: - assert self.unmatched_warning in result["logs"] - assert "blocks.*.gate_proj" in result["logs"] + assert f"{self.unmatched_message}: ['blocks.*.gate_proj']" in result["logs"] @is_context_parallel @pytest.mark.parametrize("use_conditional_layers", [True, False]) @@ -140,7 +142,6 @@ def test_context_parallel(self, tmp_path, use_conditional_layers): if use_conditional_layers: assert "pos_embed" in result["applied"] - assert self.unmatched_warning not in result["logs"] + assert self.unmatched_message not in result["logs"] else: - assert self.unmatched_warning in result["logs"] - assert "pos_embed" in result["logs"] + assert f"{self.unmatched_message}: ['pos_embed']" in result["logs"]