MaartenGr / MaartenGr/BERTopic
Memory leak in BERTopic.transform
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.8k
- Forks
- 920
- Avg merge
- 22h 24m
- Merged PRs (30d)
- 5
Description
The following minimal example repeatedly calls `BERTopic.transform` on some random texts, and it records the memory usage.
```python3
# memleak_bertopic.py
import random
import string
import psutil
from bertopic import BERTopic
def random_strings() -> [str]:
return [''.join(random.choices(string.printable, k=500)) for _ in range(200)]
model = BERTopic()
model.fit(random_strings())
print("iteration,memory_usage_in_MiB", flush=True)
for iteration in range(99999999999999):
model.transform(random_strings())
memory_usage_in_MiB = psutil.Process().memory_info().rss / (1024 * 1024)
print(f"{iteration},{memory_usage_in_MiB:.2f}", flush=True)
```
Output:
```
iteration,memory_usage_in_MiB
0,759.21
1,796.00
2,765.11
3,801.29
[...]
99,986.20
100,971.39
101,988.67
102,1006.21
[...]
488,2099.64
489,2103.87
490,2108.17
491,2121.31
[...]
761,2843.27
762,2844.84
763,2827.00
764,2859.39
[...]
961,3486.86
962,3481.87
963,3493.12
964,3511.06
[...]
```
The problem can be reproduced by building the following `Dockerfile` (no GPU):
```Dockerfile
FROM python:3.10.9
RUN pip install bertopic==0.12.0 psutil==5.9.4
ADD ./memleak_bertopic.py /
RUN python /memleak_bertopic.py
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided memleak_bertopic.py example using the Dockerfile and confirm that repeated BERTopic.transform calls increase RSS. Then trace transform's memory behavior within BERTopic and compare it across iterations. Done means the reproduction no longer shows unbounded memory growth under the supplied workload.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100