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

Optimize speed and gpu usage for embed #16

Merged
merged 1 commit into from
Oct 13, 2024
Merged
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
22 changes: 13 additions & 9 deletions lotus/models/e5_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,22 @@ def embed(self, docs: List[str], **kwargs: Dict[str, Any]) -> np.ndarray:
kwargs = {**self.kwargs, **kwargs}

batch_size = kwargs.get("batch_size", self.batch_size)
embeddings = []
for i, batch_start in enumerate(tqdm(range(0, len(docs), batch_size))):
batch = docs[batch_start : batch_start + batch_size]

with torch.no_grad():

# Calculating the embedding dimension
total_docs = len(docs)
first_batch = self.tokenizer(docs[:1], return_tensors="pt", padding=True, truncation=True)
embed_dim = self.model(**first_batch).last_hidden_state.size(-1)

# Pre-allocate a tensor for all embeddings
embeddings = torch.empty((total_docs, embed_dim), device=self.device)
# Processing batches
with torch.inference_mode(): # Slightly faster than torch.no_grad() for inference
for i, batch_start in enumerate(tqdm(range(0, total_docs, batch_size))):
batch = docs[batch_start : batch_start + batch_size]
batch_dict = self.tokenizer(batch, padding=True, truncation=True, return_tensors="pt").to(self.device)

outputs = self.model(**batch_dict)
batch_embeddings = self.average_pool(outputs.last_hidden_state, batch_dict["attention_mask"])
embeddings.append(batch_embeddings)

embeddings = torch.cat(embeddings, dim=0)
embeddings[batch_start : batch_start + batch_size] = batch_embeddings
if kwargs["normalize"]:
embeddings = F.normalize(embeddings, p=2, dim=1)

Expand Down
Loading