LoginSignup
0
1

More than 1 year has passed since last update.

Windows11 WSL2 Ubuntu-22.04にpytorch,cuda-toolkit,cudnn,jupyter notebook,tensorflowを入れるまでの個人用メモ (2023/04/10)

Last updated at Posted at 2023-04-10

※勉強用の自分のメモですので、間違えている可能性があります

とりあえず動いたので
WSLインストール完了、Windows11にNvidiaのドライバをインストール完了前提で進めていきます。

$ sudo apt update
$ nvidia-smi
Mon Apr 10 21:11:08 2023
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 530.41.03              Driver Version: 531.41       CUDA Version: 12.1     |
|-----------------------------------------+----------------------+----------------------+
| GPU  Name                  Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf            Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                                         |                      |               MIG M. |
|=========================================+======================+======================|
|   0  NVIDIA GeForce GTX **** **      On | 00000000:04:00.0 Off |                  N/A |
| **%   **C    **               **W / ***W|   ****MiB / *****MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+

+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
|    0   N/A  N/A        22      G   /Xwayland                                 N/A      |
|    0   N/A  N/A      2335      C   /python3.9                                N/A      |
+---------------------------------------------------------------------------------------+

cuda-toolkitのインストール

公式ではsud-apt install cudaとなっていますが、これを実行するとdriverまでインストールされてしまうので避けましょう

$ wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-keyring_1.0-1_all.deb
$ sudo dpkg -i cuda-keyring_1.0-1_all.deb
$ sudo apt-get update

11-8を入れます、一応確認

$ apt-cache search cuda-toolkit-11-8
# 出力
cuda-toolkit-11-8 - CUDA Toolkit 11.8 meta-package
cuda-toolkit-11-8-config-common - Common config package for CUDA Toolkit 11.8.
$ apt-cache policy cuda-toolkit-11-8
# 出力
cuda-toolkit-11-8:
  Installed: 11.8.0-1
  Candidate: 11.8.0-1
  Version table:
     11.8.0-1 600
        600 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64  Packages
        100 /var/lib/dpkg/status     
     11.8.0-1 600
        600 https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64  Packages
$ sudo apt-get -y install cuda-toolkit-11-8

cudnnのインストール

情報元
https://developer.nvidia.com/cudnn
https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#cudnn-package-manager-installation-overview

$ wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin 
$ sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600
$ sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/3bf863cc.pub
$ sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/ /"

上記後、sudo apt updateをすると以下の警告が出ます。

# W: https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.

直しちゃいます、どれが足りないのか確認してから

$ sudo apt-key list
# 出力
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
/etc/apt/trusted.gpg
--------------------
pub   rsa4096 2022-04-14 [SC]
      EB69 3B30 35CD 5710 E231  E123 A4B4 6996 3BF8 63CC
uid           [ unknown] cudatools <cudatools@nvidia.com>

/etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg
------------------------------------------------------
pub   rsa4096 2012-05-11 [SC]
      8439 38DF 228D 22F7 B374  2BC0 D94A A3F0 EFE2 1092
uid           [ unknown] Ubuntu CD Image Automatic Signing Key (2012) <cdimage@ubuntu.com>

/etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg
------------------------------------------------------
pub   rsa4096 2018-09-17 [SC]
      F6EC B376 2474 EDA9 D21B  7022 8719 20D1 991B C93C
uid           [ unknown] Ubuntu Archive Automatic Signing Key (2018) <ftpmaster@ubuntu.com>
$ ls /etc/apt/trusted.gpg.d/
# 出力
ubuntu-keyring-2012-cdimage.gpg  ubuntu-keyring-2018-archive.gpg

/etc/apt/trusted.gpg.d/の中に2022のがないです、形式を合わせて作っちゃいます。

$ sudo apt-key export 3BF863CC | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/ubuntu-keyring-2022-archive.gpg

解消しているかチェックしておきます

$ sudo apt update

libcudnn8とlibcudnn8-devのバージョンを確認して、後で入れるTensorRTのためにdev=8.6.0.163-1+cuda11.8に統一しておきます。

$ apt-cache policy libcudnn8
->出力からバージョン確認
$ apt-cache policy libcudnn8-dev
->出力からバージョン確認
$ sudo apt install libcudnn8=8.6.0.163-1+cuda11.8 libcudnn8-dev=8.6.0.163-1+cuda11.8

anacondaのインストール

$ curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -o Miniconda3-latest-Linux-x86_64.sh
$ bash Miniconda3-latest-Linux-x86_64.sh

インストール作業中に出るのはinitだけyesにします。
Enter -> q -> yes -> Enter -> by running conda init? [yes|no] yes

$ source ~/.bashrc
$ conda config --set auto_activate_base false
$ conda deactivate

仮想環境の作成

$ conda create --name tf python=3.9
$ conda activate tf

tfに入った時に、cudaのパス指定と、TensorFlowのログレベルの設定を環境変数に加えるようにする

$ mkdir -p $CONDA_PREFIX/etc/conda/activate.d
$ nano $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
env_vars.sh
export PATH="/usr/local/cuda-11.8/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-11.8/lib64:$LD_LIBRARY_PATH"
export TF_CPP_MIN_LOG_LEVEL=2
$ chmod +x $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh

仮想環境の設定

tfに
・TensorFlow
・jupyter notebook
を入れていきます

$ pip install --upgrade pip
$ pip install tensorflow==2.12.*

$ conda install matplotlib jupyter chardet

jupyter notebookの設定
自分の好きな設定をするので各自変更してください。

$ jupyter notebook --generate-config
$ jupyter notebook password
->パスワード入力

"jupyter notebook --generate-config"で"~/.jupyter/jupyter_notebook_config.py"が作成されているので設定を自分の好きなように変えます

$ nano ~/.jupyter/jupyter_notebook_config.py
~/.jupyter/jupyter_notebook_config.py
# c.NotebookApp.notebook_dir = ''
c.NotebookApp.notebook_dir = "/mnt/c/Users/***/マイドライブ/Colab Notebooks"
# .NotebookApp.open_browser = True
c.NotebookApp.open_browser = False

コメントアウトされているので、コメントアウトを解除して好きな設定を書き込みます
"/mnt/c/Users/***/マイドライブ/Colab Notebooks"は、Windows11にGoogleDriveが入っている場合、colaboratoryのファイルがあるフォルダです。

jupyter notebookを起動して確認。
終了はctl+y > yes

$ jupyter notebook

TensorRTのインストール ※ここからはたぶん違うかもしれない

参考先
https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-853/support-matrix/index.html
https://docs.nvidia.com/deeplearning/tensorrt/archives/index.html
https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-853/quick-start-guide/index.html#install
TensorRT 8.5.3を入れていきます
8.5.3はTensorRT 8.5 GA Update 2ですので、探してDLします。
https://developer.nvidia.com/tensorrt

nv-tensorrt-local-repo-ubuntu2204-8.5.3-cuda-11.8_1.0-1_amd64.debをDLした後の話をします
WindowsでDLしてwslのフォルダに入れたり各自自由に

$ sudo dpkg -i nv-tensorrt-local-repo-ubuntu2204-8.5.3-cuda-11.8_1.0-1_amd64.deb
$ sudo cp /var/nv-tensorrt-local-repo-ubuntu2204-8.5.3-cuda-11.8/nv-tensorrt-local-3E951519-keyring.gpg /usr/share/keyrings/
$ sudo apt-get update

ここからは依存関係との戦いでした。順番は忘れてしまいましたがこのようなものをインストールしました
tensorrtを入れてみて、警告が出たらその都度入れる形にしてます。
もっと賢い方法があるはず

sudo apt install \
libnvinfer8=8.6.0.12-1+cuda11.8 \
libnvinfer-plugin8=8.6.0.12-1+cuda11.8 \
libnvinfer-vc-plugin8=8.6.0.12-1+cuda11.8 \
libnvinfer-lean8=8.6.0.12-1+cuda11.8 \
libnvinfer-dispatch8=8.6.0.12-1+cuda11.8 \
libnvparsers8=8.6.0.12-1+cuda11.8 \
libnvonnxparsers8=8.6.0.12-1+cuda11.8 \
libnvinfer-bin=8.6.0.12-1+cuda11.8 \
libnvinfer-headers-dev=8.6.0.12-1+cuda11.8 \
libnvinfer-dev=8.6.0.12-1+cuda11.8 \
libnvinfer-lean-dev=8.6.0.12-1+cuda11.8 \
libnvinfer-headers-plugin-dev=8.6.0.12-1+cuda11.8 \
libnvinfer-plugin-dev=8.6.0.12-1+cuda11.8 \
libnvinfer-vc-plugin-dev=8.6.0.12-1+cuda11.8 \
libnvinfer-dispatch-dev=8.6.0.12-1+cuda11.8 \
libnvparsers-dev=8.6.0.12-1+cuda11.8 \
libnvonnxparsers-dev=8.6.0.12-1+cuda11.8 \
libnvinfer-samples=8.6.0.12-1+cuda11.8 \
tensorrt=8.6.0.12-1+cuda11.8

公式通りにインストール作業を進めていきます。

$ python3 -m pip install numpy
$ sudo apt-get install python3-libnvinfer=8.6.0.12-1+cuda11.8 python3-libnvinfer-lean=8.6.0.12-1+cuda11.8 python3-libnvinfer-dispatch=8.6.0.12-1+cuda11.8
$ sudo apt-get install python3-libnvinfer-dev=8.6.0.12-1+cuda11.8

$ python3 -m pip install protobuf
$ sudo apt install  graphsurgeon-tf=8.6.0.12-1+cuda11.8 uff-converter-tf=8.6.0.12-1+cuda11.8

$ python3 -m pip install numpy onnx
$ sudo apt install onnx-graphsurgeon=8.6.0.12-1+cuda11.8

# バージョン確認します
$ python3 -m pip install tensorrt==
ERROR: Could not find a version that satisfies the requirement tensorrt== (from versions: 0.0.1.dev5, 0.0.1, 8.5.1.7, 8.5.2.2, 8.5.3.1, 8.6.0)
ERROR: No matching distribution found for tensorrt==

$ python3 -m pip install tensorrt==8.5.3.1

TensorRTの作動確認

$ python3
>>> import tensorrt
>>> print(tensorrt.__version__)
8.5.3.1
>>> assert tensorrt.Builder(tensorrt.Logger())
[04/10/2023-12:27:36] [TRT] [W] CUDA lazy loading is not enabled. Enabling it can significantly reduce device memory usage and speed up TensorRT initialization. See "Lazy Loading" section of CUDA documentation 
https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#lazy-loading

お好みですが、遅延モジュールロードを有効にして、警告が出ないようにしておきましょう

$ export CUDA_MODULE_LOADING='LAZY' >> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
$ python3 -c 'import tensorrt;assert tensorrt.Builder(tensorrt.Logger())'

警告が出なくなったらオッケー

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1