-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathevaluate.py
166 lines (155 loc) · 7.54 KB
/
evaluate.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import argparse
import math
from tqdm import tqdm
from dataset import T5_Dataset
from torch.utils.data import DataLoader
from utils_accelerate import *
import numpy as np
from typing import Dict
from collections import defaultdict
class Evaluator:
def __init__(self, dataset: T5_Dataset, model, args):
self.device = args.device
self.dataset = dataset
self.model = model.to(self.device)
self.num_workers = args.num_workers
self.batch_size = args.batch_size
self.chunk_size = args.chunk_size
self.data_loader = DataLoader(
dataset,
batch_size=self.batch_size,
shuffle=False,
num_workers=self.num_workers,
collate_fn=dataset._collate_eval_2,
)
self.filter_dicts = dict()
self.filter_dicts["train"] = self.create_filter_dict("train")
self.filter_dicts["valid"] = self.create_filter_dict("valid")
self.filter_dicts["test"] = self.create_filter_dict("test")
def create_filter_dict(self, split: str) -> Dict[str, int]:
data = self.dataset.split(split)
filter_dict = defaultdict(list)
for input, output in zip(data["inputs"], data["outputs"]):
filter_dict[input].append(self.dataset.entity_string_to_id[output])
return filter_dict
@torch.no_grad()
def eval(self):
self.model.eval()
loader = tqdm(self.data_loader, total=len(self.data_loader), unit="batch")
ranks = {
"unfiltered": list(),
"filtered": list(),
}
for steps, batch in enumerate(loader):
ranks_in_batch = {
"unfiltered": list(),
"filtered": list()
}
input_ids, attention_mask, label_strings, input_strings = batch
input_ids = input_ids.to(self.device)
attention_mask = attention_mask.to(self.device)
# labels = labels.to(self.device)
input_ids_repeated = torch.repeat_interleave(
input_ids, len(self.dataset.entity_strings), dim=0
)
attention_mask_repeated = torch.repeat_interleave(
attention_mask, len(self.dataset.entity_strings), dim=0
)
tokenized_entities = self.dataset.tokenized_entities.input_ids.to(
self.device
)
# todo: for filtering we need to use only the filtered entities per triple here
all_entities_repeated = tokenized_entities.repeat([self.batch_size, 1])
summed_logit_chunks = []
# process chunk by chunk
for chunk_number in range(
math.ceil(len(input_ids_repeated) / self.chunk_size)
):
chunk_start = self.chunk_size * chunk_number
chunk_end = min(
self.chunk_size * (chunk_number + 1), len(input_ids_repeated)
)
current_chunk_size = chunk_end - chunk_start
outputs_chunk = self.model(
input_ids=input_ids_repeated[chunk_start:chunk_end],
attention_mask=attention_mask_repeated[chunk_start:chunk_end],
labels=all_entities_repeated[chunk_start:chunk_end],
)
logits_chunk = outputs_chunk.logits
soft_logits_chunk = torch.log_softmax(logits_chunk, dim=2)
coordinates = all_entities_repeated[chunk_start:chunk_end].view(current_chunk_size, -1, 1)
# set padded logits to zero
padded_mask = (coordinates == 0).squeeze()
soft_logits_chunk[padded_mask] = 0
needed_soft_logits_chunk = torch.gather(
soft_logits_chunk,
2,
coordinates
).view(current_chunk_size, -1)
summed_logits = torch.sum(needed_soft_logits_chunk, dim=1)
summed_logit_chunks.append(summed_logits)
summed_logits = torch.cat(summed_logit_chunks)
for summed_logits_per_triple, input_string, label in zip(
summed_logits.split(len(self.dataset.entity_strings)), input_strings, label_strings
):
# todo: currently we are calculating best rank on equality
# change to mean
arg_sorted = torch.argsort(summed_logits_per_triple, descending=True)
entity_id = self.dataset.entity_string_to_id[label]
rank = (
(arg_sorted == entity_id)
.nonzero(as_tuple=True)[0]
.item()
)
print(rank)
ranks_in_batch["unfiltered"].append(rank)
# now filter
true_score = summed_logits_per_triple[entity_id].clone()
for filter_dict in self.filter_dicts.values():
summed_logits_per_triple[filter_dict[input_string]] = -float("inf")
summed_logits_per_triple[entity_id] = true_score
arg_sorted = torch.argsort(summed_logits_per_triple, descending=True)
rank = (
(arg_sorted == entity_id)
.nonzero(as_tuple=True)[0]
.item()
)
print(rank)
ranks_in_batch["filtered"].append(rank)
ranks["filtered"].extend(ranks_in_batch["filtered"])
ranks["unfiltered"].extend(ranks_in_batch["unfiltered"])
for setting, list_of_ranks in ranks.items():
ranks[setting] = np.array(list_of_ranks, dtype=np.float32) + 1
# ranks = np.array(ranks, dtype=np.float32)
# # add 1 to have best rank 1 not 0
# ranks += 1
print("MR", ranks["unfiltered"].mean())
print("MR-filtered", ranks["filtered"].mean())
print("MRR", np.power(ranks["unfiltered"], -1).mean())
print("MRR-filtered", np.power(ranks["filtered"], -1).mean())
print("Hits@1", (ranks["unfiltered"] == 1).sum() / len(self.dataset))
print("Hits@1-filtered", (ranks["filtered"] == 1).sum() / len(self.dataset))
print("Hits@10", (ranks["unfiltered"] <= 10).sum() / len(self.dataset))
print("Hits@10-filtered", (ranks["filtered"] <= 10).sum() / len(self.dataset))
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--prefix", type=str, default="temp")
parser.add_argument("--checkpoint", type=str)
parser.add_argument("--dataset", type=str, default="codex-m")
parser.add_argument("--batch_size", type=int, default=1)
parser.add_argument("--chunk_size", type=int, default=50)
parser.add_argument("--num_beams", type=int, default=1)
parser.add_argument("--num_predictions", type=int, default=1)
parser.add_argument("--length_penalty", type=float, default=0.6)
parser.add_argument("--num_workers", type=int, default=0)
parser.add_argument("--device", type=str, default="cuda")
parser.add_argument("--split", type=str, default="test")
args = parser.parse_args()
valid_dataset = T5_Dataset(args.split, dataset_name=args.dataset)
checkpoint_location = "models/{}/{}.pt".format(args.prefix, args.checkpoint)
print("Using %s" % checkpoint_location)
model = load_accelerator_model(checkpoint_location, only_model=True)
evaluator = Evaluator(dataset=valid_dataset, model=model, args=args)
evaluator.eval()
if __name__ == "__main__":
main()