Spaces:
Running on Zero
Running on Zero
Upload diffcve/modules/vae_blocks.py with huggingface_hub
Browse files
diffcve/modules/vae_blocks.py
CHANGED
|
@@ -233,9 +233,18 @@ class MemoryEfficientAttnBlock(nn.Module):
|
|
| 233 |
weights = torch.softmax(weights, dim=2)
|
| 234 |
out = torch.bmm(weights, v)
|
| 235 |
else:
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
h_ = (
|
| 241 |
out.permute(0,2,1)
|
|
|
|
| 233 |
weights = torch.softmax(weights, dim=2)
|
| 234 |
out = torch.bmm(weights, v)
|
| 235 |
else:
|
| 236 |
+
try:
|
| 237 |
+
out = xformers.ops.memory_efficient_attention(
|
| 238 |
+
q, k, v, attn_bias=None, scale=(int(c) ** (-0.5)), op=self.attention_op
|
| 239 |
+
)
|
| 240 |
+
except NotImplementedError:
|
| 241 |
+
# No xformers kernel for this shape on this GPU (e.g. the VAE
|
| 242 |
+
# mid-block's single 512-dim head on Blackwell, where the
|
| 243 |
+
# torch-backed FA2 path caps head_dim at 256). Fall back to the
|
| 244 |
+
# mathematically-identical dense attention.
|
| 245 |
+
weights = torch.bmm(q, k.transpose(1, 2)) * (int(c) ** -0.5)
|
| 246 |
+
weights = torch.softmax(weights, dim=2)
|
| 247 |
+
out = torch.bmm(weights, v)
|
| 248 |
|
| 249 |
h_ = (
|
| 250 |
out.permute(0,2,1)
|