-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize_attn.py
More file actions
40 lines (37 loc) · 1.36 KB
/
Copy pathvisualize_attn.py
File metadata and controls
40 lines (37 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import pickle
import ipdb
import matplotlib.pyplot as plt
import numpy as np
from pathlib import Path
def get_attns(ref_attns):
timesteps = list(ref_attns.keys())
attn_blocks = ref_attns[timesteps[0]].keys()
attns = {}
for attn_block in attn_blocks:
attns[attn_block] = []
print(attn_block)
for timestep in timesteps:
# Get highest attention over all the tokens
cross_attn, cross_attn_indices = ref_attns[timestep][attn_block].max(dim=-1)
channels, pixels = cross_attn.shape
dim = int(np.sqrt(pixels))
cross_attn = cross_attn.reshape(channels, dim, dim)
cross_attn = cross_attn.mean(dim=0)
attns[attn_block].append(cross_attn)
return attns
def plot_attns(attns):
attn_blocks = attns.keys()
for attn_block in attn_blocks:
for i in range(len(attns[attn_block])):
img = attns[attn_block][i]
plt.imshow(img)
dim, _ = img.shape
plt.axis('off')
dir_path = Path(f"""viz/{attn_block.replace(".", "_")}""")
dir_path.mkdir(parents=True, exist_ok=True)
plt.title(f"{attn_block} step {i} ({dim} x {dim})")
plt.savefig(f"{dir_path}/{i}.png")
with open('attention.pkl', 'rb') as f:
ref_attns = pickle.load(f)
attns = get_attns(ref_attns)
plot_attns(attns)