Poetry で PyTorch + Torchvision 再々チャレンジ作業ログ(成功)
対戦履歴(全敗)
Poetry で PyTorch を入れる
Poetry で PyTorch + Torchvision
Poetry 🆚 PyTorch 総まとめ
対象 Poetry バージョンはα版の1.2.0a2です。なぜか?stableの1.1.xには PEP 440 に記された「local version identifier clause はバージョンマッチングにおいて無視しなければいけない」という規定を考慮していないという致命的なバグがあり使い物にならないからです。ここで開発ヒストリーを振り返ってみましょう
問題を指摘する issue
https://github.com/python-poetry/poetry/issues/892
https://github.com/python-poetry/poetry/issues/2543
当該バグを修正した PR
https://github.com/python-poetry/poetry-core/pull/140
masterにマージ済
マージされたのは 2021-03-28
その後、2021-04-23 に 1.2.0a1 にマージ
その後の正式版リリース(と、リリースの PR が出された日付)
1.1.6 (04-15) 当該PRへの言及なし
1.1.7 (06-26) 当該PRへの言及なし
1.1.8 (08-20) 当該PRへの言及なし
1.1.9 (09-19) 当該PRへの言及なし
1.1.10 (09-22) 当該PRへの言及なし
1.1.11 (10-05)
「次のリリースでレビューして検討するかも」と言及された
1.1.12 (11-28) 当該PRへの言及なし
明らかなバグで世界中の PyTorch ユーザが困っており、早くマージしてリリース出してほしいですね、、、
急にα版入れるのやだよね。しかし、ユーザに Poetry を要求せず Poetry を使う のやつでヌルっとやると楽
そもそも僕は常にこの方法で Poetry を使っているため…
あと pyenv も使わず、システムの Python + venv を使っています
これは完全に僕の嗜好なので、普通に pyenv + pyenv-virtualenv とかでも動くんじゃないですかね。動くといいね。
code::
$ mkdir hoge && cd hoge
$ python3 -m venv .
$ bin/pip install --pre poetry
$ bin/poetry --version # バージョン確認
Poetry (version 1.2.0a2)
$ yes '' | bin/poetry init # なんか色々設定したい人はちゃんと対話インターフェイスに入ると良い
pyproject.tomlを真面目に書く
code:pyproject.toml
tool.poetry
name = "hoge"
version = "0.1.0"
description = ""
authors = "ikr7 <kunpei.ikuta@gmail.com>"
readme = "README.md"
tool.poetry.dependencies
python = "^3.8"
torch = { version = "=1.10.2+cu111", source = "pytorch-cu111" }
torchvision = { version = "=0.11.3+cu111", source = "pytorch-cu111" }
tool.poetry.source
name = "pytorch-cu111"
url = "https://download.pytorch.org/whl/cu111/"
secondary = true
build-system
requires = "poetry-core"
build-backend = "poetry.core.masonry.api"
未来の自分のために、何を参考にしてこの TOML を書いたかメモしておくか。
PyPI じゃないリポジトリを使う方法 https://python-poetry.org/docs/master/repositories/
ここ source = でリポジトリを指定する方法が書いてないんだけど。何?
こっちだった https://python-poetry.org/docs/master/dependency-specification/#source-dependencies
ところでマークダウン記法ミスってますよ
いちおうキャッシュを破壊しておこう:rm -rf ~/.cache/pypoetry
poetry updateいきます
code::
$ bin/poetry update
Creating virtualenv hoge-Ho4R3mY4-py3.8 in /home/ikr7/.cache/pypoetry/virtualenvs
Updating dependencies
Resolving dependencies... (532.1s)pytorch-cu111: Authorization error accessing https://download.pytorch.org/whl/cu111/typing-extensions/
<debug>pytorch-cu111:</debug> Authorization error accessing https://download.pytorch.org/whl/cu111/typing-extensions/
Resolving dependencies... (536.0s)pytorch-cu111: Authorization error accessing https://download.pytorch.org/whl/cu111/pillow/
<debug>pytorch-cu111:</debug> Authorization error accessing https://download.pytorch.org/whl/cu111/pillow/
Resolving dependencies... (536.6s)pytorch-cu111: Authorization error accessing https://download.pytorch.org/whl/cu111/numpy/
<debug>pytorch-cu111:</debug> Authorization error accessing https://download.pytorch.org/whl/cu111/numpy/
Resolving dependencies... (553.6s)
Writing lock file
Package operations: 5 installs, 0 updates, 0 removals
• Installing typing-extensions (4.1.1)
• Installing numpy (1.22.3)
• Installing pillow (9.0.1)
• Installing torch (1.10.2+cu111)
• Installing torchvision (0.11.3+cu111)
ほぼ10分かかっていてアホっぽい。しかし待てば終わるなら待つ、そういう人生もあります
依存を PyTorch のリポジトリから取ろうとしてエラーが出てるけど、インストールできましたみたいな記述があるしうまくいったのか?試しましょう
code::
$ bin/poetry run python
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
GCC 9.3.0 on linux
Type "help", "copyright", "credits" or "license" for more information.
>> import torch
>> import torchvision
>> torch.cuda.is_available()
True
>> torch.rand(2, 2).to('cuda:0')
tensor([0.6177, 0.4732,
0.4129, 0.7298], device='cuda:0')
いいんじゃないか!?
初勝利では!?!?