BERTopic icon indicating copy to clipboard operation
BERTopic copied to clipboard

Suggestion & Feedback: Add support for Apple Silicons' GPU support

Open junxi-liu opened this issue 2 years ago • 5 comments

First of all thanks a lot for this amazing package! I am currently using a MacBook Pro 2021 version with M1 Pro chip. As many Apple users may know, PyTorch now supports GPU acceleration by using mps as computing device. However, currently in the BERTopic model, there is no support for this parameter.

After digging into the details for some time I found a easy work around: SentenceTransformer do supports the device parameter, so we can simply do the following:

embedding = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2', device = 'mps')

model = BERTopic(language="chinese (simplified)", embedding_model=embedding, nr_topics='auto',verbose=True, vectorizer_model=vectorizer,)

which will perform exactly as the default model if an user chooses to. On my Mac, this drastically reduce the training time from 4h30m to 58m.

I assume this should be also easy to incorporate into the package, which will be very helpful. Thanks!

junxi-liu avatar Aug 14 '23 16:08 junxi-liu

Thanks for the suggestion! It is indeed troublesome that the right device is nog chosen by default it seems. I am not sure though how that would be integrated within BERTopic since it is relatively minor to give it a parameter to tweak. For example, it would only work for SentenceTransformers and not necessarily for all other embedding models. I want to limit the parameter space as much as possible to prevent an overcomplicated user experience. Perhaps a mention in the documentation would be more appropriate?

MaartenGr avatar Aug 16 '23 15:08 MaartenGr

I realized that sentence_transformers utilize pytorch that supports mps by accident. A documentation entry may suffice indeed. But maybe this all concerns sentence_transformers not bertopic necessarily.

netique avatar Nov 26 '23 20:11 netique

There is a PR open that is relevant to this discussion: https://github.com/UKPLab/sentence-transformers/pull/2342 When this PR is merged, it should automatically select the right device.

MaartenGr avatar Nov 29 '23 08:11 MaartenGr

I've merged https://github.com/UKPLab/sentence-transformers/pull/2342. I intend to release within the next weeks to include it into SentenceTransformers 2.3.0. Hope that helps.

On an unrelated note, much respect for you and your work @MaartenGr.

  • Tom Aarsen

tomaarsen avatar Dec 07 '23 13:12 tomaarsen

@tomaarsen Thanks for replying here! It will definitely help users wanting an easier workflow. Thank you for the kind words! Glad to have you on the sentence-transformers package. You're an amazing engineer and I'm excited to see your work in the sentence-transformers package.

MaartenGr avatar Dec 08 '23 11:12 MaartenGr