painter3000 commited on
Commit
c91c102
·
verified ·
1 Parent(s): c22347a

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
- """Ruft xformers.ops.memory_efficient_attention auf mit per-call Fallback.
23
 
24
- Warum per-call statt einmaligem hasattr-Check:
25
- Auf ZeroGPU wird die GPU erst beim @spaces.GPU-Decorator allokiert.
26
- xformers._C.so initialisiert sich erst wenn wirklich CUDA verfügbar ist.
27
- Ein hasattr-Check beim Import (ohne GPU) gibt False zurück, obwohl die
28
- Funktion später im GPU-Kontext problemlos verfügbar wäre.
 
 
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
- # xformers geladen, aber _C.so noch nicht initialisiert (kein GPU-Kontext)
37
- # → torch.bmm-Fallback
38
- pass
39
-
40
- # Fallback: skaliertes Dot-Product
41
- scale = query.shape[-1] ** -0.5
42
- attn_scores = torch.bmm(query * scale, key.transpose(-1, -2))
43
- if attn_bias is not None:
44
- attn_scores = attn_scores + attn_bias
45
- attn_probs = torch.nn.functional.softmax(attn_scores, dim=-1)
46
- return torch.bmm(attn_probs, value)
 
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(