DSPy: The framework for programming—not prompting—language models
Python
perf(dspy): reduce memory use in exact embedding retrieval (#10398)
Bozhen Peng63acea7b1f4a
diff --git a/dspy/retrievers/embeddings.py b/dspy/retrievers/embeddings.pyindex e5fcd4eb3..8690c96c5 100644--- a/dspy/retrievers/embeddings.py+++ b/dspy/retrievers/embeddings.py@@ -63,7 +63,6 @@ class Embeddings:q_embeds = self._normalize(q_embeds) if self.normalize else q_embedspids = self._faiss_search(q_embeds, self.k * 10) if self.index else None- pids = np.tile(np.arange(len(self.corpus)), (len(queries), 1)) if pids is None else pidsreturn self._rerank_and_predict(q_embeds, pids)@@ -93,12 +92,20 @@ class Embeddings:def _faiss_search(self, query_embeddings: np.ndarray, num_candidates: int):return self.index.search(query_embeddings, num_candidates)[1]- def _rerank_and_predict(self, q_embeds: np.ndarray, candidate_indices: np.ndarray):- candidate_embeddings = self.corpus_embeddings[candidate_indices]- scores = np.einsum("qd,qkd->qk", q_embeds, candidate_embeddings)+ def _rerank_and_predict(self, q_embeds: np.ndarray, candidate_indices: np.ndarray | None):+ if candidate_indices is None:+ self.corpus_embeddings = np.ascontiguousarray(self.corpus_embeddings)+ scores = np.einsum("qd,kd->qk", q_embeds, self.corpus_embeddings, order="C")+ else:+ candidate_embeddings = self.corpus_embeddings[candidate_indices]+ scores = np.einsum("qd,qkd->qk", q_embeds, candidate_embeddings)top_k_indices = np.argsort(-scores, axis=1)[:, : self.k]- top_indices = candidate_indices[np.arange(len(q_embeds))[:, None], top_k_indices]+ top_indices = (+ top_k_indices+ if candidate_indices is None+ else candidate_indices[np.arange(len(q_embeds))[:, None], top_k_indices]+ )top_scores = scores[np.arange(len(q_embeds))[:, None], top_k_indices]results = []