HunyuanVideo: A Systematic Framework For Large Video Generation Model
move parallel attention to attention.py
Xibo Sun05d41c2972fc
diff --git a/hyvideo/modules/attenion.py b/hyvideo/modules/attenion.pyindex aa319f7..4454879 100644--- a/hyvideo/modules/attenion.py+++ b/hyvideo/modules/attenion.py@@ -6,9 +6,13 @@ import torch.nn as nnimport torch.nn.functional as Ftry:+ import flash_attn+ from flash_attn.flash_attn_interface import _flash_attn_forwardfrom flash_attn.flash_attn_interface import flash_attn_varlen_funcexcept ImportError:+ flash_attn = Noneflash_attn_varlen_func = None+ _flash_attn_forward = NoneMEMORY_LAYOUT = {@@ -150,3 +154,59 @@ def attention(b, s, a, d = x.shapeout = x.reshape(b, s, -1)return out+++def parallel_attention(+ hybrid_seq_parallel_attn,+ q,+ k,+ v,+ img_q_len,+ img_kv_len,+ cu_seqlens_q,+ cu_seqlens_kv+):+ attn1 = hybrid_seq_parallel_attn(+ None,+ q[:, :img_q_len, :, :],+ k[:, :img_kv_len, :, :],+ v[:, :img_kv_len, :, :],+ dropout_p=0.0,+ causal=False,+ joint_tensor_query=q[:,img_q_len:cu_seqlens_q[1]],+ joint_tensor_key=k[:,img_kv_len:cu_seqlens_kv[1]],+ joint_tensor_value=v[:,img_kv_len:cu_seqlens_kv[1]],+ joint_strategy="rear",+ )+ if flash_attn.__version__ >= '2.7.0':+ attn2, *_ = _flash_attn_forward(+ q[:,cu_seqlens_q[1]:],+ k[:,cu_seqlens_kv[1]:],+ v[:,cu_seqlens_kv[1]:],+ dropout_p=0.0,+ softmax_scale=q.shape[-1] ** (-0.5),+ causal=False,+ window_size_left=-1,+ window_size_right=-1,+ softcap=0.0,+ alibi_slopes=None,+ return_softmax=False,+ )+ else:+ attn2, *_ = _flash_attn_forward(+ q[:,cu_seqlens_q[1]:],+ k[:,cu_seqlens_kv[1]:],+ v[:,cu_seqlens_kv[1]:],+ dropout_p=0.0,+ softmax_scale=q.shape[-1] ** (-0.5),+ causal=False,+ window_size=(-1, -1),+ softcap=0.0,+ alibi_slopes=None,+ return_softmax=False,+ )+ attn = torch.cat([attn1, attn2], dim=1)+ b, s, a, d = attn.shape+ attn = attn.reshape(b, s, -1)++ return attndiff --git a/hyvideo/modules/models.py b/hyvideo/modules/models.pyindex c44bd47..646a42d 100644--- a/hyvideo/modules/models.py+++ b/hyvideo/modules/models.py@@ -11,18 +11,12 @@ from diffusers.configuration_utils import ConfigMixin, register_to_configfrom .activation_layers import get_activation_layerfrom .norm_layers import get_norm_layerfrom .embed_layers import TimestepEmbedder, PatchEmbed, TextProjection-from .attenion import attention, get_cu_seqlens+from .attenion import attention, parallel_attention, get_cu_seqlensfrom .posemb_layers import apply_rotary_embfrom .mlp_layers import MLP, MLPEmbedder, FinalLayerfrom .modulate_layers import ModulateDiT, modulate, apply_gatefrom .token_refiner import SingleTokenRefiner-try:- import flash_attn- from flash_attn.flash_attn_interface import _flash_attn_forward-except:- _flash_attn_forward = None-class MMDoubleStreamBlock(nn.Module):"""@@ -218,48 +212,17 @@ class MMDoubleStreamBlock(nn.Module):batch_size=img_k.shape[0],)else:- attn1 = self.hybrid_seq_parallel_attn(- None,- img_q,- img_k,- img_v,- dropout_p=0.0,- causal=False,- joint_tensor_query=txt_q[:,:cu_seqlens_q[1] - img_q.shape[1]],- joint_tensor_key=txt_k[:,:cu_seqlens_kv[1] - img_k.shape[1]],- joint_tensor_value=txt_v[:,:cu_seqlens_kv[1] - img_v.shape[1]],- joint_strategy="rear",+ attn = parallel_attention(+ self.hybrid_seq_parallel_attn,+ q,+ k,+ v,+ img_q_len=img_q.shape[1],+ img_kv_len=img_k.shape[1],+ cu_seqlens_q=cu_seqlens_q,+ cu_seqlens_kv=cu_seqlens_kv)- if flash_attn.__version__ >= '2.7.0':- attn2, *_ = _flash_attn_forward(- txt_q[:,cu_seqlens_q[1] - img_q.shape[1]:],- txt_k[:,cu_seqlens_kv[1] - img_k.shape[1]:],- txt_v[:,cu_seqlens_kv[1] - img_v.shape[1]:],- dropout_p=0.0,- softmax_scale=txt_q.shape[-1] ** (-0.5),- causal=False,- window_size_left=-1,- window_size_right=-1,- softcap=0.0,- alibi_slopes=None,- return_softmax=False,- )- else:- attn2, *_ = _flash_attn_forward(- txt_q[:,cu_seqlens_q[1] - img_q.shape[1]:],- txt_k[:,cu_seqlens_kv[1] - img_k.shape[1]:],- txt_v[:,cu_seqlens_kv[1] - img_v.shape[1]:],- dropout_p=0.0,- softmax_scale=txt_q.shape[-1] ** (-0.5),- causal=False,- window_size=(-1, -1),- softcap=0.0,- alibi_slopes=None,- return_softmax=False,- )- attn = torch.cat([attn1, attn2], dim=1)- b, s, a, d = attn.shape- attn = attn.reshape(b, s, -1)+# attention computation endimg_attn, txt_attn = attn[:, : img.shape[1]], attn[:, img.shape[1] :]@@ -413,48 +376,16 @@ class MMSingleStreamBlock(nn.Module):batch_size=x.shape[0],)else:- attn1 = self.hybrid_seq_parallel_attn(- None,- q[:, :-txt_len, :, :],- k[:, :-txt_len, :, :],- v[:, :-txt_len, :, :],- dropout_p=0.0,- causal=False,- joint_tensor_query=q[:, -txt_len:cu_seqlens_q[1], :, :],- joint_tensor_key=k[:, -txt_len:cu_seqlens_kv[1], :, :],- joint_tensor_value=v[:, -txt_len:cu_seqlens_kv[1], :, :],- joint_strategy="rear",+ attn = parallel_attention(+ self.hybrid_seq_parallel_attn,+ q,+ k,+ v,+ img_q_len=img_q.shape[1],+ img_kv_len=img_k.shape[1],+ cu_seqlens_q=cu_seqlens_q,+ cu_seqlens_kv=cu_seqlens_kv)- if flash_attn.__version__ >= '2.7.0':- attn2, *_ = _flash_attn_forward(- q[:,cu_seqlens_q[1]:],- k[:,cu_seqlens_kv[1]:],- v[:,cu_seqlens_kv[1]:],- dropout_p=0.0,- softmax_scale=txt_q.shape[-1] ** (-0.5),- causal=False,- window_size_left=-1,- window_size_right=-1,- softcap=0.0,- alibi_slopes=None,- return_softmax=False,- )- else:- attn2, *_ = _flash_attn_forward(- q[:,cu_seqlens_q[1]:],- k[:,cu_seqlens_kv[1]:],- v[:,cu_seqlens_kv[1]:],- dropout_p=0.0,- softmax_scale=txt_q.shape[-1] ** (-0.5),- causal=False,- window_size=(-1, -1),- softcap=0.0,- alibi_slopes=None,- return_softmax=False,- )- attn = torch.cat([attn1, attn2], dim=1)- b, s, a, d = attn.shape- attn = attn.reshape(b, s, -1)# attention computation end# Compute activation in mlp stream, cat again and run second linear layer.