Data Mastery
Data Mastery

Reputation: 2095

Poetry install equivalent for -f flag in pip

I need to install packages with poetry (add them to pyproject.toml). I have to translate this to poetry. But in the documentation I did not find something similar to the -f flag. This is the command:

pip install torch==1.8.1+cu111 torchvision==0.9.1+cu111 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

Does anyone know how to translate that?

Upvotes: 2

Views: 853

Answers (2)

Louis Lac
Louis Lac

Reputation: 6426

Poetry (but also pip) cannot know if you want the CUDA wheel and where to find it, thus you have to explicitly tell it where to look for.

First, if you want to use the CUDA 11.8 version you should add the source explicitly, like this:

poetry source add pytorch --priority explicit https://download.pytorch.org/whl/cu118

Then, add the PyTorch and Torchvision dependencies mentioning explicitly this source:

poetry add --source pytorch torch torchvision

This should results in a TOML roughtly like this:

[tool.poetry.dependencies]
python = "^3.9,<3.12"
torch = {version = "^2.0.1", source = "pytorch"}
torchvision = {version = "^0.15.2", source = "pytorch"}

[[tool.poetry.source]]
name = "pytorch"
url = "https://download.pytorch.org/whl/cu118"
priority = "explicit"

Note that you should also add NumPy as a dependency.

This configuration supports Linux and Windows with CUDA. If you need support for macOS (without CUDA) you can edit the TOML config like this:

[tool.poetry.dependencies]
python = "^3.9,<3.12"
torch = [
  {version = "^2.0.1", source = "pytorch", platform = "!=darwin"},
  {version = "^2.0.1", source = "pypi", platform = "darwin"},
]
torchvision = [
  {version = "^0.15.0", source = "pytorch", platform = "!=darwin"},
  {version = "^0.15.0", source = "pypi", platform = "darwin"},
]

[[tool.poetry.source]]
name = "pytorch"
url = "https://download.pytorch.org/whl/cu118"
priority = "explicit"

Upvotes: 0

John Sane
John Sane

Reputation: 41

Even though this question already aged a bit, it ended up quite high in my google search. Since I think I found a quite useable solution to this problem I wanted to share it here:

Since Poetry still can't resolve pytorch+cuda bundles you have to specify the path to its specific wheel. Since that broke my use case where I need Cuda support on some platforms and CPU only on others, I ended up with this specification:

torch = [
{url="https://download.pytorch.org/whl/cu113/torch-1.11.0%2Bcu113-cp310-cp310-linux_x86_64.whl",markers = "sys_platform == 'linux'"},
{version="^1.11.0",markers = "sys_platform == 'darwin'"}]

torchvision = [
    {url="https://download.pytorch.org/whl/cu113/torchvision-0.12.0%2Bcu113-cp310-cp310-linux_x86_64.whl",markers = "sys_platform == 'linux'"},
    {version="^0.12.0",markers = "sys_platform == 'darwin'"}]

torchaudio = [
    {url="https://download.pytorch.org/whl/cu113/torchaudio-0.11.0%2Bcu113-cp310-cp310-linux_x86_64.whl",markers = "sys_platform == 'linux'"},
    {version="^0.11.0",markers = "sys_platform == 'darwin'"}]

Hope this helps, cheers!

Upvotes: 2

Related Questions