[Diffusion] Non-Intrusive Sequence Parallelism (SP) Model Support Abstraction for vLLM-Omni Framework (#779)
Signed-off-by: mxuax <mxuax@connect.ust.hk> Signed-off-by: XU Mingshi <91017482+mxuax@users.noreply.github.com>
This commit is contained in:
1 parent
334d306d9a
commit
25b08b3371
17 files changed
+3369
-479
No files matched your search
@@ -3,9 +3,15 @@
|
||||
|
||||
import importlib
|
||||
|
||||
import torch.nn as nn
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor.models.registry import _LazyRegisteredModel, _ModelRegistry
|
||||
|
||||
from vllm_omni.diffusion.data import OmniDiffusionConfig
|
||||
from vllm_omni.diffusion.distributed.sp_plan import SequenceParallelConfig, get_sp_plan_from_model
|
||||
from vllm_omni.diffusion.hooks.sequence_parallel import apply_sequence_parallel
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
_DIFFUSION_MODELS = {
|
||||
# arch:(mod_folder, mod_relname, cls_name)
|
||||
@@ -100,7 +106,24 @@ DiffusionModelRegistry = _ModelRegistry(
|
||||
|
||||
def initialize_model(
|
||||
od_config: OmniDiffusionConfig,
|
||||
):
|
||||
) -> nn.Module:
|
||||
"""Initialize a diffusion model from the registry.
|
||||
|
||||
This function:
|
||||
1. Loads the model class from the registry
|
||||
2. Instantiates the model with the config
|
||||
3. Configures VAE optimization settings
|
||||
4. Applies sequence parallelism if enabled (similar to diffusers' enable_parallelism)
|
||||
|
||||
Args:
|
||||
od_config: The OmniDiffusion configuration.
|
||||
|
||||
Returns:
|
||||
The initialized pipeline model.
|
||||
|
||||
Raises:
|
||||
ValueError: If the model class is not found in the registry.
|
||||
"""
|
||||
model_class = DiffusionModelRegistry._try_load_model_cls(od_config.model_class_name)
|
||||
if model_class is not None:
|
||||
model = model_class(od_config=od_config)
|
||||
@@ -110,11 +133,73 @@ def initialize_model(
|
||||
if hasattr(model.vae, "use_tiling"):
|
||||
model.vae.use_tiling = od_config.vae_use_tiling
|
||||
|
||||
# Apply sequence parallelism if enabled
|
||||
# This follows diffusers' pattern where enable_parallelism() is called
|
||||
# at model loading time, not inside individual model files
|
||||
_apply_sequence_parallel_if_enabled(model, od_config)
|
||||
|
||||
return model
|
||||
else:
|
||||
raise ValueError(f"Model class {od_config.model_class_name} not found in diffusion model registry.")
|
||||
|
||||
|
||||
def _apply_sequence_parallel_if_enabled(model, od_config: OmniDiffusionConfig) -> None:
|
||||
"""Apply sequence parallelism hooks if SP is enabled.
|
||||
|
||||
This is the centralized location for enabling SP, similar to diffusers'
|
||||
ModelMixin.enable_parallelism() method. It applies _sp_plan hooks to
|
||||
transformer models that define them.
|
||||
|
||||
Note: Our "Sequence Parallelism" (SP) corresponds to "Context Parallelism" (CP) in diffusers.
|
||||
We use _sp_plan instead of diffusers' _cp_plan.
|
||||
|
||||
Args:
|
||||
model: The pipeline model (e.g., ZImagePipeline).
|
||||
od_config: The OmniDiffusion configuration.
|
||||
"""
|
||||
|
||||
try:
|
||||
sp_size = od_config.parallel_config.sequence_parallel_size
|
||||
if sp_size <= 1:
|
||||
return
|
||||
|
||||
# Find transformer model(s) in the pipeline that have _sp_plan
|
||||
transformer_attrs = ["transformer", "dit", "unet"]
|
||||
for attr in transformer_attrs:
|
||||
if not hasattr(model, attr):
|
||||
continue
|
||||
|
||||
transformer = getattr(model, attr)
|
||||
if transformer is None:
|
||||
continue
|
||||
|
||||
plan = get_sp_plan_from_model(transformer)
|
||||
if plan is None:
|
||||
continue
|
||||
|
||||
# Create SP config
|
||||
sp_config = SequenceParallelConfig(
|
||||
ulysses_degree=od_config.parallel_config.ulysses_degree,
|
||||
ring_degree=od_config.parallel_config.ring_degree,
|
||||
)
|
||||
|
||||
# Apply hooks according to the plan
|
||||
mode = (
|
||||
"hybrid"
|
||||
if sp_config.ulysses_degree > 1 and sp_config.ring_degree > 1
|
||||
else ("ulysses" if sp_config.ulysses_degree > 1 else "ring")
|
||||
)
|
||||
logger.info(
|
||||
f"Applying sequence parallelism to {transformer.__class__.__name__} "
|
||||
f"(sp_size={sp_size}, mode={mode}, ulysses={sp_config.ulysses_degree}, ring={sp_config.ring_degree})"
|
||||
)
|
||||
apply_sequence_parallel(transformer, sp_config, plan)
|
||||
return # Only apply to first transformer found
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to apply sequence parallelism: {e}. Continuing without SP hooks.")
|
||||
|
||||
|
||||
_DIFFUSION_POST_PROCESS_FUNCS = {
|
||||
# arch: post_process_func
|
||||
# `post_process_func` function must be placed in {mod_folder}/{mod_relname}.py,
|
||||
|
||||
Reference in new issue
Block a user