[Diffusion][Attention] Support per-role attention backend via CLI (#2681)
Signed-off-by: gcanlin <canlinguosdu@gmail.com> Signed-off-by: Hongsheng Liu <liuhongsheng4@huawei.com> Co-authored-by: Hongsheng Liu <liuhongsheng4@huawei.com>
This commit is contained in:
1 parent
26d481fc84
commit
11c4c7f0ff
29 files changed
+1262
-106
No files matched your search
@@ -2,8 +2,8 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass
|
||||
from typing import Generic, TypeVar
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Generic, TypeVar
|
||||
|
||||
import torch
|
||||
|
||||
@@ -64,6 +64,9 @@ class AttentionMetadata:
|
||||
# a replicated tensor among processes appended to the front or rear of value, depends the joint_strategy
|
||||
joint_strategy: str = "front"
|
||||
# the strategy to joint the query, key, and value, can be "front" or "rear"
|
||||
extra: dict[str, Any] = field(default_factory=dict)
|
||||
# Opaque backend-specific per-forward parameters (e.g. block masks, KV indices).
|
||||
# Backends MUST silently ignore unknown keys.
|
||||
|
||||
|
||||
T = TypeVar("T", bound=AttentionMetadata)
|
||||
@@ -79,6 +82,8 @@ class AttentionImpl(ABC, Generic[T]):
|
||||
causal: bool = False,
|
||||
num_kv_heads: int | None = None,
|
||||
prefix: str = "",
|
||||
qkv_layout: str | None = None,
|
||||
backend_kwargs: dict[str, Any] | None = None,
|
||||
**extra_impl_args,
|
||||
) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -43,11 +43,16 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
causal: bool = False,
|
||||
num_kv_heads: int | None = None,
|
||||
prefix: str = "",
|
||||
qkv_layout: str | None = None,
|
||||
backend_kwargs: dict | None = None,
|
||||
**extra_impl_args,
|
||||
) -> None:
|
||||
self.num_heads = num_heads
|
||||
self.causal = causal
|
||||
self.softmax_scale = softmax_scale
|
||||
self.qkv_layout = qkv_layout
|
||||
if backend_kwargs:
|
||||
logger.warning("FlashAttentionImpl ignoring backend_kwargs: %s", list(backend_kwargs.keys()))
|
||||
|
||||
@staticmethod
|
||||
def _unwrap_flash_output(out: torch.Tensor | tuple[torch.Tensor, ...]) -> torch.Tensor:
|
||||
@@ -230,6 +235,7 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
)
|
||||
|
||||
attention_mask = attn_metadata.attn_mask if attn_metadata else None
|
||||
layout = self.qkv_layout or "BNSD"
|
||||
output = attention_forward(
|
||||
query,
|
||||
key,
|
||||
@@ -237,6 +243,6 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
attn_mask=attention_mask,
|
||||
opt_mode="manual",
|
||||
op_type="fused_attn_score",
|
||||
layout="BNSD",
|
||||
layout=layout,
|
||||
)
|
||||
return output
|
||||
@@ -49,10 +49,13 @@ class SageAttentionImpl(AttentionImpl):
|
||||
causal: bool = False,
|
||||
num_kv_heads: int | None = None,
|
||||
prefix: str = "",
|
||||
backend_kwargs: dict | None = None,
|
||||
**extra_impl_args,
|
||||
) -> None:
|
||||
self.causal = causal
|
||||
self.softmax_scale = softmax_scale
|
||||
if backend_kwargs:
|
||||
logger.warning("SageAttentionImpl ignoring backend_kwargs: %s", list(backend_kwargs.keys()))
|
||||
|
||||
def forward_cuda(
|
||||
self,
|
||||
|
||||
@@ -84,10 +84,13 @@ class SDPAImpl(AttentionImpl):
|
||||
causal: bool = False,
|
||||
num_kv_heads: int | None = None,
|
||||
prefix: str = "",
|
||||
backend_kwargs: dict | None = None,
|
||||
**extra_impl_args,
|
||||
) -> None:
|
||||
self.causal = causal
|
||||
self.softmax_scale = softmax_scale
|
||||
if backend_kwargs:
|
||||
logger.warning("SDPAImpl ignoring backend_kwargs: %s", list(backend_kwargs.keys()))
|
||||
|
||||
def _forward_impl(
|
||||
self,
|
||||
|
||||
@@ -16,7 +16,8 @@ from vllm_omni.diffusion.attention.backends.sdpa import SDPABackend
|
||||
from vllm_omni.diffusion.attention.parallel import build_parallel_attention_strategy
|
||||
from vllm_omni.diffusion.attention.parallel.base import NoParallelAttention
|
||||
from vllm_omni.diffusion.attention.parallel.ring import RingParallelAttention
|
||||
from vllm_omni.diffusion.attention.selector import get_attn_backend
|
||||
from vllm_omni.diffusion.attention.selector import get_attn_backend_for_role
|
||||
from vllm_omni.diffusion.config import get_current_diffusion_config_or_none
|
||||
from vllm_omni.diffusion.distributed.parallel_state import get_sp_group
|
||||
from vllm_omni.diffusion.forward_context import get_forward_context, is_forward_context_available
|
||||
|
||||
@@ -32,6 +33,11 @@ class Attention(nn.Module):
|
||||
softmax_scale: float,
|
||||
num_kv_heads: int | None = None,
|
||||
prefix: str = "",
|
||||
# Per-role backend selection (RFC: per-role attention backend)
|
||||
role: str = "self",
|
||||
role_category: str | None = None,
|
||||
# Model-defined Q/K/V tensor layout hint for backend execution.
|
||||
qkv_layout: str | None = None,
|
||||
# ulysses attention
|
||||
scatter_idx: int = 2,
|
||||
gather_idx: int = 1,
|
||||
@@ -39,7 +45,34 @@ class Attention(nn.Module):
|
||||
skip_sequence_parallel: bool = False,
|
||||
):
|
||||
super().__init__()
|
||||
self.attn_backend = get_attn_backend(-1)
|
||||
|
||||
self.role = role
|
||||
self.role_category = role_category
|
||||
self.qkv_layout = qkv_layout
|
||||
|
||||
# Resolve backend via role-aware config.
|
||||
# The global diffusion config is set during model init via
|
||||
# set_current_diffusion_config(); no env-var re-parsing needed here.
|
||||
backend_kwargs: dict | None = None
|
||||
self.backend_pref = None
|
||||
|
||||
config = get_current_diffusion_config_or_none()
|
||||
attention_config = config.attention_config if config is not None else None
|
||||
|
||||
attn_backend_cls, spec = get_attn_backend_for_role(
|
||||
role=role,
|
||||
head_size=head_size,
|
||||
attention_config=attention_config,
|
||||
role_category=role_category,
|
||||
)
|
||||
if spec is not None:
|
||||
backend_kwargs = spec.extra or None
|
||||
self.backend_pref = spec.backend
|
||||
logger.debug("Attention(role=%s) → backend=%s", role, spec.backend)
|
||||
else:
|
||||
logger.debug("Attention(role=%s) → platform default", role)
|
||||
|
||||
self.attn_backend = attn_backend_cls
|
||||
self.attn_impl_cls = self.attn_backend.get_impl_cls()
|
||||
self.attention = self.attn_impl_cls(
|
||||
num_heads=num_heads,
|
||||
@@ -47,6 +80,8 @@ class Attention(nn.Module):
|
||||
softmax_scale=softmax_scale,
|
||||
causal=causal,
|
||||
num_kv_heads=num_kv_heads,
|
||||
qkv_layout=qkv_layout,
|
||||
backend_kwargs=backend_kwargs,
|
||||
)
|
||||
# Instantiate fallback backend for float32 support
|
||||
self.sdpa_fallback = SDPABackend.get_impl_cls()(
|
||||
@@ -55,8 +90,8 @@ class Attention(nn.Module):
|
||||
softmax_scale=softmax_scale,
|
||||
causal=causal,
|
||||
num_kv_heads=num_kv_heads,
|
||||
qkv_layout=qkv_layout,
|
||||
)
|
||||
self.backend_pref = None
|
||||
|
||||
self.softmax_scale = softmax_scale
|
||||
self.scatter_idx = scatter_idx
|
||||
@@ -69,21 +104,19 @@ class Attention(nn.Module):
|
||||
self.ring_pg = None
|
||||
self.ring_runner = None
|
||||
|
||||
try:
|
||||
config = get_forward_context().omni_diffusion_config
|
||||
self.backend_pref = config.attention_backend
|
||||
if config is not None:
|
||||
if config.parallel_config.ring_degree > 1:
|
||||
self.use_ring = True
|
||||
try:
|
||||
sp_group = get_sp_group()
|
||||
self.ring_pg = sp_group.ring_group
|
||||
self.ring_runner = RingParallelAttention(sp_group)
|
||||
self.ring_runner = RingParallelAttention(
|
||||
sp_group,
|
||||
attn_backend_pref=self.backend_pref,
|
||||
)
|
||||
except Exception:
|
||||
self.use_ring = False
|
||||
self.ring_runner = None
|
||||
except Exception:
|
||||
self.use_ring = False
|
||||
self.ring_runner = None
|
||||
|
||||
self.parallel_strategy = build_parallel_attention_strategy(
|
||||
scatter_idx=scatter_idx,
|
||||
|
||||
@@ -18,12 +18,11 @@ from vllm_omni.diffusion.attention.parallel.base import (
|
||||
)
|
||||
from vllm_omni.diffusion.distributed.group_coordinator import SequenceParallelGroupCoordinator
|
||||
|
||||
# from vllm_omni.diffusion.attention.backends.ring_selector import AttnType # Already imported above
|
||||
from vllm_omni.diffusion.forward_context import get_forward_context
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm_omni.diffusion.attention.backends.abstract import AttentionMetadata
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class _RingCtx(ParallelAttentionContext):
|
||||
@@ -108,13 +107,8 @@ class RingParallelAttention:
|
||||
softmax_scale = query.shape[-1] ** -0.5
|
||||
|
||||
backend_pref = self.attn_backend_pref
|
||||
if backend_pref is None:
|
||||
try:
|
||||
config = get_forward_context().omni_diffusion_config
|
||||
# config might not have attention_backend attribute if not updated
|
||||
backend_pref = getattr(config, "attention_backend", None)
|
||||
except Exception:
|
||||
backend_pref = None
|
||||
if backend_pref is not None:
|
||||
backend_pref = backend_pref.lower()
|
||||
|
||||
# Determine attention type with fallback chain: FA3 -> FA2 -> SDPA
|
||||
# FP32 is not supported by Flash Attention, force SDPA
|
||||
@@ -122,7 +116,6 @@ class RingParallelAttention:
|
||||
backend_pref = "sdpa"
|
||||
elif not HAS_FA3 and not HAS_FLASH_ATTN:
|
||||
if backend_pref != "sdpa":
|
||||
logger = init_logger(__name__)
|
||||
logger.warning_once("Flash Attention (FA2/FA3) is not available! Force enabling SDPA.")
|
||||
backend_pref = "sdpa"
|
||||
|
||||
@@ -135,7 +128,7 @@ class RingParallelAttention:
|
||||
if attn_metadata.joint_strategy is not None:
|
||||
joint_strategy = attn_metadata.joint_strategy
|
||||
|
||||
if backend_pref == "sdpa" or backend_pref == "torch":
|
||||
if backend_pref in {"sdpa", "torch", "torch_sdpa"}:
|
||||
from vllm_omni.diffusion.attention.backends.ring_pytorch_attn import ring_pytorch_attn_func
|
||||
|
||||
return ring_pytorch_attn_func(
|
||||
|
||||
@@ -3,23 +3,16 @@
|
||||
"""
|
||||
Diffusion attention backend selector.
|
||||
|
||||
This module provides the interface for selecting diffusion attention backends.
|
||||
The actual backend selection logic is delegated to the platform layer
|
||||
(vllm_omni.platforms), similar to how vLLM handles attention backend selection.
|
||||
|
||||
Usage:
|
||||
from vllm_omni.diffusion.attention.selector import get_attn_backend
|
||||
|
||||
# Get the appropriate backend for current platform
|
||||
backend_cls = get_attn_backend(head_size=64)
|
||||
|
||||
# Or override via environment variable
|
||||
# export DIFFUSION_ATTENTION_BACKEND=FLASH_ATTN
|
||||
This module resolves diffusion attention backends from:
|
||||
1. per-role AttentionConfig
|
||||
2. platform default
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import os
|
||||
from functools import cache
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from vllm.logger import init_logger
|
||||
|
||||
@@ -27,6 +20,9 @@ from vllm_omni.diffusion.attention.backends.abstract import (
|
||||
AttentionBackend,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm_omni.diffusion.data import AttentionConfig, AttentionSpec
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
@@ -52,34 +48,103 @@ def _load_backend_cls(cls_path: str) -> type[AttentionBackend]:
|
||||
|
||||
|
||||
@cache
|
||||
def get_attn_backend(head_size: int) -> type[AttentionBackend]:
|
||||
"""
|
||||
Get attention backend for diffusion models.
|
||||
def _cached_get_backend_cls(
|
||||
backend_name: str | None,
|
||||
head_size: int,
|
||||
) -> type[AttentionBackend]:
|
||||
"""Cache backend class resolution by (backend_name, head_size).
|
||||
|
||||
The backend selection is delegated to the current platform
|
||||
(vllm_omni.platforms.current_omni_platform), which selects the
|
||||
appropriate backend based on:
|
||||
1. User override via DIFFUSION_ATTENTION_BACKEND environment variable
|
||||
2. Platform-specific defaults and capabilities
|
||||
|
||||
This is similar to how vLLM's get_attn_backend_cls works, where the
|
||||
platform layer decides which backend to use based on hardware capabilities.
|
||||
|
||||
Args:
|
||||
head_size: Head size for attention computation (may affect backend selection)
|
||||
|
||||
Returns:
|
||||
The selected attention backend class
|
||||
This ensures platform validation (compute capability checks, package
|
||||
availability, etc.) runs only once per unique (backend_name, head_size)
|
||||
combination, avoiding repeated log messages.
|
||||
"""
|
||||
from vllm_omni.platforms import current_omni_platform
|
||||
|
||||
# Check environment variable for user override
|
||||
selected_backend = os.environ.get("DIFFUSION_ATTENTION_BACKEND")
|
||||
|
||||
# Delegate to platform for backend selection
|
||||
backend_cls_path = current_omni_platform.get_diffusion_attn_backend_cls(
|
||||
selected_backend=selected_backend,
|
||||
selected_backend=backend_name,
|
||||
head_size=head_size,
|
||||
)
|
||||
|
||||
return _load_backend_cls(backend_cls_path)
|
||||
|
||||
|
||||
@cache
|
||||
def _log_backend_resolution(
|
||||
role: str,
|
||||
role_category: str | None,
|
||||
backend_name: str,
|
||||
source: str,
|
||||
) -> None:
|
||||
if role_category is not None:
|
||||
logger.info(
|
||||
"Resolved diffusion attention backend '%s' for role=%r (role_category=%r) via %s",
|
||||
backend_name,
|
||||
role,
|
||||
role_category,
|
||||
source,
|
||||
)
|
||||
return
|
||||
|
||||
logger.info(
|
||||
"Resolved diffusion attention backend '%s' for role=%r via %s",
|
||||
backend_name,
|
||||
role,
|
||||
source,
|
||||
)
|
||||
|
||||
|
||||
def get_attn_backend_for_role(
|
||||
role: str,
|
||||
head_size: int,
|
||||
attention_config: AttentionConfig | None = None,
|
||||
role_category: str | None = None,
|
||||
) -> tuple[type[AttentionBackend], AttentionSpec | None]:
|
||||
"""
|
||||
Get attention backend for a specific attention role.
|
||||
|
||||
Lookup precedence:
|
||||
1. attention_config.per_role[role] — exact match
|
||||
2. attention_config.per_role[role_category] — category fallback
|
||||
3. attention_config.default — global default
|
||||
4. Platform default — hardware-specific
|
||||
|
||||
Args:
|
||||
role: Attention role string (e.g. "self", "cross", "joint",
|
||||
"ltx2.audio_to_video")
|
||||
head_size: Head size for attention computation
|
||||
attention_config: The AttentionConfig from OmniDiffusionConfig.
|
||||
If None, falls back to platform default behavior.
|
||||
role_category: Optional category for fallback (e.g. "cross" for
|
||||
"ltx2.audio_to_video")
|
||||
|
||||
Returns:
|
||||
Tuple of (backend_class, AttentionSpec or None).
|
||||
AttentionSpec is None when using platform default without explicit config.
|
||||
"""
|
||||
# 1. Try config from OmniDiffusionConfig
|
||||
spec = None
|
||||
source = None
|
||||
if attention_config is not None:
|
||||
spec, source = attention_config.resolve_with_source(
|
||||
role=role,
|
||||
role_category=role_category,
|
||||
)
|
||||
|
||||
if spec is not None:
|
||||
backend_cls = _cached_get_backend_cls(spec.backend, head_size)
|
||||
_log_backend_resolution(
|
||||
role=role,
|
||||
role_category=role_category,
|
||||
backend_name=backend_cls.get_name(),
|
||||
source=source or "attention_config",
|
||||
)
|
||||
return backend_cls, spec
|
||||
|
||||
# 2. Platform default
|
||||
backend_cls = _cached_get_backend_cls(None, head_size)
|
||||
_log_backend_resolution(
|
||||
role=role,
|
||||
role_category=role_category,
|
||||
backend_name=backend_cls.get_name(),
|
||||
source="platform default",
|
||||
)
|
||||
return backend_cls, None
|
||||
@@ -0,0 +1,43 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Global diffusion config — mirrors vLLM's set_current_vllm_config pattern.
|
||||
|
||||
Set once during model initialisation so that every ``Attention`` layer can
|
||||
read the ``OmniDiffusionConfig`` without coupling ``__init__`` to the
|
||||
runtime ``ForwardContext``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import contextmanager
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm_omni.diffusion.data import OmniDiffusionConfig
|
||||
|
||||
_current_diffusion_config: OmniDiffusionConfig | None = None
|
||||
|
||||
|
||||
@contextmanager
|
||||
def set_current_diffusion_config(config: OmniDiffusionConfig):
|
||||
"""Context manager that sets the global diffusion config for model init."""
|
||||
global _current_diffusion_config
|
||||
old = _current_diffusion_config
|
||||
_current_diffusion_config = config
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
_current_diffusion_config = old
|
||||
|
||||
|
||||
def get_current_diffusion_config() -> OmniDiffusionConfig:
|
||||
"""Return the current diffusion config or raise."""
|
||||
assert _current_diffusion_config is not None, (
|
||||
"Diffusion config is not set. Wrap model construction with set_current_diffusion_config()."
|
||||
)
|
||||
return _current_diffusion_config
|
||||
|
||||
|
||||
def get_current_diffusion_config_or_none() -> OmniDiffusionConfig | None:
|
||||
"""Return the current diffusion config, or ``None`` if not set."""
|
||||
return _current_diffusion_config
|
||||
+206
-14
@@ -1,7 +1,7 @@
|
||||
# adapted from sglang and fastvideo
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
import enum
|
||||
import copy
|
||||
import os
|
||||
import random
|
||||
from collections.abc import Callable, Mapping
|
||||
@@ -367,7 +367,7 @@ class OmniDiffusionConfig:
|
||||
tf_model_config: TransformerConfig = field(default_factory=TransformerConfig)
|
||||
|
||||
# Attention
|
||||
attention_backend: str | None = None
|
||||
attention_config: "AttentionConfig" = field(default_factory=lambda: AttentionConfig())
|
||||
|
||||
# Running mode
|
||||
# mode: ExecutionMode = ExecutionMode.INFERENCE
|
||||
@@ -653,6 +653,10 @@ class OmniDiffusionConfig:
|
||||
f"got {type(self.quantization_config)!r}"
|
||||
)
|
||||
|
||||
# Match vLLM's config flow: parse entrypoint shorthands before the
|
||||
# config object is built, and keep a single runtime truth source.
|
||||
self.attention_config = build_attention_config(self.attention_config)
|
||||
|
||||
if self.max_cpu_loras is None:
|
||||
self.max_cpu_loras = 1
|
||||
elif self.max_cpu_loras < 1:
|
||||
@@ -794,6 +798,24 @@ class OmniDiffusionConfig:
|
||||
else:
|
||||
kwargs.pop("quantization", None)
|
||||
|
||||
# Map "diffusion_attention_config" to "attention_config" so the
|
||||
# engine_args key (namespaced to avoid collision with vLLM's own
|
||||
# attention_config) is mapped to the dataclass field.
|
||||
if "diffusion_attention_config" in kwargs and "attention_config" not in kwargs:
|
||||
kwargs["attention_config"] = kwargs.pop("diffusion_attention_config")
|
||||
else:
|
||||
kwargs.pop("diffusion_attention_config", None)
|
||||
|
||||
# Handle "diffusion_attention_backend" shorthand: merge into
|
||||
# attention_config before field filtering.
|
||||
diffusion_attn_backend = kwargs.pop("diffusion_attention_backend", None)
|
||||
if diffusion_attn_backend is not None:
|
||||
existing = kwargs.get("attention_config")
|
||||
kwargs["attention_config"] = parse_attention_config(
|
||||
existing,
|
||||
attention_backend=diffusion_attn_backend,
|
||||
)
|
||||
|
||||
# Check environment variable as fallback for cache_backend
|
||||
# Support both old DIFFUSION_CACHE_ADAPTER and new DIFFUSION_CACHE_BACKEND for backwards compatibility
|
||||
if "cache_backend" not in kwargs:
|
||||
@@ -848,19 +870,189 @@ class DiffusionRequestAbortedError(RuntimeError):
|
||||
"""Raised when a diffusion request ends via user-visible abort."""
|
||||
|
||||
|
||||
class AttentionBackendEnum(enum.Enum):
|
||||
FA = enum.auto()
|
||||
SLIDING_TILE_ATTN = enum.auto()
|
||||
TORCH_SDPA = enum.auto()
|
||||
SAGE_ATTN = enum.auto()
|
||||
SAGE_ATTN_THREE = enum.auto()
|
||||
VIDEO_SPARSE_ATTN = enum.auto()
|
||||
VMOBA_ATTN = enum.auto()
|
||||
AITER = enum.auto()
|
||||
NO_ATTENTION = enum.auto()
|
||||
@dataclass
|
||||
class AttentionSpec:
|
||||
"""Specifies a backend and its backend-specific parameters for one attention role."""
|
||||
|
||||
def __str__(self):
|
||||
return self.name.lower()
|
||||
backend: str # registry name, e.g. "FLASH_ATTN"
|
||||
extra: dict[str, Any] = field(default_factory=dict) # backend-specific kwargs
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.backend, str):
|
||||
raise TypeError(f"Expected str for AttentionSpec.backend, got {type(self.backend)!r}")
|
||||
|
||||
if self.extra is None:
|
||||
self.extra = {}
|
||||
elif isinstance(self.extra, Mapping):
|
||||
self.extra = dict(self.extra)
|
||||
else:
|
||||
raise TypeError(f"Expected dict for AttentionSpec.extra, got {type(self.extra)!r}")
|
||||
|
||||
|
||||
@dataclass
|
||||
class AttentionConfig:
|
||||
"""Per-role attention backend configuration.
|
||||
|
||||
Lookup precedence for a given (role, role_category):
|
||||
1. per_role[role] — exact match
|
||||
2. per_role[role_category] — category fallback (e.g. "ltx2.audio_to_video" → "cross")
|
||||
3. default — global default
|
||||
4. platform default — unchanged platform logic
|
||||
"""
|
||||
|
||||
default: AttentionSpec | None = None
|
||||
per_role: dict[str, AttentionSpec] = field(default_factory=dict)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if self.default is not None:
|
||||
self.default = self._coerce_spec_or_none(self.default, "default")
|
||||
|
||||
normalized_per_role: dict[str, AttentionSpec] = {}
|
||||
for role_key, spec_data in self._normalize_per_role_mapping(self.per_role).items():
|
||||
spec = self._coerce_spec_or_none(spec_data, f"per_role[{role_key!r}]")
|
||||
if spec is not None:
|
||||
normalized_per_role[role_key] = spec
|
||||
self.per_role = normalized_per_role
|
||||
|
||||
@staticmethod
|
||||
def _coerce_spec(spec_data: Any, field_name: str) -> AttentionSpec:
|
||||
if isinstance(spec_data, AttentionSpec):
|
||||
return spec_data
|
||||
if isinstance(spec_data, str):
|
||||
return AttentionSpec(backend=spec_data)
|
||||
if isinstance(spec_data, Mapping):
|
||||
return AttentionSpec(**dict(spec_data))
|
||||
raise TypeError(f"Expected str, dict, or AttentionSpec for {field_name}, got {type(spec_data)!r}")
|
||||
|
||||
@classmethod
|
||||
def _coerce_spec_or_none(cls, spec_data: Any, field_name: str) -> AttentionSpec | None:
|
||||
spec = cls._coerce_spec(spec_data, field_name)
|
||||
if spec.backend.lower() == "auto":
|
||||
return None
|
||||
return spec
|
||||
|
||||
@classmethod
|
||||
def _normalize_per_role_mapping(cls, raw_per_role: Any) -> dict[str, Any]:
|
||||
if raw_per_role is None:
|
||||
return {}
|
||||
if not isinstance(raw_per_role, Mapping):
|
||||
raise TypeError(f"Expected dict for AttentionConfig.per_role, got {type(raw_per_role)!r}")
|
||||
|
||||
normalized: dict[str, Any] = {}
|
||||
for role_key, spec_data in raw_per_role.items():
|
||||
cls._flatten_per_role_entry([role_key], spec_data, normalized)
|
||||
return normalized
|
||||
|
||||
@classmethod
|
||||
def _flatten_per_role_entry(
|
||||
cls,
|
||||
path: list[str],
|
||||
node: Any,
|
||||
normalized: dict[str, Any],
|
||||
) -> None:
|
||||
role = ".".join(path)
|
||||
if not isinstance(node, Mapping):
|
||||
normalized[role] = node
|
||||
return
|
||||
|
||||
spec_keys = {"backend", "extra"}
|
||||
node_dict = dict(node)
|
||||
node_keys = set(node_dict)
|
||||
if node_keys & spec_keys:
|
||||
if not node_keys <= spec_keys:
|
||||
raise ValueError(
|
||||
f"Invalid per_role entry for role {role!r}: cannot mix backend/extra with nested role keys."
|
||||
)
|
||||
normalized[role] = node_dict
|
||||
return
|
||||
|
||||
if not node_dict:
|
||||
raise ValueError(f"Empty per_role entry for role {role!r}")
|
||||
|
||||
for child_key, child_value in node_dict.items():
|
||||
cls._flatten_per_role_entry([*path, child_key], child_value, normalized)
|
||||
|
||||
def resolve_with_source(
|
||||
self,
|
||||
role: str = "self",
|
||||
role_category: str | None = None,
|
||||
) -> tuple[AttentionSpec | None, str | None]:
|
||||
"""Resolve the AttentionSpec and report which config entry matched."""
|
||||
spec = self.per_role.get(role)
|
||||
if spec is not None:
|
||||
return spec, f"attention_config.per_role[{role!r}]"
|
||||
if role_category is not None:
|
||||
spec = self.per_role.get(role_category)
|
||||
if spec is not None:
|
||||
return spec, f"attention_config.per_role[{role_category!r}] (role_category fallback)"
|
||||
if self.default is not None:
|
||||
return self.default, "attention_config.default"
|
||||
return None, None
|
||||
|
||||
|
||||
def parse_attention_config(
|
||||
attention_config: AttentionConfig | Mapping[str, Any] | None = None,
|
||||
*,
|
||||
attention_backend: str | None = None,
|
||||
) -> AttentionConfig:
|
||||
"""Pure type-conversion: coerce *attention_config* to an AttentionConfig.
|
||||
|
||||
Optionally merges an ``attention_backend`` shorthand into the config's
|
||||
``default`` field. This does **not** read environment variables —
|
||||
use :func:`build_attention_config` for the full normalisation that
|
||||
should happen exactly once in ``OmniDiffusionConfig.__post_init__``.
|
||||
"""
|
||||
if attention_config is None:
|
||||
normalized = AttentionConfig()
|
||||
elif isinstance(attention_config, AttentionConfig):
|
||||
normalized = copy.deepcopy(attention_config)
|
||||
elif isinstance(attention_config, Mapping):
|
||||
normalized = AttentionConfig(**dict(attention_config))
|
||||
else:
|
||||
raise TypeError(
|
||||
f"attention_config must be an AttentionConfig, mapping, or None; got {type(attention_config)!r}"
|
||||
)
|
||||
|
||||
if attention_backend is not None:
|
||||
if normalized.default is not None:
|
||||
raise ValueError(
|
||||
"--diffusion-attention-backend is mutually exclusive with --diffusion-attention-config.default.backend."
|
||||
)
|
||||
if attention_backend.lower() != "auto":
|
||||
normalized.default = AttentionSpec(backend=attention_backend)
|
||||
|
||||
return normalized
|
||||
|
||||
|
||||
def build_attention_config(
|
||||
attention_config: AttentionConfig | Mapping[str, Any] | None = None,
|
||||
) -> AttentionConfig:
|
||||
"""Normalize diffusion attention config — the single authoritative entry point.
|
||||
|
||||
Called exactly once in ``OmniDiffusionConfig.__post_init__``.
|
||||
Handles type-conversion **and** env-var fallback
|
||||
(``DIFFUSION_ATTENTION_BACKEND``).
|
||||
"""
|
||||
normalized = parse_attention_config(attention_config)
|
||||
|
||||
if normalized.default is not None:
|
||||
return normalized
|
||||
|
||||
env_attention_backend = os.environ.get("DIFFUSION_ATTENTION_BACKEND")
|
||||
if env_attention_backend is None:
|
||||
return normalized
|
||||
|
||||
if env_attention_backend.lower() == "auto":
|
||||
return normalized
|
||||
|
||||
normalized.default = AttentionSpec(backend=env_attention_backend)
|
||||
logger.info(
|
||||
"Parsed attention config from DIFFUSION_ATTENTION_BACKEND '%s': default=%s, per_role=%s",
|
||||
env_attention_backend,
|
||||
normalized.default,
|
||||
{k: v.backend for k, v in normalized.per_role.items()},
|
||||
)
|
||||
return normalized
|
||||
|
||||
|
||||
@dataclass
|
||||
|
||||
@@ -375,7 +375,7 @@ class SequenceParallelSplitHook(ModelHook):
|
||||
2. Creates an attention mask indicating valid vs padding positions
|
||||
3. Stores the mask and padding info in ForwardContext
|
||||
"""
|
||||
from vllm_omni.diffusion.attention.selector import get_attn_backend
|
||||
from vllm_omni.diffusion.attention.selector import get_attn_backend_for_role
|
||||
from vllm_omni.diffusion.distributed.parallel_state import (
|
||||
get_ring_parallel_world_size,
|
||||
get_sequence_parallel_rank,
|
||||
@@ -395,7 +395,17 @@ class SequenceParallelSplitHook(ModelHook):
|
||||
return sp_shard(x, dim, validate=False)
|
||||
|
||||
# Check backend compatibility
|
||||
attn_backend = get_attn_backend(-1)
|
||||
attention_config = None
|
||||
if is_forward_context_available():
|
||||
od_config = get_forward_context().omni_diffusion_config
|
||||
if od_config is not None:
|
||||
attention_config = od_config.attention_config
|
||||
|
||||
attn_backend, _ = get_attn_backend_for_role(
|
||||
role="self",
|
||||
head_size=-1,
|
||||
attention_config=attention_config,
|
||||
)
|
||||
if not attn_backend.supports_attention_mask:
|
||||
raise ValueError(
|
||||
f"Sequence length ({seq_len}) is not divisible by SP world size ({world_size}). "
|
||||
|
||||
@@ -319,8 +319,11 @@ class DiffusersPipelineLoader:
|
||||
elif load_format == "diffusers":
|
||||
model = DiffusersAdapterPipeline(od_config=od_config, device=target_device)
|
||||
elif load_format == "custom_pipeline":
|
||||
from vllm_omni.diffusion.config import set_current_diffusion_config
|
||||
|
||||
model_cls = resolve_obj_by_qualname(custom_pipeline_name)
|
||||
model = model_cls(od_config=od_config)
|
||||
with set_current_diffusion_config(od_config):
|
||||
model = model_cls(od_config=od_config)
|
||||
else:
|
||||
raise ValueError(f"Unknown load_format: {load_format}")
|
||||
logger.debug("Loading weights on %s ...", load_device)
|
||||
@@ -588,8 +591,11 @@ class DiffusersPipelineLoader:
|
||||
if load_format == "default":
|
||||
model = initialize_model(od_config)
|
||||
elif load_format == "custom_pipeline":
|
||||
from vllm_omni.diffusion.config import set_current_diffusion_config
|
||||
|
||||
model_cls = resolve_obj_by_qualname(custom_pipeline_name)
|
||||
model = model_cls(od_config=od_config)
|
||||
with set_current_diffusion_config(od_config):
|
||||
model = model_cls(od_config=od_config)
|
||||
self.load_weights(model)
|
||||
|
||||
# Collect all transformers to shard (some models have transformer_2 for MoE)
|
||||
|
||||
@@ -15,7 +15,6 @@ It does NOT support:
|
||||
|
||||
import inspect
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from typing import Any, cast
|
||||
|
||||
@@ -211,7 +210,8 @@ class DiffusersAdapterPipeline(nn.Module, DiffusionPipelineProfilerMixin):
|
||||
logging.info("No transformer found in diffusers pipeline. Skipping attention backend setting.")
|
||||
return
|
||||
|
||||
attention_backend_config = self.od_config.attention_backend or os.environ.get("DIFFUSION_ATTENTION_BACKEND")
|
||||
default_spec = self.od_config.attention_config.default
|
||||
attention_backend_config = default_spec.backend if default_spec is not None else None
|
||||
attention_backend_attempts: list[str] = []
|
||||
match attention_backend_config:
|
||||
case "FLASH_ATTN" | None:
|
||||
|
||||
@@ -373,6 +373,7 @@ class WanSelfAttention(nn.Module):
|
||||
num_kv_heads=self.num_kv_heads,
|
||||
softmax_scale=1.0 / (head_dim**0.5),
|
||||
causal=False,
|
||||
role="self",
|
||||
)
|
||||
|
||||
def forward(
|
||||
@@ -525,6 +526,8 @@ class WanCrossAttention(nn.Module):
|
||||
num_kv_heads=self.num_heads,
|
||||
softmax_scale=1.0 / (head_dim**0.5),
|
||||
causal=False,
|
||||
role="cross",
|
||||
qkv_layout="BSND",
|
||||
skip_sequence_parallel=True,
|
||||
)
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ from vllm.logger import init_logger
|
||||
from vllm.model_executor.model_loader.utils import configure_quant_config
|
||||
from vllm.model_executor.models.registry import _LazyRegisteredModel, _ModelRegistry
|
||||
|
||||
from vllm_omni.diffusion.config import set_current_diffusion_config
|
||||
from vllm_omni.diffusion.data import OmniDiffusionConfig
|
||||
from vllm_omni.diffusion.distributed.autoencoders.distributed_vae_executor import DistributedVaeMixin
|
||||
from vllm_omni.diffusion.distributed.sp_plan import SequenceParallelConfig, get_sp_plan_from_model
|
||||
@@ -280,7 +281,7 @@ def _prepare_diffusion_quant_config(
|
||||
model_class: type[nn.Module],
|
||||
) -> None:
|
||||
"""Prepare diffusion quant config using vLLM-style model bindings."""
|
||||
quant_config = od_config.quantization_config
|
||||
quant_config = getattr(od_config, "quantization_config", None)
|
||||
if quant_config is None:
|
||||
return
|
||||
if hasattr(quant_config, "maybe_update_config"):
|
||||
@@ -314,7 +315,8 @@ def initialize_model(
|
||||
model_class = DiffusionModelRegistry._try_load_model_cls(od_config.model_class_name)
|
||||
if model_class is not None:
|
||||
_prepare_diffusion_quant_config(od_config, model_class)
|
||||
model = model_class(od_config=od_config)
|
||||
with set_current_diffusion_config(od_config):
|
||||
model = model_class(od_config=od_config)
|
||||
|
||||
vae_pp_size = od_config.parallel_config.vae_patch_parallel_size
|
||||
is_distributed_vae = hasattr(model, "vae") and isinstance(model.vae, DistributedVaeMixin)
|
||||
|
||||
Reference in new issue
Block a user