-
Notifications
You must be signed in to change notification settings - Fork 3
/
searcher.py
148 lines (122 loc) · 5.89 KB
/
searcher.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
import json
import os
import re
import subprocess
import sys
class Searcher:
def __init__(self, searcher_config=None):
self.config = searcher_config
def set_config(self, searcher_config):
self.config = searcher_config
def search(self, client, output_path_guest, topic_path_guest, test_split_path_guest, generate_save_tag):
"""
Runs the search and evaluates the results (run files placed into the /output directory) using trec_eval
"""
save_tag = generate_save_tag(self.config.tag, self.config.load_from_snapshot)
exists = len(client.images.list(filters={"reference": "{}:{}".format(self.config.repo, save_tag)})) != 0
if not exists:
sys.exit("Must prepare image first...")
topic_path_host = os.path.dirname(os.path.abspath(self.config.topic))
output_path = os.path.abspath(self.config.output)
if not os.path.exists(output_path):
os.makedirs(output_path)
volumes = {
output_path: {
"bind": output_path_guest,
"mode": "rw"
},
topic_path_host: {
"bind": topic_path_guest,
"mode": "ro"
}
}
if len(self.config.test_split) > 0:
volumes[os.path.abspath(self.config.test_split)] = {"bind": test_split_path_guest, "mode": "ro"}
search_args = {
"collection": {
"name": self.config.collection
},
"opts": {key: value for (key, value) in map(lambda x: x.split("="), self.config.opts)},
"topic": {
"path": os.path.join(topic_path_guest, os.path.basename(self.config.topic)),
"format": self.config.topic_format
},
"top_k": self.config.top_k
}
# The search command
command = "sh -c '/search --json {}'"
runtime = "nvidia" if self.config.gpu else "runc"
if self.config.timings:
# The search command with timings
command = "sh -c 'time -p /search --json {}'"
# Duplicate first query
single_query_file = ''
with open(os.path.join(topic_path_host, os.path.basename(self.config.topic)), 'r') as file:
queries = file.read()
query_end = queries.find('</top>')
if query_end == -1:
sys.exit('Query format unknown...')
single_query = queries[:query_end + 6]
single_query_file = os.path.splitext(os.path.basename(self.config.topic))[0] + '.single.txt'
out_file = open(os.path.join(topic_path_host, single_query_file), 'w')
out_file.write(single_query)
out_file.close()
# Time empty search
search_args['topic']['path'] = os.path.join(topic_path_guest, single_query_file)
container = client.containers.run("{}:{}".format(self.config.repo, save_tag), command.format(json.dumps(json.dumps(search_args))), volumes=volumes,
detach=True, runtime=runtime)
load_times = []
for line in container.logs(stream=True):
match = re.match('^(real|user|sys)\\s(.*)$', line.decode('utf-8'))
if match:
load_times.append(match)
# Time actual search
search_args['topic']['path'] = os.path.join(topic_path_guest, os.path.basename(self.config.topic))
print("Starting container from saved image...")
container = client.containers.run("{}:{}".format(self.config.repo, save_tag), command.format(json.dumps(json.dumps(search_args))), volumes=volumes,
detach=True, runtime=runtime)
search_times = []
print("Logs for search in container with ID {}...".format(container.id))
for line in container.logs(stream=True):
if self.config.timings:
match = re.match('^(real|user|sys)\\s(.*)$', line.decode('utf-8'))
if match:
search_times.append(match)
print(str(line.decode('utf-8')), end="")
if self.config.timings:
print()
print('**********')
print('Index load timing information')
print(load_times[0].group(0))
print(load_times[1].group(0))
print(load_times[2].group(0))
print()
print('**********')
print('Search timing information')
print(search_times[0].group(0))
print(search_times[1].group(0))
print(search_times[2].group(0))
print()
result = []
for i in range(len(load_times)):
result.append(float(search_times[i].group(2)) - float(load_times[i].group(2)))
print('**********')
print('Search timing less load')
print('real {:.2f}'.format(result[0]))
print('user {:.2f}'.format(result[1]))
print('sys {:.2f}'.format(result[2]))
print()
# The measure string passed to trec_eval
measures = " ".join(map(lambda x: "-c -m {}".format(x), self.config.measures))
print("Evaluating results using trec_eval...")
for file in os.listdir(self.config.output):
if not file.endswith("trec_eval"):
run = os.path.join(self.config.output, file)
print("###\n# {}\n###".format(run))
try:
result = subprocess.check_output("trec_eval/trec_eval {} {} {}".format(measures, self.config.qrels, run).split())
print(result.decode("UTF-8"))
with open("{}.trec_eval".format(run), "w+") as out:
out.write(result.decode("UTF-8"))
except subprocess.CalledProcessError:
print("Unable to evaluate {} - is it a run file?".format(run))