[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
|
# 2. Cross-attention
|
||||||
# Only T2V for now
|
# 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(
|
self.cross_attn_residual_norm = ScaleResidualLayerNormScaleShift(
|
||||||
dim, eps=eps, elementwise_affine=False, dtype=torch.float32
|
dim, eps=eps, elementwise_affine=False, dtype=torch.float32
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -220,7 +220,6 @@ class WanI2VCrossAttention(WanSelfAttention):
|
|||||||
eps=1e-6,
|
eps=1e-6,
|
||||||
supported_attention_backends: set[AttentionBackendEnum] | None = None,
|
supported_attention_backends: set[AttentionBackendEnum] | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
# VSA should not be in supported_attention_backends
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
dim,
|
dim,
|
||||||
num_heads,
|
num_heads,
|
||||||
@@ -371,6 +370,9 @@ class WanTransformerBlock(nn.Module):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# 2. Cross-attention
|
# 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:
|
if added_kv_proj_dim is not None:
|
||||||
# I2V
|
# I2V
|
||||||
self.attn2 = WanI2VCrossAttention(
|
self.attn2 = WanI2VCrossAttention(
|
||||||
@@ -564,9 +566,10 @@ class WanTransformerBlock_VSA(nn.Module):
|
|||||||
dtype=torch.float32,
|
dtype=torch.float32,
|
||||||
)
|
)
|
||||||
|
|
||||||
if AttentionBackendEnum.VIDEO_SPARSE_ATTN in supported_attention_backends:
|
|
||||||
supported_attention_backends.remove(AttentionBackendEnum.VIDEO_SPARSE_ATTN)
|
|
||||||
# 2. Cross-attention
|
# 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:
|
if added_kv_proj_dim is not None:
|
||||||
# I2V
|
# I2V
|
||||||
self.attn2 = WanI2VCrossAttention(
|
self.attn2 = WanI2VCrossAttention(
|
||||||
@@ -574,7 +577,7 @@ class WanTransformerBlock_VSA(nn.Module):
|
|||||||
num_heads,
|
num_heads,
|
||||||
qk_norm=qk_norm,
|
qk_norm=qk_norm,
|
||||||
eps=eps,
|
eps=eps,
|
||||||
supported_attention_backends=supported_attention_backends,
|
supported_attention_backends=cross_attn_backends,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# T2V
|
# T2V
|
||||||
@@ -583,7 +586,7 @@ class WanTransformerBlock_VSA(nn.Module):
|
|||||||
num_heads,
|
num_heads,
|
||||||
qk_norm=qk_norm,
|
qk_norm=qk_norm,
|
||||||
eps=eps,
|
eps=eps,
|
||||||
supported_attention_backends=supported_attention_backends,
|
supported_attention_backends=cross_attn_backends,
|
||||||
)
|
)
|
||||||
self.cross_attn_residual_norm = ScaleResidualLayerNormScaleShift(
|
self.cross_attn_residual_norm = ScaleResidualLayerNormScaleShift(
|
||||||
dim,
|
dim,
|
||||||
|
|||||||
@@ -41,6 +41,16 @@ class AttentionBackendEnum(enum.Enum):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name.lower()
|
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):
|
class PlatformEnum(enum.Enum):
|
||||||
CUDA = enum.auto()
|
CUDA = enum.auto()
|
||||||
|
|||||||
Reference in New Issue
Block a user