-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Poetry: Using PyTorch CPU (or other methods to reduce size) #1409
Comments
I'm sadly not familiar with poetry. But installing Pytorch only with CPU support works and significantly reduced the docker image size. I did it here for another project: |
Thanks. It may not be unique to Poetry. Our main issue is that when we install the CPU only version, by resolving dependencies we still end up with the regular one because the CPU only one we have doesn't fulfill the S-BERT dependency. Did your setup meet the dependencies / version ranges listed by S-BERT these days? If so, we might be able to adjust our versions and maybe what we are doing will work. |
Yes, the setup works and you get the CPU version of Pytorch. As mentioned, I'm just familiar with pip, so sadly don't know what happens with poetry there. But maybe you can install sentence-transformers without dependencies? So you install:
|
That's a great lead. Do you have the versions that you used for these that worked? |
No specific version needed. For pytorch I used 1.8.0 as it was the most recent when I created the docker. |
How would I "Install sentence transformers without dependencies"? Thanks. I got the same problem -- I only got 2G disk available. I tried to |
First install the dependencies individually and then install sentence-transformers without dependencies |
👍 Thanks for the reply. I give another try. |
pip install --no-cache-dir torch==1.8.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
pip install transformers tqdm numpy scikit-learn scipy nltk sentencepiece
pip install sentence-transformers I tried this in debian 11 python 3.8.13 -- it does not seem to work. The last step ( |
OK I got it: pip install --no-deps sentence-transformers |
I think the issue happens as pip isn't able to resolve dependencies with suffixes like '+cpu' after the version number. So, if you have a CPU only version of torch, it fails the dependency check 'torch>=1.6.0' in sentence-transformers. 2 solutions.
|
This worked for me.
|
A
(To add support for Windows, find the wheel in the list here). |
I also needed pillow |
This came from a slightly different context of me trying to get sentence transformers working as a dependency in a CI runner. Not exactly a pure poetry solution, but it will allow you to still use poetry outside of the docker build.
From here you could uninstall the libraries you specifically want to exclude,
|
For a pip only solution just add
I am not too familiar with |
It would be great if both this project and pytorch allowed you to I suspect quite a few users of this library will end up wanting to instrument simple services where they occassionally generate embeddings/vectors for small samples of text, possibly surrounded by cache layers, and not have something so complex where they also need to instrument GPUs to run the workloads efficiently. |
@cyrfar's solution worked for me. Saved my bacon. Thanks! |
Hey! I wanted to share a solution I found for managing CPU-only dependencies with Poetry, particularly for PyTorch and sentence-transformers. This approach ensures no unnecessary GPU-related dependencies are installed, keeping the environment and Docker images lightweight. Here's the
Key points:
To use this configuration:
I hope this helps others facing the same issue. Feel free to adapt the wheel URLs or system markers as required for your setup. |
@vitormanita, your method works — my team did that independently of your comment literally yesterday! But, a problem in how Poetry's requirements.txt export plugin works blocks us from exporting a requirements.txt used in building our production containers. See python-poetry/poetry-plugin-export#183 (comment) if you try this and get a "Dependency walk failed" error in Poetry 1.7.x and older. |
The way I went about it was using this stackoverflow answer to add the
btw @colindean I had the exact same issue with exporting to requirements.txt with the above answer as well, but with this way I don't seem to run into that 🤷 |
@colindean , |
In some projects we are doing, we are running into some issues with docker container sizes getting quite large. One of the main culprits is PyTorch. As we are deploying to systems that don't have GPU's, we believe could save space by using the PyTorch CPU only release. However, we have had some trouble trying to get Poetry to set up dependencies in a way where S-BERT will use PyTorch CPU rather than also installing its preferred PyTorch flavor.
Is there any advice or recipies for using Poetry to install a version of PyTorch CPU-only that S-BERT will accept to fill its dependencies (or to override the dependencies in a way that is safe)? We are also welcome to hear other methods to try to reduce image size, but PyTorch is currently our focus as it is substantially larger than other dependencies.
The text was updated successfully, but these errors were encountered: