-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
104 lines (76 loc) · 4.21 KB
/
utilities.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import matplotlib.pyplot as plt
import torch
from transformer import create_mask
class Utilities:
def __init__(self, tokenizer, model):
self.tokenizer = tokenizer
self.model = model
def sanity_check(self, sentence, block_size):
# Encode the sentence using the tokenizer
wordids = self.tokenizer.encode(sentence)
# Prepare the padded input for the model
padded_sentence = wordids[:block_size] + [0] * (block_size - len(wordids))
input_tensor = torch.tensor(padded_sentence, dtype=torch.long).unsqueeze(1)
# Display input tensor shape
print("Input tensor shape:", input_tensor.shape)
# Device management
device = next(self.model.parameters()).device
input_tensor = input_tensor.to(device)
# Process the input tensor through the encoder model
_, attn_maps = self.model(input_tensor) # Ignore the output of the model, and only get the attention maps; make sure your encoder returns the attention maps
# Display the number of attention maps
print("Number of attention maps:", len(attn_maps))
# Visualize and save the attention maps
for j, attn_map in enumerate(attn_maps):
att_map = attn_map.squeeze(0).detach().cpu().numpy() # Remove batch dimension and convert to NumPy array
# Check if the attention probabilities sum to 1 over rows
total_prob_over_rows = torch.sum(attn_map[0], dim=1).cpu() # Move to CPU before converting to numpy array
if torch.any(total_prob_over_rows < 0.99) or torch.any(total_prob_over_rows > 1.01):
print("Failed normalization test: probabilities do not sum to 1.0 over rows")
print("Total probability over rows:", total_prob_over_rows.numpy())
# Create a heatmap of the attention map
fig, ax = plt.subplots()
cax = ax.imshow(att_map, cmap='hot', interpolation='nearest')
ax.xaxis.tick_top()
fig.colorbar(cax, ax=ax)
plt.title(f"Attention Map {j + 1}")
# Save the plot
plt.savefig(f"attention_map_{j + 1}.pdf")
# Show the plot
plt.show()
def sanity_check_decoder(self, sentence, block_size):
# Encode the sentence using the tokenizer
wordids = self.tokenizer.encode(sentence)
# Prepare the padded input for the model
padded_sentence = wordids[:block_size] + [0] * (block_size - len(wordids))
input_tensor = torch.tensor(padded_sentence, dtype=torch.long).unsqueeze(1)
# Display input tensor shape, should be (block_size, 1)
print("Input tensor shape:", input_tensor.shape)
# Create the mask
mask = create_mask(input_tensor.size(0)).to(input_tensor.device)
# Device management
device = next(self.model.parameters()).device
input_tensor = input_tensor.to(device)
mask = mask.to(device)
# Process the input tensor through the decoder model
_, attn_maps = self.model(input_tensor, mask)
# Display the number of attention maps
print("Number of attention maps:", len(attn_maps))
# Visualize and save the attention maps
for j, attn_map in enumerate(attn_maps):
att_map = attn_map.squeeze(0).detach().cpu().numpy() # Remove batch dimension and convert to NumPy array
# Check if the attention probabilities sum to 1 over rows
total_prob_over_rows = torch.sum(attn_map[0], dim=1)
if torch.any(total_prob_over_rows < 0.99) or torch.any(total_prob_over_rows > 1.01):
print("Failed normalization test: probabilities do not sum to 1.0 over rows")
print("Total probability over rows:", total_prob_over_rows.detach().cpu().numpy())
# Create a heatmap of the attention map
fig, ax = plt.subplots()
cax = ax.imshow(att_map, cmap='hot', interpolation='nearest')
ax.xaxis.tick_top()
fig.colorbar(cax, ax=ax)
plt.title(f"Attention Map {j + 1}")
# Save the plot
plt.savefig(f"attention_map_{j + 1}.pdf")
# Show the plot
plt.show()