MaartenGr / MaartenGr/BERTopic

`MaximalMarginalRelevance` makes 2N embedding calls instead of 1

Open
#2,498 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.8k
Forks
920
Avg merge
22h 24m
Merged PRs (30d)
5

Description

### Feature request

`MaximalMarginalRelevance.extract_topics()` calls the embedding model **twice per topic**: 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 (for remote embedding services like OpenAI).

Batch all words and all sentences across every topic into a single embedding call instead of 2N.

### Motivation

For a typical run with 50 topics using OpenAI embeddings, this is 100 API calls where 1 would suffice. The improvement scales linearly with topic count and is most impactful with API-based embedding models where each call has network latency and rate-limiting overhead.

### Your contribution

I can submit a PR that collects all candidate words and all topic sentences across all topics into a single flat list, makes 1 embedding call, then slices the result array back into per-topic chunks using pre-computed index ranges.

This reduces 2N calls to exactly 1 call, with identical output.

I've already been prototyping this in my fork, so I can open a PR quickly if this looks like a good direction.

---

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating MaximalMarginalRelevance.extract_topics() in the BERTopic source and inspect where it embeds candidate words and topic sentences. Confirm the current per-topic calls and verify that the completed change makes one combined embedding call while preserving identical per-topic outputs.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning, performance
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.