Skip to main content

Patch pip to play ball with PyTorch

Project description

pytorch-pip-shim

package

License Project Status: WIP

code

isort black mypy Lint status via GitHub Actions

tests

Test status via GitHub Actions Test coverage via codecov.io

Disclaimer

Neither this project (pytorch-pip-shim) nor its author (Philip Meier) are affiliated with PyTorch in any way. PyTorch and any related marks are trademarks of Facebook, Inc.

What is it?

pytorch-pip-shim is a small background utility that eases the installation process with pip for PyTorch and third-party packages that depend on its distributions. After the shim is inserted, you can install PyTorch with pip like you do with any other package.

Why do I need it?

PyTorch is fully pip install able, but PyPI, the default pip search index, has some limitations:

  1. PyPI regularly only allows binaries up to a size of approximately 60 MB. You can request a file size limit increase (and the PyTorch team probably did that), but it is still not enough: the Windows binaries cannot be installed through PyPI due to their size.

  2. PyTorch uses local version specifiers to indicate for which computation backend the binary was compiled, for example torch==1.6.0+cpu. Unfortunately, local specifiers are not allowed on PyPI. Thus, only the binaries compiled with the latest CUDA version are uploaded. If you do not have a CUDA capable GPU, downloading this is only a waste of bandwidth and disk capacity. If on the other hand simply don’t have the latest CUDA driver installed, you can’t use any of the GPU features.

To overcome this, PyTorch alos hosts all binaries themselves. To access them, you still can use pip install, but have to use some additional options:

$ pip install torch==1.6.0+cpu -f https://download.pytorch.org/whl/torch_stable.html

While this is certainly an improvement, it also has it downside: in addition to the computation backend, the version has to be specified exactly. Without knowing what the latest release is, it is impossible to install it as simple as pip install torch normally would.

At this point you might justifiably as: why don’t you just use conda as PyTorch recommends?

$ conda install pytorch cpuonly -c pytorch

This should cover all cases, right? Well, almost. The above command is enough if you just need PyTorch. Imagine the case of a package that depends on PyTorch, but cannot be installed with conda since it is hosted on PyPI? You can’t use the -f option since the package in question is not hosted by PyTorch. Thus, you now have to manually track down (and resolve in the case of multiple packages) the PyTorch distributions, install them in a first step and only install the actual package (and all other dependencies) afterwards.

If just want to use pip install like you always did before without worrying about any of the stuff above, pytorch-pip-shim was made for you.

How do I install it?

Installing pytorch-pip-shim is as easy as

$ pip install pytorch-pip-shim

Since it depends on pip and it might be upgraded during installation, Windows users should install it with

$ python -m pip install pytorch-pip-shim

How do I use it?

After pytorch-pip-shim is installed there is only a single step to insert the shim:

$ pytorch-pip-shim insert

After that you can use pip as you did before and pytorch-pip-shim handles the computation backend auto-detection for you in the background.

If you want to remove the shim you can do so with

$ pytorch-pip-shim remove

You can check its status with

$ pytorch-pip-shim status

How do I uninstall it?

Uninstalling is as easy as

$ pip uninstall pytorch-pip-shim

By doing so, pytorch-pip-shim automatically removes the shim if inserted.

How do I configure it?

Once inserted, you don’t need to configure anything. If you don’t want the computation backend auto-detected but rather want to set it manually pytorch-pip-shim adds two CLI options to pip install:

  • --computation-backend <computation_backend>

  • --cpu

How does it work?

The authors of pip do not condone the use of pip internals as they might break without warning. As a results of this, pip has no capability for plugins to hook into specific tasks. Thus, the only way to patch pip s functionality is to adapt its source in-place. Although this is really bad practice, it is unavoidable for the goal of this package.

pystiche-pip-shim inserts a shim into the pip main file, which decorates the main function. Everytime you call pip install, some aspects of the installation process are patched:

  • While searching for a download link for a PyTorch distribution, pytorch-pip-shim replaces the default search index. This is equivalent to calling pip install with the -f option only for PyTorch distributions.

  • While evaluating possible PyTorch installation candidates, pytorch-pip-shim culls binaries not compatible with the available hardware.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytorch_pip_shim-0.1.0.tar.gz (15.5 kB view hashes)

Uploaded Source

Built Distribution

pytorch_pip_shim-0.1.0-py3-none-any.whl (13.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page