Add memory diagnostics at each attention call to see if VRAM usage is growing unbounded or just spiking once
Browse files
mvdiffusion/models_unclip/attn_processors.py
CHANGED
|
@@ -11,6 +11,22 @@ from einops import rearrange, repeat
|
|
| 11 |
import math
|
| 12 |
|
| 13 |
import torch.nn.functional as F
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
if is_xformers_available():
|
| 15 |
import xformers
|
| 16 |
import xformers.ops
|
|
@@ -117,7 +133,7 @@ class XFormersMVAttnProcessor:
|
|
| 117 |
key = attn.head_to_batch_dim(key)
|
| 118 |
value = attn.head_to_batch_dim(value)
|
| 119 |
|
| 120 |
-
hidden_states =
|
| 121 |
hidden_states = attn.batch_to_head_dim(hidden_states)
|
| 122 |
|
| 123 |
# linear proj
|
|
@@ -245,7 +261,7 @@ class XFormersIPCDAttnProcessor:
|
|
| 245 |
key = attn.head_to_batch_dim(key).contiguous()
|
| 246 |
value = attn.head_to_batch_dim(value).contiguous()
|
| 247 |
|
| 248 |
-
hidden_states =
|
| 249 |
hidden_states = attn.batch_to_head_dim(hidden_states)
|
| 250 |
|
| 251 |
# linear proj
|
|
@@ -376,7 +392,7 @@ class XFormersIPCrossAttnProcessor:
|
|
| 376 |
key = attn.head_to_batch_dim(key).contiguous()
|
| 377 |
value = attn.head_to_batch_dim(value).contiguous()
|
| 378 |
|
| 379 |
-
hidden_states =
|
| 380 |
hidden_states = attn.batch_to_head_dim(hidden_states)
|
| 381 |
|
| 382 |
# ip attn
|
|
@@ -610,7 +626,7 @@ class XFormersCDAttnProcessor:
|
|
| 610 |
key = attn.head_to_batch_dim(key).contiguous()
|
| 611 |
value = attn.head_to_batch_dim(value).contiguous()
|
| 612 |
|
| 613 |
-
hidden_states =
|
| 614 |
hidden_states = attn.batch_to_head_dim(hidden_states)
|
| 615 |
|
| 616 |
# linear proj
|
|
|
|
| 11 |
import math
|
| 12 |
|
| 13 |
import torch.nn.functional as F
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
_sdpa_call_count = [0]
|
| 17 |
+
|
| 18 |
+
def _sdpa_diag(query, key, value, attention_mask):
|
| 19 |
+
_sdpa_call_count[0] += 1
|
| 20 |
+
allocated = torch.cuda.memory_allocated() / 1e9
|
| 21 |
+
reserved = torch.cuda.memory_reserved() / 1e9
|
| 22 |
+
free, total = torch.cuda.mem_get_info()
|
| 23 |
+
print(
|
| 24 |
+
f"MEM_DIAG call={_sdpa_call_count[0]} q={tuple(query.shape)} "
|
| 25 |
+
f"allocated={allocated:.2f}GB reserved={reserved:.2f}GB "
|
| 26 |
+
f"free={free/1e9:.2f}GB total={total/1e9:.2f}GB",
|
| 27 |
+
flush=True,
|
| 28 |
+
)
|
| 29 |
+
return F.scaled_dot_product_attention(query, key, value, attn_mask=attention_mask)
|
| 30 |
if is_xformers_available():
|
| 31 |
import xformers
|
| 32 |
import xformers.ops
|
|
|
|
| 133 |
key = attn.head_to_batch_dim(key)
|
| 134 |
value = attn.head_to_batch_dim(value)
|
| 135 |
|
| 136 |
+
hidden_states = _sdpa_diag(query, key, value, attention_mask)
|
| 137 |
hidden_states = attn.batch_to_head_dim(hidden_states)
|
| 138 |
|
| 139 |
# linear proj
|
|
|
|
| 261 |
key = attn.head_to_batch_dim(key).contiguous()
|
| 262 |
value = attn.head_to_batch_dim(value).contiguous()
|
| 263 |
|
| 264 |
+
hidden_states = _sdpa_diag(query, key, value, attention_mask)
|
| 265 |
hidden_states = attn.batch_to_head_dim(hidden_states)
|
| 266 |
|
| 267 |
# linear proj
|
|
|
|
| 392 |
key = attn.head_to_batch_dim(key).contiguous()
|
| 393 |
value = attn.head_to_batch_dim(value).contiguous()
|
| 394 |
|
| 395 |
+
hidden_states = _sdpa_diag(query, key, value, attention_mask)
|
| 396 |
hidden_states = attn.batch_to_head_dim(hidden_states)
|
| 397 |
|
| 398 |
# ip attn
|
|
|
|
| 626 |
key = attn.head_to_batch_dim(key).contiguous()
|
| 627 |
value = attn.head_to_batch_dim(value).contiguous()
|
| 628 |
|
| 629 |
+
hidden_states = _sdpa_diag(query, key, value, attention_mask)
|
| 630 |
hidden_states = attn.batch_to_head_dim(hidden_states)
|
| 631 |
|
| 632 |
# linear proj
|