Instructions for installing PyTorch

See original GitHub issue
  • I have searched the issues of this repo and believe that this is not a duplicate.

Issue

As mentioned in issue https://github.com/python-poetry/poetry/issues/4231 there is some confusion around installing PyTorch with CUDA but it is now somewhat resolved. It still requires a few steps, and all options have pretty serious flaws. Below are two options that worked for me, on Poetry version 1.2.0.

Option 1 - wheel URLs for a specific platform

  • You will need to pick the specific wheels you want. These are listed here: https://download.pytorch.org/whl/torch_stable.html. E.g. if you want CUDA 11.6, Python 3.10 and Windows, search that page for cu116-cp310-cp310-win_amd64.whl to see the matches for torch, torchaudio and torchvision
  • In your pyproject.toml file add the URLs like:
[tool.poetry.dependencies]
python = "^3.10"
numpy = "^1.23.2"
torch = { url = "https://download.pytorch.org/whl/cu116/torch-1.12.1%2Bcu116-cp310-cp310-win_amd64.whl"}
torchaudio = { url = "https://download.pytorch.org/whl/cu116/torchaudio-0.12.1%2Bcu116-cp310-cp310-win_amd64.whl"}
torchvision = { url = "https://download.pytorch.org/whl/cu116/torchvision-0.13.1%2Bcu116-cp310-cp310-win_amd64.whl"}
  • Run poetry update. It will download a lot of data (many GB) and take quite some time. And this doesn’t seem to cache reliably (at least, I’ve waited 30 minutes+ at 56 Mbps three separate times while troubleshooting this, for the exact same wheels)

Note that each subsequent poetry update will do another huge download and you’ll see this message:

  • Updating torch (1.12.1+cu116 -> 1.12.1+cu116 https://download.pytorch.org/whl/cu116/torch-1.12.1%2Bcu116-cp310-cp310-win_amd64.whl)
  • Updating torchaudio (0.12.1+cu116 -> 0.12.1+cu116 https://download.pytorch.org/whl/cu116/torchaudio-0.12.1%2Bcu116-cp310-cp310-win_amd64.whl)
  • Updating torchvision (0.13.1+cu116 -> 0.13.1+cu116 https://download.pytorch.org/whl/cu116/torchvision-0.13.1%2Bcu116-cp310-cp310-win_amd64.whl)

Option 2 - alternate source

[tool.poetry.dependencies]
python = "^3.10"
numpy = "^1.23.2"
torch = { version = "1.12.1", source="torch"}
torchaudio = { version = "0.12.1", source="torch"}
torchvision = { version = "0.13.1", source="torch"}

[[tool.poetry.source]]
name = "torch"
url = "https://download.pytorch.org/whl/cu116"
secondary = true

This seems to have worked (although I already had the packages installed) but it reports errors like Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/pillow/, but I think they get installed anyway (maybe a better message would be “Can’t access pillow at ‘https://download.pytorch.org/whl/cu116’, falling back to pypi”)

Also, if you later go on to do, say poetry add pandas (a completely unrelated library) you’ll get a wall of messages like:

Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/pandas/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/pandas/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/pytz/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/python-dateutil/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/numpy/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/pillow/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/requests/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/typing-extensions/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/certifi/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/urllib3/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/idna/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/charset-normalizer/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/python-dateutil/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/six/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/pytz/
Source (torch): Authorization error accessing https://download.pytorch.org/whl/cu116/six/

This happens with or without secondary = true in the source config.

Maintainers: please feel free to edit the text of this if I’ve got something wrong.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:113
  • Comments:32 (12 by maintainers)

github_iconTop GitHub Comments

12reactions
variolamcommented, Sep 6, 2022

Solution 1 seems infeasible when working in a team with machines on different operating systems, due to the need of providing the complete URL of the wheel including operating system and exact version number.

Solution 2 seems to work, but it results in downloading every single PyTorch version that can found, independent on the operating system. I’m running Windows and the download looks like this:

image

A single installation takes around 15-20 minutes at ~250 Mbps.

10reactions
Queuecumbercommented, Nov 10, 2022

Maybe I’m misunderstanding but I think there’s some confusion

Ref: https://pytorch.org/get-started/locally/

First of all (on Linux) pip install torch does not install the CPU version, it installs the default cuda version which is currently 11.7. If you want to install a cpu version you need to use a different command: pip3 install torch --extra-index-url https://download.pytorch.org/whl/cpu. This requires an extra index URL but it also identifies a different package version using the + convention.

So you could do something like

[tool.poetry.dependencies]
torch = { version = "^1.12.1+cpu", source = "torchcpu" }

[[tool.poetry.source]]
name = "torchcpu"
url = "https://download.pytorch.org/whl/cpu"
default = false
secondary = true

if you want CPU support, or if you want a specific cuda version:

torch = { version = "^1.12.1+cu116", source = "torchcu116" }

[[tool.poetry.source]]
name = "torchcu116"
url = "https://download.pytorch.org/whl/cu116"
default = false
secondary = true

The issue that is being discussed on this bug report is that it’s really slow/throws a bunch of warnings depending on which command you run. Also I have no idea if this works on windows, but you probably shouldnt be using windows.

Do you know if it’s possible to specify two different optional versions of torch in the pyproject.toml

I was actually pretty surprised to find that this doesn’t work with config groups and I think fixing this should be considered. I think something like the following should be possible:

[tool.poetry.group.gpu]
optional = true

[tool.poetry.group.gpu.dependencies]
torch = { version = "1.12.1+cu116", source = "torchcu116" }

[tool.poetry.group.cpu]
optional = true

[tool.poetry.group.cpu.dependencies]
torch = { version = "1.12.1+cpu", source = "torchcpu" }

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[[tool.poetry.source]]
name = "torchcpu"
url = "https://download.pytorch.org/whl/cpu"
default = false
secondary = true

[[tool.poetry.source]]
name = "torchcu116"
url = "https://download.pytorch.org/whl/cu116"
default = false
secondary = true

where you can then do poetry install --with cpu for your cpu environment or poetry install --with gpu for your cu116 GPU environment. But the dependencies in all groups need to be consistent so you get Because test depends on both torch (1.12.1+cu116) and torch (1.12.1+cpu), version solving failed.. I dont think that should be enforced for optional groups unless the user tries to actually install two groups with conflicting packages

Read more comments on GitHub >

github_iconTop Results From Across the Web

Start Locally - PyTorch
To install PyTorch via pip, and do have a CUDA-capable system, in the above selector, choose OS: Linux, Package: Pip, Language: Python and...
Read more >
Install Pytorch on Windows - GeeksforGeeks
Step 3: Enter any one of the following commands (according to your system specifications) to install the latest stable release of Pytorch. 1....
Read more >
Install and configure PyTorch on your machine. | Microsoft Learn
First, you'll need to setup a Python environment. We recommend setting up a virtual Python environment inside Windows, using Anaconda as a ...
Read more >
How To Install and Use PyTorch - DigitalOcean
Step 1 — Installing PyTorch ... Let's create a workspace for this project and install the dependencies you'll need. You'll call your workspace ......
Read more >
How to Install PyTorch on Windows Step by Step | by Bryant Kou
Install Anaconda · Open Anaconda Prompt (NOT Anaconda Navigator) · conda install pytorch -c pytorch · pip install torchvision · Add environment to ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found