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

topic extraction from 'Quick Start' taking forever #510

Closed
nzaw96 opened this issue Apr 16, 2022 · 22 comments
Closed

topic extraction from 'Quick Start' taking forever #510

nzaw96 opened this issue Apr 16, 2022 · 22 comments

Comments

@nzaw96
Copy link

nzaw96 commented Apr 16, 2022

Hi Maarten,
I've been following your Github. I installed Bertopic using conda. Then I tried to replicate your Quick Start to see if it's working as expected:

from bertopic import BERTopic
from sklearn.datasets import fetch_20newsgroups

docs = fetch_20newsgroups(subset='all', remove=('headers', 'footers', 'quotes'))['data']

topic_model = BERTopic()
topics, probs = topic_model.fit_transform(docs)

Then, at first I was getting the following (which goes on forever):

runfile('/Users/nayza/Desktop/YTproject/AAHSA/addictionStudy_2.py', wdir='/Users/nayza/Desktop/YTproject/AAHSA')
Ignored unknown kwarg option direction
Ignored unknown kwarg option direction
Ignored unknown kwarg option direction
Ignored unknown kwarg option direction
Ignored unknown kwarg option direction
Ignored unknown kwarg option direction
Ignored unknown kwarg option direction
Ignored unknown kwarg option direction
Traceback (most recent call last):

But then I suspected that I needed to update my tokenizer so I updated it from version 0.10.3 to 0.11.0. Then, I see that it doesn't show the 'Ignored unknown...' output anymore but it's taking forever to run. Plus, my Mac started to get really loud as well.
Do you an idea what might be an issue here?

@MaartenGr
Copy link
Owner

Typically, when running takes longer than you would have expected it is due to the absence of a GPU or perhaps some dependency issue. Having said that, which GPU are you currently using? Also, could you share the versions of all dependencies in your environment?

@MaartenGr
Copy link
Owner

Just realized this but it might be worthwhile to set verbose=True to track in which steps the algorithm is taking a long time to train. That way, it is a bit easier to understand where things are going wrong.

@nzaw96
Copy link
Author

nzaw96 commented May 1, 2022

Screen Shot 2022-05-01 at 4 37 59 PM

Hey @MaartenGr , sorry that I couldn't get back to you until now as I was dealing with my finals. So, I've attached the screenshot above. Basically, a brief recap: after updating my tokenizer version from 0.10.3 to 0.11.0, I no longer get the "Ignored unknown kwarg option direction" message but it was taking forever to run. So, I tried again with verbose=True, like you suggested, and I could see that it was running but was just taking too long. It took a little over 54 minutes to run your Quick Start tutorial, which I'm pretty sure is a lot longer than it should be. I have attached the screenshot above.

I'm running it on M1 13-inch Macbook Pro 2020, which I'm pretty sure has the integrated GPU.

Regarding the dependencies, I have the following packages:

abseil-cpp 20210324.2 hbdafb3b_0 conda-forge
absl-py 1.0.0 pyhd8ed1ab_0 conda-forge
aiohttp 3.8.1 py38h33210d7_1 conda-forge
aiosignal 1.2.0 pyhd8ed1ab_0 conda-forge
anyio 3.5.0 py38h10201cd_0 conda-forge
aom 3.3.0 h07bb92c_1 conda-forge
appnope 0.1.3 pyhd8ed1ab_0 conda-forge
argon2-cffi 21.3.0 pyhd8ed1ab_0 conda-forge
argon2-cffi-bindings 21.2.0 py38h33210d7_2 conda-forge
asttokens 2.0.5 pyhd8ed1ab_0 conda-forge
astunparse 1.6.3 pyhd8ed1ab_0 conda-forge
async-timeout 4.0.2 pyhd8ed1ab_0 conda-forge
attrs 21.4.0 pyhd8ed1ab_0 conda-forge
babel 2.9.1 pyh44b312d_0 conda-forge
backcall 0.2.0 pyh9f0ad1d_0 conda-forge
backports 1.0 py_2 conda-forge
backports.functools_lru_cache 1.6.4 pyhd8ed1ab_0 conda-forge
beautifulsoup4 4.11.1 pyha770c72_0 conda-forge
bertopic 0.9.4 pyhd8ed1ab_0 conda-forge
bleach 5.0.0 pyhd8ed1ab_0 conda-forge
blinker 1.4 py_1 conda-forge
brotli 1.0.9 h1c322ee_7 conda-forge
brotli-bin 1.0.9 h1c322ee_7 conda-forge
brotlipy 0.7.0 py38h33210d7_1004 conda-forge
bzip2 1.0.8 h3422bc3_4 conda-forge
c-ares 1.18.1 h3422bc3_0 conda-forge
ca-certificates 2021.10.8 h4653dfc_0 conda-forge
cached-property 1.5.2 hd8ed1ab_1 conda-forge
cached_property 1.5.2 pyha770c72_1 conda-forge
cachetools 4.2.4 pyhd8ed1ab_0 conda-forge
certifi 2021.10.8 pypi_0 pypi
cffi 1.15.0 py38hc67bbb8_0 conda-forge
charset-normalizer 2.0.12 pyhd8ed1ab_0 conda-forge
click 8.1.2 py38h10201cd_0 conda-forge
colorama 0.4.4 pyh9f0ad1d_0 conda-forge
cryptography 36.0.2 py38h7f91714_1 conda-forge
cycler 0.11.0 pyhd8ed1ab_0 conda-forge
cython 0.29.28 py38hea55cf5_2 conda-forge
dataclasses 0.8 pyhc8e2a94_3 conda-forge
debugpy 1.6.0 py38hea55cf5_0 conda-forge
decorator 5.1.1 pyhd8ed1ab_0 conda-forge
defusedxml 0.7.1 pyhd8ed1ab_0 conda-forge
dill 0.3.4 pypi_0 pypi
entrypoints 0.4 pyhd8ed1ab_0 conda-forge
executing 0.8.3 pyhd8ed1ab_0 conda-forge
ffmpeg 4.4.1 h2f96316_2 conda-forge
filelock 3.6.0 pyhd8ed1ab_0 conda-forge
flit-core 3.7.1 pyhd8ed1ab_0 conda-forge
fonttools 4.32.0 py38h33210d7_0 conda-forge
freetype 2.10.4 h17b34a0_1 conda-forge
frozenlist 1.3.0 py38h33210d7_1 conda-forge
gast 0.5.3 pypi_0 pypi
gettext 0.19.8.1 h049c9fb_1008 conda-forge
giflib 5.2.1 h27ca646_2 conda-forge
gmp 6.2.1 h9f76cd9_0 conda-forge
gnutls 3.6.13 h706517b_1 conda-forge
google-api-core 2.5.0 pyhd8ed1ab_0 conda-forge
google-api-python-client 2.44.0 pyhd8ed1ab_0 conda-forge
google-auth 1.35.0 pyh6c4a22f_0 conda-forge
google-auth-httplib2 0.1.0 pyhd8ed1ab_0 conda-forge
google-auth-oauthlib 0.4.6 pyhd8ed1ab_0 conda-forge
google-pasta 0.2.0 pyh8c360ce_0 conda-forge
googleapis-common-protos 1.56.0 py38h10201cd_0 conda-forge
grpc-cpp 1.42.0 hedfbb7c_1 conda-forge
grpcio 1.42.0 py38h69ee544_0 conda-forge
h5py 3.6.0 nompi_py38hacf61ce_100 conda-forge
hdbscan 0.8.28 py38h691f20f_1 conda-forge
hdf5 1.12.1 nompi_hf9525e8_104 conda-forge
httplib2 0.20.4 pyhd8ed1ab_0 conda-forge
huggingface_hub 0.5.1 pyhd8ed1ab_0 conda-forge
icu 69.1 hbdafb3b_0 conda-forge
idna 3.3 pyhd8ed1ab_0 conda-forge
importlib-metadata 4.11.3 py38h10201cd_1 conda-forge
importlib_metadata 4.11.3 hd8ed1ab_1 conda-forge
importlib_resources 5.7.0 pyhd8ed1ab_0 conda-forge
ipykernel 6.13.0 py38h2cb4d76_0 conda-forge
ipython 8.2.0 py38h10201cd_0 conda-forge
ipython_genutils 0.2.0 py_1 conda-forge
ipywidgets 7.7.0 pyhd8ed1ab_0 conda-forge
jbig 2.1 h3422bc3_2003 conda-forge
jedi 0.18.1 py38h10201cd_1 conda-forge
jinja2 3.1.1 pyhd8ed1ab_0 conda-forge
joblib 1.1.0 pyhd8ed1ab_0 conda-forge
jpeg 9e h3422bc3_0 conda-forge
json5 0.9.5 pyh9f0ad1d_0 conda-forge
jsonschema 4.4.0 pyhd8ed1ab_0 conda-forge
jupyter 1.0.0 py38h10201cd_7 conda-forge
jupyter_client 7.2.2 pyhd8ed1ab_1 conda-forge
jupyter_console 6.4.3 pyhd8ed1ab_0 conda-forge
jupyter_core 4.9.2 py38h10201cd_0 conda-forge
jupyter_server 1.16.0 pyhd8ed1ab_0 conda-forge
jupyterlab 3.3.4 pyhd8ed1ab_0 conda-forge
jupyterlab_pygments 0.2.2 pyhd8ed1ab_0 conda-forge
jupyterlab_server 2.12.0 pyhd8ed1ab_0 conda-forge
jupyterlab_widgets 1.1.0 pyhd8ed1ab_0 conda-forge
keras 2.8.0 pypi_0 pypi
keras-preprocessing 1.1.2 pyhd8ed1ab_0 conda-forge
kiwisolver 1.4.2 py38h0f71543_1 conda-forge
krb5 1.19.3 hf9b2bbe_0 conda-forge
lame 3.100 h27ca646_1001 conda-forge
lcms2 2.12 had6a04f_0 conda-forge
lerc 3.0 hbdafb3b_0 conda-forge
libblas 3.9.0 14_osxarm64_openblas conda-forge
libbrotlicommon 1.0.9 h1c322ee_7 conda-forge
libbrotlidec 1.0.9 h1c322ee_7 conda-forge
libbrotlienc 1.0.9 h1c322ee_7 conda-forge
libcblas 3.9.0 14_osxarm64_openblas conda-forge
libclang 13.0.0 pypi_0 pypi
libcurl 7.82.0 hb0e6552_0 conda-forge
libcxx 13.0.1 h6a5c8ee_0 conda-forge
libdeflate 1.10 h3422bc3_0 conda-forge
libedit 3.1.20191231 hc8eb9b7_2 conda-forge
libev 4.33 h642e427_1 conda-forge
libffi 3.4.2 h3422bc3_5 conda-forge
libgfortran 5.0.0.dev0 11_0_1_hf114ba7_23 conda-forge
libgfortran5 11.0.1.dev0 hf114ba7_23 conda-forge
libiconv 1.16 h642e427_0 conda-forge
liblapack 3.9.0 14_osxarm64_openblas conda-forge
libllvm11 11.1.0 h93073aa_3 conda-forge
libnghttp2 1.47.0 he723fca_0 conda-forge
libopenblas 0.3.20 openmp_h2209c59_0 conda-forge
libpng 1.6.37 hf7e6567_2 conda-forge
libprotobuf 3.19.4 hccf11d3_0 conda-forge
libsodium 1.0.18 h27ca646_1 conda-forge
libssh2 1.10.0 hb80f160_2 conda-forge
libtiff 4.3.0 h77dc3b6_3 conda-forge
libvpx 1.11.0 hc470f4d_3 conda-forge
libwebp 1.2.2 h0d20362_0 conda-forge
libwebp-base 1.2.2 h3422bc3_1 conda-forge
libxcb 1.13 h9b22ae9_1004 conda-forge
libxml2 2.9.12 hedbfbf4_1 conda-forge
libzlib 1.2.11 h90dfc92_1014 conda-forge
llvm-openmp 13.0.1 h455960f_1 conda-forge
llvmlite 0.38.0 py38hd1051fe_1 conda-forge
lz4-c 1.9.3 hbdafb3b_1 conda-forge
markdown 3.3.6 pyhd8ed1ab_0 conda-forge
markupsafe 2.1.1 py38h33210d7_1 conda-forge
matplotlib 3.5.1 py38h150bfb4_0 conda-forge
matplotlib-base 3.5.1 py38hb140015_0 conda-forge
matplotlib-inline 0.1.3 pyhd8ed1ab_0 conda-forge
mistune 0.8.4 py38hea4295b_1005 conda-forge
multidict 6.0.2 py38h33210d7_1 conda-forge
munkres 1.1.4 pyh9f0ad1d_0 conda-forge
nbclassic 0.3.7 pyhd8ed1ab_0 conda-forge
nbclient 0.5.13 pyhd8ed1ab_0 conda-forge
nbconvert 6.4.5 py38h10201cd_0 conda-forge
nbformat 5.3.0 pyhd8ed1ab_0 conda-forge
ncurses 6.3 h07bb92c_1 conda-forge
nest-asyncio 1.5.5 pyhd8ed1ab_0 conda-forge
nettle 3.6 hc6a1b29_0 conda-forge
ninja 1.10.2 hc021e02_1 conda-forge
nltk 3.6.7 pyhd8ed1ab_0 conda-forge
notebook 6.4.10 pyha770c72_0 conda-forge
notebook-shim 0.1.0 pyhd8ed1ab_0 conda-forge
numba 0.55.1 py38h63e909c_0 conda-forge
numpy 1.21.6 py38hf29d37f_0 conda-forge
oauthlib 3.2.0 pyhd8ed1ab_0 conda-forge
openh264 2.1.1 habe5f53_0 conda-forge
openjpeg 2.4.0 h062765e_1 conda-forge
openssl 1.1.1n h90dfc92_0 conda-forge
opt_einsum 3.3.0 pyhd8ed1ab_1 conda-forge
packaging 21.3 pyhd8ed1ab_0 conda-forge
pandas 1.4.2 py38h55de146_1 conda-forge
pandocfilters 1.5.0 pyhd8ed1ab_0 conda-forge
parso 0.8.3 pyhd8ed1ab_0 conda-forge
patsy 0.5.2 pyhd8ed1ab_0 conda-forge
pexpect 4.8.0 pyh9f0ad1d_2 conda-forge
pickleshare 0.7.5 py_1003 conda-forge
pillow 9.1.0 py38h7ff1586_2 conda-forge
pip 22.0.4 pyhd8ed1ab_0 conda-forge
plotly 5.7.0 pyhd8ed1ab_0 conda-forge
prometheus_client 0.14.1 pyhd8ed1ab_0 conda-forge
promise 2.3 pypi_0 pypi
prompt-toolkit 3.0.29 pyha770c72_0 conda-forge
prompt_toolkit 3.0.29 hd8ed1ab_0 conda-forge
protobuf 3.20.0 pypi_0 pypi
psutil 5.9.0 py38h33210d7_1 conda-forge
pthread-stubs 0.4 h27ca646_1001 conda-forge
ptyprocess 0.7.0 pyhd3deb0d_0 conda-forge
pure_eval 0.2.2 pyhd8ed1ab_0 conda-forge
pyasn1 0.4.8 py_0 conda-forge
pyasn1-modules 0.2.8 pypi_0 pypi
pycparser 2.21 pyhd8ed1ab_0 conda-forge
pygments 2.11.2 pyhd8ed1ab_0 conda-forge
pyjwt 2.3.0 pyhd8ed1ab_1 conda-forge
pynndescent 0.5.6 pyh6c4a22f_0 conda-forge
pyopenssl 22.0.0 pyhd8ed1ab_0 conda-forge
pyparsing 3.0.8 pyhd8ed1ab_0 conda-forge
pyrsistent 0.18.1 py38h33210d7_1 conda-forge
pysocks 1.7.1 py38h10201cd_5 conda-forge
python 3.8.13 h7e095e3_0_cpython conda-forge
python-dateutil 2.8.2 pyhd8ed1ab_0 conda-forge
python-fastjsonschema 2.15.3 pyhd8ed1ab_0 conda-forge
python-flatbuffers 2.0 pyhd8ed1ab_0 conda-forge
python.app 1.4 py38h561f5be_0 conda-forge
python_abi 3.8 2_cp38 conda-forge
pytorch 1.11.0 cpu_py38h1b6422d_0 conda-forge
pytz 2022.1 pyhd8ed1ab_0 conda-forge
pyu2f 0.1.5 pyhd8ed1ab_0 conda-forge
pyyaml 5.4.1 py38hea4295b_1 conda-forge
pyzmq 22.3.0 py38h5f86631_2 conda-forge
re2 2021.11.01 hbdafb3b_0 conda-forge
readline 8.1 hedafd6a_0 conda-forge
regex 2022.3.15 py38h33210d7_1 conda-forge
requests 2.27.1 pyhd8ed1ab_0 conda-forge
requests-oauthlib 1.3.1 pyhd8ed1ab_0 conda-forge
rsa 4.8 pyhd8ed1ab_0 conda-forge
sacremoses 0.0.49 pyhd8ed1ab_0 conda-forge
scikit-learn 1.0.2 py38h2cd4032_0 conda-forge
scipy 1.8.0 py38hd0c9ec0_1 conda-forge
seaborn 0.11.2 hd8ed1ab_0 conda-forge
seaborn-base 0.11.2 pyhd8ed1ab_0 conda-forge
send2trash 1.8.0 pyhd8ed1ab_0 conda-forge
sentence-transformers 2.0.0 pyhd8ed1ab_0 conda-forge
sentencepiece 0.1.96 py38h0f71543_1 conda-forge
setuptools 62.1.0 py38h10201cd_0 conda-forge
six 1.15.0 pypi_0 pypi
sleef 3.5.1 h156473d_2 conda-forge
snappy 1.1.8 hc88da5d_3 conda-forge
sniffio 1.2.0 py38h10201cd_3 conda-forge
soupsieve 2.3.1 pyhd8ed1ab_0 conda-forge
sqlite 3.38.2 h7e3ccbd_0 conda-forge
stack_data 0.2.0 pyhd8ed1ab_0 conda-forge
statsmodels 0.13.2 py38h691f20f_0 conda-forge
svt-av1 0.9.1 h07bb92c_0 conda-forge
tbb 2021.5.0 h3e96240_1 conda-forge
tenacity 8.0.1 pyhd8ed1ab_0 conda-forge
tensorboard 2.8.0 pypi_0 pypi
tensorboard-data-server 0.6.1 pypi_0 pypi
tensorboard-plugin-wit 1.8.1 pyhd8ed1ab_0 conda-forge
tensorflow 2.7.0 cpu_py38h4487131_0 conda-forge
tensorflow-base 2.7.0 cpu_py38h93cb87e_0 conda-forge
tensorflow-datasets 4.5.2 pypi_0 pypi
tensorflow-deps 2.8.0 0 apple
tensorflow-estimator 2.7.0 cpu_py38h4dea37b_0 conda-forge
tensorflow-macos 2.8.0 pypi_0 pypi
tensorflow-metadata 1.7.0 pypi_0 pypi
tensorflow-metal 0.4.0 pypi_0 pypi
termcolor 1.1.0 pypi_0 pypi
terminado 0.13.3 py38h10201cd_1 conda-forge
testpath 0.6.0 pyhd8ed1ab_0 conda-forge
textblob 0.15.3 py_0 conda-forge
tf-estimator-nightly 2.8.0.dev2021122109 pypi_0 pypi
threadpoolctl 3.1.0 pyh8a188c0_0 conda-forge
tk 8.6.12 he1e0b03_0 conda-forge
tokenizers 0.11.0 py38he82c38c_0 conda-forge
torchvision 0.12.0 cpu_py38h13a9dc5_1 conda-forge
tornado 6.1 py38h33210d7_3 conda-forge
tqdm 4.64.0 pyhd8ed1ab_0 conda-forge
traitlets 5.1.1 pyhd8ed1ab_0 conda-forge
transformers 3.3.1 py_0 conda-forge
typing-extensions 4.1.1 hd8ed1ab_0 conda-forge
typing_extensions 4.1.1 pyha770c72_0 conda-forge
umap-learn 0.5.3 py38h10201cd_0 conda-forge
unicodedata2 14.0.0 py38h33210d7_1 conda-forge
uritemplate 4.1.1 pyhd8ed1ab_0 conda-forge
urllib3 1.26.9 pyhd8ed1ab_0 conda-forge
wcwidth 0.2.5 pyh9f0ad1d_2 conda-forge
webencodings 0.5.1 py_1 conda-forge
websocket-client 1.3.2 pyhd8ed1ab_0 conda-forge
werkzeug 2.1.1 pyhd8ed1ab_0 conda-forge
wheel 0.37.1 pyhd8ed1ab_0 conda-forge
widgetsnbextension 3.6.0 py38h10201cd_0 conda-forge
wrapt 1.14.0 py38h33210d7_1 conda-forge
x264 1!161.3030 h3422bc3_1 conda-forge
x265 3.5 hbc6ce65_3 conda-forge
xorg-libxau 1.0.9 h27ca646_0 conda-forge
xorg-libxdmcp 1.1.3 h27ca646_0 conda-forge
xz 5.2.5 h642e427_1 conda-forge
yaml 0.2.5 h3422bc3_2 conda-forge
yarl 1.7.2 py38h33210d7_2 conda-forge
zeromq 4.3.4 hbdafb3b_1 conda-forge
zipp 3.8.0 pyhd8ed1ab_0 conda-forge
zlib 1.2.11 h90dfc92_1014 conda-forge
zstd 1.5.2 h861e0a7_0 conda-forge

Thank you and I look forward to hearing from you.

@MaartenGr
Copy link
Owner

There are several things happening here that I believe might explain the issue you are experiencing.

Screen Shot 2022-05-01 at 4 37 59 PM

Starting with the screenshot that you posted. It seems that you checked the GPU instance with tensorflow whilst sentence-transformers rely primarily on pytorch. I should have been more clear about that, my apologies! Could you instead follow along with the code here and share the results?

So, I tried again with verbose=True, as you suggested, and I could see that it was running but was just taking too long.

Based on the output, it seems that both UMAP and HDBSCAN are relatively fast and only take one or two minutes to compute. Moreover, from the tqdm progress bar it is clear that embedding the documents takes the most time here. Which takes me to what you mentioned below.

I'm running it on M1 13-inch Macbook Pro 2020, which I'm pretty sure has the integrated GPU.

It could be that although tensorflow recognizes the GPU, the same might not be said for pytorch. Another reason for the long computation time is that although your laptop has an integrated GPU it might not be a fast one. Although the existence of a GPU definitely helps it might also depend on the GPU itself.

Although I am not entirely certain yet, my guess would be either pytorch nog recognizing the GPU on a M1 chip or that the GPU is quite slow. However, I have little familiarity with Macbooks...

@nzaw96
Copy link
Author

nzaw96 commented May 2, 2022

Screen Shot 2022-05-02 at 5 32 20 PM

Screen Shot 2022-05-02 at 5 32 33 PM

Hi @MaartenGr, I followed along the code for the gpu test you suggested. Above are the results. It looks like indeed Pytorch isn't detecting my GPU. I also tried the gpu test with tensorflow gpu test using 'tf.test.gpu_device_name()' and I get the error that says 'Could not identify NUMA node...' which is shown at the top of the screenshot. I'm not exactly sure how to interpret it correctly (I tried stackoverflow but couldn't really get a solid answer).

So yeah basically looks like when I ran your 'Quick Start' tutorial it ran on CPU. Any thoughts on I can tell jupyter to run on GPU and not CPU?

I bought this macbook thinking that M1 chip would help run ML experiments a lot faster but it has been nothing but a headache.

@MaartenGr
Copy link
Owner

I think this is related to the M1 chip that you are using that doesn't natively supports cuda. From what I could gather here, it seems that they are currently working hard on GPU acceleration for the M1 chips but that there isn't a concrete time yet when they think it can be published.

Unfortunately, that seems to mean that it is not possible as of this moment and you will have to wait until they added support. Instead, it might be interesting to use USE instead of sentence-transformers as it uses tensorflow, and, from what I remember, is quite fast.

@nzaw96
Copy link
Author

nzaw96 commented May 11, 2022

Screen Shot 2022-05-11 at 3 39 08 PM

Hi @MaartenGr , I tried USE like you suggested. Please take a look at the screenshot above. As you can see this one only took approximately 11.5 minutes which is five times faster than running BERTopic using Pytorch. However, I'm not quite sure if my Tensorflow is running on M1 GPU as well since I feel it shouldn't take 11 minutes. In particular, I want to refer to the Warning in the screenshot above:

"""
WARNING:tensorflow:From /Users/nayza/tensorflow-test/env/lib/python3.8/site-packages/bertopic/backend/_use.py:52: _EagerTensorBase.cpu (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.identity instead.
"""

Have you seen this warning before? I'm not quite sure how to interpret this to be honest but it looks to me like Tensorflow is attempting to run/running on CPU. Would you be able to share how long it took you to run your 'Quick Start' code above? Thank you and I look forward to hearing from you.

@nzaw96
Copy link
Author

nzaw96 commented May 11, 2022

Screen Shot 2022-05-11 at 3 48 09 PM

Also another quick follow-up, I did the tensorflow GPU access test and as you can see in the screenshot, it suggests that Tensorflow is able to access the GPU. However, when I tried

                     tf.test.gpu_device_name()

I got the following error (which is shown in the at the bottom of the screenshot above):

"""
2022-05-11 15:22:03.986737: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2022-05-11 15:22:03.987312: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: )
"""

So, I'm not sure if it means my Tensorflow can access the GPU but couldn't run on it (?)

@MaartenGr
Copy link
Owner

Have you seen this warning before? I'm not quite sure how to interpret this to be honest but it looks to me like Tensorflow is attempting to run/running on CPU. Would you be able to share how long it took you to run your 'Quick Start' code above? Thank you and I look forward to hearing from you.

Unfortunately, I do not have experience with Tensorflow on M1 chips, so I am not sure how much I can be when debugging this. It does seem that you are not the only one experiencing this issue although no clear solutions seem to be given.

However, I'm not quite sure if my Tensorflow is running on M1 GPU as well since I feel it shouldn't take 11 minutes. In particular, I want to refer to the Warning in the screenshot above:

I just ran your code in a Kaggle notebook and for me, it can create the embeddings in only one minute, which is quite a bit faster than your case. I should note that because it has different hardware, differences are to be expected.

It might be helpful to disable the GPU in order to see if the bottleneck can somehow be found there.

@leifericf
Copy link

I'm having the same performance problem on a MacBook Pro (16-inch, 2021) Apple M1 Pro with macOS Monterey 12.3.1. I'll probably spin up a virtual machine in the cloud until the M1 chip supports GPU acceleration.

@MaartenGr
Copy link
Owner

It seems that quite recently PyTorch started supporting M1 chips and now allows for GPU acceleration. You can find a bit more about that here. Do note that I have not tested this newest version out with BERTopic yet, so I cannot be sure it is working with the current set of dependencies. Having said that, with a bit of luck, this might solve the issues M1 users are having.

@leifericf
Copy link

leifericf commented Jun 4, 2022

Thanks for the tip, @MaartenGr!

To get started, just install the latest Preview (Nightly) build […]"

Can I somehow "force" BERTopic to use the latest preview/nightly build of PyTorch to try it out?

Edit:

I suppose I would have to clone the BERTopic repository, chance the PyTorch dependency to the latest preview (nightly) build, then install my own fork on BERTopic in my Python project? That's what I'm trying to do now.

I found the dependency to PyTorch in setup.py.

But I'm not sure how to change that to include the preview (nightly) build of PyTorch.

I've installed the preview (nightly) build locally via pip, as recommended in the documentation:

pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu

But since I'm using virtual environments and Poetry to manage my Python project, the version of PyTorch which comes bundled with BERTopic seems to take precedence over the system-wide installation (as expected).

@MaartenGr
Copy link
Owner

@leifericf Since you are using Poetry, I believe you will have to do some manual installation as I am not sure whether supplying a link during the pip install is possible within Poetry. If you are okay with a manual pip install, then I would advise installing BERTopic first and after that install the nightly pytorch as follows:

pip3 install --upgrade --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cu113

That will override the pytorch install that you made before with BERTopic and install the nightly pytorch.

@leifericf
Copy link

Thank you for the advice, @MaartenGr!

I think you're right, and I got stuck trying to install the pre-release version of PyTorch via Poetry. I opened this issue on Stack Overflow to get help with that. I think it's possible somehow, but I'm not skilled enough with the more advanced options of Poetry. I'll update this issue if I figure out how to do it.

@leifericf
Copy link

leifericf commented Jun 7, 2022

Update:

I managed to successfully install the latest preview (nightly) build of PyTorch and the newest version of BERTopic after ditching Poetry in favor of using conda and pip together (following these guidelines carefully).

Here is how I installed PyTorch:

$ conda install pytorch -c pytorch-nightly

Then, I installed BERTopic via pip (from within my conda virtual environment):

$ pip install bertopic

When installing BERTopic, I encountered two other issues.

The first issue was that the tokenizers library (one of BERTopics dependencies) failed to build. This was because the Rust compiler was not installed on my system. I fixed this by installing the Rust compiler:

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

See this issue for more detailed information.

The second issue was a bit more tricky. The sentencepiece library failed to build. To fix this, I first had to update my macOS command-line tools:

$ xcode-select --install

Then, I had to download the newest source code (from here), and use pip to build and install it from source:

$ wget https://files.pythonhosted.org/packages/aa/71/bb7d64dcd80a6506146397bca7310d5a8684f0f9ef035f03affb657f1aec/sentencepiece-0.1.96.tar.gz ~/Downloads

$ pip -v install ~/Downloads/sentencepiece-0.1.96.tar.gz

See this issue for more detailed information.

Here is gist containing an export of my conda environment after all of that was fixed.

But now I'm getting this somewhat cryptic error when I try to use BERTopic:

$  cd ~/Code/my-project ; /usr/bin/env ~/opt/miniconda3/envs/my-project/bin/python ~/.vscode/extensions/ms-python.python-2022.6.3/pythonFiles/lib/python/debugpy/launcher 61574 -- ~/Code/my-project/src/nl
p.py 
Traceback (most recent call last):
  File "~/Code/my-project/src/nlp.py", line 1, in <module>
    from bertopic import BERTopic
  File "~/opt/miniconda3/envs/my-project/lib/python3.10/site-packages/bertopic/__init__.py", line 1, in <module>
    from bertopic._bertopic import BERTopic
  File "~/opt/miniconda3/envs/my-project/lib/python3.10/site-packages/bertopic/_bertopic.py", line 21, in <module>
    import hdbscan
  File "~/opt/miniconda3/envs/my-project/lib/python3.10/site-packages/hdbscan/__init__.py", line 1, in <module>
    from .hdbscan_ import HDBSCAN, hdbscan
  File "~/opt/miniconda3/envs/my-project/lib/python3.10/site-packages/hdbscan/hdbscan_.py", line 20, in <module>
    from ._hdbscan_linkage import (
ImportError: dlopen(~/opt/miniconda3/envs/my-project/lib/python3.10/site-packages/hdbscan/_hdbscan_linkage.cpython-310-darwin.so, 0x0002): tried: '~/opt/miniconda3/envs/my-project/lib/python3.10/site-packages/hdbscan/_hdbscan_linkage.cpython-310-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))

That error is unrelated to "quick start taking forever," so I will create a separate bug report for it.

I hope this information can help fellow Apple M1 Pro users get started with GPU-accelerated BERTopic.

@leifericf
Copy link

leifericf commented Jun 8, 2022

I have opened the issue mentioned above here.

@leifericf
Copy link

leifericf commented Jun 8, 2022

Scratch my last comment.

I've been struggling all day, and there is some flavor of circular dependency hell between pytorch, hdbscan, and blas because of other packages I'm using. At this point, I've tried to recreate my conda environment probably 30+ times, using conda and pip in different orders, manually editing my environment.yaml, etc.

I have been able to fix each of the different issues independently, but every time I fix one problem, it causes another. I'm not smart enough to figure this out, so I'm just going to give up. I might try again after the most recent version of BERTopic is available via conda-forge (instead of pip), and the version of PyTorch with support for Apple M1 Pro is stable.

I will try to use Gensim for topic modeling instead for the time being.

@MaartenGr
Copy link
Owner

That is a shame to hear! Hopefully, other users will figure out a way to approach it as I unfortunately do not have a MacBook to test on. At the very least, thank you for going in that much depth trying to figure out a solution.

@leifericf
Copy link

No problem, and thank you for sharing this project and taking the time to help. I should emphasize that the issues I'm experiencing are not the fault of BERTopic. My particular project seems to have irreconcilable dependencies when using conda and pip on an M1 Mac. And certain low-level dependencies are missing wheels, won't compile, etc. I still want to use BERTopic, and I’m happy to help with testing and debugging, but I might need some guidance to be helpful. Please let me know if and how I can help.

@leifericf
Copy link

leifericf commented Jun 10, 2022

Good news! I got BERTopic working now after the latest version was added to conda-forge.

To avoid dependency conflicts, I had to create a new (empty) Conda environment, install BERTopic, and install the PyTorch preview (nightly) build in that specific order. This is exactly what I did:

$ conda create --name bertopic

$ conda activate bertopic

$ conda install -c conda-forge bertopic

$ conda install pytorch -c pytorch-nightly

And here is a gist showing the resulting conda environment.

Attempting to install BERTopic and PyTorch in my existing Conda environment (which includes a lot of other packages for my project) resulted in an insane amount of dependency conflicts. I had to start with a clean environment.

That said, I'm unsure whether I'm utilizing Apple's GPU. I don't know how to tell, except by looking at the verbose output of BERTopic to see if it's faster than before. Batching fetch_20newsgroups runs at ~2.2 seconds per iteration.

And I can see that the CPU/GPU usage is quite high and constant:

bertopic-cpu-usage

@MaartenGr
Copy link
Owner

Glad to hear that the installation has gone well! The difference in speed when encoding the documents to embeddings is where you will see the most difference between using a CPU and GPU. If you find that there is a big speedup, you will indeed know that it has worked. Thanks for sharing the steps, that will definitely help out others experiencing this issue!

@fxavier-maf
Copy link

@leifericf this was super helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants