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

[WIP] #107

Open
wants to merge 4 commits into
base: index_on_ssd
Choose a base branch
from
Open

[WIP] #107

Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 22 additions & 13 deletions colbert/indexing/codecs/residual_embeddings.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import os
import torch
import ujson
from collections import defaultdict

from colbert.indexing.codecs.residual_embeddings_strided import ResidualEmbeddingsStrided


class ResidualEmbeddings:
Strided = ResidualEmbeddingsStrided

def __init__(self, codes, residuals, mmap_index=False):
def __init__(self, codes, residuals, mmap_index=False, pid_to_chunk_metadata=None):
"""
Supply the already compressed residuals.
"""
Expand All @@ -17,6 +18,7 @@ def __init__(self, codes, residuals, mmap_index=False):
if self.mmap_index:
self.codes = codes
self.residuals = residuals
self.pid_to_chunk_metadata = pid_to_chunk_metadata

# assert isinstance(residuals, bitarray), type(residuals)
assert codes.size(0) == residuals.size(0), (codes.size(), residuals.size())
Expand Down Expand Up @@ -45,20 +47,22 @@ def load_chunks(cls, index_path, chunk_idxs, num_embeddings, mmap_index=False):
codes_offset = 0
pid_offset = 0

per_chunk_doclens = {}
pid_to_chunk_idx = {}
pid_to_chunk_metadata = {} # pid -> [chunk id, passage doclen, passage offset in the chunk]

for chunk_idx in chunk_idxs:
with open(os.path.join(index_path, f'{chunk_idx}.metadata.json')) as f:
metadata = ujson.load(f)

with open(os.path.join(index_path, f'{chunk_idx}.doclens.json')) as f:
chunk_doclens = ujson.load(f)

pid_offset_in_chunk = 0
for pid in range(pid_offset, pid_offset + metadata["num_passages"]):
pid_to_chunk_idx[pid] = chunk_idx
pid_doclen = chunk_doclens[pid - pid_offset]
pid_to_chunk_metadata[pid] = [chunk_idx, pid_doclen, pid_offset_in_chunk]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this a NamedTuple?

pid_offset_in_chunk += pid_doclen
pid_offset += metadata["num_passages"]

with open(os.path.join(index_path, f'{chunk_idx}.doclens.json')) as f:
per_chunk_doclens[chunk_idx] = ujson.load(f)

codes_endpos = codes_offset + metadata["num_embeddings"]

chunk = cls.load(index_path, chunk_idx, codes_offset, codes_endpos, packed_dim, mmap_index)
Expand All @@ -77,7 +81,7 @@ def load_chunks(cls, index_path, chunk_idxs, num_embeddings, mmap_index=False):

# codes, residuals = codes.cuda(), residuals.cuda() # FIXME: REMOVE THIS LINE!

return cls(codes, residuals)
return cls(codes, residuals, mmap_index=mmap_index, pid_to_chunk_metadata=pid_to_chunk_metadata)

@classmethod
def load(cls, index_path, chunk_idx, offset, endpos, packed_dim, mmap_index=False):
Expand Down Expand Up @@ -119,17 +123,22 @@ def save(self, path_prefix):

def lookup_codes(self, pids):
assert self.mmap_index
codes = torch.zeros((sum(self.doclens[pid] for pid in pids]))
codes = torch.zeros(sum([self.pid_to_chunk_metadata[pid][1] for pid in pids]))

pids_per_chunk = defaultdict(list)
for pid in pids:
chunk_idx = self.pid_to_chunk_idx[pid]
chunk_idx = self.pid_to_chunk_metadata[pid][0]
pids_per_chunk[chunk_idx].append(pid)

offset = 0
for chunk_idx in sorted(chunks.keys()):
for chunk_idx in sorted(pids_per_chunk.keys()):
Copy link
Collaborator

@santhnm2 santhnm2 May 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a problem in the code I wrote, but one concern I have is that it may be possible for the pids passed in to lookup_pids to not be in sorted order - if that is the case, then iterating over the chunks in sorted order would produce an output that's inconsistent with what the calling function expects

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the solution would be to iterate over the pids in the original order, then find the chunk each pid belongs to and extract the relevant data from that chunk
Or alternatively, we could sort the pids upstream somewhere (assuming this is not done already)

pids_ = pids_per_chunk[chunk_idx]
for pid in pids_:
codes[offset:offset + self.doclens[pid]] = self.codes[chunk_idx][self.chunk_offsets[pid]:self.chunk_offsets[pid] + doclens[pid]]
offset += doclens[pid]
pid_doclen = self.pid_to_chunk_metadata[pid][1]
pid_offset_in_chunk = self.pid_to_chunk_metadata[pid][2]
codes[offset:offset + pid_doclen] = \
self.codes[chunk_idx][pid_offset_in_chunk:pid_offset_in_chunk + pid_doclen]
offset += pid_doclen

return codes

Expand Down
1 change: 1 addition & 0 deletions colbert/search/index_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from colbert.indexing.loaders import load_doclens
from colbert.indexing.codecs.residual_embeddings_strided import ResidualEmbeddingsStrided
from colbert.indexing.codecs import residual_embeddings

from colbert.search.strided_tensor import StridedTensor
from colbert.search.candidate_generation import CandidateGeneration
Expand Down