[Feature] Add AllGather-KV sequence-parallel attention str… (#4968)
Signed-off-by: zhuhh97 <zhuhonghan@huawei.com> Signed-off-by: dph97 <nenbaying@163.com> Signed-off-by: zhuhh97 <zhh@unknown> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com> Co-authored-by: dengyunyang <584797741@qq.com>
This commit is contained in:
33 files changed
+1181
-135
No files matched your search
@@ -14,6 +14,7 @@ class AttentionBackend(ABC):
|
||||
"""Abstract class for diffusion attention backends."""
|
||||
|
||||
accept_output_buffer: bool = False
|
||||
supports_piecewise_spans: bool = False
|
||||
|
||||
@classmethod
|
||||
def supports_attention_mask(cls) -> bool:
|
||||
@@ -51,6 +52,13 @@ class AttentionBackend(ABC):
|
||||
return (not supported_head_sizes) or head_size in supported_head_sizes
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class QueryRange:
|
||||
local_start: int
|
||||
local_end: int
|
||||
global_start: int
|
||||
|
||||
|
||||
@dataclass
|
||||
class AttentionMetadata:
|
||||
attn_mask: torch.Tensor | None = None
|
||||
@@ -75,6 +83,7 @@ class AttentionMetadata:
|
||||
# Piecewise attention metadata (mixed causal/full masks).
|
||||
# full_attn_spans: per-sample [start, end) spans in global coordinates using full attention.
|
||||
full_attn_spans: list[list[tuple[int, int]]] | None = None
|
||||
query_ranges: tuple[QueryRange, ...] | None = None
|
||||
|
||||
|
||||
T = TypeVar("T", bound=AttentionMetadata)
|
||||
|
||||
@@ -51,7 +51,6 @@ class CuDNNAttentionImpl(AttentionImpl):
|
||||
) -> None:
|
||||
self.causal = causal
|
||||
self.softmax_scale = softmax_scale
|
||||
self.requires_gqa = num_heads != num_kv_heads
|
||||
|
||||
def forward_cuda(
|
||||
self,
|
||||
@@ -64,6 +63,7 @@ class CuDNNAttentionImpl(AttentionImpl):
|
||||
if attn_metadata:
|
||||
attention_mask = _maybe_reshape_attn_mask(query, key, attn_metadata.attn_mask, mask_mode="broadcast_k")
|
||||
|
||||
enable_gqa = query.shape[2] != key.shape[2]
|
||||
query, key, value = (x.permute(0, 2, 1, 3) for x in (query, key, value))
|
||||
# Pin cuDNN exclusively. A priority list like [CUDNN, FLASH, MATH] hits a
|
||||
# PyTorch SDPA dispatch quirk: when FLASH rejects a non-None attn_mask,
|
||||
@@ -85,7 +85,7 @@ class CuDNNAttentionImpl(AttentionImpl):
|
||||
dropout_p=0.0,
|
||||
is_causal=self.causal,
|
||||
scale=self.softmax_scale,
|
||||
enable_gqa=self.requires_gqa,
|
||||
enable_gqa=enable_gqa,
|
||||
)
|
||||
except RuntimeError as e:
|
||||
if "No available kernel" not in str(e):
|
||||
@@ -102,6 +102,6 @@ class CuDNNAttentionImpl(AttentionImpl):
|
||||
dropout_p=0.0,
|
||||
is_causal=self.causal,
|
||||
scale=self.softmax_scale,
|
||||
enable_gqa=self.requires_gqa,
|
||||
enable_gqa=enable_gqa,
|
||||
)
|
||||
return output.permute(0, 2, 1, 3)
|
||||
@@ -21,6 +21,7 @@ logger = init_logger(__name__)
|
||||
|
||||
class FlashAttentionBackend(AttentionBackend):
|
||||
accept_output_buffer: bool = True
|
||||
supports_piecewise_spans: bool = True
|
||||
|
||||
@classmethod
|
||||
def supports_attention_mask(cls) -> bool:
|
||||
@@ -82,6 +83,28 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
def _flash_wrapper(q, k, v, *, attn_func, **kwargs):
|
||||
return FlashAttentionImpl._unwrap_flash_output(attn_func(q, k, v, **kwargs))
|
||||
|
||||
@staticmethod
|
||||
def _flash_varlen_wrapper(q, k, v, *, attn_func, causal, softmax_scale, **kwargs):
|
||||
"""Call a varlen-only FlashAttention backend for a dense segment."""
|
||||
del kwargs
|
||||
batch_size, q_len = q.shape[:2]
|
||||
k_len = k.shape[1]
|
||||
cu_seqlens_q = torch.arange(0, (batch_size + 1) * q_len, q_len, dtype=torch.int32, device=q.device)
|
||||
cu_seqlens_k = torch.arange(0, (batch_size + 1) * k_len, k_len, dtype=torch.int32, device=q.device)
|
||||
out = attn_func(
|
||||
q=q.flatten(0, 1),
|
||||
k=k.flatten(0, 1),
|
||||
v=v.flatten(0, 1),
|
||||
cu_seqlens_q=cu_seqlens_q,
|
||||
cu_seqlens_k=cu_seqlens_k,
|
||||
max_seqlen_q=q_len,
|
||||
max_seqlen_k=k_len,
|
||||
causal=causal,
|
||||
softmax_scale=softmax_scale,
|
||||
)
|
||||
out = FlashAttentionImpl._unwrap_flash_output(out)
|
||||
return out.reshape(batch_size, q_len, *out.shape[1:])
|
||||
|
||||
def _forward_varlen_masked(
|
||||
self,
|
||||
query: torch.Tensor,
|
||||
@@ -171,6 +194,7 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
from vllm_omni.diffusion.attention.backends.utils.fa import (
|
||||
HAS_FLASH_ATTN,
|
||||
flash_attn_func,
|
||||
flash_attn_varlen_func,
|
||||
)
|
||||
|
||||
if not HAS_FLASH_ATTN:
|
||||
@@ -186,10 +210,18 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
# Try piecewise attention
|
||||
if full_attn_spans is not None:
|
||||
logger.debug("Using piecewise Flash Attention for mixed causal/full mask")
|
||||
attn_func = partial(
|
||||
FlashAttentionImpl._flash_wrapper,
|
||||
attn_func=flash_attn_func,
|
||||
)
|
||||
if flash_attn_func is not None:
|
||||
attn_func = partial(
|
||||
FlashAttentionImpl._flash_wrapper,
|
||||
attn_func=flash_attn_func,
|
||||
)
|
||||
elif flash_attn_varlen_func is not None:
|
||||
attn_func = partial(
|
||||
FlashAttentionImpl._flash_varlen_wrapper,
|
||||
attn_func=flash_attn_varlen_func,
|
||||
)
|
||||
else:
|
||||
raise ImportError("Piecewise FlashAttention requires a dense or varlen FlashAttention function")
|
||||
|
||||
return piecewise_attn(
|
||||
query,
|
||||
@@ -198,6 +230,7 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
full_attn_spans,
|
||||
self.softmax_scale,
|
||||
attn_func,
|
||||
query_ranges=attn_metadata.query_ranges,
|
||||
)
|
||||
|
||||
if attention_mask is not None and torch.any(~attention_mask):
|
||||
|
||||
@@ -12,9 +12,7 @@ from vllm_omni.diffusion.attention.backends.abstract import (
|
||||
AttentionImpl,
|
||||
AttentionMetadata,
|
||||
)
|
||||
from vllm_omni.diffusion.attention.backends.utils.piecewise_attn import (
|
||||
piecewise_attn,
|
||||
)
|
||||
from vllm_omni.diffusion.attention.backends.utils.piecewise_attn import piecewise_attn
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -78,6 +76,7 @@ def _run_varlen_dense(
|
||||
|
||||
class FlashAttentionHubBackend(AttentionBackend):
|
||||
accept_output_buffer: bool = True
|
||||
supports_piecewise_spans: bool = True
|
||||
|
||||
@classmethod
|
||||
def supports_attention_mask(cls) -> bool:
|
||||
@@ -214,6 +213,7 @@ class FlashAttentionHubImpl(AttentionImpl):
|
||||
full_attn_spans,
|
||||
self.softmax_scale,
|
||||
attn_func,
|
||||
query_ranges=attn_metadata.query_ranges,
|
||||
)
|
||||
|
||||
if attention_mask is not None and torch.any(~attention_mask):
|
||||
@@ -243,6 +243,7 @@ class FlashAttentionHubImpl(AttentionImpl):
|
||||
|
||||
class FlashAttention3HubBackend(AttentionBackend):
|
||||
accept_output_buffer: bool = True
|
||||
supports_piecewise_spans: bool = True
|
||||
|
||||
@classmethod
|
||||
def supports_attention_mask(cls) -> bool:
|
||||
@@ -379,6 +380,7 @@ class FlashAttention3HubImpl(AttentionImpl):
|
||||
full_attn_spans,
|
||||
self.softmax_scale,
|
||||
attn_func,
|
||||
query_ranges=attn_metadata.query_ranges,
|
||||
)
|
||||
|
||||
if attention_mask is not None and torch.any(~attention_mask):
|
||||
|
||||
@@ -11,6 +11,7 @@ from vllm_omni.diffusion.attention.backends.abstract import (
|
||||
AttentionImpl,
|
||||
AttentionMetadata,
|
||||
)
|
||||
from vllm_omni.diffusion.attention.backends.utils.attn_runtime_selector import can_sdpa_use_fused_gqa
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -85,7 +86,6 @@ class SDPAImpl(AttentionImpl):
|
||||
) -> None:
|
||||
self.causal = causal
|
||||
self.softmax_scale = softmax_scale
|
||||
self.requires_gqa = num_heads != num_kv_heads
|
||||
if backend_kwargs:
|
||||
logger.warning("SDPAImpl ignoring backend_kwargs: %s", list(backend_kwargs.keys()))
|
||||
|
||||
@@ -103,7 +103,24 @@ class SDPAImpl(AttentionImpl):
|
||||
if attn_metadata:
|
||||
attention_mask = _maybe_reshape_attn_mask(query, key, attn_metadata.attn_mask, mask_mode=mask_mode)
|
||||
|
||||
enable_gqa = query.shape[2] != key.shape[2]
|
||||
query, key, value = (x.permute(0, 2, 1, 3) for x in (query, key, value))
|
||||
# Only the PyTorch SDPA backend needs this dispatch check. If SDPA
|
||||
# cannot select a fused GQA kernel for the runtime shape/mask, expand
|
||||
# K/V locally so it can use the better-supported equal-head path.
|
||||
if enable_gqa and not can_sdpa_use_fused_gqa(query, key, value, attention_mask, self.causal):
|
||||
if query.shape[1] % key.shape[1] != 0:
|
||||
raise ValueError(
|
||||
"GQA requires query heads to be a multiple of KV heads, "
|
||||
f"got q_heads={query.shape[1]} and kv_heads={key.shape[1]}."
|
||||
)
|
||||
repeat_num = query.shape[1] // key.shape[1]
|
||||
key = key.repeat_interleave(repeat_num, dim=1)
|
||||
value = value.repeat_interleave(repeat_num, dim=1)
|
||||
enable_gqa = False
|
||||
logger.debug(
|
||||
"CUDA SDPA cannot use a fused native-GQA kernel for this shape; expanding K/V heads before SDPA."
|
||||
)
|
||||
output = torch.nn.functional.scaled_dot_product_attention(
|
||||
query,
|
||||
key,
|
||||
@@ -112,7 +129,7 @@ class SDPAImpl(AttentionImpl):
|
||||
dropout_p=0.0,
|
||||
is_causal=self.causal,
|
||||
scale=self.softmax_scale,
|
||||
enable_gqa=self.requires_gqa,
|
||||
enable_gqa=enable_gqa,
|
||||
)
|
||||
out = output.permute(0, 2, 1, 3)
|
||||
return out
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Runtime kernel-selection helpers for attention backends."""
|
||||
|
||||
import torch
|
||||
|
||||
|
||||
def can_sdpa_use_fused_gqa(
|
||||
query: torch.Tensor,
|
||||
key: torch.Tensor,
|
||||
value: torch.Tensor,
|
||||
attention_mask: torch.Tensor | None,
|
||||
causal: bool,
|
||||
) -> bool:
|
||||
"""Return whether PyTorch SDPA can use a fused kernel for this GQA call.
|
||||
|
||||
This check is specific to the SDPA backend. Native GQA backends such as
|
||||
FlashAttention should keep compressed K/V heads and do not use this helper.
|
||||
"""
|
||||
if not query.is_cuda:
|
||||
return False
|
||||
|
||||
try:
|
||||
params = torch.backends.cuda.SDPAParams(
|
||||
query,
|
||||
key,
|
||||
value,
|
||||
attention_mask,
|
||||
0.0,
|
||||
causal,
|
||||
True,
|
||||
)
|
||||
can_use_flash = torch.backends.cuda.can_use_flash_attention(params)
|
||||
can_use_cudnn = getattr(torch.backends.cuda, "can_use_cudnn_attention", None)
|
||||
return can_use_flash or (can_use_cudnn is not None and can_use_cudnn(params))
|
||||
except (AttributeError, RuntimeError, TypeError):
|
||||
# Older PyTorch versions may not expose GQA-aware SDPA capability checks.
|
||||
# Returning False lets the SDPA backend expand K/V and use the mature
|
||||
# equal-head fused-kernel path instead.
|
||||
return False
|
||||
@@ -9,17 +9,24 @@ follows FlashAttention's bottom-right convention (``K[:e]`` is attended by
|
||||
|
||||
Per segment:
|
||||
- causal segment ``[s, e)``: ``attn(Q[:, s:e], K[:, :e], V[:, :e], causal=True)``
|
||||
- full-attn span ``[a, e)``: ``attn(Q[:, a:e], K[:, :e], V[:, :e], causal=False)``
|
||||
- full-attn span ``[a, b)`` intersecting the query range at ``[s, e)``:
|
||||
``attn(Q[:, s:e], K[:, :b], V[:, :b], causal=False)``
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Literal, NamedTuple
|
||||
from typing import TYPE_CHECKING, Literal, NamedTuple
|
||||
|
||||
import torch
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm_omni.diffusion.attention.backends.abstract import QueryRange
|
||||
|
||||
|
||||
class Segment(NamedTuple):
|
||||
start: int
|
||||
end: int
|
||||
q_start: int
|
||||
q_end: int
|
||||
kv_end: int
|
||||
mode: Literal["causal", "full"]
|
||||
|
||||
|
||||
@@ -30,30 +37,33 @@ def build_segments(full_attn_spans, query_offset, query_len):
|
||||
query_len: length of the query
|
||||
|
||||
return:
|
||||
List[Segment] in global coordinates, clipped to [query_offset, query_offset + query_len)
|
||||
List[Segment] in global coordinates, clipped to
|
||||
[query_offset, query_offset + query_len). Full-attention segments retain
|
||||
the original span end as kv_end so a local query shard can attend past
|
||||
its own boundary.
|
||||
"""
|
||||
q_start = query_offset
|
||||
q_end = query_offset + query_len
|
||||
|
||||
segs: list[Segment] = []
|
||||
segments: list[Segment] = []
|
||||
cur = q_start
|
||||
|
||||
for a, e in full_attn_spans:
|
||||
for span_start, span_end in full_attn_spans:
|
||||
# clip span to query range
|
||||
a_clipped = max(a, q_start)
|
||||
e_clipped = min(e, q_end)
|
||||
if a_clipped >= e_clipped:
|
||||
overlap_start = max(span_start, q_start)
|
||||
overlap_end = min(span_end, q_end)
|
||||
if overlap_start >= overlap_end:
|
||||
continue
|
||||
|
||||
if cur < a_clipped:
|
||||
segs.append(Segment(cur, a_clipped, "causal"))
|
||||
segs.append(Segment(a_clipped, e_clipped, "full"))
|
||||
cur = e_clipped
|
||||
if cur < overlap_start:
|
||||
segments.append(Segment(cur, overlap_start, overlap_start, "causal"))
|
||||
segments.append(Segment(overlap_start, overlap_end, span_end, "full"))
|
||||
cur = overlap_end
|
||||
|
||||
if cur < q_end:
|
||||
segs.append(Segment(cur, q_end, "causal"))
|
||||
segments.append(Segment(cur, q_end, q_end, "causal"))
|
||||
|
||||
return segs
|
||||
return segments
|
||||
|
||||
|
||||
def _check_homogeneous(
|
||||
@@ -76,23 +86,40 @@ def piecewise_attn(
|
||||
full_attn_spans: list[list[tuple[int, int]]],
|
||||
softmax_scale: float,
|
||||
attn_func,
|
||||
query_ranges: tuple[QueryRange, ...] | None = None,
|
||||
):
|
||||
B, Sq, H, D = query.shape
|
||||
_check_homogeneous(full_attn_spans)
|
||||
|
||||
query_offset = key.shape[1] - Sq
|
||||
spans = full_attn_spans[0]
|
||||
out = query.new_zeros(B, Sq, H, D)
|
||||
if query_ranges is None:
|
||||
query_len = query.shape[1]
|
||||
ranges = ((0, query_len, key.shape[1] - query_len),)
|
||||
else:
|
||||
ranges = tuple((r.local_start, r.local_end, r.global_start) for r in query_ranges)
|
||||
|
||||
for s, e, mode in build_segments(spans, query_offset, Sq):
|
||||
q_s = s - query_offset
|
||||
q_e = e - query_offset
|
||||
out_seg = attn_func(
|
||||
query[:, q_s:q_e],
|
||||
key[:, :e],
|
||||
value[:, :e],
|
||||
causal=(mode == "causal"),
|
||||
softmax_scale=softmax_scale,
|
||||
)
|
||||
out[:, q_s:q_e] = out_seg
|
||||
return out
|
||||
outputs = []
|
||||
covered = 0
|
||||
for local_start, local_end, global_start in ranges:
|
||||
query_len = local_end - local_start
|
||||
if local_start != covered or query_len < 0:
|
||||
raise ValueError("query_ranges must cover local query contiguously")
|
||||
for segment in build_segments(spans, global_start, query_len):
|
||||
q_start = local_start + segment.q_start - global_start
|
||||
q_end = local_start + segment.q_end - global_start
|
||||
outputs.append(
|
||||
attn_func(
|
||||
query[:, q_start:q_end],
|
||||
key[:, : segment.kv_end],
|
||||
value[:, : segment.kv_end],
|
||||
causal=(segment.mode == "causal"),
|
||||
softmax_scale=softmax_scale,
|
||||
)
|
||||
)
|
||||
covered = local_end
|
||||
|
||||
if covered != query.shape[1]:
|
||||
raise ValueError("query_ranges must cover the full local query")
|
||||
if not outputs:
|
||||
return torch.empty_like(query)
|
||||
if len(outputs) == 1:
|
||||
return outputs[0]
|
||||
return torch.cat(outputs, dim=1)
|
||||
@@ -140,6 +140,7 @@ class Attention(nn.Module):
|
||||
scatter_idx=scatter_idx,
|
||||
gather_idx=gather_idx,
|
||||
use_sync=use_sync,
|
||||
causal=causal,
|
||||
)
|
||||
# Fallback strategy when SP is not active (outside sharded regions)
|
||||
self._no_parallel_strategy = NoParallelAttention()
|
||||
@@ -282,6 +283,8 @@ class Attention(nn.Module):
|
||||
return out
|
||||
|
||||
def _run_local_attention(self, query, key, value, attn_metadata):
|
||||
self._assert_piecewise_compatible(attn_metadata)
|
||||
|
||||
if query.dtype == torch.float32:
|
||||
logger.warning_once(
|
||||
f"Only SDPA supports float32. Overriding user config {type(self.attention)} "
|
||||
@@ -292,6 +295,20 @@ class Attention(nn.Module):
|
||||
# Fallback to standard attention
|
||||
return self.attention.forward(query, key, value, attn_metadata)
|
||||
|
||||
def _assert_piecewise_compatible(self, attn_metadata: AttentionMetadata | None) -> None:
|
||||
if attn_metadata is None or attn_metadata.full_attn_spans is None:
|
||||
return
|
||||
if attn_metadata.attn_mask is not None and attn_metadata.attn_mask.ndim == 4:
|
||||
return
|
||||
backend_name = self.attn_backend.get_name()
|
||||
if not self.attn_backend.supports_piecewise_spans:
|
||||
raise ValueError(
|
||||
f"Attention backend '{backend_name}' does not support "
|
||||
f"piecewise attention (full_attn_spans without a 4D attn_mask). "
|
||||
f"Use a Flash backend (FLASH_ATTN / FLASH_ATTN_HUB / FLASH_ATTN_3_HUB), "
|
||||
f"or provide a 4D attn_mask that encodes the mixed causal/full pattern."
|
||||
)
|
||||
|
||||
def _run_ring_attention(self, query, key, value, attn_metadata):
|
||||
# Delegate to RingParallelAttention strategy if available
|
||||
if self.ring_runner is not None:
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, replace
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import torch
|
||||
|
||||
from vllm_omni.diffusion.attention.backends.abstract import QueryRange
|
||||
from vllm_omni.diffusion.attention.parallel.base import ParallelAttentionContext
|
||||
from vllm_omni.diffusion.distributed.group_coordinator import (
|
||||
SequenceParallelGroupCoordinator,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm_omni.diffusion.attention.backends.abstract import AttentionMetadata
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class _AllGatherKVCtx(ParallelAttentionContext):
|
||||
pass
|
||||
|
||||
|
||||
class AllGatherKVParallelAttention:
|
||||
"""Compute local Q against AllGathered K/V."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
sp_group: SequenceParallelGroupCoordinator,
|
||||
) -> None:
|
||||
self._sp_group = sp_group
|
||||
self._allgather_group = sp_group.allgather_group
|
||||
self._sp_size = sp_group.allgather_world_size
|
||||
self._sp_rank = sp_group.allgather_rank
|
||||
|
||||
@property
|
||||
def enabled(self) -> bool:
|
||||
return True
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return "allgather_kv"
|
||||
|
||||
def pre_attention(
|
||||
self,
|
||||
query: torch.Tensor,
|
||||
key: torch.Tensor,
|
||||
value: torch.Tensor,
|
||||
attn_metadata: AttentionMetadata | None,
|
||||
):
|
||||
joint_q = joint_k = joint_v = None
|
||||
joint_strategy = "front"
|
||||
if attn_metadata is not None:
|
||||
joint_q = attn_metadata.joint_query
|
||||
joint_k = attn_metadata.joint_key
|
||||
joint_v = attn_metadata.joint_value
|
||||
joint_strategy = attn_metadata.joint_strategy or "front"
|
||||
if joint_strategy not in {"front", "rear"}:
|
||||
raise ValueError(f"Unsupported joint_strategy: {joint_strategy!r}")
|
||||
|
||||
k_img_full = self._sp_group.all_gather(key, dim=1, group=self._allgather_group)
|
||||
v_img_full = self._sp_group.all_gather(value, dim=1, group=self._allgather_group)
|
||||
|
||||
if joint_k is not None:
|
||||
if joint_k.shape[2] != key.shape[2]:
|
||||
raise ValueError(
|
||||
"AllGather-KV SP expects joint_k to be GQA-compressed with the "
|
||||
f"same num_kv_heads as the image shard, got joint_k.heads="
|
||||
f"{joint_k.shape[2]} vs key.heads={key.shape[2]}."
|
||||
)
|
||||
if joint_strategy == "front":
|
||||
k_full = torch.cat([joint_k, k_img_full], dim=1)
|
||||
v_full = torch.cat([joint_v, v_img_full], dim=1)
|
||||
else:
|
||||
k_full = torch.cat([k_img_full, joint_k], dim=1)
|
||||
v_full = torch.cat([v_img_full, joint_v], dim=1)
|
||||
else:
|
||||
k_full, v_full = k_img_full, v_img_full
|
||||
|
||||
joint_len = joint_q.shape[1] if joint_q is not None else 0
|
||||
logical_q_full_len = joint_len + k_img_full.shape[1]
|
||||
query_global_offset = k_full.shape[1] - logical_q_full_len
|
||||
if query_global_offset < 0:
|
||||
raise ValueError(
|
||||
"AllGather-KV SP global K length is shorter than the logical Q length: "
|
||||
f"k_len={k_full.shape[1]}, logical_q_len={logical_q_full_len}."
|
||||
)
|
||||
|
||||
if joint_q is not None:
|
||||
if joint_strategy == "front":
|
||||
q_local = torch.cat([joint_q, query], dim=1)
|
||||
else:
|
||||
q_local = torch.cat([query, joint_q], dim=1)
|
||||
else:
|
||||
q_local = query
|
||||
|
||||
attn_metadata = self._slice_attn_metadata_for_local_query(
|
||||
attn_metadata,
|
||||
q_local_len=q_local.shape[1],
|
||||
img_seq_local=query.shape[1],
|
||||
img_seq_full=k_img_full.shape[1],
|
||||
joint_len=joint_len,
|
||||
joint_strategy=joint_strategy,
|
||||
query_global_offset=query_global_offset,
|
||||
)
|
||||
|
||||
return q_local, k_full, v_full, attn_metadata, _AllGatherKVCtx(name=self.name)
|
||||
|
||||
def _slice_attn_metadata_for_local_query(
|
||||
self,
|
||||
attn_metadata: AttentionMetadata | None,
|
||||
*,
|
||||
q_local_len: int,
|
||||
img_seq_local: int,
|
||||
img_seq_full: int,
|
||||
joint_len: int,
|
||||
joint_strategy: str,
|
||||
query_global_offset: int,
|
||||
) -> AttentionMetadata | None:
|
||||
"""Slice global query metadata for the local query shard."""
|
||||
if attn_metadata is None:
|
||||
return None
|
||||
|
||||
img_start = self._sp_rank * img_seq_local
|
||||
img_end = img_start + img_seq_local
|
||||
if img_end > img_seq_full:
|
||||
raise ValueError(
|
||||
"AllGather-KV SP local image query range exceeds gathered image length: "
|
||||
f"rank={self._sp_rank}, img_start={img_start}, img_end={img_end}, img_seq_full={img_seq_full}."
|
||||
)
|
||||
|
||||
if joint_strategy == "front":
|
||||
ranges = (
|
||||
*((QueryRange(0, joint_len, query_global_offset),) if joint_len else ()),
|
||||
QueryRange(joint_len, q_local_len, query_global_offset + joint_len + img_start),
|
||||
)
|
||||
else:
|
||||
ranges = (
|
||||
QueryRange(0, img_seq_local, query_global_offset + img_start),
|
||||
*((QueryRange(img_seq_local, q_local_len, query_global_offset + img_seq_full),) if joint_len else ()),
|
||||
)
|
||||
|
||||
if attn_metadata.attn_mask is None:
|
||||
return replace(attn_metadata, query_ranges=ranges)
|
||||
|
||||
mask = attn_metadata.attn_mask
|
||||
if mask.ndim != 4:
|
||||
return replace(attn_metadata, query_ranges=ranges)
|
||||
|
||||
if mask.shape[-2] == q_local_len:
|
||||
local_mask = mask
|
||||
else:
|
||||
q_full_len = joint_len + img_seq_full
|
||||
if mask.shape[-2] != q_full_len:
|
||||
raise ValueError(
|
||||
"AllGather-KV SP received an attention mask with incompatible Q length: "
|
||||
f"mask_q={mask.shape[-2]}, expected local_q={q_local_len} or full_q={q_full_len} "
|
||||
f"(joint_len={joint_len}, img_seq_local={img_seq_local}, img_seq_full={img_seq_full})."
|
||||
)
|
||||
|
||||
parts = [
|
||||
mask[..., local_start : local_start + (r.local_end - r.local_start), :]
|
||||
for r in ranges
|
||||
for local_start in (r.global_start - query_global_offset,)
|
||||
]
|
||||
local_mask = torch.cat(parts, dim=-2)
|
||||
|
||||
if local_mask.shape[-2] != q_local_len:
|
||||
raise ValueError(
|
||||
"AllGather-KV SP produced an attention mask with incompatible local Q length: "
|
||||
f"mask_q={local_mask.shape[-2]}, q_local={q_local_len}."
|
||||
)
|
||||
return replace(attn_metadata, attn_mask=local_mask.contiguous(), query_ranges=ranges)
|
||||
|
||||
def post_attention(
|
||||
self,
|
||||
attn_output: torch.Tensor,
|
||||
ctx: ParallelAttentionContext | None,
|
||||
) -> torch.Tensor:
|
||||
return attn_output
|
||||
@@ -5,10 +5,19 @@ from __future__ import annotations
|
||||
|
||||
from vllm.logger import init_logger
|
||||
|
||||
from vllm_omni.diffusion.attention.parallel.base import NoParallelAttention, ParallelAttentionStrategy
|
||||
from vllm_omni.diffusion.attention.parallel.allgather_kv import (
|
||||
AllGatherKVParallelAttention,
|
||||
)
|
||||
from vllm_omni.diffusion.attention.parallel.base import (
|
||||
NoParallelAttention,
|
||||
ParallelAttentionStrategy,
|
||||
)
|
||||
from vllm_omni.diffusion.attention.parallel.ring import RingParallelAttention
|
||||
from vllm_omni.diffusion.attention.parallel.ulysses import UlyssesParallelAttention
|
||||
from vllm_omni.diffusion.distributed.parallel_state import get_sequence_parallel_world_size, get_sp_group
|
||||
from vllm_omni.diffusion.distributed.parallel_state import (
|
||||
get_sequence_parallel_world_size,
|
||||
get_sp_group,
|
||||
)
|
||||
from vllm_omni.diffusion.forward_context import get_forward_context
|
||||
|
||||
logger = init_logger(__name__)
|
||||
@@ -19,6 +28,7 @@ def build_parallel_attention_strategy(
|
||||
scatter_idx: int,
|
||||
gather_idx: int,
|
||||
use_sync: bool,
|
||||
causal: bool = False,
|
||||
) -> ParallelAttentionStrategy:
|
||||
"""Select a parallel attention strategy based on current diffusion config.
|
||||
|
||||
@@ -36,6 +46,7 @@ def build_parallel_attention_strategy(
|
||||
|
||||
ulysses_degree = getattr(p, "ulysses_degree", 1)
|
||||
ring_degree = getattr(p, "ring_degree", 1)
|
||||
allgather_degree = getattr(p, "allgather_degree", 1)
|
||||
|
||||
try:
|
||||
sp_group = get_sp_group()
|
||||
@@ -44,13 +55,26 @@ def build_parallel_attention_strategy(
|
||||
return NoParallelAttention()
|
||||
except Exception as e:
|
||||
# Log warning if SP is configured but group is not available
|
||||
if ulysses_degree > 1 or ring_degree > 1:
|
||||
if ulysses_degree > 1 or ring_degree > 1 or allgather_degree > 1:
|
||||
logger.warning(
|
||||
f"SP configured (ulysses={ulysses_degree}, ring={ring_degree}) but SP group not available: {e}. "
|
||||
f"SP configured (ulysses={ulysses_degree}, ring={ring_degree}, "
|
||||
f"allgather={allgather_degree}) but SP group not available: {e}. "
|
||||
f"Falling back to NoParallelAttention. This may cause incorrect results."
|
||||
)
|
||||
return NoParallelAttention()
|
||||
|
||||
if allgather_degree > 1:
|
||||
if causal:
|
||||
raise ValueError("AllGather-KV SP only supports non-causal attention.")
|
||||
if ulysses_degree > 1 or ring_degree > 1:
|
||||
raise ValueError(
|
||||
f"AllGather-KV SP is mutually exclusive with Ulysses/Ring in v1 "
|
||||
f"(got ulysses_degree={ulysses_degree}, ring_degree={ring_degree}, "
|
||||
f"allgather_degree={allgather_degree})."
|
||||
)
|
||||
logger.debug(f"Using AllGatherKVParallelAttention (allgather_degree={allgather_degree})")
|
||||
return AllGatherKVParallelAttention(sp_group=sp_group)
|
||||
|
||||
# Ulysses (or Hybrid Ulysses+Ring)
|
||||
if ulysses_degree > 1:
|
||||
logger.debug(f"Using UlyssesParallelAttention (ulysses_degree={ulysses_degree})")
|
||||
|
||||
Reference in new issue
Block a user