[Bugfix][Diffusion] Keep the GQA ratio when padding Ulysses heads (#5716)

Signed-off-by: Xenoryn <timberh2000@outlook.com>
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
XenorynandClaude authored and GitHub committed 2026-09-02 08:24:51 +08:00
1 parent 5f49848536
commit e51fe6ec1b
2 files changed
+566 -79

No files matched your search

+161 -40
View File
@@ -8,6 +8,7 @@ from dataclasses import dataclass
import torch
import torch.distributed as dist
import torch.nn.functional as F
from vllm.logger import init_logger
from vllm_omni.diffusion.attention.backends.abstract import AttentionMetadata
from vllm_omni.diffusion.attention.parallel.base import ParallelAttentionContext
@@ -15,6 +16,55 @@ from vllm_omni.diffusion.distributed.comm import SeqAllToAll4D
from vllm_omni.diffusion.distributed.group_coordinator import SequenceParallelGroupCoordinator
from vllm_omni.diffusion.forward_context import get_ulysses_mode
logger = init_logger(__name__)
# When advanced_uaa pads Q by the GQA ratio, MQA/very-uneven-GQA shapes can
# inflate the query-head count substantially (worst case: MQA @ U=N pads Q from
# H to N*H, an N x blow-up in Q attention work and temporary VRAM). Warn once
# per (Q, KV, world_size) tuple when the ratio crosses this threshold so users
# can pick a friendlier ulysses_degree if the overhead is unacceptable.
_UAA_PAD_RATIO_WARN_THRESHOLD = 1.5
_uaa_pad_ratio_warned: set[tuple[int, int, int, str]] = set()
def _maybe_warn_uaa_pad_ratio(
query_head_cnt: int,
kv_head_cnt: int,
padded_query_head_cnt: int,
ulysses_world_size: int,
tensor_label: str,
) -> None:
"""Emit a one-shot warning when advanced_uaa padding materially inflates Q.
The padding itself is mandatory for correctness (Ulysses splits along the
head dim, so KV must be a multiple of ulysses_world_size and Q must stay a
multiple of KV to remain a valid GQA shape). There is no cheap fallback,
but the caller may want to know when the inflation is large enough to
justify picking a different ulysses_degree.
"""
if query_head_cnt <= 0 or padded_query_head_cnt <= query_head_cnt:
return
ratio = padded_query_head_cnt / query_head_cnt
if ratio < _UAA_PAD_RATIO_WARN_THRESHOLD:
return
key = (query_head_cnt, kv_head_cnt, ulysses_world_size, tensor_label)
if key in _uaa_pad_ratio_warned:
return
_uaa_pad_ratio_warned.add(key)
logger.warning(
"Ulysses advanced_uaa GQA padding inflates %s heads %.2fx "
"(Q=%d, KV=%d, ulysses_degree=%d -> padded Q=%d). "
"Attention FLOPs and temporary VRAM for this tensor grow by the same "
"factor. If this is unacceptable, choose a ulysses_degree that divides "
"KV_heads (or the GCD of Q/KV) to reduce or eliminate padding.",
tensor_label,
ratio,
query_head_cnt,
kv_head_cnt,
ulysses_world_size,
padded_query_head_cnt,
)
def _a2a_permute_enabled(enabled: bool, scatter_idx: int, gather_idx: int, world_size: int) -> bool:
"""Whether the fused permute-free all-to-all applies (strict layout only)."""
@@ -60,6 +110,7 @@ def _ulysses_all_to_all_any_qkv(
*,
seq_lens: list[int],
use_sync: bool,
padded_head_cnt: int | None = None,
) -> tuple[torch.Tensor, int]:
"""UAA forward all-to-all: (B, S_local, H, D) -> (B, S_global, H_local, D).
@@ -72,7 +123,12 @@ def _ulysses_all_to_all_any_qkv(
bsz, s_local, head_cnt, head_dim = x.shape
orig_head_cnt = int(head_cnt)
padded_head_cnt = _ceil_div(orig_head_cnt, world_size) * world_size
if padded_head_cnt is None:
padded_head_cnt = _ceil_div(orig_head_cnt, world_size) * world_size
if padded_head_cnt < orig_head_cnt or padded_head_cnt % world_size != 0:
raise ValueError(
f"Invalid padded head count {padded_head_cnt} for original heads={orig_head_cnt}, world_size={world_size}."
)
head_pad = padded_head_cnt - orig_head_cnt
if head_pad:
x = F.pad(x, (0, 0, 0, head_pad))
@@ -223,21 +279,9 @@ class UlyssesParallelAttention:
ulysses_world_size = self._sp_group.ulysses_world_size
# advanced_uaa pads non-divisible head counts before the Ulysses
# all-to-all. Padding K/V is not valid in hybrid Ulysses+Ring: the Ring
# GQA/MQA path would repeat the padded zero heads as real K/V heads.
# Reject this layout until K/V replication is performed before Ulysses.
if mode == "advanced_uaa" and self._sp_group.ring_world_size > 1:
for name, tensor in (("key", key), ("value", value)):
kv_head_cnt = int(tensor.shape[2])
if kv_head_cnt % ulysses_world_size != 0:
raise ValueError(
"ulysses_mode='advanced_uaa' with hybrid Ulysses+Ring "
"does not support K/V head padding. "
f"{name}_head_cnt={kv_head_cnt}, "
f"ulysses_degree={ulysses_world_size}. "
"Use ring_degree=1, choose a compatible ulysses_degree, "
"or replicate K/V heads before Ulysses."
)
# all-to-all. This also holds in hybrid Ulysses+Ring: Q is derived
# from the padded K/V count by the GQA ratio (see below), so padded
# heads pair with padded heads; non-GQA layouts are rejected below.
joint_tensor_query = joint_tensor_key = joint_tensor_value = None
joint_strategy = "front"
@@ -262,32 +306,61 @@ class UlyssesParallelAttention:
# Slice joint_query for this Ulysses rank
# joint_query is (B, S, H, D). We split H (dim 2).
ulysses_rank = self._sp_group.ulysses_rank
joint_head_cnt = int(joint_tensor_query.shape[-2])
joint_orig_head_cnt = joint_head_cnt
joint_q_head_cnt = int(joint_tensor_query.shape[-2])
joint_k_head_cnt = int(joint_tensor_key.shape[-2])
joint_v_head_cnt = int(joint_tensor_value.shape[-2])
joint_orig_head_cnt = joint_q_head_cnt
if joint_k_head_cnt != joint_v_head_cnt or joint_q_head_cnt % joint_k_head_cnt != 0:
raise ValueError(
"Ulysses joint attention requires equal K/V head counts and joint query "
"heads to be a multiple of joint KV heads, got "
f"joint_Q={joint_q_head_cnt}, joint_K={joint_k_head_cnt}, joint_V={joint_v_head_cnt}."
)
if mode == "advanced_uaa":
padded_joint_head_cnt = _ceil_div(joint_head_cnt, ulysses_world_size) * ulysses_world_size
joint_head_pad = padded_joint_head_cnt - joint_head_cnt
if joint_head_pad:
joint_tensor_query = F.pad(joint_tensor_query, (0, 0, 0, joint_head_pad))
joint_tensor_key = F.pad(joint_tensor_key, (0, 0, 0, joint_head_pad))
joint_tensor_value = F.pad(joint_tensor_value, (0, 0, 0, joint_head_pad))
joint_head_cnt = padded_joint_head_cnt
# Pad joint KV to a world_size multiple, then derive joint Q by the
# GQA ratio so the ratio survives the head-dim split (mirrors the
# main-tensor path below; e.g. 28Q/7KV at SP2 becomes 32/8).
padded_joint_kv_head_cnt = _ceil_div(joint_k_head_cnt, ulysses_world_size) * ulysses_world_size
gqa_ratio = joint_q_head_cnt // joint_k_head_cnt
padded_joint_q_head_cnt = padded_joint_kv_head_cnt * gqa_ratio
_maybe_warn_uaa_pad_ratio(
joint_q_head_cnt,
joint_k_head_cnt,
padded_joint_q_head_cnt,
ulysses_world_size,
"joint_query",
)
joint_q_pad = padded_joint_q_head_cnt - joint_q_head_cnt
joint_kv_pad = padded_joint_kv_head_cnt - joint_k_head_cnt
if joint_q_pad:
joint_tensor_query = F.pad(joint_tensor_query, (0, 0, 0, joint_q_pad))
if joint_kv_pad:
joint_tensor_key = F.pad(joint_tensor_key, (0, 0, 0, joint_kv_pad))
joint_tensor_value = F.pad(joint_tensor_value, (0, 0, 0, joint_kv_pad))
joint_q_head_cnt = padded_joint_q_head_cnt
joint_k_head_cnt = padded_joint_kv_head_cnt
else:
if joint_head_cnt % ulysses_world_size != 0:
supported = _positive_divisors(joint_head_cnt)
raise ValueError(
"Ulysses-SP strict mode requires joint head_cnt divisible by ulysses_degree. "
f"joint_head_cnt={joint_head_cnt}, ulysses_degree={ulysses_world_size}. "
f"Try ulysses_degree in {supported}, or set ulysses_mode='advanced_uaa'."
)
for name, cnt in (
("joint_query", joint_q_head_cnt),
("joint_key", joint_k_head_cnt),
("joint_value", joint_v_head_cnt),
):
if cnt % ulysses_world_size != 0:
supported = _positive_divisors(cnt)
raise ValueError(
"Ulysses-SP strict mode requires joint head_cnt divisible by ulysses_degree. "
f"{name}_head_cnt={cnt}, ulysses_degree={ulysses_world_size}. "
f"Try ulysses_degree in {supported}, or set ulysses_mode='advanced_uaa'."
)
attn_heads_per_ulysses_rank = joint_head_cnt // ulysses_world_size
attn_heads_per_ulysses_rank_q = joint_q_head_cnt // ulysses_world_size
# Note: We use the same heads for Q/K/V
joint_tensor_query = joint_tensor_query[
...,
attn_heads_per_ulysses_rank * ulysses_rank : attn_heads_per_ulysses_rank * (ulysses_rank + 1),
attn_heads_per_ulysses_rank_q * ulysses_rank : attn_heads_per_ulysses_rank_q * (ulysses_rank + 1),
:,
]
@@ -301,8 +374,9 @@ class UlyssesParallelAttention:
if is_joint:
# Slice joint key/value heads for this ulysses rank.
# Using same slicing logic as query
attn_heads_per_ulysses_rank_kv = joint_tensor_key.shape[-2] // ulysses_world_size
# Using the KV head count (post-pad in advanced_uaa) so the per-rank
# KV shape matches the main K/V slice and preserves the GQA ratio.
attn_heads_per_ulysses_rank_kv = joint_k_head_cnt // ulysses_world_size
joint_tensor_key = joint_tensor_key[
...,
@@ -345,11 +419,58 @@ class UlyssesParallelAttention:
"This typically means the input sequence was not evenly shardable across the ring. "
"Try setting ring_degree=1, or choose a sequence length divisible by ring_degree."
)
# Pad KV to a world_size multiple, then scale Q by the GQA ratio so
# the ratio survives the head-dim split (e.g. 28Q/7KV at SP2 becomes
# 32/8, i.e. 16/4 per rank -- padding each independently would give
# 14/4, which is not a valid GQA shape).
#
# Overhead: padded_Q = ceil(KV/U)*U * (Q/KV). This is exact for
# well-aligned shapes (0% overhead when U divides KV) but can be
# substantial for uneven GQA/MQA: 28Q/7KV @ U=2 pads to 32Q (+14%),
# 32Q/1KV @ U=8 pads to 256Q (8x). _maybe_warn_uaa_pad_ratio()
# surfaces a one-shot warning once the blow-up crosses
# _UAA_PAD_RATIO_WARN_THRESHOLD so callers can adjust
# ulysses_degree if the extra work / VRAM is unacceptable.
query_head_cnt = int(query.shape[2])
kv_head_cnt = int(key.shape[2])
if key.shape[2] != value.shape[2] or query_head_cnt % kv_head_cnt != 0:
raise ValueError(
"Ulysses GQA requires equal K/V head counts and query heads "
f"to be a multiple of KV heads, got Q={query_head_cnt}, "
f"K={kv_head_cnt}, V={int(value.shape[2])}."
)
padded_kv_head_cnt = _ceil_div(kv_head_cnt, ulysses_world_size) * ulysses_world_size
padded_query_head_cnt = padded_kv_head_cnt * (query_head_cnt // kv_head_cnt)
_maybe_warn_uaa_pad_ratio(
query_head_cnt,
kv_head_cnt,
padded_query_head_cnt,
ulysses_world_size,
"query",
)
query, orig_head_cnt = _ulysses_all_to_all_any_qkv(
self._ulysses_pg, query, seq_lens=seq_lens, use_sync=self._use_sync
self._ulysses_pg,
query,
seq_lens=seq_lens,
use_sync=self._use_sync,
padded_head_cnt=padded_query_head_cnt,
)
key, _ = _ulysses_all_to_all_any_qkv(
self._ulysses_pg,
key,
seq_lens=seq_lens,
use_sync=self._use_sync,
padded_head_cnt=padded_kv_head_cnt,
)
value, _ = _ulysses_all_to_all_any_qkv(
self._ulysses_pg,
value,
seq_lens=seq_lens,
use_sync=self._use_sync,
padded_head_cnt=padded_kv_head_cnt,
)
key, _ = _ulysses_all_to_all_any_qkv(self._ulysses_pg, key, seq_lens=seq_lens, use_sync=self._use_sync)
value, _ = _ulysses_all_to_all_any_qkv(self._ulysses_pg, value, seq_lens=seq_lens, use_sync=self._use_sync)
else:
# Strict mode: fail fast with actionable errors for head divisibility.
for name, t in (("query", query), ("key", key), ("value", value)):