perf: batch embedding calls in MaximalMarginalRelevance (2N → 1 call) [stacks on #2496]#2514
Draft
pidefrem wants to merge 2 commits into
Draft
perf: batch embedding calls in MaximalMarginalRelevance (2N → 1 call) [stacks on #2496]#2514pidefrem wants to merge 2 commits into
MaximalMarginalRelevance (2N → 1 call) [stacks on #2496]#2514pidefrem wants to merge 2 commits into
Conversation
Draft
5 tasks
MaximalMarginalRelevance (2N → 1 call)MaximalMarginalRelevance (2N → 1 call) [stacks on #2496]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
perf: batch embedding calls in
MaximalMarginalRelevance(2N → 1 call)MaximalMarginalRelevance.extract_topics()calls the embedding model twice per topic inside a loop: once for the individual candidate words and once for the concatenated sentence. With N topics, this means 2N separate embedding calls — each with its own model inference overhead, GPU kernel launch, or API round-trip.Changes:
Collect all candidate words and all topic sentences across all topics into a single flat list. Make 1 embedding call for the combined list. Slice the result array back into per-topic chunks using pre-computed index ranges.
This reduces 2N calls to exactly 1 call, with identical output. The improvement scales linearly with topic count and is most impactful with API-based embedding models (e.g., OpenAI: 100 API calls → 1 for 50 topics).
Benchmark (to be filled in before submission):
2N(TODO)1(TODO)2N(TODO)1(TODO)The call-count reduction (2N → 1) is deterministic and is the core argument; the wall-time row illustrates the real-world impact with remote embedding models.
Fixes #2498
Before submitting