Spaces:
Running on Zero
Running on Zero
Add SDPA fallback in flash_attention for GPUs without flash-attn
Browse files- wan/modules/attention.py +17 -0
wan/modules/attention.py
CHANGED
|
@@ -192,6 +192,23 @@ def flash_attention(
|
|
| 192 |
assert dtype in half_dtypes
|
| 193 |
assert q.device.type == 'cuda' and q.size(-1) <= 256
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
# params
|
| 196 |
b, lq, lk, out_dtype = q.size(0), q.size(1), k.size(1), q.dtype
|
| 197 |
|
|
|
|
| 192 |
assert dtype in half_dtypes
|
| 193 |
assert q.device.type == 'cuda' and q.size(-1) <= 256
|
| 194 |
|
| 195 |
+
# Fallback: on hardware without FlashAttention (e.g. ZeroGPU / Blackwell
|
| 196 |
+
# without a prebuilt flash-attn wheel), use exact SDPA. Inputs here are
|
| 197 |
+
# [B, L, N, D]; SDPA expects [B, N, L, D]. This matches the math of the
|
| 198 |
+
# flash path (full, non-varlen attention over the given sequences).
|
| 199 |
+
if not FLASH_ATTN_3_AVAILABLE and not FLASH_ATTN_2_AVAILABLE:
|
| 200 |
+
out_dtype = q.dtype
|
| 201 |
+
qs = q.transpose(1, 2).to(dtype)
|
| 202 |
+
ks = k.transpose(1, 2).to(dtype)
|
| 203 |
+
vs = v.transpose(1, 2).to(dtype)
|
| 204 |
+
if q_scale is not None:
|
| 205 |
+
qs = qs * q_scale
|
| 206 |
+
x = torch.nn.functional.scaled_dot_product_attention(
|
| 207 |
+
qs, ks, vs, attn_mask=None, is_causal=causal,
|
| 208 |
+
dropout_p=dropout_p, scale=softmax_scale,
|
| 209 |
+
)
|
| 210 |
+
return x.transpose(1, 2).contiguous().type(out_dtype)
|
| 211 |
+
|
| 212 |
# params
|
| 213 |
b, lq, lk, out_dtype = q.size(0), q.size(1), k.size(1), q.dtype
|
| 214 |
|