Spaces:
Running on Zero
Running on Zero
Update mvdiffusion/models_unclip/attn_processors.py
Browse files
mvdiffusion/models_unclip/attn_processors.py
CHANGED
|
@@ -19,31 +19,35 @@ else:
|
|
| 19 |
|
| 20 |
|
| 21 |
def _memory_efficient_attention(query, key, value, attn_bias=None):
|
| 22 |
-
"""
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
xformers.
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
"""
|
|
|
|
| 30 |
if xformers is not None:
|
| 31 |
try:
|
| 32 |
return xformers.ops.memory_efficient_attention(
|
| 33 |
query, key, value, attn_bias=attn_bias
|
| 34 |
)
|
| 35 |
except AttributeError:
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
if attn_bias is not None
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
class RowwiseMVAttention(Attention):
|
| 49 |
def set_use_memory_efficient_attention_xformers(
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
def _memory_efficient_attention(query, key, value, attn_bias=None):
|
| 22 |
+
"""Speichereffiziente Attention mit xformers → SDPA → bmm Fallback-Kette.
|
| 23 |
|
| 24 |
+
Tensoren nach head_to_batch_dim: (batch*heads, seq, dim)
|
| 25 |
+
|
| 26 |
+
1. xformers.ops.memory_efficient_attention – Flash Attention, O(N) Speicher
|
| 27 |
+
Wird per-call versucht: auf ZeroGPU lädt _C.so erst im GPU-Kontext.
|
| 28 |
+
2. torch.nn.functional.scaled_dot_product_attention (SDPA) – PyTorch 2.x
|
| 29 |
+
Flash Attention 2 intern, O(N) Speicher, kein quadratischer Bedarf.
|
| 30 |
+
3. torch.bmm – nur als letzter Fallback, quadratischer Speicher (44 GB bei PSHuman!)
|
| 31 |
"""
|
| 32 |
+
# 1. xformers (per-call: funktioniert im GPU-Kontext auch wenn beim Start False)
|
| 33 |
if xformers is not None:
|
| 34 |
try:
|
| 35 |
return xformers.ops.memory_efficient_attention(
|
| 36 |
query, key, value, attn_bias=attn_bias
|
| 37 |
)
|
| 38 |
except AttributeError:
|
| 39 |
+
pass # _C.so noch nicht initialisiert → SDPA
|
| 40 |
+
|
| 41 |
+
# 2. PyTorch SDPA mit Flash Attention 2 (memory-effizient, seit torch 2.0)
|
| 42 |
+
# Reshape: (batch*heads, seq, dim) → (batch*heads, 1, seq, dim)
|
| 43 |
+
q = query.unsqueeze(1)
|
| 44 |
+
k = key.unsqueeze(1)
|
| 45 |
+
v = value.unsqueeze(1)
|
| 46 |
+
mask = attn_bias.unsqueeze(1) if attn_bias is not None else None
|
| 47 |
+
out = torch.nn.functional.scaled_dot_product_attention(q, k, v, attn_mask=mask)
|
| 48 |
+
return out.squeeze(1)
|
| 49 |
+
|
| 50 |
+
# 3. torch.bmm – NICHT verwendet (44 GB für PSHuman Multi-View → OOM)
|
| 51 |
|
| 52 |
class RowwiseMVAttention(Attention):
|
| 53 |
def set_use_memory_efficient_attention_xformers(
|