[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
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user