[diffusion][Wan]: fix sparse attention backends being applied to cross-attention (#17596)
This commit is contained in:
@@ -301,7 +301,16 @@ class CausalWanTransformerBlock(nn.Module):
|
||||
|
||||
# 2. Cross-attention
|
||||
# Only T2V for now
|
||||
self.attn2 = WanT2VCrossAttention(dim, num_heads, qk_norm=qk_norm, eps=eps)
|
||||
cross_attn_backends = {
|
||||
b for b in supported_attention_backends if not b.is_sparse
|
||||
}
|
||||
self.attn2 = WanT2VCrossAttention(
|
||||
dim,
|
||||
num_heads,
|
||||
qk_norm=qk_norm,
|
||||
eps=eps,
|
||||
supported_attention_backends=cross_attn_backends,
|
||||
)
|
||||
self.cross_attn_residual_norm = ScaleResidualLayerNormScaleShift(
|
||||
dim, eps=eps, elementwise_affine=False, dtype=torch.float32
|
||||
)
|
||||
|
||||
@@ -220,7 +220,6 @@ class WanI2VCrossAttention(WanSelfAttention):
|
||||
eps=1e-6,
|
||||
supported_attention_backends: set[AttentionBackendEnum] | None = None,
|
||||
) -> None:
|
||||
# VSA should not be in supported_attention_backends
|
||||
super().__init__(
|
||||
dim,
|
||||
num_heads,
|
||||
@@ -371,6 +370,9 @@ class WanTransformerBlock(nn.Module):
|
||||
)
|
||||
|
||||
# 2. Cross-attention
|
||||
cross_attn_backends = {
|
||||
b for b in supported_attention_backends if not b.is_sparse
|
||||
}
|
||||
if added_kv_proj_dim is not None:
|
||||
# I2V
|
||||
self.attn2 = WanI2VCrossAttention(
|
||||
@@ -564,9 +566,10 @@ class WanTransformerBlock_VSA(nn.Module):
|
||||
dtype=torch.float32,
|
||||
)
|
||||
|
||||
if AttentionBackendEnum.VIDEO_SPARSE_ATTN in supported_attention_backends:
|
||||
supported_attention_backends.remove(AttentionBackendEnum.VIDEO_SPARSE_ATTN)
|
||||
# 2. Cross-attention
|
||||
cross_attn_backends = {
|
||||
b for b in supported_attention_backends if not b.is_sparse
|
||||
}
|
||||
if added_kv_proj_dim is not None:
|
||||
# I2V
|
||||
self.attn2 = WanI2VCrossAttention(
|
||||
@@ -574,7 +577,7 @@ class WanTransformerBlock_VSA(nn.Module):
|
||||
num_heads,
|
||||
qk_norm=qk_norm,
|
||||
eps=eps,
|
||||
supported_attention_backends=supported_attention_backends,
|
||||
supported_attention_backends=cross_attn_backends,
|
||||
)
|
||||
else:
|
||||
# T2V
|
||||
@@ -583,7 +586,7 @@ class WanTransformerBlock_VSA(nn.Module):
|
||||
num_heads,
|
||||
qk_norm=qk_norm,
|
||||
eps=eps,
|
||||
supported_attention_backends=supported_attention_backends,
|
||||
supported_attention_backends=cross_attn_backends,
|
||||
)
|
||||
self.cross_attn_residual_norm = ScaleResidualLayerNormScaleShift(
|
||||
dim,
|
||||
|
||||
@@ -41,6 +41,16 @@ class AttentionBackendEnum(enum.Enum):
|
||||
def __str__(self):
|
||||
return self.name.lower()
|
||||
|
||||
@property
|
||||
def is_sparse(self) -> bool:
|
||||
return self in {
|
||||
AttentionBackendEnum.SLIDING_TILE_ATTN,
|
||||
AttentionBackendEnum.VIDEO_SPARSE_ATTN,
|
||||
AttentionBackendEnum.VMOBA_ATTN,
|
||||
AttentionBackendEnum.SLA_ATTN,
|
||||
AttentionBackendEnum.SAGE_SLA_ATTN,
|
||||
}
|
||||
|
||||
|
||||
class PlatformEnum(enum.Enum):
|
||||
CUDA = enum.auto()
|
||||
|
||||
Reference in New Issue
Block a user