诗歌和 PyTorch

Poetry and PyTorch

我最近发现 poetry 可以管理依赖项。在一个项目中,我们使用 PyTorch。如何将其添加到 poetry

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

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

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 命令。

目前,Poetry 没有 -f 选项(有一个 open issue and an 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" }

诗歌 this issue 中的更新解决方案 github:

poetry add torch --platform linux --python "^3.7"

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

我正在使用 Poe The Poet,这是一个很好的任务 运行ner for 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

我正在维护一个名为 relaxed-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 年底,利用标记和多重约束应该会奏效。

$ 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)来测试它。