Ubuntu 20.04
Sponsored Link

TensorFlow : インストール (GPU サポート)2020/07/24

 
機械学習ライブラリー, TensorFlow をインストールします。
コンピューターに搭載の NVIDIA GPU が使用できるようにセットアップします。
当例では以下の組み合わせでセットアップします。
⇒ TensorFlow 2.2, CUDA 10.1, cuDNN 7.6, Python 3.8
TensorFlow 公式のテスト済みのビルド構成は以下から確認可能です。
⇒ https://www.tensorflow.org/install/source?hl=en#gpu_support_2
[1]
[2]
[3]
cuDNN (CUDA Deep Neural Network library) を NVIDIA 社のサイトからダウンロードしておきます。(ダウンロードにはアカウント登録が必要)
⇒ https://developer.nvidia.com/rdp/cudnn-download
[4] ダウンロードした cuDNN をサーバーにアップロードして、所定の場所へ配置しておきます。
root@dlp:~#
tar zxvf cudnn-10.1-linux-x64-v7.6.5.32.tgz

root@dlp:~#
cp ./cuda/include/cudnn.h /usr/lib/cuda/include/

root@dlp:~#
cp -a ./cuda/lib64/libcudnn* /usr/lib/cuda/lib64/

root@dlp:~#
ldconfig

root@dlp:~#
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/cuda/lib64' >> /etc/profile.d/cuda101.sh

root@dlp:~#
source /etc/profile.d/cuda101.sh

[5] その他必要なパッケージをインストールしておきます。
root@dlp:~#
apt -y install python3-dev virtualenv gcc g++ make

[6] 任意の一般ユーザーでログインして、TensorFlow インストール用の Python 仮想環境を準備します。
TensorFlow をシステムワイドにインストールする場合は、当作業は不要で、root ユーザーで [7] を実行すれば OK です。
ubuntu@dlp:~$
virtualenv --system-site-packages -p python3 ./venv

created virtual environment CPython3.8.2.final.0-64 in 570ms
  creator CPython3Posix(dest=/home/ubuntu/venv, clear=False, global=True)
  seeder FromAppData(download=False, setuptools=latest, CacheControl=latest, six=latest, pkg_resources=latest, idna=latest, webencodings=latest, pip=latest, urllib3=latest, distro=latest, html5lib=latest, pytoml=latest, ipaddr=latest, msgpack=latest, packaging=latest, chardet=latest, progress=latest, certifi=latest, pyparsing=latest, retrying=latest, colorama=latest, lockfile=latest, requests=latest, distlib=latest, appdirs=latest, wheel=latest, pep517=latest, contextlib2=latest, via=copy, app_data_dir=/home/ubuntu/.local/share/virtualenv/seed-app-data/v1.0.1.debian)
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

ubuntu@dlp:~$
source ./venv/bin/activate

(venv) ubuntu@dlp:~$
[7] TensorFlow 2.2 をインストールします。
(venv) ubuntu@dlp:~$
pip3 install --upgrade tensorflow==2.2.0
# TensorFlow 動作確認

(venv) ubuntu@dlp:~$
python3 -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

2020-07-23 19:30:43.594084: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2020-07-23 19:30:43.612592: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-07-23 19:30:43.613926: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:04:00.0 name: GeForce GTX 1070 computeCapability: 6.1
coreClock: 1.7845GHz coreCount: 15 deviceMemorySize: 7.93GiB deviceMemoryBandwidth: 238.66GiB/s
2020-07-23 19:30:43.614482: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-07-23 19:30:43.618089: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
2020-07-23 19:30:43.620679: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10
2020-07-23 19:30:43.621136: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10
2020-07-23 19:30:43.624040: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10
2020-07-23 19:30:43.625717: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10
2020-07-23 19:30:43.681531: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2020-07-23 19:30:43.682298: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-07-23 19:30:43.683370: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-07-23 19:30:43.684167: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
2020-07-23 19:30:43.696972: I tensorflow/core/platform/profile_utils/cpu_utils.cc:102] CPU Frequency: 2801460000 Hz
2020-07-23 19:30:43.698834: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fedc0000b60 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-07-23 19:30:43.699461: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-07-23 19:30:44.201996: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-07-23 19:30:44.203387: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x4183a90 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2020-07-23 19:30:44.203542: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): GeForce GTX 1070, Compute Capability 6.1
2020-07-23 19:30:44.203983: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-07-23 19:30:44.204601: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:04:00.0 name: GeForce GTX 1070 computeCapability: 6.1
coreClock: 1.7845GHz coreCount: 15 deviceMemorySize: 7.93GiB deviceMemoryBandwidth: 238.66GiB/s
2020-07-23 19:30:44.204801: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-07-23 19:30:44.204945: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
2020-07-23 19:30:44.205125: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10
2020-07-23 19:30:44.205307: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10
2020-07-23 19:30:44.205468: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10
2020-07-23 19:30:44.205647: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10
2020-07-23 19:30:44.205809: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2020-07-23 19:30:44.206075: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-07-23 19:30:44.206732: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-07-23 19:30:44.207401: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
2020-07-23 19:30:44.207599: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-07-23 19:30:44.233507: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-07-23 19:30:44.233918: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]      0
2020-07-23 19:30:44.234099: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 0:   N
2020-07-23 19:30:44.234443: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-07-23 19:30:44.235207: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-07-23 19:30:44.236077: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1247] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 7531 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1070, pci bus id: 0000:04:00.0, compute capability: 6.1)
tf.Tensor(-221.96506, shape=(), dtype=float32)
関連コンテンツ