[Diffusion][Bug-Fix]: Fix Ulysses SP Accurary Error (#377)
Signed-off-by: Didan Deng <33117903+wtomin@users.noreply.github.com> Signed-off-by: zjy0516 <riverclouds.zhu@qq.com> Co-authored-by: Jiangyun Zhu <riverclouds.zhu@qq.com>
This commit is contained in:
1 parent
b06a5148f6
commit
ee2b78e826
12 files changed
+747
-199
No files matched your search
@@ -7,17 +7,11 @@
|
||||
# https://github.com/feifeibear/long-context-attention/blob/main/yunchang/attention/layer.py
|
||||
|
||||
import torch
|
||||
import torch.distributed as dist
|
||||
import torch.nn as nn
|
||||
from torch import Tensor
|
||||
|
||||
from vllm_omni.diffusion.attention.backends.abstract import (
|
||||
AttentionMetadata,
|
||||
)
|
||||
from vllm_omni.diffusion.attention.backends.abstract import AttentionMetadata
|
||||
from vllm_omni.diffusion.attention.parallel import build_parallel_attention_strategy
|
||||
from vllm_omni.diffusion.attention.selector import get_attn_backend
|
||||
from vllm_omni.diffusion.data import get_current_omni_diffusion_config
|
||||
from vllm_omni.diffusion.distributed.comm import SeqAllToAll4D
|
||||
from vllm_omni.diffusion.distributed.parallel_state import get_sequence_parallel_world_size, get_sp_group
|
||||
|
||||
|
||||
class Attention(nn.Module):
|
||||
@@ -49,25 +43,13 @@ class Attention(nn.Module):
|
||||
self.scatter_idx = scatter_idx
|
||||
self.gather_idx = gather_idx
|
||||
self.use_sync = use_sync
|
||||
self.ring_pg: dist.ProcessGroup | None = None
|
||||
self.ulysses_pg: dist.ProcessGroup | None = None
|
||||
self.use_ulysses = False
|
||||
|
||||
try:
|
||||
config = get_current_omni_diffusion_config()
|
||||
if config.parallel_config.ulysses_degree > 1:
|
||||
self.use_ulysses = True
|
||||
# Get sequence parallel process group
|
||||
try:
|
||||
sp_group = get_sp_group()
|
||||
self.ring_pg = sp_group.ring_group
|
||||
self.ulysses_pg = sp_group.ulysses_group
|
||||
assert get_sequence_parallel_world_size() > 1, "Sequence parallel world size must be > 1"
|
||||
except (AssertionError, RuntimeError):
|
||||
# If sequence parallel group is not initialized, disable Ulysses
|
||||
self.use_ulysses = False
|
||||
except Exception:
|
||||
self.use_ulysses = False
|
||||
# Parallel attention (communication / resharding) is a pluggable strategy.
|
||||
# This keeps the attention kernel backend selection orthogonal.
|
||||
self.parallel = build_parallel_attention_strategy(
|
||||
scatter_idx=scatter_idx,
|
||||
gather_idx=gather_idx,
|
||||
use_sync=use_sync,
|
||||
)
|
||||
|
||||
def forward(
|
||||
self,
|
||||
@@ -76,43 +58,13 @@ class Attention(nn.Module):
|
||||
value: torch.Tensor,
|
||||
attn_metadata: AttentionMetadata = None,
|
||||
) -> torch.Tensor:
|
||||
if self.use_ulysses:
|
||||
return self._forward_ulysses(query, key, value, attn_metadata)
|
||||
else:
|
||||
# shape: (batch_size, seq_len, num_heads, head_size)
|
||||
attn_output = self.attention.forward(query, key, value, attn_metadata)
|
||||
return attn_output
|
||||
# Parallel strategy may reshard/communicate QKV before the attention kernel.
|
||||
query, key, value, attn_metadata, ctx = self.parallel.pre_attention(query, key, value, attn_metadata)
|
||||
|
||||
def _forward_ulysses(
|
||||
self,
|
||||
query: Tensor,
|
||||
key: Tensor,
|
||||
value: Tensor,
|
||||
attn_metadata: AttentionMetadata = None,
|
||||
) -> Tensor:
|
||||
"""Ulysses attention forward pass with sequence parallelism."""
|
||||
# scatter 2, gather 1
|
||||
# (bs, seq_len/N, head_cnt, head_size) -> (bs, seq_len, head_cnt/N, head_size)
|
||||
q = SeqAllToAll4D.apply(self.ulysses_pg, query, self.scatter_idx, self.gather_idx, self.use_sync)
|
||||
k = SeqAllToAll4D.apply(self.ulysses_pg, key, self.scatter_idx, self.gather_idx, self.use_sync)
|
||||
v = SeqAllToAll4D.apply(self.ulysses_pg, value, self.scatter_idx, self.gather_idx, self.use_sync)
|
||||
# shape: (batch_size, seq_len, num_heads, head_size)
|
||||
attn_output = self.attention.forward(query, key, value, attn_metadata)
|
||||
if isinstance(attn_output, tuple):
|
||||
attn_output = attn_output[0]
|
||||
|
||||
softmax_scale = self.softmax_scale
|
||||
if softmax_scale is None:
|
||||
softmax_scale = q.shape[-1] ** -0.5
|
||||
|
||||
context_layer = self.attention.forward(
|
||||
q,
|
||||
k,
|
||||
v,
|
||||
attn_metadata=attn_metadata,
|
||||
)
|
||||
|
||||
if isinstance(context_layer, tuple):
|
||||
context_layer = context_layer[0]
|
||||
|
||||
# (bs, seq_len, head_cnt/N, head_size) -> (bs, seq_len/N, head_cnt, head_size)
|
||||
# scatter 1, gather 2
|
||||
output = SeqAllToAll4D.apply(self.ulysses_pg, context_layer, self.gather_idx, self.scatter_idx, self.use_sync)
|
||||
|
||||
return output
|
||||
# Parallel strategy may need to reverse resharding after the kernel.
|
||||
return self.parallel.post_attention(attn_output, ctx)
|
||||
Reference in new issue
Block a user