20

我最近发现poetry管理依赖项。在一个项目中,我们使用 PyTorch。我该如何添加这个poetry

我们正在研究无法访问 CUDA GPU(用于简单的道路推理/测试)的机器和我们可以访问 CUDA GPU 的工作站。是否可以使用诗歌来确保每个开发人员都使用相同的 PyTorch 版本?

似乎没有明显的方法来决定要安装哪个 PyTorch 版本。我曾考虑将不同的安装说明添加为额外的依赖项,但我找不到获得等效设置的选项,例如:

pip3 install torch==1.3.1+cpu torchvision==0.4.2+cpu -f https://download.pytorch.org/whl/torch_stable.html

我可以设置不同在线轮子的总路径,例如: https://download.pytorch.org/whl/torch_stable.html/cpu/torch-1.3.1%2Bcpu-cp36-cp36m-win_amd64.whl

但我宁愿不直接在 git 中使用它们……我在诗歌中看到的最接近的选项是手动下载它们,然后使用file = X命令。

4

5 回答 5

17

目前,Poetry 没有-f选项(有一个open issue和一个open PR),所以你不能使用这些pip说明。您可以直接安装.whl文件:

poetry add https://download.pytorch.org/whl/torch_stable.html/cpu/torch-1.3.1%2Bcpu-cp36-cp36m-win_amd64.whl

或将依赖项直接添加到您的.toml文件中:

[tool.poetry.dependencies]
torch = { url = "https://download.pytorch.org/whl/torch_stable.html/cpu/torch-1.3.1%2Bcpu-cp36-cp36m-win_amd64.whl" }
于 2020-02-09T13:10:33.473 回答
8

在这个问题上花了几个小时后,我找到了一个“解决方案”,将 Poetry 和 pip 仅用于 PyTorch。您无需直接指定车轮 URL,因此保持跨平台。

我正在使用Poe The Poet,这是一个很好的 Poetry 任务运行器,它允许运行任何任意命令。

[tool.poetry.dev-dependencies]
poethepoet = "^0.10.0"

[tool.poe.tasks]
force-cuda11 = "python -m pip install torch==1.8.0+cu111 torchvision==0.9.0+cu111 -f https://download.pytorch.org/whl/torch_stable.html"

你可以运行:

poetry install

接着:

poe force-cuda11  # relies on pip and use PyTorch wheels repo
于 2021-03-15T19:27:25.790 回答
7

Poetry github 中此问题的更新解决方案:

poetry add torch --platform linux --python "^3.7"
于 2020-12-09T20:57:51.260 回答
2

我正在维护一个名为Relax-poetry的分支,它是一个非常年轻的分支,但它通过以下配置支持您想要的内容:


# pyproject.toml

[tool.poetry.dependencies]
python = "^3.8"
torch = { version = "=1.90+cu111", source = "pytorch" }

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

喜欢的话可以打勾,可以和诗歌并排安装。

于 2021-08-30T13:16:10.343 回答
2

在 2021 年末,利用标记和多重约束应该会奏效。

$ poetry --version
Poetry version 1.1.11
# pyproject.toml
[tool.poetry.dependencies]
python = "~3.9"
torch = [
  {url = "https://download.pytorch.org/whl/cpu/torch-1.10.0%2Bcpu-cp39-cp39-linux_x86_64.whl", markers = "sys_platform == 'linux'"},
  {url = "https://download.pytorch.org/whl/cpu/torch-1.10.0%2Bcpu-cp39-cp39-win_amd64.whl", markers = "sys_platform == 'win32'", }
]
numpy = "^1.21.4"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
$ poetry install
The currently activated Python version 3.8.12 is not supported by the project (~3.9).
Trying to find and use a compatible version. 
Using python3.9 (3.9.9)
Creating virtualenv machine-learning in /home/redqueen/machine_learning/.venv
Updating dependencies
Resolving dependencies... (36.0s)

Writing lock file

Package operations: 3 installs, 0 updates, 0 removals

  • Installing typing-extensions (4.0.1)
  • Installing numpy (1.21.4)
  • Installing torch (1.10.0+cpu https://download.pytorch.org/whl/cpu/torch-1.10.0%2Bcpu-cp39-cp39-linux_x86_64.whl)

注意:必须列出 Numpy。否则你会得到一个导入错误。

没有 numpy:

$ python
Python 3.9.9 (main, Nov 23 2021, 00:34:08) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
/home/redqueen/machine_learning/.venv/lib/python3.9/site-packages/torch/package/_directory_reader.py:17: UserWarning: Failed to initialize NumPy: No module named 'numpy' (Triggered internally at  ../torch/csrc/utils/tensor_numpy.cpp:68.)
  _dtype_to_storage = {data_type(0).dtype: data_type for data_type in _storages}
>>> quit()

使用 numpy:

$ python
Python 3.9.9 (main, Nov 23 2021, 00:34:08) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.cuda.is_available()
False
>>> quit()

参考:

https://python-poetry.org/docs/dependency-specification/#python-restricted-dependencies

免责声明

我没有 Windows(或 Mac)来测试它。

于 2021-12-13T23:56:23.683 回答