Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning passage ID in addition to passage index #326

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion colbert/data/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def _load_file(self, path):
return self._load_tsv(path) if path.endswith('.tsv') else self._load_jsonl(path)

def _load_tsv(self, path):
return load_collection(path)
collection, pid_list = load_collection(path)
self.pid_list = pid_list
return collection

def _load_jsonl(self, path):
raise NotImplementedError()
Expand Down
6 changes: 4 additions & 2 deletions colbert/evaluation/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,16 @@ def load_collection(collection_path):
print_message("#> Loading collection...")

collection = []
pid_list = []

with open(collection_path) as f:
for line_idx, line in enumerate(f):
if line_idx % (1000*1000) == 0:
print(f'{line_idx // 1000 // 1000}M', end=' ', flush=True)

pid, passage, *rest = line.strip('\n\r ').split('\t')
assert pid == 'id' or int(pid) == line_idx, f"pid={pid}, line_idx={line_idx}"
pid_list.append(pid)
# assert pid == 'id' or int(pid) == line_idx, f"pid={pid}, line_idx={line_idx}"

if len(rest) >= 1:
title = rest[0]
Expand All @@ -173,7 +175,7 @@ def load_collection(collection_path):

print()

return collection
return collection, pid_list


def load_colbert(args, do_print=True):
Expand Down
9 changes: 9 additions & 0 deletions colbert/searcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self, index, checkpoint=None, collection=None, config=None, index_r
self.config = ColBERTConfig.from_existing(self.checkpoint_config, self.index_config, initial_config)

self.collection = Collection.cast(collection or self.config.collection)
self.pid_list = self.idx2pid(self.config.collection)
self.configure(checkpoint=self.checkpoint, collection=self.collection)

self.checkpoint = Checkpoint(self.checkpoint, colbert_config=self.config, verbose=self.verbose)
Expand All @@ -49,6 +50,14 @@ def __init__(self, index, checkpoint=None, collection=None, config=None, index_r
self.ranker = IndexScorer(self.index, use_gpu, load_index_with_mmap)

print_memory_stats()

def idx2pid(self, collection_path):
pid_list = []
with open(collection_path) as f:
for line_idx, line in enumerate(f):
pid, passage, *rest = line.strip('\n\r ').split('\t')
pid_list.append(pid)
return pid_list

def configure(self, **kw_args):
self.config.configure(**kw_args)
Expand Down