アプリケーション開発ポータルサイト
ServerNote.NET
カテゴリー【UbuntuHardwarePython
TSUKUMOのマルチGPUパソコンWA9J-X211/XTにUbuntu Server 20.04.2 LTSを導入する【2】
POSTED BY
2024-11-06

TSUKUMOのマルチGPUパソコンWA9J-X211/XTにUbuntu Server 20.04.2 LTSを導入する【1】

続きです。

1、CUDA Toolkitをインストール

どうやらこれにはNVIDIA GPUドライバ最新版も含まれているようで、前回のようにnvidia-470を入れてしまっていると、最新版とコンクリフトしてうまく入らない。

https://developer.nvidia.com/cuda-downloads

ここから、Linux→x86_64→Ubuntu→20.04→deb[network]を選択すると、スクリプトが表示されるので、その通り入力する。しかし、どのサイトもsudosudo連発馬鹿ですかと言いたい。最初にsudo -s してからコマンド打ったほうが簡単ではないか。

  1. sudo -s
  2. cd # /rootで作業
  3. wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
  4. mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
  5. apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub
  6. add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /"
  7. apt-get update
  8. apt-get -y install cuda

終わったらrebootする。

  1. reboot

再起動以降爆音開始となるのは、正しくGPUを認識している証(GPUファン)ではあるが、うるさすぎるので、永続待機オプションをオフにして未使用状態では止める。

  1. nvidia-smi -pm 0
  2.  
  3. Disabled persistence mode for GPU 00000000:17:00.0.
  4. Disabled persistence mode for GPU 00000000:65:00.0.
  5. All done.

永続待機状態にあるかどうかは、GPUを使っていないにもかかわらず、powertopコマンドでirq/91-nvidiaのようなプロセスが居て電力を食っていたら、永続待機であるので、上記オプションで切る。

環境変数のセットアップ

.bashrcなどに追記する。/usr/local/cuda-11.5が入ったはずであるので、

  1. export CUDA_HOME=/usr/local/cuda-11.5
  2. export PATH=$CUDA_HOME/bin:$PATH
  3. export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH

として読み込んだあと、

  1. nvcc -V
  2.  
  3. nvcc: NVIDIA (R) Cuda compiler driver
  4. Copyright (c) 2005-2021 NVIDIA Corporation
  5. Built on Mon_Sep_13_19:13:29_PDT_2021
  6. Cuda compilation tools, release 11.5, V11.5.50
  7. Build cuda_11.5.r11.5/compiler.30411180_0

コンパイラが読めればパスが通っている状態。

2、cuDNNライブラリのインストール

https://developer.nvidia.com/rdp/cudnn-download

から、入れたCUDAのバージョンと一致するものをダウンロードする。ユーザー登録が必要な場合JOIN NOWで登録。無料。

TSUKUMO-PCの場合、cuDNN Library for Linux (x86_64)→cudnn-11.5-linux-x64-v8.3.0.98.tgzをダウンロードする。

ダウンロードしたtgzファイルを展開する。

  1. tar xvfzp cudnn-11.5-linux-x64-v8.3.0.98.tgz

suになって、念のため元のcuda-11.5ディレクトリのバックアップを取っておく。

  1. suod -s
  2. cd /usr/local
  3. cp -Rp cuda-11.5 cuda-11.5.default

その後、展開したcudnnのcuda/include, lib64をcuda-11.5以下にコピーする。

  1. cd cuda/include
  2. cp -Rp * /usr/local/cuda-11.5/include
  3. cd cuda/lib64
  4. cp -Rp * /usr/local/cuda-11.5/lib64

環境変数のセットアップ

.bashrcなどに追記する。

  1. export CUDNN_HOME=$CUDA_HOME

3、Python関係のインストール

Pythonは最新版が入っている模様。

  1. python3 -V
  2.  
  3. Python 3.9.7

pipが無いので、インストールする。

  1. sudo apt install python3-pip

ここからは、suでシステム全体か、suせず自分専用で入れるか、好みが分かれるが、当方はsuで全体に入れたい派。

Pytorchのインストール

PytorchはTensorFlowと並ぶGPUをふんだんに使用する機械学習ライブラリである。

https://pytorch.org/

から、Stable→Linux→Pip→Python→CUDA 11.3を選ぶとコマンドが出るので、コピー&実行。

  1. sudo -s
  2.  
  3. pip3 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio==0.10.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
  4.  
  5. ...
  6. Successfully installed numpy-1.21.4 pillow-8.4.0 torch-1.10.0+cu113 torchaudio-0.10.0+cu113 torchvision-0.11.1+cu113 typing-extensions-3.10.0.2

GPUの認識確認

  1. python3
  2. Python 3.9.7 (default, Sep 10 2021, 14:59:43)
  3. [GCC 11.2.0] on linux
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> import torch
  6. >>> print(torch.__version__)
  7. 1.10.0+cu113
  8. >>> print(torch.cuda.is_available())
  9. True
  10. >>> print(torch.cuda.device_count())
  11. 2
  12. >>> print(torch.cuda.current_device())
  13. 0
  14. >>> print(torch.cuda.get_device_name(0))
  15. NVIDIA GeForce RTX 3090
  16. >>> print(torch.cuda.get_device_name(1))
  17. NVIDIA GeForce RTX 3090
  18. >>> quit()

NVIDIA GeForce RTX 3090がPyTorchで無事認識できた。2枚のGPUを使ったPytorch使用コードは後日テストの上、アップしたい。

ここで、pythonインタープリタが起動中、import torch後、psやpowertopすると、

  1. 12.0 mW 112.6 s/s 3.0 Process [PID 2526] [irq/90-nvidia]
  2. 12.0 mW 111.8 s/s 3.0 Process [PID 2529] [irq/91-nvidia]

と、GPUのプロセスが居て、ファンが轟音であり稼働中であることがわかる。quit()としてPython, torchを終了すると、プロセスから居なくなり、ファンの轟音も消えるので、さきほどのnvidia-smi -pm 0の設定が効いているものと思われる。

TensorFlowのインストール

続けて定番TensorFlowを入れて、GPUの認識確認を行う。

旧ライブラリのアンインストール。最新版を入れるので念のため消しておく。

  1. sudo -s
  2. pip3 uninstall -y tensorflow tensorflow-cpu tensorflow-gpu tf-models-official tensorflow_datasets tensorflow-hub keras-nightly keras

あらためて本体のインストール

  1. sudo -s
  2. pip3 install -U tensorflow tf-models-official tf_slim tensorflow_datasets tensorflow-hub
  3. pip3 install git+https://github.com/tensorflow/docs
  4. pip3 install git+https://github.com/tensorflow/examples.git

エラーの対処

  1. launchpadlib 1.10.13 requires testresources, which is not installed

と出ていたので、あらためてlaunchpadlib単体をインストール。

  1. pip3 install launchpadlib
  2.  
  3. Requirement already satisfied: launchpadlib in /usr/lib/python3/dist-packages (1.10.13)
  4. Requirement already satisfied: httplib2 in /usr/lib/python3/dist-packages (from launchpadlib) (0.18.1)
  5. Requirement already satisfied: keyring in /usr/lib/python3/dist-packages (from launchpadlib) (23.0.1)
  6. Requirement already satisfied: lazr.restfulclient>=0.9.19 in /usr/lib/python3/dist-packages (from launchpadlib) (0.14.2)
  7. Requirement already satisfied: lazr.uri in /usr/lib/python3/dist-packages (from launchpadlib) (1.0.5)
  8. Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (from launchpadlib) (52.0.0)
  9. Requirement already satisfied: six in /usr/local/lib/python3.9/dist-packages (from launchpadlib) (1.15.0)
  10. Collecting testresources
  11. Downloading testresources-2.0.1-py2.py3-none-any.whl (36 kB)
  12. Requirement already satisfied: wadllib in /usr/lib/python3/dist-packages (from launchpadlib) (1.3.5)
  13. Requirement already satisfied: SecretStorage>=3.2 in /usr/lib/python3/dist-packages (from keyring->launchpadlib) (3.3.1)
  14. Requirement already satisfied: jeepney>=0.4.2 in /usr/lib/python3/dist-packages (from keyring->launchpadlib) (0.7.1)
  15. Collecting pbr>=1.8
  16. Downloading pbr-5.7.0-py2.py3-none-any.whl (112 kB)
  17. |????????????????????????????????| 112 kB 4.4 MB/s
  18. Installing collected packages: pbr, testresources
  19. Successfully installed pbr-5.7.0 testresources-2.0.1

また、kerasはtensorflow同梱のはずなのに何故かkeras単体もインストールされてしまっていて、import tensorflow.keras.などとやると、tensorflow.python.framework.errors_impl.AlreadyExistsError: Another metric with the same name already exists.という重複モジュールエラーが出てしまった。なので、kerasをアンインストールした。(ら動いた。)

  1. pip3 uninstall keras

なお、keras_preprocessingはアンインストールしてはならない。おそらくこれはtensorflowがkerasを包含した際水面下で使用しているモジュールである。

バージョン確認

  1. pip3 show tensorflow
  2. Name: tensorflow
  3. Version: 2.6.0
  4. Summary: TensorFlow is an open source machine learning framework for everyone.
  5. Home-page: https://www.tensorflow.org/
  6. Author: Google Inc.
  7. Author-email: packages@tensorflow.org
  8. License: Apache 2.0
  9. Location: /usr/local/lib/python3.9/dist-packages
  10. Requires: grpcio, flatbuffers, tensorboard, protobuf, termcolor, gast, clang, numpy, wheel, opt-einsum, h5py, keras-preprocessing, wrapt, typing-extensions, absl-py, six, astunparse, tensorflow-estimator, keras, google-pasta
  11. Required-by: tf-models-official, tensorflow-text

GPUの認識確認

  1. python3
  2. Python 3.9.7 (default, Sep 10 2021, 14:59:43)
  3. [GCC 11.2.0] on linux
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> from tensorflow.python.client import device_lib
  6. >>> print(device_lib.list_local_devices())

出力結果

  1. 2021-11-06 20:11:53.599224: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F FMA
  2. To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
  3. 2021-11-06 20:11:55.860076: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /device:GPU:0 with 22311 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3090, pci bus id: 0000:17:00.0, compute capability: 8.6
  4. 2021-11-06 20:11:55.860668: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /device:GPU:1 with 22320 MB memory: -> device: 1, name: NVIDIA GeForce RTX 3090, pci bus id: 0000:65:00.0, compute capability: 8.6
  5. [name: "/device:CPU:0"
  6. device_type: "CPU"
  7. memory_limit: 268435456
  8. locality {
  9. }
  10. incarnation: 12562245482708196956
  11. , name: "/device:GPU:0"
  12. device_type: "GPU"
  13. memory_limit: 23394975744
  14. locality {
  15. bus_id: 1
  16. links {
  17. }
  18. }
  19. incarnation: 12141721341066513620
  20. physical_device_desc: "device: 0, name: NVIDIA GeForce RTX 3090, pci bus id: 0000:17:00.0, compute capability: 8.6"
  21. , name: "/device:GPU:1"
  22. device_type: "GPU"
  23. memory_limit: 23404806144
  24. locality {
  25. bus_id: 1
  26. links {
  27. }
  28. }
  29. incarnation: 8591648343889229398
  30. physical_device_desc: "device: 1, name: NVIDIA GeForce RTX 3090, pci bus id: 0000:65:00.0, compute capability: 8.6"
  31. ]

無事認識できている。これまた、このコマンド後、psやpowertopすると、nvidiaプロセスが2個増えており、quit()すると、消えてファン音も無くなるので、永続使用設定オフが効いている。

Pytorchはこれからだが、TensorFlowについてはこのTSUKUMOマシンでデュアルGPUを使ったトレーニングを以下試して成功している。
【Windows】Python3.10+TensorFlow2.6-GPU+CUDA11.4+cuDNN8.2を動かす【4】

※本記事は当サイト管理人の個人的な備忘録です。本記事の参照又は付随ソースコード利用後にいかなる損害が発生しても当サイト及び管理人は一切責任を負いません。
※本記事内容の無断転載を禁じます。
【WEBMASTER/管理人】
自営業プログラマーです。お仕事ください!
ご連絡は以下アドレスまでお願いします★

【キーワード検索】